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 {
30
30
private ForceOption force = ForceOption .NONE ;
31
31
private TemporaryOption temp = TemporaryOption .NONE ;
32
32
private boolean withReadOnly = false ;
33
+ private boolean ifNotExists = false ;
33
34
34
35
@ Override
35
36
public void accept (StatementVisitor statementVisitor ) {
@@ -103,6 +104,14 @@ public void setWithReadOnly(boolean withReadOnly) {
103
104
this .withReadOnly = withReadOnly ;
104
105
}
105
106
107
+ public boolean isIfNotExists () {
108
+ return ifNotExists ;
109
+ }
110
+
111
+ public void setIfNotExists (boolean ifNotExists ) {
112
+ this .ifNotExists = ifNotExists ;
113
+ }
114
+
106
115
@ Override
107
116
public String toString () {
108
117
StringBuilder sql = new StringBuilder ("CREATE " );
@@ -129,6 +138,9 @@ public String toString() {
129
138
}
130
139
sql .append ("VIEW " );
131
140
sql .append (view );
141
+ if (ifNotExists ) {
142
+ sql .append (" IF NOT EXISTS" );
143
+ }
132
144
if (columnNames != null ) {
133
145
sql .append (PlainSelect .getStringList (columnNames , true , true ));
134
146
}
Original file line number Diff line number Diff line change @@ -60,6 +60,9 @@ public void deParse(CreateView createView) {
60
60
buffer .append ("MATERIALIZED " );
61
61
}
62
62
buffer .append ("VIEW " ).append (createView .getView ().getFullyQualifiedName ());
63
+ if (createView .isIfNotExists ()) {
64
+ buffer .append (" IF NOT EXISTS" );
65
+ }
63
66
if (createView .getColumnNames () != null ) {
64
67
buffer .append (PlainSelect .getStringList (createView .getColumnNames (), true , true ));
65
68
}
Original file line number Diff line number Diff line change @@ -5486,6 +5486,7 @@ CreateView CreateView():
5486
5486
]
5487
5487
[ <K_MATERIALIZED> { createView.setMaterialized(true);} ]
5488
5488
<K_VIEW> view=Table() { createView.setView(view); }
5489
+ [ LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);} ]
5489
5490
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
5490
5491
<K_AS>
5491
5492
select=SelectWithWithItems( ) { createView.setSelect(select); }
Original file line number Diff line number Diff line change 18
18
import static net .sf .jsqlparser .test .TestUtils .*;
19
19
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
20
import static org .junit .jupiter .api .Assertions .assertFalse ;
21
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22
+
21
23
import org .junit .jupiter .api .Test ;
22
24
23
25
public class CreateViewTest {
@@ -121,4 +123,19 @@ public void testCreateTemporaryViewIssue665() throws JSQLParserException {
121
123
public void testCreateWithReadOnlyViewIssue838 () throws JSQLParserException {
122
124
assertSqlCanBeParsedAndDeparsed ("CREATE VIEW v14(c1, c2) AS SELECT c1, C2 FROM t1 WITH READ ONLY" );
123
125
}
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
+ }
124
141
}
You can’t perform that action at this time.
0 commit comments