Skip to content

Commit 3e84a37

Browse files
committed
fixes #1059
1 parent 6b35e2f commit 3e84a37

File tree

6 files changed

+116
-81
lines changed

6 files changed

+116
-81
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Also I would like to know about needed examples or documentation stuff.
6767

6868
## Extensions in the latest SNAPSHOT version 4.0
6969

70+
* support for **CREATE OR REPLACE** for create function statements
7071
* support for JDBCParameter for interval expressions
7172
* support for **xmlserialize(xmlagg(xmltext( <column> ) ORDER BY <list of columns>) as <column data type> )** expression
7273
* first try to support conditions as select items: **SELECT a < b**

src/main/java/net/sf/jsqlparser/statement/CreateFunctionalStatement.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public abstract class CreateFunctionalStatement implements Statement {
2222

2323
private String kind;
24+
private boolean orReplace = false;
2425

2526
private List<String> functionDeclarationParts;
2627

@@ -29,6 +30,11 @@ protected CreateFunctionalStatement(String kind) {
2930
}
3031

3132
protected CreateFunctionalStatement(String kind, List<String> functionDeclarationParts) {
33+
this(false, kind, functionDeclarationParts);
34+
}
35+
36+
protected CreateFunctionalStatement(boolean orReplace, String kind, List<String> functionDeclarationParts) {
37+
this.orReplace = orReplace;
3238
this.kind = kind;
3339
this.functionDeclarationParts = functionDeclarationParts;
3440
}
@@ -50,6 +56,11 @@ public List<String> getFunctionDeclarationParts() {
5056
public String getKind() {
5157
return kind;
5258
}
59+
60+
public CreateFunctionalStatement withOrReplace(boolean orReplace) {
61+
this.orReplace = orReplace;
62+
return this;
63+
}
5364

5465
/**
5566
* @return a whitespace appended String with the declaration parts with some
@@ -81,7 +92,9 @@ public void accept(StatementVisitor statementVisitor) {
8192

8293
@Override
8394
public String toString() {
84-
return "CREATE " + kind + " " + formatDeclaration();
95+
return "CREATE "
96+
+ (orReplace?"OR REPLACE ":"")
97+
+ kind + " " + formatDeclaration();
8598
}
8699

87100
public CreateFunctionalStatement withFunctionDeclarationParts(List<String> functionDeclarationParts) {

src/main/java/net/sf/jsqlparser/statement/create/function/CreateFunction.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public CreateFunction() {
2424
}
2525

2626
public CreateFunction(List<String> functionDeclarationParts) {
27-
super("FUNCTION", functionDeclarationParts);
27+
this(false, functionDeclarationParts);
28+
}
29+
30+
public CreateFunction(boolean orReplace, List<String> functionDeclarationParts) {
31+
super(orReplace, "FUNCTION", functionDeclarationParts);
2832
}
2933

3034
@Override

src/main/java/net/sf/jsqlparser/statement/create/procedure/CreateProcedure.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public CreateProcedure() {
2323
}
2424

2525
public CreateProcedure(List<String> functionDeclarationParts) {
26-
super("PROCEDURE", functionDeclarationParts);
26+
this(false, functionDeclarationParts);
27+
}
28+
29+
public CreateProcedure(boolean orReplace, List<String> functionDeclarationParts) {
30+
super(orReplace, "PROCEDURE", functionDeclarationParts);
2731
}
2832

2933
@Override

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5092,9 +5092,10 @@ CreateFunctionalStatement CreateFunctionStatement():
50925092
CreateFunctionalStatement type = null;
50935093
List<String> tokens = new LinkedList<String>();
50945094
String statementType = null;
5095+
boolean orReplace = false;
50955096
}
50965097
{
5097-
<K_CREATE>
5098+
<K_CREATE> [<K_OR> <K_REPLACE> { orReplace = true; } ]
50985099
(
50995100
<K_FUNCTION> { statementType = "FUNCTION"; }
51005101
|
@@ -5103,10 +5104,10 @@ CreateFunctionalStatement CreateFunctionStatement():
51035104
tokens=captureRest()
51045105
{
51055106
if(statementType.equals("FUNCTION")) {
5106-
type = new CreateFunction(tokens);
5107+
type = new CreateFunction(orReplace, tokens);
51075108
}
51085109
if(statementType.equals("PROCEDURE")) {
5109-
type = new CreateProcedure(tokens);
5110+
type = new CreateProcedure(orReplace, tokens);
51105111
}
51115112

51125113
return type;
Lines changed: 87 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,87 @@
1-
/*-
2-
* #%L
3-
* JSQLParser library
4-
* %%
5-
* Copyright (C) 2004 - 2020 JSQLParser
6-
* %%
7-
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8-
* #L%
9-
*/
10-
package net.sf.jsqlparser.statement.create;
11-
12-
import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
13-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
14-
import static org.assertj.core.api.Assertions.assertThat;
15-
import java.util.Arrays;
16-
import org.junit.Test;
17-
import net.sf.jsqlparser.JSQLParserException;
18-
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
19-
import net.sf.jsqlparser.statement.create.function.CreateFunction;
20-
import net.sf.jsqlparser.statement.create.procedure.CreateProcedure;
21-
22-
/**
23-
* Tests the behavior of {@link net.sf.jsqlparser.statement.CreateFunctionalStatement funtion statements}
24-
*/
25-
public class CreateFunctionalStatementTest {
26-
27-
@Test
28-
public void createFunctionMinimal() throws JSQLParserException {
29-
String statement = "CREATE FUNCTION foo RETURN 5; END;";
30-
assertSqlCanBeParsedAndDeparsed(statement);
31-
assertDeparse(
32-
new CreateFunction().addFunctionDeclarationParts("foo")
33-
.addFunctionDeclarationParts(Arrays.asList("RETURN 5;", "END;")),
34-
statement);
35-
}
36-
37-
@Test
38-
public void createFunctionLong() throws JSQLParserException {
39-
CreateFunction stm = (CreateFunction) CCJSqlParserUtil.parse("CREATE FUNCTION fun(query_from_time date) RETURNS TABLE(foo double precision, bar double precision)\n" +
40-
" LANGUAGE plpgsql\n" +
41-
" AS $$\n" +
42-
" BEGIN\n" +
43-
" RETURN QUERY\n" +
44-
" WITH bla AS (\n" +
45-
" SELECT * from foo)\n" +
46-
" Select * from bla;\n" +
47-
" END;\n" +
48-
" $$;");
49-
assertThat(stm).isNotNull();
50-
assertThat(stm.formatDeclaration()).contains("fun ( query_from_time date )");
51-
}
52-
53-
@Test
54-
public void createProcedureMinimal() throws JSQLParserException {
55-
String statement = "CREATE PROCEDURE foo AS BEGIN END;";
56-
assertSqlCanBeParsedAndDeparsed(statement);
57-
assertDeparse(
58-
new CreateProcedure().addFunctionDeclarationParts("foo", "AS")
59-
.addFunctionDeclarationParts(Arrays.asList("BEGIN", "END;")),
60-
statement);
61-
}
62-
63-
@Test
64-
public void createProcedureLong() throws JSQLParserException {
65-
CreateProcedure stm = (CreateProcedure) CCJSqlParserUtil.parse("CREATE PROCEDURE remove_emp (employee_id NUMBER) AS\n" +
66-
" tot_emps NUMBER;\n" +
67-
" BEGIN\n" +
68-
" DELETE FROM employees\n" +
69-
" WHERE employees.employee_id = remove_emp.employee_id;\n" +
70-
" tot_emps := tot_emps - 1;\n" +
71-
" END;");
72-
assertThat(stm).isNotNull();
73-
assertThat(stm.formatDeclaration()).contains("remove_emp ( employee_id NUMBER )");
74-
}
75-
}
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2020 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create;
11+
12+
import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
13+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import java.util.Arrays;
16+
import org.junit.Test;
17+
import net.sf.jsqlparser.JSQLParserException;
18+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
19+
import net.sf.jsqlparser.statement.create.function.CreateFunction;
20+
import net.sf.jsqlparser.statement.create.procedure.CreateProcedure;
21+
22+
/**
23+
* Tests the behavior of {@link net.sf.jsqlparser.statement.CreateFunctionalStatement funtion statements}
24+
*/
25+
public class CreateFunctionalStatementTest {
26+
27+
@Test
28+
public void createFunctionMinimal() throws JSQLParserException {
29+
String statement = "CREATE FUNCTION foo RETURN 5; END;";
30+
assertSqlCanBeParsedAndDeparsed(statement);
31+
assertDeparse(
32+
new CreateFunction().addFunctionDeclarationParts("foo")
33+
.addFunctionDeclarationParts(Arrays.asList("RETURN 5;", "END;")),
34+
statement);
35+
}
36+
37+
@Test
38+
public void createFunctionLong() throws JSQLParserException {
39+
CreateFunction stm = (CreateFunction) CCJSqlParserUtil.parse("CREATE FUNCTION fun(query_from_time date) RETURNS TABLE(foo double precision, bar double precision)\n" +
40+
" LANGUAGE plpgsql\n" +
41+
" AS $$\n" +
42+
" BEGIN\n" +
43+
" RETURN QUERY\n" +
44+
" WITH bla AS (\n" +
45+
" SELECT * from foo)\n" +
46+
" Select * from bla;\n" +
47+
" END;\n" +
48+
" $$;");
49+
assertThat(stm).isNotNull();
50+
assertThat(stm.formatDeclaration()).contains("fun ( query_from_time date )");
51+
}
52+
53+
@Test
54+
public void createProcedureMinimal() throws JSQLParserException {
55+
String statement = "CREATE PROCEDURE foo AS BEGIN END;";
56+
assertSqlCanBeParsedAndDeparsed(statement);
57+
assertDeparse(
58+
new CreateProcedure().addFunctionDeclarationParts("foo", "AS")
59+
.addFunctionDeclarationParts(Arrays.asList("BEGIN", "END;")),
60+
statement);
61+
}
62+
63+
@Test
64+
public void createProcedureLong() throws JSQLParserException {
65+
CreateProcedure stm = (CreateProcedure) CCJSqlParserUtil.parse("CREATE PROCEDURE remove_emp (employee_id NUMBER) AS\n" +
66+
" tot_emps NUMBER;\n" +
67+
" BEGIN\n" +
68+
" DELETE FROM employees\n" +
69+
" WHERE employees.employee_id = remove_emp.employee_id;\n" +
70+
" tot_emps := tot_emps - 1;\n" +
71+
" END;");
72+
assertThat(stm).isNotNull();
73+
assertThat(stm.formatDeclaration()).contains("remove_emp ( employee_id NUMBER )");
74+
}
75+
76+
@Test
77+
public void createOrReplaceFunctionMinimal() throws JSQLParserException {
78+
String statement = "CREATE OR REPLACE FUNCTION foo RETURN 5; END;";
79+
assertSqlCanBeParsedAndDeparsed(statement);
80+
assertDeparse(
81+
new CreateFunction()
82+
.withOrReplace(true)
83+
.addFunctionDeclarationParts("foo")
84+
.addFunctionDeclarationParts(Arrays.asList("RETURN 5;", "END;")),
85+
statement);
86+
}
87+
}

0 commit comments

Comments
 (0)