File tree Expand file tree Collapse file tree 4 files changed +33
-0
lines changed
jjtree/net/sf/jsqlparser/parser
test/java/net/sf/jsqlparser/statement/create Expand file tree Collapse file tree 4 files changed +33
-0
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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); }
Original file line number Diff line number Diff line change 1818import static net .sf .jsqlparser .test .TestUtils .*;
1919import static org .junit .jupiter .api .Assertions .assertEquals ;
2020import static org .junit .jupiter .api .Assertions .assertFalse ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22+
2123import org .junit .jupiter .api .Test ;
2224
2325public 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}
You can’t perform that action at this time.
0 commit comments