Skip to content

Commit 2ab3a7a

Browse files
authored
add createProcedure rule for oracle (#20422)
* add createProcedure rule for oracle #1 * add createProcedure rule for oracle #2 * add createProcedure rule for oracle #3 * add createProcedure rule for oracle #4 * add createProcedure rule for oracle #5 * add createProcedure rule for oracle #6 * add createProcedure rule for oracle #6 * add createProcedure rule for oracle #7
1 parent b872e74 commit 2ab3a7a

File tree

9 files changed

+258
-2
lines changed

9 files changed

+258
-2
lines changed

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/BaseRule.g4

+2-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ unreservedWord
161161
| COARSE | FINE | ALIAS | SCRUB | DISMOUNT | REBALANCE | COMPUTATION | CONSIDER | FRESH | MASTER
162162
| ENFORCED | TRUSTED | ID | SYNCHRONOUS | ASYNCHRONOUS | REPEAT | FEATURE | STATEMENT | CLAUSE | UNPLUG
163163
| HOST | PORT | EVERY | MINUTES | HOURS | NORELOCATE | SAVE | DISCARD | APPLICATION | INSTALL
164-
| MINIMUM | VERSION | UNINSTALL | COMPATIBILITY | MATERIALIZE
164+
| MINIMUM | VERSION | UNINSTALL | COMPATIBILITY | MATERIALIZE | SUBTYPE | RECORD | CONSTANT | CURSOR
165+
| OTHERS | EXCEPTION
165166
;
166167

167168
schemaName

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/Keyword.g4

+4
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,7 @@ TRIGGERS
570570
GLOBAL_NAME
571571
: G L O B A L UL_ N A M E
572572
;
573+
574+
ROWTYPE
575+
: R O W T Y P E
576+
;

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4

+28
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ VARCHAR2
723723
: V A R C H A R [2]
724724
;
725725

726+
STRING
727+
: S T R I N G
728+
;
729+
726730
NVARCHAR2
727731
: N V A R C H A R [2]
728732
;
@@ -3035,3 +3039,27 @@ COMPATIBILITY
30353039
MATERIALIZE
30363040
: M A T E R I A L I Z E
30373041
;
3042+
3043+
SUBTYPE
3044+
: S U B T Y P E
3045+
;
3046+
3047+
RECORD
3048+
: R E C O R D
3049+
;
3050+
3051+
CONSTANT
3052+
: C O N S T A N T
3053+
;
3054+
3055+
CURSOR
3056+
: C U R S O R
3057+
;
3058+
3059+
OTHERS
3060+
: O T H E R S
3061+
;
3062+
3063+
EXCEPTION
3064+
: E X C E P T I O N
3065+
;

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/StoreProcedure.g4

+185-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,192 @@
1717

1818
grammar StoreProcedure;
1919

20-
import Keyword;
20+
import Keyword, BaseRule, DDLStatement, DMLStatement;
2121

2222
call
2323
: CALL
2424
;
25+
26+
createProcedure
27+
: CREATE (OR REPLACE)? (EDITIONABLE | NONEDITIONABLE)? PROCEDURE plsqlProcedureSource
28+
;
29+
30+
plsqlProcedureSource
31+
: (schemaName DOT_)? procedureName ( LP_ parameterDeclaration ( COMMA_ parameterDeclaration )* RP_)? sharingClause?
32+
((defaultCollationClause | invokerRightsClause | accessibleByClause)*)? (IS | AS) (callSpec | declareSection? body)
33+
;
34+
35+
body
36+
: BEGIN statement+ (EXCEPTION (exceptionHandler)+)? END (identifier)? SEMI_
37+
;
38+
39+
//need add more statement type according to the doc
40+
statement
41+
: ( SIGNED_LEFT_SHIFT_ label SIGNED_RIGHT_SHIFT_ ( SIGNED_LEFT_SHIFT_ label SIGNED_RIGHT_SHIFT_ ) *)?
42+
( select
43+
| update
44+
| delete
45+
| insert
46+
| lockTable
47+
| merge
48+
) SEMI_
49+
;
50+
51+
exceptionHandler
52+
: WHEN ( (typeName (OR typeName)* )| OTHERS ) THEN statement+
53+
;
54+
55+
declareSection
56+
: itemList1 itemList2?
57+
| itemList2
58+
;
59+
60+
itemList2
61+
: cursorDeclaration | cursorDefinition | functionDeclaration | functionDefinition | procedureDeclaration | procedureDefinition
62+
;
63+
64+
cursorDefinition
65+
: CURSOR variableName ( LP_ cursorParameterDec ( COMMA_ cursorParameterDec )* RP_)? ( RETURN rowtype)? IS select SEMI_
66+
;
67+
68+
functionDefinition
69+
: functionHeading ( DETERMINISTIC | PIPELINED | PARALLEL_ENABLE | resultCacheClause )+ ( IS | AS ) ( declareSection ? body | callSpec )
70+
;
71+
72+
procedureDefinition
73+
: procedureDeclaration (IS | AS) (callSpec | declareSection? body)
74+
;
75+
76+
itemList1
77+
:( typeDefinition | cursorDeclaration | itemDeclaration | functionDeclaration | procedureDeclaration )*
78+
;
79+
80+
cursorDeclaration
81+
: CURSOR variableName ( ( cursorParameterDec (COMMA_ cursorParameterDec )* ) )? RETURN rowtype SEMI_
82+
;
83+
84+
cursorParameterDec
85+
: variableName IN? dataType ( (COLON_ EQ_ | DEFAULT) expr )?
86+
;
87+
88+
rowtype
89+
: typeName MOD_ ROWTYPE
90+
| typeName (MOD_ TYPE)?
91+
;
92+
93+
itemDeclaration
94+
: collectionVariableDecl | constantDeclaration | cursorVariableDeclaration | exceptionDeclaration | recordVariableDeclaration | variableDeclaration
95+
;
96+
97+
collectionVariableDecl
98+
: variableName
99+
(
100+
typeName ( COLON_ EQ_ ( qualifiedExpression | functionCall | variableName ) )?
101+
| typeName ( COLON_ EQ_ ( collectionConstructor | variableName ) )?
102+
| typeName MOD_ TYPE
103+
)
104+
SEMI_
105+
;
106+
107+
qualifiedExpression
108+
: typemark LP_ aggregate RP_
109+
;
110+
111+
aggregate
112+
: positionalChoiceList? explicitChoiceList?
113+
;
114+
115+
explicitChoiceList
116+
: namedChoiceList | indexedChoiceList
117+
;
118+
119+
namedChoiceList
120+
: identifier EQ_ GT_ expr (COMMA_ identifier EQ_ GT_ expr)*
121+
;
122+
123+
indexedChoiceList
124+
: expr EQ_ GT_ expr (COMMA_ expr EQ_ GT_ expr)*
125+
;
126+
127+
positionalChoiceList
128+
: expr (COMMA_ expr)*
129+
;
130+
131+
typemark
132+
: typeName
133+
;
134+
135+
collectionConstructor
136+
: typeName LP_ ( identifier (COMMA_ identifier)* )? RP_
137+
;
138+
139+
constantDeclaration
140+
: variableName CONSTANT dataType ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr SEMI_
141+
;
142+
143+
cursorVariableDeclaration
144+
: variableName typeName SEMI_
145+
;
146+
147+
exceptionDeclaration
148+
: variableName EXCEPTION SEMI_
149+
;
150+
151+
recordVariableDeclaration
152+
: variableName ( typeName | rowtypeAttribute | typeName MOD_ TYPE ) SEMI_
153+
;
154+
155+
variableDeclaration
156+
: variableName dataType ( ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr )? SEMI_
157+
;
158+
159+
typeDefinition
160+
: collectionTypeDefinition | recordTypeDefinition | refCursorTypeDefinition | subtypeDefinition
161+
;
162+
163+
recordTypeDefinition
164+
: TYPE typeName IS RECORD LP_ fieldDefinition ( COMMA_ fieldDefinition )* RP_ SEMI_
165+
;
166+
167+
fieldDefinition
168+
: typeName dataType ( ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr )?
169+
;
170+
171+
refCursorTypeDefinition
172+
: TYPE typeName IS REF CURSOR ( RETURN (
173+
(typeName MOD_ ROWTYPE)
174+
| (typeName (MOD_ TYPE)?)
175+
) )? SEMI_
176+
;
177+
178+
subtypeDefinition
179+
: SUBTYPE typeName IS dataType ( constraint | characterSetClause )? ( NOT NULL )?
180+
;
181+
182+
constraint
183+
: (INTEGER_ COMMA_ INTEGER_) | (RANGE NUMBER_ DOT_ DOT_ NUMBER_)
184+
;
185+
186+
collectionTypeDefinition
187+
: TYPE typeName IS ( assocArrayTypeDef | varrayTypeDef | nestedTableTypeDef ) SEMI_
188+
;
189+
190+
varrayTypeDef
191+
: ( VARRAY | (VARYING? ARRAY) ) LP_ INTEGER_ RP_ OF dataType ( NOT NULL )?
192+
;
193+
194+
nestedTableTypeDef
195+
: TABLE OF dataType ( NOT NULL )?
196+
;
197+
198+
assocArrayTypeDef
199+
: TABLE OF dataType ( NOT NULL )? INDEX BY ( PLS_INTEGER | BINARY_INTEGER | ( VARCHAR2 | VARCHAR2 | STRING ) LP_ INTEGER_ RP_ | LONG | typeAttribute | rowtypeAttribute )
200+
;
201+
202+
typeAttribute
203+
: ( variableName | objectName ) MOD_ TYPE
204+
;
205+
206+
rowtypeAttribute
207+
: ( variableName | objectName ) MOD_ ROWTYPE
208+
;

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/OracleStatement.g4

+1
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,6 @@ execute
122122
| alterHierarchy
123123
| alterLockdownProfile
124124
| alterPluggableDatabase
125+
| createProcedure
125126
) SEMI_?
126127
;

shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDDLStatementSQLVisitor.java

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateInmemoryJoinGroupContext;
8080
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateLockdownProfileContext;
8181
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreatePFileContext;
82+
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateProcedureContext;
8283
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateRestorePointContext;
8384
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateRollbackSegmentContext;
8485
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateSPFileContext;
@@ -198,6 +199,7 @@
198199
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateInmemoryJoinGroupStatement;
199200
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateLockdownProfileStatement;
200201
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreatePFileStatement;
202+
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateProcedureStatement;
201203
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateRestorePointStatement;
202204
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateRollbackSegmentStatement;
203205
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateSPFileStatement;
@@ -976,4 +978,10 @@ public ASTNode visitAlterLockdownProfile(final AlterLockdownProfileContext ctx)
976978
public ASTNode visitAlterPluggableDatabase(final AlterPluggableDatabaseContext ctx) {
977979
return new OracleAlterPluggableDatabaseStatement();
978980
}
981+
982+
@Override
983+
public ASTNode visitCreateProcedure(final CreateProcedureContext ctx) {
984+
OracleCreateProcedureStatement result = new OracleCreateProcedureStatement();
985+
return result;
986+
}
979987
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl;
19+
20+
import lombok.Setter;
21+
import lombok.ToString;
22+
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateProcedureStatement;
23+
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;
24+
25+
@Setter
26+
@ToString(callSuper = true)
27+
public class OracleCreateProcedureStatement extends CreateProcedureStatement implements OracleStatement {
28+
}

shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/create-procedure.xml

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
<create-procedure sql-case-id="create_procedure_with_create_view_as_select_into_outfile" />
3131
<create-procedure sql-case-id="create_procedure_with_sqlexception_and_create_view" />
3232
<create-procedure sql-case-id="create_procedure_with_deterministic_create_view" />
33+
<create-procedure sql-case-id="create_procedure_with_update_statement_oracle" />
3334
</sql-parser-test-cases>

shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-procedure.xml

+1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@
4242
<sql-case id="create_procedure_with_create_view_as_select_into_outfile" value="CREATE PROCEDURE bug20953() CREATE VIEW v AS SELECT 1 INTO OUTFILE &quot;file&quot;" db-types="MySQL" />
4343
<sql-case id="create_procedure_with_sqlexception_and_create_view" value="create procedure p() begin declare continue handler for sqlexception begin end; create view a as select 1; end" db-types="MySQL" />
4444
<sql-case id="create_procedure_with_deterministic_create_view" value="create procedure p1 () deterministic begin create view v1 as select 1; end;" db-types="MySQL" />
45+
<sql-case id="create_procedure_with_update_statement_oracle" value="CREATE PROCEDURE update_order (order_id NUMBER,status NUMBER) AS BEGIN UPDATE t_order SET status = status WHERE order_id = order_id;END;" db-types="Oracle" />
4546
</sql-cases>

0 commit comments

Comments
 (0)