Skip to content

Commit d603e41

Browse files
committed
fixes #102
1 parent 495a7f2 commit d603e41

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

pom.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,6 @@
298298
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
299299
</properties>
300300

301-
<description>
302-
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
303-
The generated hierarchy can be navigated using the Visitor Pattern.
304-
</description>
301+
<description>JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.
302+
The generated hierarchy can be navigated using the Visitor Pattern.</description>
305303
</project>

src/main/java/net/sf/jsqlparser/statement/create/table/CreateTable.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class CreateTable implements Statement {
4242
private List<ColumnDefinition> columnDefinitions;
4343
private List<Index> indexes;
4444
private Select select;
45+
private boolean ifNotExists = false;
4546

4647
@Override
4748
public void accept(StatementVisitor statementVisitor) {
@@ -121,14 +122,22 @@ public void setSelect(Select select) {
121122
this.select = select;
122123
}
123124

125+
public boolean isIfNotExists() {
126+
return ifNotExists;
127+
}
128+
129+
public void setIfNotExists(boolean ifNotExists) {
130+
this.ifNotExists = ifNotExists;
131+
}
132+
124133
@Override
125134
public String toString() {
126135
String sql = "";
127136
String createOps = PlainSelect.getStringList(createOptionsStrings, false, false);
128137

129138
sql = "CREATE " + (unlogged ? "UNLOGGED " : "") +
130139
(!"".equals(createOps)?createOps + " ":"") +
131-
"TABLE " + table;
140+
"TABLE " + (ifNotExists?"IF NOT EXISTS ":"") + table;
132141

133142
if (select != null) {
134143
sql += " AS " + select.toString();

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public void deParse(CreateTable createTable) {
5353
buffer.append(params).append(' ');
5454
}
5555

56-
buffer.append("TABLE ").append(createTable.getTable().getFullyQualifiedName());
56+
buffer.append("TABLE ");
57+
if (createTable.isIfNotExists()) {
58+
buffer.append("IF NOT EXISTS ");
59+
}
60+
buffer.append(createTable.getTable().getFullyQualifiedName());
5761
if (createTable.getSelect() != null) {
5862
buffer.append(" AS ").append(createTable.getSelect().toString());
5963
} else {
@@ -87,7 +91,7 @@ public void deParse(CreateTable createTable) {
8791
buffer.append(")");
8892
}
8993
}
90-
94+
9195
params = PlainSelect.getStringList(createTable.getTableOptionsStrings(), false, false);
9296
if (!"".equals(params)) {
9397
buffer.append(' ').append(params);

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
207207
| <K_COMMIT:"COMMIT">
208208
| <K_UNIQUE:"UNIQUE">
209209
| <K_WITHIN:"WITHIN">
210+
| <K_IF:"IF">
210211
}
211212

212213
TOKEN : /* Numeric Constants */
@@ -2152,7 +2153,9 @@ CreateTable CreateTable():
21522153
[ <K_UNLOGGED> { createTable.setUnlogged(true); } ]
21532154
(parameter = CreateParameter() { createOptions.add(parameter); })*
21542155

2155-
<K_TABLE> table=Table()
2156+
<K_TABLE>
2157+
[ <K_IF> <K_NOT> <K_EXISTS> { createTable.setIfNotExists(true); }]
2158+
table=Table()
21562159
[
21572160
("("
21582161
(columnName=<S_IDENTIFIER>
@@ -2344,7 +2347,7 @@ String CreateParameter():
23442347
tk=<K_NOT> { retval = tk.image; }
23452348
|
23462349
tk=<K_PRIMARY> { retval = tk.image; }
2347-
|
2350+
|
23482351
tk=<K_KEY> { retval = tk.image; }
23492352
|
23502353
tk=<S_CHAR_LITERAL> { retval = tk.image; }

src/test/java/net/sf/jsqlparser/test/create/CreateTableTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public void testCreateTableDefault() throws JSQLParserException {
107107
public void testCreateTableDefault2() throws JSQLParserException {
108108
assertSqlCanBeParsedAndDeparsed("CREATE TABLE T1 (id integer default 1)");
109109
}
110+
111+
public void testCreateTableIfNotExists() throws JSQLParserException {
112+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS animals (id INT NOT NULL)");
113+
}
110114

111115
public void testRUBiSCreateList() throws Exception {
112116
BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));

0 commit comments

Comments
 (0)