Skip to content

Commit

Permalink
add createProcedure rule for oracle (#20422)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tianhao960 authored Aug 23, 2022
1 parent b872e74 commit 2ab3a7a
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ unreservedWord
| COARSE | FINE | ALIAS | SCRUB | DISMOUNT | REBALANCE | COMPUTATION | CONSIDER | FRESH | MASTER
| ENFORCED | TRUSTED | ID | SYNCHRONOUS | ASYNCHRONOUS | REPEAT | FEATURE | STATEMENT | CLAUSE | UNPLUG
| HOST | PORT | EVERY | MINUTES | HOURS | NORELOCATE | SAVE | DISCARD | APPLICATION | INSTALL
| MINIMUM | VERSION | UNINSTALL | COMPATIBILITY | MATERIALIZE
| MINIMUM | VERSION | UNINSTALL | COMPATIBILITY | MATERIALIZE | SUBTYPE | RECORD | CONSTANT | CURSOR
| OTHERS | EXCEPTION
;

schemaName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,7 @@ TRIGGERS
GLOBAL_NAME
: G L O B A L UL_ N A M E
;

ROWTYPE
: R O W T Y P E
;
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ VARCHAR2
: V A R C H A R [2]
;

STRING
: S T R I N G
;

NVARCHAR2
: N V A R C H A R [2]
;
Expand Down Expand Up @@ -3035,3 +3039,27 @@ COMPATIBILITY
MATERIALIZE
: M A T E R I A L I Z E
;

SUBTYPE
: S U B T Y P E
;

RECORD
: R E C O R D
;

CONSTANT
: C O N S T A N T
;

CURSOR
: C U R S O R
;

OTHERS
: O T H E R S
;

EXCEPTION
: E X C E P T I O N
;
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,192 @@

grammar StoreProcedure;

import Keyword;
import Keyword, BaseRule, DDLStatement, DMLStatement;

call
: CALL
;

createProcedure
: CREATE (OR REPLACE)? (EDITIONABLE | NONEDITIONABLE)? PROCEDURE plsqlProcedureSource
;

plsqlProcedureSource
: (schemaName DOT_)? procedureName ( LP_ parameterDeclaration ( COMMA_ parameterDeclaration )* RP_)? sharingClause?
((defaultCollationClause | invokerRightsClause | accessibleByClause)*)? (IS | AS) (callSpec | declareSection? body)
;

body
: BEGIN statement+ (EXCEPTION (exceptionHandler)+)? END (identifier)? SEMI_
;

//need add more statement type according to the doc
statement
: ( SIGNED_LEFT_SHIFT_ label SIGNED_RIGHT_SHIFT_ ( SIGNED_LEFT_SHIFT_ label SIGNED_RIGHT_SHIFT_ ) *)?
( select
| update
| delete
| insert
| lockTable
| merge
) SEMI_
;

exceptionHandler
: WHEN ( (typeName (OR typeName)* )| OTHERS ) THEN statement+
;

declareSection
: itemList1 itemList2?
| itemList2
;

itemList2
: cursorDeclaration | cursorDefinition | functionDeclaration | functionDefinition | procedureDeclaration | procedureDefinition
;

cursorDefinition
: CURSOR variableName ( LP_ cursorParameterDec ( COMMA_ cursorParameterDec )* RP_)? ( RETURN rowtype)? IS select SEMI_
;

functionDefinition
: functionHeading ( DETERMINISTIC | PIPELINED | PARALLEL_ENABLE | resultCacheClause )+ ( IS | AS ) ( declareSection ? body | callSpec )
;

procedureDefinition
: procedureDeclaration (IS | AS) (callSpec | declareSection? body)
;

itemList1
:( typeDefinition | cursorDeclaration | itemDeclaration | functionDeclaration | procedureDeclaration )*
;

cursorDeclaration
: CURSOR variableName ( ( cursorParameterDec (COMMA_ cursorParameterDec )* ) )? RETURN rowtype SEMI_
;

cursorParameterDec
: variableName IN? dataType ( (COLON_ EQ_ | DEFAULT) expr )?
;

rowtype
: typeName MOD_ ROWTYPE
| typeName (MOD_ TYPE)?
;

itemDeclaration
: collectionVariableDecl | constantDeclaration | cursorVariableDeclaration | exceptionDeclaration | recordVariableDeclaration | variableDeclaration
;

collectionVariableDecl
: variableName
(
typeName ( COLON_ EQ_ ( qualifiedExpression | functionCall | variableName ) )?
| typeName ( COLON_ EQ_ ( collectionConstructor | variableName ) )?
| typeName MOD_ TYPE
)
SEMI_
;

qualifiedExpression
: typemark LP_ aggregate RP_
;

aggregate
: positionalChoiceList? explicitChoiceList?
;

explicitChoiceList
: namedChoiceList | indexedChoiceList
;

namedChoiceList
: identifier EQ_ GT_ expr (COMMA_ identifier EQ_ GT_ expr)*
;

indexedChoiceList
: expr EQ_ GT_ expr (COMMA_ expr EQ_ GT_ expr)*
;

positionalChoiceList
: expr (COMMA_ expr)*
;

typemark
: typeName
;

collectionConstructor
: typeName LP_ ( identifier (COMMA_ identifier)* )? RP_
;

constantDeclaration
: variableName CONSTANT dataType ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr SEMI_
;

cursorVariableDeclaration
: variableName typeName SEMI_
;

exceptionDeclaration
: variableName EXCEPTION SEMI_
;

recordVariableDeclaration
: variableName ( typeName | rowtypeAttribute | typeName MOD_ TYPE ) SEMI_
;

variableDeclaration
: variableName dataType ( ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr )? SEMI_
;

typeDefinition
: collectionTypeDefinition | recordTypeDefinition | refCursorTypeDefinition | subtypeDefinition
;

recordTypeDefinition
: TYPE typeName IS RECORD LP_ fieldDefinition ( COMMA_ fieldDefinition )* RP_ SEMI_
;

fieldDefinition
: typeName dataType ( ( NOT NULL )? ( COLON_ EQ_ | DEFAULT ) expr )?
;

refCursorTypeDefinition
: TYPE typeName IS REF CURSOR ( RETURN (
(typeName MOD_ ROWTYPE)
| (typeName (MOD_ TYPE)?)
) )? SEMI_
;

subtypeDefinition
: SUBTYPE typeName IS dataType ( constraint | characterSetClause )? ( NOT NULL )?
;

constraint
: (INTEGER_ COMMA_ INTEGER_) | (RANGE NUMBER_ DOT_ DOT_ NUMBER_)
;

collectionTypeDefinition
: TYPE typeName IS ( assocArrayTypeDef | varrayTypeDef | nestedTableTypeDef ) SEMI_
;

varrayTypeDef
: ( VARRAY | (VARYING? ARRAY) ) LP_ INTEGER_ RP_ OF dataType ( NOT NULL )?
;

nestedTableTypeDef
: TABLE OF dataType ( NOT NULL )?
;

assocArrayTypeDef
: TABLE OF dataType ( NOT NULL )? INDEX BY ( PLS_INTEGER | BINARY_INTEGER | ( VARCHAR2 | VARCHAR2 | STRING ) LP_ INTEGER_ RP_ | LONG | typeAttribute | rowtypeAttribute )
;

typeAttribute
: ( variableName | objectName ) MOD_ TYPE
;

rowtypeAttribute
: ( variableName | objectName ) MOD_ ROWTYPE
;
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@ execute
| alterHierarchy
| alterLockdownProfile
| alterPluggableDatabase
| createProcedure
) SEMI_?
;
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateInmemoryJoinGroupContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateLockdownProfileContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreatePFileContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateProcedureContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateRestorePointContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateRollbackSegmentContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.CreateSPFileContext;
Expand Down Expand Up @@ -198,6 +199,7 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateInmemoryJoinGroupStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateLockdownProfileStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreatePFileStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateProcedureStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateRestorePointStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateRollbackSegmentStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleCreateSPFileStatement;
Expand Down Expand Up @@ -976,4 +978,10 @@ public ASTNode visitAlterLockdownProfile(final AlterLockdownProfileContext ctx)
public ASTNode visitAlterPluggableDatabase(final AlterPluggableDatabaseContext ctx) {
return new OracleAlterPluggableDatabaseStatement();
}

@Override
public ASTNode visitCreateProcedure(final CreateProcedureContext ctx) {
OracleCreateProcedureStatement result = new OracleCreateProcedureStatement();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl;

import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateProcedureStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;

@Setter
@ToString(callSuper = true)
public class OracleCreateProcedureStatement extends CreateProcedureStatement implements OracleStatement {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
<create-procedure sql-case-id="create_procedure_with_create_view_as_select_into_outfile" />
<create-procedure sql-case-id="create_procedure_with_sqlexception_and_create_view" />
<create-procedure sql-case-id="create_procedure_with_deterministic_create_view" />
<create-procedure sql-case-id="create_procedure_with_update_statement_oracle" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
<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" />
<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" />
<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" />
<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" />
</sql-cases>

0 comments on commit 2ab3a7a

Please sign in to comment.