Skip to content

Commit 6fb15a1

Browse files
committed
fixes #402
1 parent 46720ef commit 6fb15a1

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class CreateTable implements Statement {
4141
private List<ColumnDefinition> columnDefinitions;
4242
private List<Index> indexes;
4343
private Select select;
44+
private boolean selectParenthesis;
4445
private boolean ifNotExists = false;
4546

4647
@Override
@@ -117,8 +118,9 @@ public Select getSelect() {
117118
return select;
118119
}
119120

120-
public void setSelect(Select select) {
121+
public void setSelect(Select select, boolean parenthesis) {
121122
this.select = select;
123+
this.selectParenthesis = parenthesis;
122124
}
123125

124126
public boolean isIfNotExists() {
@@ -129,6 +131,14 @@ public void setIfNotExists(boolean ifNotExists) {
129131
this.ifNotExists = ifNotExists;
130132
}
131133

134+
public boolean isSelectParenthesis() {
135+
return selectParenthesis;
136+
}
137+
138+
public void setSelectParenthesis(boolean selectParenthesis) {
139+
this.selectParenthesis = selectParenthesis;
140+
}
141+
132142
@Override
133143
public String toString() {
134144
String sql;
@@ -139,7 +149,7 @@ public String toString() {
139149
"TABLE " + (ifNotExists?"IF NOT EXISTS ":"") + table;
140150

141151
if (select != null) {
142-
sql += " AS " + select.toString();
152+
sql += " AS " + (selectParenthesis?"(":"") + select.toString() + (selectParenthesis?")":"");
143153
} else {
144154
sql += " (";
145155

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import net.sf.jsqlparser.statement.select.PlainSelect;
3030

3131
/**
32-
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
33-
* string) a {@link net.sf.jsqlparser.statement.create.table.CreateTable}
32+
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a string) a
33+
* {@link net.sf.jsqlparser.statement.create.table.CreateTable}
3434
*/
3535
public class CreateTableDeParser {
3636

@@ -48,7 +48,8 @@ public void deParse(CreateTable createTable) {
4848
if (createTable.isUnlogged()) {
4949
buffer.append("UNLOGGED ");
5050
}
51-
String params = PlainSelect.getStringList(createTable.getCreateOptionsStrings(), false, false);
51+
String params = PlainSelect.
52+
getStringList(createTable.getCreateOptionsStrings(), false, false);
5253
if (!"".equals(params)) {
5354
buffer.append(params).append(' ');
5455
}
@@ -59,11 +60,19 @@ public void deParse(CreateTable createTable) {
5960
}
6061
buffer.append(createTable.getTable().getFullyQualifiedName());
6162
if (createTable.getSelect() != null) {
62-
buffer.append(" AS ").append(createTable.getSelect().toString());
63+
buffer.append(" AS ");
64+
if (createTable.isSelectParenthesis()) {
65+
buffer.append("(");
66+
}
67+
buffer.append(createTable.getSelect().toString());
68+
if (createTable.isSelectParenthesis()) {
69+
buffer.append(")");
70+
}
6371
} else {
6472
if (createTable.getColumnDefinitions() != null) {
6573
buffer.append(" (");
66-
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) {
74+
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.
75+
hasNext();) {
6776
ColumnDefinition columnDefinition = iter.next();
6877
buffer.append(columnDefinition.getColumnName());
6978
buffer.append(" ");

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,8 @@ CreateTable CreateTable():
28342834
)
28352835
|
28362836
(
2837-
<K_AS> select = Select() { createTable.setSelect(select); }
2837+
<K_AS> ( LOOKAHEAD("(" Select() ")") "(" select = Select() { createTable.setSelect(select, true); } ")"
2838+
| select = Select() { createTable.setSelect(select, false); } )
28382839
)
28392840
]
28402841

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ public void testExcludeWhereConstraint() throws JSQLParserException {
243243
public void testTimestampWithoutTimezone() throws JSQLParserException {
244244
assertSqlCanBeParsedAndDeparsed("CREATE TABLE abc.tabc (transaction_date TIMESTAMP WITHOUT TIME ZONE)");
245245
}
246+
247+
public void testCreateUnitonIssue402() throws JSQLParserException {
248+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE temp.abc AS SELECT sku FROM temp.a UNION SELECT sku FROM temp.b");
249+
}
250+
251+
public void testCreateUnitonIssue402_2() throws JSQLParserException {
252+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE temp.abc AS (SELECT sku FROM temp.a UNION SELECT sku FROM temp.b)");
253+
}
246254

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

0 commit comments

Comments
 (0)