Skip to content

Commit

Permalink
[Oracle SQL] Enhancing CreateFunction of Oracle by adding several kin…
Browse files Browse the repository at this point in the history
…ds of statement (#28625)

* tmp

* add statement

* add statement

* add test
  • Loading branch information
lancelly authored Oct 9, 2023
1 parent 109cb1c commit 1147d58
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ unreservedWord1
| HOST | PORT | EVERY | MINUTES | HOURS | NORELOCATE | SAVE | DISCARD | APPLICATION | INSTALL
| MINIMUM | VERSION | UNINSTALL | COMPATIBILITY | MATERIALIZE | SUBTYPE | RECORD | CONSTANT | CURSOR
| OTHERS | EXCEPTION | CPU_PER_SESSION | CONNECT_TIME | LOGICAL_READS_PER_SESSION | PRIVATE_SGA | PERCENT_RANK | ROWID
| LPAD | ZONE | SESSIONTIMEZONE | TO_CHAR | XMLELEMENT | COLUMN_VALUE | EVALNAME | LEVEL | CONTENT | ON
| LPAD | ZONE | SESSIONTIMEZONE | TO_CHAR | XMLELEMENT | COLUMN_VALUE | EVALNAME | LEVEL | CONTENT | ON | LOOP | EXIT | ELSIF
;

unreservedWord2
Expand Down Expand Up @@ -471,6 +471,10 @@ functionName
: identifier
;

cursorName
: identifier
;

featureId
: numberLiterals
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ ELSE
: E L S E
;

ELSIF
: E L S I F
;

THEN
: T H E N
;
Expand Down Expand Up @@ -416,6 +420,14 @@ LOCALTIMESTAMP
: L O C A L T I M E S T A M P
;

LOOP
: L O O P
;

EXIT
: E X I T
;

YEAR
: Y E A R
;
Expand Down
31 changes: 31 additions & 0 deletions parser/sql/dialect/oracle/src/main/antlr4/imports/oracle/PLSQL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,40 @@ statement
| insert
| lockTable
| merge
| assignmentStatement
| basicLoopStatement
| closeStatement
| fetchStatement
| ifStatment
| returnStatement
) SEMI_
;

basicLoopStatement
: LOOP (statement (EXIT label? (WHEN booleanPrimary)? SEMI_)?)+ END LOOP label?
;

assignmentStatement
: variableName ASSIGNMENT_OPERATOR_ expr
;

closeStatement
: CLOSE cursorName
;

fetchStatement
: FETCH cursorName INTO identifier
;

ifStatment
: IF booleanPrimary THEN statement+ (ELSIF booleanPrimary THEN statement+)? (ELSE statement)? END IF
;

returnStatement
: RETURN expr
;


exceptionHandler
: WHEN ((typeName (OR typeName)*)| OTHERS) THEN statement+
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
<create-function sql-case-id="create_function_call_spec_c" />
<create-function sql-case-id="create_function_with_set_var" />
<create-function sql-case-id="create_function_with_create_view" />
<create-function sql-case-id="create_function_with_loop" />
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@

<sql-case id="create_function_with_set_var" value="CREATE DEFINER = u1@localhost FUNCTION f2() RETURNS int BEGIN DECLARE n int; DECLARE m int; SET n:= (SELECT min(a) FROM t1); SET m:= (SELECT max(a) FROM t1); RETURN n &lt; m; END ;" db-types="MySQL" />
<sql-case id="create_function_with_create_view" value="CREATE FUNCTION bug_13627_f() returns int BEGIN create view v1 as select 1; return 1; END" db-types="MySQL" />
<sql-case id="create_function_with_loop" value="CREATE FUNCTION f(cur SYS_REFCURSOR, mgr_hiredate DATE) RETURN NUMBER IS emp_hiredate DATE; before number :=0; after number:=0; begin loop fetch cur into emp_hiredate; exit when cur%NOTFOUND; if emp_hiredate > mgr_hiredate then after:=after+1; else before:=before+1; end if; end loop; close cur; if before > after then return 1; else return 0; end if; end;" db-types="Oracle" />
</sql-cases>

0 comments on commit 1147d58

Please sign in to comment.