Skip to content

Commit f35d24c

Browse files
Richard KooijmanRichard Kooijman
andauthored
added USE SCHEMA <schema> and CREATE OR REPLACE <table> support; things that are allowed in Snowflake SQL (#1409)
Co-authored-by: Richard Kooijman <[email protected]>
1 parent 8eaa4d2 commit f35d24c

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public class UseStatement implements Statement {
1313

1414
private String name;
15+
private boolean schemaKeyword;
1516

1617
public UseStatement() {
1718
// empty constructor
@@ -21,6 +22,11 @@ public UseStatement(String name) {
2122
this.name = name;
2223
}
2324

25+
public UseStatement(String name, boolean hasSchemaKeyword) {
26+
this.name = name;
27+
this.schemaKeyword = hasSchemaKeyword;
28+
}
29+
2430
public String getName() {
2531
return name;
2632
}
@@ -29,9 +35,17 @@ public void setName(String name) {
2935
this.name = name;
3036
}
3137

38+
public boolean hasSchemaKeyword() {
39+
return schemaKeyword;
40+
}
41+
42+
public void setSchemaKeyword(boolean schemaKeyword) {
43+
this.schemaKeyword = schemaKeyword;
44+
}
45+
3246
@Override
3347
public String toString() {
34-
return "USE " + name;
48+
return "USE " + (schemaKeyword ? "SCHEMA " : "") + name;
3549
}
3650

3751
@Override

src/main/java/net/sf/jsqlparser/statement/create/table/CreateTable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class CreateTable implements Statement {
3434
private Table likeTable;
3535
private boolean selectParenthesis;
3636
private boolean ifNotExists = false;
37+
private boolean orReplace = false;
38+
3739
private RowMovement rowMovement;
3840

3941
@Override
@@ -134,6 +136,14 @@ public void setIfNotExists(boolean ifNotExists) {
134136
this.ifNotExists = ifNotExists;
135137
}
136138

139+
public boolean isOrReplace() {
140+
return orReplace;
141+
}
142+
143+
public void setOrReplace(boolean orReplace) {
144+
this.orReplace = orReplace;
145+
}
146+
137147
public boolean isSelectParenthesis() {
138148
return selectParenthesis;
139149
}
@@ -158,6 +168,7 @@ public String toString() {
158168

159169
sql = "CREATE " + (unlogged ? "UNLOGGED " : "")
160170
+ (!"".equals(createOps) ? createOps + " " : "")
171+
+ (orReplace ? "OR REPLACE " : "")
161172
+ "TABLE " + (ifNotExists ? "IF NOT EXISTS " : "") + table;
162173

163174
if (columns != null && !columns.isEmpty()) {

src/main/java/net/sf/jsqlparser/util/deparser/CreateTableDeParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public CreateTableDeParser(StatementDeParser statementDeParser, StringBuilder bu
3535
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
3636
public void deParse(CreateTable createTable) {
3737
buffer.append("CREATE ");
38+
if (createTable.isOrReplace()) {
39+
buffer.append("OR REPLACE ");
40+
}
3841
if (createTable.isUnlogged()) {
3942
buffer.append("UNLOGGED ");
4043
}

src/main/java/net/sf/jsqlparser/util/deparser/UseStatementDeParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public UseStatementDeParser(StringBuilder buffer) {
1919

2020
@Override
2121
public void deParse(UseStatement set) {
22-
buffer.append("USE ").append(set.getName());
22+
buffer.append("USE ");
23+
if (set.hasSchemaKeyword()) {
24+
buffer.append("SCHEMA ");
25+
}
26+
buffer.append(set.getName());
2327
}
2428
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,12 @@ List<ExplainStatement.Option> ExplainStatementOptions():
10351035

10361036
UseStatement Use(): {
10371037
String name;
1038+
boolean hasSchemaKeyword = false;
10381039
}
10391040
{
1040-
<K_USE> name = RelObjectNameExt()
1041+
<K_USE> [ LOOKAHEAD(2) <K_SCHEMA> { hasSchemaKeyword = true; } ] name = RelObjectNameExt()
10411042
{
1042-
return new UseStatement(name);
1043+
return new UseStatement(name, hasSchemaKeyword);
10431044
}
10441045
}
10451046

@@ -4834,6 +4835,7 @@ CreateTable CreateTable():
48344835
}
48354836
{
48364837
<K_CREATE>
4838+
[ <K_OR> <K_REPLACE> { createTable.setOrReplace(true);} ]
48374839
[ <K_UNLOGGED> { createTable.setUnlogged(true); } ]
48384840

48394841
// table options, not required but 1 or none

src/test/java/net/sf/jsqlparser/statement/UseStatementTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
*/
2020
public class UseStatementTest {
2121

22-
22+
@Test
23+
public void testUseSchema() throws JSQLParserException {
24+
assertSqlCanBeParsedAndDeparsed("USE SCHEMA myschema");
25+
}
26+
2327
@Test
2428
public void testSimpleUse() throws JSQLParserException {
2529
assertSqlCanBeParsedAndDeparsed("USE mydatabase");

src/test/java/net/sf/jsqlparser/statement/create/CreateTableTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class CreateTableTest {
4444

4545
private final CCJSqlParserManager parserManager = new CCJSqlParserManager();
4646

47+
@Test
48+
public void testCreateTableOrReplace() throws JSQLParserException {
49+
String statement = "CREATE OR REPLACE TABLE testtab (\"test\" varchar (255))";
50+
assertSqlCanBeParsedAndDeparsed(statement);
51+
}
52+
4753
@Test
4854
public void testCreateTable2() throws JSQLParserException {
4955
String statement = "CREATE TABLE testtab (\"test\" varchar (255))";

0 commit comments

Comments
 (0)