Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5420,6 +5420,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());
}
}