Skip to content

Commit

Permalink
added USE SCHEMA <schema> and CREATE OR REPLACE <table> support; thin…
Browse files Browse the repository at this point in the history
…gs that are allowed in Snowflake SQL (#1409)

Co-authored-by: Richard Kooijman <richard.kooijman@inergy.nl>
  • Loading branch information
Richard Kooijman and Richard Kooijman authored Nov 19, 2021
1 parent 8eaa4d2 commit f35d24c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/main/java/net/sf/jsqlparser/statement/UseStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class UseStatement implements Statement {

private String name;
private boolean schemaKeyword;

public UseStatement() {
// empty constructor
Expand All @@ -21,6 +22,11 @@ public UseStatement(String name) {
this.name = name;
}

public UseStatement(String name, boolean hasSchemaKeyword) {
this.name = name;
this.schemaKeyword = hasSchemaKeyword;
}

public String getName() {
return name;
}
Expand All @@ -29,9 +35,17 @@ public void setName(String name) {
this.name = name;
}

public boolean hasSchemaKeyword() {
return schemaKeyword;
}

public void setSchemaKeyword(boolean schemaKeyword) {
this.schemaKeyword = schemaKeyword;
}

@Override
public String toString() {
return "USE " + name;
return "USE " + (schemaKeyword ? "SCHEMA " : "") + name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class CreateTable implements Statement {
private Table likeTable;
private boolean selectParenthesis;
private boolean ifNotExists = false;
private boolean orReplace = false;

private RowMovement rowMovement;

@Override
Expand Down Expand Up @@ -134,6 +136,14 @@ public void setIfNotExists(boolean ifNotExists) {
this.ifNotExists = ifNotExists;
}

public boolean isOrReplace() {
return orReplace;
}

public void setOrReplace(boolean orReplace) {
this.orReplace = orReplace;
}

public boolean isSelectParenthesis() {
return selectParenthesis;
}
Expand All @@ -158,6 +168,7 @@ public String toString() {

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

if (columns != null && !columns.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public CreateTableDeParser(StatementDeParser statementDeParser, StringBuilder bu
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void deParse(CreateTable createTable) {
buffer.append("CREATE ");
if (createTable.isOrReplace()) {
buffer.append("OR REPLACE ");
}
if (createTable.isUnlogged()) {
buffer.append("UNLOGGED ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public UseStatementDeParser(StringBuilder buffer) {

@Override
public void deParse(UseStatement set) {
buffer.append("USE ").append(set.getName());
buffer.append("USE ");
if (set.hasSchemaKeyword()) {
buffer.append("SCHEMA ");
}
buffer.append(set.getName());
}
}
6 changes: 4 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -1035,11 +1035,12 @@ List<ExplainStatement.Option> ExplainStatementOptions():

UseStatement Use(): {
String name;
boolean hasSchemaKeyword = false;
}
{
<K_USE> name = RelObjectNameExt()
<K_USE> [ LOOKAHEAD(2) <K_SCHEMA> { hasSchemaKeyword = true; } ] name = RelObjectNameExt()
{
return new UseStatement(name);
return new UseStatement(name, hasSchemaKeyword);
}
}

Expand Down Expand Up @@ -4834,6 +4835,7 @@ CreateTable CreateTable():
}
{
<K_CREATE>
[ <K_OR> <K_REPLACE> { createTable.setOrReplace(true);} ]
[ <K_UNLOGGED> { createTable.setUnlogged(true); } ]

// table options, not required but 1 or none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
*/
public class UseStatementTest {


@Test
public void testUseSchema() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("USE SCHEMA myschema");
}

@Test
public void testSimpleUse() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("USE mydatabase");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class CreateTableTest {

private final CCJSqlParserManager parserManager = new CCJSqlParserManager();

@Test
public void testCreateTableOrReplace() throws JSQLParserException {
String statement = "CREATE OR REPLACE TABLE testtab (\"test\" varchar (255))";
assertSqlCanBeParsedAndDeparsed(statement);
}

@Test
public void testCreateTable2() throws JSQLParserException {
String statement = "CREATE TABLE testtab (\"test\" varchar (255))";
Expand Down

0 comments on commit f35d24c

Please sign in to comment.