Skip to content

Commit b62f19f

Browse files
Create temporary table t(c1, c2) as select ... (#1225)
Co-authored-by: Francois Secherre <[email protected]>
1 parent 8eb3d9a commit b62f19f

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Collections;
1515
import java.util.List;
1616
import java.util.Optional;
17+
1718
import net.sf.jsqlparser.schema.Table;
1819
import net.sf.jsqlparser.statement.Statement;
1920
import net.sf.jsqlparser.statement.StatementVisitor;
@@ -27,6 +28,7 @@ public class CreateTable implements Statement {
2728
private List<String> createOptionsStrings;
2829
private List<String> tableOptionsStrings;
2930
private List<ColumnDefinition> columnDefinitions;
31+
private List<String> columns;
3032
private List<Index> indexes;
3133
private Select select;
3234
private Table likeTable;
@@ -66,6 +68,14 @@ public void setColumnDefinitions(List<ColumnDefinition> list) {
6668
columnDefinitions = list;
6769
}
6870

71+
public List<String> getColumns() {
72+
return this.columns;
73+
}
74+
75+
public void setColumns(List<String> columns) {
76+
this.columns =columns;
77+
}
78+
6979
/**
7080
* @return a list of options (as simple strings) of this table definition, as ("TYPE", "=", "MYISAM")
7181
*/
@@ -150,6 +160,10 @@ public String toString() {
150160
+ (!"".equals(createOps) ? createOps + " " : "")
151161
+ "TABLE " + (ifNotExists ? "IF NOT EXISTS " : "") + table;
152162

163+
if (columns != null && !columns.isEmpty()) {
164+
sql += " ";
165+
sql += PlainSelect.getStringList(columns, true, true);
166+
}
153167
if (columnDefinitions != null && !columnDefinitions.isEmpty()) {
154168
sql += " (";
155169

@@ -217,6 +231,11 @@ public CreateTable withColumnDefinitions(List<ColumnDefinition> columnDefinition
217231
return this;
218232
}
219233

234+
public CreateTable withColumns(List<String> columns) {
235+
this.setColumns(columns);
236+
return this;
237+
}
238+
220239
public CreateTable withIndexes(List<Index> indexes) {
221240
this.setIndexes(indexes);
222241
return this;
@@ -246,6 +265,18 @@ public CreateTable addColumnDefinitions(Collection<? extends ColumnDefinition> c
246265
return this.withColumnDefinitions(collection);
247266
}
248267

268+
public CreateTable addColumns(String... columns) {
269+
List<String> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
270+
Collections.addAll(collection, columns);
271+
return this.withColumns(collection);
272+
}
273+
274+
public CreateTable addColumns(Collection<String> columns) {
275+
List<String> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
276+
collection.addAll(columns);
277+
return this.withColumns(collection);
278+
}
279+
249280
public CreateTable addIndexes(Index... indexes) {
250281
List<Index> collection = Optional.ofNullable(getIndexes()).orElseGet(ArrayList::new);
251282
Collections.addAll(collection, indexes);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ public void deParse(CreateTable createTable) {
4949
}
5050
buffer.append(createTable.getTable().getFullyQualifiedName());
5151

52+
if (createTable.getColumns() != null && !createTable.getColumns().isEmpty()) {
53+
buffer.append(" (");
54+
Iterator<String> columnIterator = createTable.getColumns().iterator();
55+
buffer.append(columnIterator.next());
56+
while (columnIterator.hasNext()) {
57+
buffer.append(", ").append(columnIterator.next());
58+
}
59+
buffer.append(")");
60+
}
5261
if (createTable.getColumnDefinitions() != null) {
5362
buffer.append(" (");
5463
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4253,6 +4253,8 @@ CreateTable CreateTable():
42534253
ExcludeConstraint excludeC = null;
42544254
RowMovement rowMovement = null;
42554255
ReferentialAction.Action action = null;
4256+
String tableColumn = null;
4257+
List<String> columns = new ArrayList<String>();
42564258
}
42574259
{
42584260
<K_CREATE>
@@ -4268,7 +4270,8 @@ CreateTable CreateTable():
42684270
<K_TABLE>
42694271
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { createTable.setIfNotExists(true); }]
42704272
table=Table()
4271-
[ LOOKAHEAD(2)
4273+
[ ( LOOKAHEAD(3) "(" tableColumn=RelObjectName() { columns.add(tableColumn); } ("," tableColumn=RelObjectName() { columns.add(tableColumn); } )* ")" |
4274+
LOOKAHEAD(2)
42724275
("("
42734276
coldef = ColumnDefinition()
42744277

@@ -4397,6 +4400,7 @@ CreateTable CreateTable():
43974400

43984401
")"
43994402
)
4403+
)
44004404
]
44014405
( parameter=CreateParameter() { tableOptions.addAll(parameter); } )*
44024406

@@ -4421,6 +4425,8 @@ CreateTable CreateTable():
44214425
createTable.setTableOptionsStrings(tableOptions);
44224426
if (columnDefinitions.size() > 0)
44234427
createTable.setColumnDefinitions(columnDefinitions);
4428+
if (columns.size() > 0)
4429+
createTable.setColumns(columns);
44244430
return createTable;
44254431
}
44264432
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,4 +821,16 @@ public void testDefaultArray() throws JSQLParserException {
821821
assertSqlCanBeParsedAndDeparsed(
822822
"CREATE TABLE t (f1 text[] DEFAULT ARRAY[] :: text[] NOT NULL, f2 int[] DEFAULT ARRAY[1, 2])");
823823
}
824+
825+
@Test
826+
public void testCreateTemporaryTableAsSelect() throws JSQLParserException {
827+
assertSqlCanBeParsedAndDeparsed(
828+
"CREATE TEMPORARY TABLE T1 (C1, C2) AS SELECT C3, C4 FROM T2");
829+
}
830+
831+
@Test
832+
public void testCreateTempTableAsSelect() throws JSQLParserException {
833+
assertSqlCanBeParsedAndDeparsed(
834+
"CREATE TEMP TABLE T1 (C1, C2) AS SELECT C3, C4 FROM T2");
835+
}
824836
}

0 commit comments

Comments
 (0)