Skip to content

Commit

Permalink
Fix #1686: add support for creating views with "IF NOT EXISTS" clause (
Browse files Browse the repository at this point in the history
  • Loading branch information
zaza authored Dec 22, 2022
1 parent 8d9db70 commit 0f34f5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class CreateView implements Statement {
private ForceOption force = ForceOption.NONE;
private TemporaryOption temp = TemporaryOption.NONE;
private boolean withReadOnly = false;
private boolean ifNotExists = false;

@Override
public void accept(StatementVisitor statementVisitor) {
Expand Down Expand Up @@ -103,6 +104,14 @@ public void setWithReadOnly(boolean withReadOnly) {
this.withReadOnly = withReadOnly;
}

public boolean isIfNotExists() {
return ifNotExists;
}

public void setIfNotExists(boolean ifNotExists) {
this.ifNotExists = ifNotExists;
}

@Override
public String toString() {
StringBuilder sql = new StringBuilder("CREATE ");
Expand All @@ -129,6 +138,9 @@ public String toString() {
}
sql.append("VIEW ");
sql.append(view);
if (ifNotExists) {
sql.append(" IF NOT EXISTS");
}
if (columnNames != null) {
sql.append(PlainSelect.getStringList(columnNames, true, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public void deParse(CreateView createView) {
buffer.append("MATERIALIZED ");
}
buffer.append("VIEW ").append(createView.getView().getFullyQualifiedName());
if (createView.isIfNotExists()) {
buffer.append(" IF NOT EXISTS");
}
if (createView.getColumnNames() != null) {
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
}
Expand Down
1 change: 1 addition & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -5486,6 +5486,7 @@ CreateView CreateView():
]
[ <K_MATERIALIZED> { createView.setMaterialized(true);} ]
<K_VIEW> view=Table() { createView.setView(view); }
[ LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);} ]
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
<K_AS>
select=SelectWithWithItems( ) { createView.setSelect(select); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static net.sf.jsqlparser.test.TestUtils.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class CreateViewTest {
Expand Down Expand Up @@ -121,4 +123,19 @@ public void testCreateTemporaryViewIssue665() throws JSQLParserException {
public void testCreateWithReadOnlyViewIssue838() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE VIEW v14(c1, c2) AS SELECT c1, C2 FROM t1 WITH READ ONLY");
}

@Test
public void testCreateViewIfNotExists() throws JSQLParserException {
String stmt = "CREATE VIEW myview IF NOT EXISTS AS SELECT * FROM mytab";
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
assertTrue(createView.isIfNotExists());
}

@Test
public void testCreateMaterializedViewIfNotExists() throws JSQLParserException {
String stmt = "CREATE MATERIALIZED VIEW myview IF NOT EXISTS AS SELECT * FROM mytab";
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
assertTrue(createView.isMaterialized());
assertTrue(createView.isIfNotExists());
}
}

0 comments on commit 0f34f5b

Please sign in to comment.