Skip to content

Commit

Permalink
[Oracle SQL] Add PLSQL_BLOCK for PLSQL of Oracle (#28757)
Browse files Browse the repository at this point in the history
* add plsqlblock

* add plsqlblock

* fix
  • Loading branch information
lancelly authored Oct 18, 2023
1 parent 2ee5baf commit 5a26933
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ plsqlProcedureSource
((defaultCollationClause | invokerRightsClause | accessibleByClause)*)? (IS | AS) (callSpec | declareSection? body)
;

plsqlBlock
: (SIGNED_LEFT_SHIFT_ label SIGNED_RIGHT_SHIFT_)* DECLARE declareSection body
;

createFunction
: CREATE (OR REPLACE)? (EDITIONABLE | NONEDITIONABLE)? FUNCTION plsqlFunctionSource
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,6 @@ execute
| createType
| createCluster
| createJava
| plsqlBlock
) SEMI_?
;
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.OutOfLineConstraintContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.OutOfLineRefConstraintContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.PackageNameContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.PlsqlBlockContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.PurgeContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.RelationalPropertyContext;
import org.apache.shardingsphere.sql.parser.autogen.OracleStatementParser.RenameContext;
Expand Down Expand Up @@ -301,6 +302,7 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleFlashbackDatabaseStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleFlashbackTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleNoAuditStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OraclePLSQLBlockStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OraclePurgeStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleRenameStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.ddl.OracleSystemActionStatement;
Expand Down Expand Up @@ -1241,6 +1243,11 @@ public ASTNode visitCreateProcedure(final CreateProcedureContext ctx) {
return new OracleCreateProcedureStatement();
}

@Override
public ASTNode visitPlsqlBlock(final PlsqlBlockContext ctx) {
return new OraclePLSQLBlockStatement();
}

@Override
public ASTNode visitAlterProcedure(final AlterProcedureContext ctx) {
return new OracleAlterProcedureStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ public enum SQLVisitorRule {

EMPTY_STATEMENT("EmptyStatement", SQLStatementType.DAL),

CREATE_JAVA("CreateJava", SQLStatementType.DDL);
CREATE_JAVA("CreateJava", SQLStatementType.DDL),

PLSQL_BLOCK("PlsqlBlock", SQLStatementType.DDL);

private final String name;

Expand Down
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 org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;

public final class OraclePLSQLBlockStatement implements OracleStatement {

@Override
public int getParameterCount() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<create-procedure sql-case-id="create_procedure_with_prepare_commit" />
<create-procedure sql-case-id="create_procedure_with_cursor_definition" />
<create-procedure sql-case-id="create_procedure_with_collection_type_definition" />
<create-procedure sql-case-id="create_plsql_block" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@
<sql-case id="create_procedure_with_prepare_commit" value="CREATE DEFINER=`sys_data`@`%` PROCEDURE `TEST1` (a VARCHAR(64),b VARCHAR(64),c VARCHAR(64),d VARCHAR(8),e INT) BEGIN DECLARE f VARCHAR(8); SET f=DATE_FORMAT(DATE_ADD((STR_TO_DATE(d,'%Y%m%d')),INTERVAL -(e) MONTH),'%Y%m%d'); SET @aaa=CONCAT('delete from ',a,'.',b,' where ',c,'&lt;=',f,' or ', c,'&lt;&gt; LAST_DAY(',c,')', 'or ',c,'=', d); PREPARE stmt FROM @aaa; EXECUTE stmt; COMMIT; DEALLOCATE PREPARE stmt; END" db-types="MySQL" />
<sql-case id="create_procedure_with_cursor_definition" value="CREATE OR REPLACE EDITIONABLE PROCEDURE my_proc AS cursor cursor1 is select distinct a from tbl; var1 varchar2(500); var2 varchar2(500); BEGIN INSERT INTO t VALUES (var1, var2); END;" db-types="Oracle" />
<sql-case id="create_procedure_with_collection_type_definition" value="CREATE OR REPLACE EDITIONABLE PROCEDURE my_proc AS TYPE my_type is table of my_table.id%TYPE; BEGIN INSERT INTO t VALUES (var1, var2); END;" db-types="Oracle" />
<sql-case id="create_plsql_block" value="DECLARE warehouse NUMBER := 1; ground NUMBER := 1; insured NUMBER := 1; result NUMBER; BEGIN SELECT BIN_TO_NUM(warehouse, ground, insured) INTO result FROM DUAL; UPDATE orders SET order_status = result WHERE order_id = 2441; END;" db-types="Oracle" />
</sql-cases>

0 comments on commit 5a26933

Please sign in to comment.