Skip to content

Commit d7bf249

Browse files
feat: CREATE SCHEMA IF NOT EXISTS ...
- fixes #2061 Signed-off-by: Andreas Reichel <[email protected]> Signed-off-by: manticore-projects <[email protected]>
1 parent 11616a0 commit d7bf249

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/main/java/net/sf/jsqlparser/statement/create/schema/CreateSchema.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class CreateSchema implements Statement {
2424
private String schemaName;
2525
private List<String> schemaPath;
2626
private List<Statement> statements = new ArrayList<>();
27+
private boolean hasIfNotExists = false;
2728

2829
@Override
2930
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
@@ -103,8 +104,20 @@ public List<Statement> getStatements() {
103104
return statements;
104105
}
105106

107+
public boolean hasIfNotExists() {
108+
return hasIfNotExists;
109+
}
110+
111+
public CreateSchema setIfNotExists(boolean hasIfNotExists) {
112+
this.hasIfNotExists = hasIfNotExists;
113+
return this;
114+
}
115+
106116
public String toString() {
107117
String sql = "CREATE SCHEMA";
118+
if (hasIfNotExists) {
119+
sql += " IF NOT EXISTS";
120+
}
108121
if (schemaName != null) {
109122
sql += " " + schemaName;
110123
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5946,7 +5946,7 @@ CreateSchema CreateSchema():
59465946
List<Statement> statements = new ArrayList<Statement>();
59475947
}
59485948
{
5949-
<K_SCHEMA>
5949+
<K_SCHEMA> [ <K_IF> <K_NOT> <K_EXISTS> { schema.setIfNotExists(true); } ]
59505950
[ ( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setSchemaName(tk.image); } ]
59515951
[ <K_AUTHORIZATION>
59525952
(tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { schema.setAuthorization(tk.image); }

src/test/java/net/sf/jsqlparser/statement/create/schema/CreateSchemaTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public void testSimpleCreateWithAuth() throws JSQLParserException {
3434
assertDeparse(new CreateSchema().withSchemaName("myschema").withAuthorization("myauth"),
3535
statement);
3636
}
37+
38+
@Test
39+
void testIfNotExistsIssue2061() throws JSQLParserException {
40+
String sqlStr = "CREATE SCHEMA IF NOT EXISTS sales_kpi";
41+
assertSqlCanBeParsedAndDeparsed(sqlStr);
42+
}
3743
}

0 commit comments

Comments
 (0)