From 2ab3a7ab659a2e25cf9e47035c612070c25ae5f9 Mon Sep 17 00:00:00 2001 From: tianhao960 Date: Tue, 23 Aug 2022 11:55:05 +0800 Subject: [PATCH] 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 --- .../main/antlr4/imports/oracle/BaseRule.g4 | 3 +- .../src/main/antlr4/imports/oracle/Keyword.g4 | 4 + .../antlr4/imports/oracle/OracleKeyword.g4 | 28 +++ .../antlr4/imports/oracle/StoreProcedure.g4 | 186 +++++++++++++++++- .../sql/parser/autogen/OracleStatement.g4 | 1 + .../impl/OracleDDLStatementSQLVisitor.java | 8 + .../ddl/OracleCreateProcedureStatement.java | 28 +++ .../resources/case/ddl/create-procedure.xml | 1 + .../sql/supported/ddl/create-procedure.xml | 1 + 9 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/ddl/OracleCreateProcedureStatement.java diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/BaseRule.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/BaseRule.g4 index 3a589f49165e5..9132adff4fbb1 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/BaseRule.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/BaseRule.g4 @@ -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 diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/Keyword.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/Keyword.g4 index 7b9dbfa1c792c..91866dcb39002 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/Keyword.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/Keyword.g4 @@ -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 + ; \ No newline at end of file diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 index a54e1930e7248..7489a37b0fdbb 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/OracleKeyword.g4 @@ -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] ; @@ -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 + ; \ No newline at end of file diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/StoreProcedure.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/StoreProcedure.g4 index 685a33e49c60c..23b2713aae3dd 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/StoreProcedure.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/imports/oracle/StoreProcedure.g4 @@ -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 + ; diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/OracleStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/OracleStatement.g4 index 1fdc2a528f14f..9e284a848173b 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/OracleStatement.g4 +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-oracle/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/OracleStatement.g4 @@ -122,5 +122,6 @@ execute | alterHierarchy | alterLockdownProfile | alterPluggableDatabase + | createProcedure ) SEMI_? ; diff --git a/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 b/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 index 652c08bb309ee..fa81af7034971 100644 --- a/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 +++ b/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 @@ -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; @@ -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; @@ -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; + } } diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/ddl/OracleCreateProcedureStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/ddl/OracleCreateProcedureStatement.java new file mode 100644 index 0000000000000..af7f53ca7e346 --- /dev/null +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/oracle/ddl/OracleCreateProcedureStatement.java @@ -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 { +} diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/create-procedure.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/create-procedure.xml index 9fe6b773ada00..4356dc3a684f3 100644 --- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/create-procedure.xml +++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/create-procedure.xml @@ -30,4 +30,5 @@ + diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-procedure.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-procedure.xml index 6346024a60573..a6dd6e1471f80 100644 --- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-procedure.xml +++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/create-procedure.xml @@ -42,4 +42,5 @@ +