Skip to content

Commit

Permalink
Create temporary table t(c1, c2) as select ... (#1225)
Browse files Browse the repository at this point in the history
Co-authored-by: Francois Secherre <secherre.nospam@gmail.com>
  • Loading branch information
francois-secherre and fanchuo authored Jun 14, 2021
1 parent 8eb3d9a commit b62f19f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
Expand All @@ -27,6 +28,7 @@ public class CreateTable implements Statement {
private List<String> createOptionsStrings;
private List<String> tableOptionsStrings;
private List<ColumnDefinition> columnDefinitions;
private List<String> columns;
private List<Index> indexes;
private Select select;
private Table likeTable;
Expand Down Expand Up @@ -66,6 +68,14 @@ public void setColumnDefinitions(List<ColumnDefinition> list) {
columnDefinitions = list;
}

public List<String> getColumns() {
return this.columns;
}

public void setColumns(List<String> columns) {
this.columns =columns;
}

/**
* @return a list of options (as simple strings) of this table definition, as ("TYPE", "=", "MYISAM")
*/
Expand Down Expand Up @@ -150,6 +160,10 @@ public String toString() {
+ (!"".equals(createOps) ? createOps + " " : "")
+ "TABLE " + (ifNotExists ? "IF NOT EXISTS " : "") + table;

if (columns != null && !columns.isEmpty()) {
sql += " ";
sql += PlainSelect.getStringList(columns, true, true);
}
if (columnDefinitions != null && !columnDefinitions.isEmpty()) {
sql += " (";

Expand Down Expand Up @@ -217,6 +231,11 @@ public CreateTable withColumnDefinitions(List<ColumnDefinition> columnDefinition
return this;
}

public CreateTable withColumns(List<String> columns) {
this.setColumns(columns);
return this;
}

public CreateTable withIndexes(List<Index> indexes) {
this.setIndexes(indexes);
return this;
Expand Down Expand Up @@ -246,6 +265,18 @@ public CreateTable addColumnDefinitions(Collection<? extends ColumnDefinition> c
return this.withColumnDefinitions(collection);
}

public CreateTable addColumns(String... columns) {
List<String> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
Collections.addAll(collection, columns);
return this.withColumns(collection);
}

public CreateTable addColumns(Collection<String> columns) {
List<String> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
collection.addAll(columns);
return this.withColumns(collection);
}

public CreateTable addIndexes(Index... indexes) {
List<Index> collection = Optional.ofNullable(getIndexes()).orElseGet(ArrayList::new);
Collections.addAll(collection, indexes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public void deParse(CreateTable createTable) {
}
buffer.append(createTable.getTable().getFullyQualifiedName());

if (createTable.getColumns() != null && !createTable.getColumns().isEmpty()) {
buffer.append(" (");
Iterator<String> columnIterator = createTable.getColumns().iterator();
buffer.append(columnIterator.next());
while (columnIterator.hasNext()) {
buffer.append(", ").append(columnIterator.next());
}
buffer.append(")");
}
if (createTable.getColumnDefinitions() != null) {
buffer.append(" (");
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -4253,6 +4253,8 @@ CreateTable CreateTable():
ExcludeConstraint excludeC = null;
RowMovement rowMovement = null;
ReferentialAction.Action action = null;
String tableColumn = null;
List<String> columns = new ArrayList<String>();
}
{
<K_CREATE>
Expand All @@ -4268,7 +4270,8 @@ CreateTable CreateTable():
<K_TABLE>
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { createTable.setIfNotExists(true); }]
table=Table()
[ LOOKAHEAD(2)
[ ( LOOKAHEAD(3) "(" tableColumn=RelObjectName() { columns.add(tableColumn); } ("," tableColumn=RelObjectName() { columns.add(tableColumn); } )* ")" |
LOOKAHEAD(2)
("("
coldef = ColumnDefinition()

Expand Down Expand Up @@ -4397,6 +4400,7 @@ CreateTable CreateTable():

")"
)
)
]
( parameter=CreateParameter() { tableOptions.addAll(parameter); } )*

Expand All @@ -4421,6 +4425,8 @@ CreateTable CreateTable():
createTable.setTableOptionsStrings(tableOptions);
if (columnDefinitions.size() > 0)
createTable.setColumnDefinitions(columnDefinitions);
if (columns.size() > 0)
createTable.setColumns(columns);
return createTable;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,16 @@ public void testDefaultArray() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"CREATE TABLE t (f1 text[] DEFAULT ARRAY[] :: text[] NOT NULL, f2 int[] DEFAULT ARRAY[1, 2])");
}

@Test
public void testCreateTemporaryTableAsSelect() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"CREATE TEMPORARY TABLE T1 (C1, C2) AS SELECT C3, C4 FROM T2");
}

@Test
public void testCreateTempTableAsSelect() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"CREATE TEMP TABLE T1 (C1, C2) AS SELECT C3, C4 FROM T2");
}
}

0 comments on commit b62f19f

Please sign in to comment.