Skip to content

Commit 0f34f5b

Browse files
authored
Fix #1686: add support for creating views with "IF NOT EXISTS" clause (#1690)
1 parent 8d9db70 commit 0f34f5b

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/main/java/net/sf/jsqlparser/statement/create/view/CreateView.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CreateView implements Statement {
3030
private ForceOption force = ForceOption.NONE;
3131
private TemporaryOption temp = TemporaryOption.NONE;
3232
private boolean withReadOnly = false;
33+
private boolean ifNotExists = false;
3334

3435
@Override
3536
public void accept(StatementVisitor statementVisitor) {
@@ -103,6 +104,14 @@ public void setWithReadOnly(boolean withReadOnly) {
103104
this.withReadOnly = withReadOnly;
104105
}
105106

107+
public boolean isIfNotExists() {
108+
return ifNotExists;
109+
}
110+
111+
public void setIfNotExists(boolean ifNotExists) {
112+
this.ifNotExists = ifNotExists;
113+
}
114+
106115
@Override
107116
public String toString() {
108117
StringBuilder sql = new StringBuilder("CREATE ");
@@ -129,6 +138,9 @@ public String toString() {
129138
}
130139
sql.append("VIEW ");
131140
sql.append(view);
141+
if (ifNotExists) {
142+
sql.append(" IF NOT EXISTS");
143+
}
132144
if (columnNames != null) {
133145
sql.append(PlainSelect.getStringList(columnNames, true, true));
134146
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public void deParse(CreateView createView) {
6060
buffer.append("MATERIALIZED ");
6161
}
6262
buffer.append("VIEW ").append(createView.getView().getFullyQualifiedName());
63+
if (createView.isIfNotExists()) {
64+
buffer.append(" IF NOT EXISTS");
65+
}
6366
if (createView.getColumnNames() != null) {
6467
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
6568
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5486,6 +5486,7 @@ CreateView CreateView():
54865486
]
54875487
[ <K_MATERIALIZED> { createView.setMaterialized(true);} ]
54885488
<K_VIEW> view=Table() { createView.setView(view); }
5489+
[ LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);} ]
54895490
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
54905491
<K_AS>
54915492
select=SelectWithWithItems( ) { createView.setSelect(select); }

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static net.sf.jsqlparser.test.TestUtils.*;
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
2123
import org.junit.jupiter.api.Test;
2224

2325
public class CreateViewTest {
@@ -121,4 +123,19 @@ public void testCreateTemporaryViewIssue665() throws JSQLParserException {
121123
public void testCreateWithReadOnlyViewIssue838() throws JSQLParserException {
122124
assertSqlCanBeParsedAndDeparsed("CREATE VIEW v14(c1, c2) AS SELECT c1, C2 FROM t1 WITH READ ONLY");
123125
}
126+
127+
@Test
128+
public void testCreateViewIfNotExists() throws JSQLParserException {
129+
String stmt = "CREATE VIEW myview IF NOT EXISTS AS SELECT * FROM mytab";
130+
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
131+
assertTrue(createView.isIfNotExists());
132+
}
133+
134+
@Test
135+
public void testCreateMaterializedViewIfNotExists() throws JSQLParserException {
136+
String stmt = "CREATE MATERIALIZED VIEW myview IF NOT EXISTS AS SELECT * FROM mytab";
137+
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
138+
assertTrue(createView.isMaterialized());
139+
assertTrue(createView.isIfNotExists());
140+
}
124141
}

0 commit comments

Comments
 (0)