Skip to content

Commit 23b938c

Browse files
committed
implemented create table .. as select ..
1 parent 5b3ea03 commit 23b938c

File tree

4 files changed

+149
-119
lines changed

4 files changed

+149
-119
lines changed

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

Lines changed: 92 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,86 +27,102 @@
2727
import net.sf.jsqlparser.statement.Statement;
2828
import net.sf.jsqlparser.statement.StatementVisitor;
2929
import net.sf.jsqlparser.statement.select.PlainSelect;
30+
import net.sf.jsqlparser.statement.select.SelectBody;
3031

3132
/**
3233
* A "CREATE TABLE" statement
3334
*/
3435
public class CreateTable implements Statement {
3536

36-
private Table table;
37-
private List<String> tableOptionsStrings;
38-
private List<ColumnDefinition> columnDefinitions;
39-
private List<Index> indexes;
40-
41-
@Override
42-
public void accept(StatementVisitor statementVisitor) {
43-
statementVisitor.visit(this);
44-
}
45-
46-
/**
47-
* The name of the table to be created
48-
*/
49-
public Table getTable() {
50-
return table;
51-
}
52-
53-
public void setTable(Table table) {
54-
this.table = table;
55-
}
56-
57-
/**
58-
* A list of {@link ColumnDefinition}s of this table.
59-
*/
60-
public List<ColumnDefinition> getColumnDefinitions() {
61-
return columnDefinitions;
62-
}
63-
64-
public void setColumnDefinitions(List<ColumnDefinition> list) {
65-
columnDefinitions = list;
66-
}
67-
68-
/**
69-
* A list of options (as simple strings) of this table definition, as
70-
* ("TYPE", "=", "MYISAM")
71-
*/
72-
public List<?> getTableOptionsStrings() {
73-
return tableOptionsStrings;
74-
}
75-
76-
public void setTableOptionsStrings(List<String> list) {
77-
tableOptionsStrings = list;
78-
}
79-
80-
/**
81-
* A list of {@link Index}es (for example "PRIMARY KEY") of this table.<br>
82-
* Indexes created with column definitions (as in mycol INT PRIMARY KEY) are
83-
* not inserted into this list.
84-
*/
85-
public List<Index> getIndexes() {
86-
return indexes;
87-
}
88-
89-
public void setIndexes(List<Index> list) {
90-
indexes = list;
91-
}
92-
93-
@Override
94-
public String toString() {
95-
String sql = "";
96-
97-
sql = "CREATE TABLE " + table + " (";
98-
99-
sql += PlainSelect.getStringList(columnDefinitions, true, false);
100-
if (indexes != null && indexes.size() != 0) {
101-
sql += ", ";
102-
sql += PlainSelect.getStringList(indexes);
103-
}
104-
sql += ")";
105-
String options = PlainSelect.getStringList(tableOptionsStrings, false, false);
106-
if (options != null && options.length() > 0) {
107-
sql += " " + options;
108-
}
109-
110-
return sql;
111-
}
37+
private Table table;
38+
private List<String> tableOptionsStrings;
39+
private List<ColumnDefinition> columnDefinitions;
40+
private List<Index> indexes;
41+
private SelectBody selectBody;
42+
43+
@Override
44+
public void accept(StatementVisitor statementVisitor) {
45+
statementVisitor.visit(this);
46+
}
47+
48+
/**
49+
* The name of the table to be created
50+
*/
51+
public Table getTable() {
52+
return table;
53+
}
54+
55+
public void setTable(Table table) {
56+
this.table = table;
57+
}
58+
59+
/**
60+
* A list of {@link ColumnDefinition}s of this table.
61+
*/
62+
public List<ColumnDefinition> getColumnDefinitions() {
63+
return columnDefinitions;
64+
}
65+
66+
public void setColumnDefinitions(List<ColumnDefinition> list) {
67+
columnDefinitions = list;
68+
}
69+
70+
/**
71+
* A list of options (as simple strings) of this table definition, as
72+
* ("TYPE", "=", "MYISAM")
73+
*/
74+
public List<?> getTableOptionsStrings() {
75+
return tableOptionsStrings;
76+
}
77+
78+
public void setTableOptionsStrings(List<String> list) {
79+
tableOptionsStrings = list;
80+
}
81+
82+
/**
83+
* A list of {@link Index}es (for example "PRIMARY KEY") of this table.<br>
84+
* Indexes created with column definitions (as in mycol INT PRIMARY KEY) are
85+
* not inserted into this list.
86+
*/
87+
public List<Index> getIndexes() {
88+
return indexes;
89+
}
90+
91+
public void setIndexes(List<Index> list) {
92+
indexes = list;
93+
}
94+
95+
public SelectBody getSelectBody() {
96+
return selectBody;
97+
}
98+
99+
public void setSelectBody(SelectBody selectBody) {
100+
this.selectBody = selectBody;
101+
}
102+
103+
@Override
104+
public String toString() {
105+
String sql = "";
106+
107+
sql = "CREATE TABLE " + table;
108+
109+
if (selectBody != null) {
110+
sql += " AS " + selectBody.toString();
111+
} else {
112+
sql += " (";
113+
114+
sql += PlainSelect.getStringList(columnDefinitions, true, false);
115+
if (indexes != null && indexes.size() != 0) {
116+
sql += ", ";
117+
sql += PlainSelect.getStringList(indexes);
118+
}
119+
sql += ")";
120+
String options = PlainSelect.getStringList(tableOptionsStrings, false, false);
121+
if (options != null && options.length() > 0) {
122+
sql += " " + options;
123+
}
124+
}
125+
126+
return sql;
127+
}
112128
}

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

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,57 @@
3333
*/
3434
public class CreateTableDeParser {
3535

36-
private StringBuilder buffer;
36+
private StringBuilder buffer;
3737

38-
/**
39-
* @param buffer the buffer that will be filled with the select
40-
*/
41-
public CreateTableDeParser(StringBuilder buffer) {
42-
this.buffer = buffer;
43-
}
38+
/**
39+
* @param buffer the buffer that will be filled with the select
40+
*/
41+
public CreateTableDeParser(StringBuilder buffer) {
42+
this.buffer = buffer;
43+
}
4444

45-
public void deParse(CreateTable createTable) {
46-
buffer.append("CREATE TABLE ").append(createTable.getTable().getFullyQualifiedName());
47-
if (createTable.getColumnDefinitions() != null) {
48-
buffer.append(" (");
49-
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) {
50-
ColumnDefinition columnDefinition = iter.next();
51-
buffer.append(columnDefinition.getColumnName());
52-
buffer.append(" ");
53-
buffer.append(columnDefinition.getColDataType().toString());
54-
if (columnDefinition.getColumnSpecStrings() != null) {
55-
for (String s : columnDefinition.getColumnSpecStrings()) {
56-
buffer.append(" ");
57-
buffer.append(s);
45+
public void deParse(CreateTable createTable) {
46+
buffer.append("CREATE TABLE ").append(createTable.getTable().getFullyQualifiedName());
47+
if (createTable.getSelectBody() != null) {
48+
buffer.append(" AS ").append(createTable.getSelectBody().toString());
49+
} else {
50+
if (createTable.getColumnDefinitions() != null) {
51+
buffer.append(" (");
52+
for (Iterator<ColumnDefinition> iter = createTable.getColumnDefinitions().iterator(); iter.hasNext();) {
53+
ColumnDefinition columnDefinition = iter.next();
54+
buffer.append(columnDefinition.getColumnName());
55+
buffer.append(" ");
56+
buffer.append(columnDefinition.getColDataType().toString());
57+
if (columnDefinition.getColumnSpecStrings() != null) {
58+
for (String s : columnDefinition.getColumnSpecStrings()) {
59+
buffer.append(" ");
60+
buffer.append(s);
61+
}
5862
}
59-
}
6063

61-
if (iter.hasNext()) {
62-
buffer.append(", ");
63-
}
64-
}
64+
if (iter.hasNext()) {
65+
buffer.append(", ");
66+
}
67+
}
6568

66-
if (createTable.getIndexes() != null) {
67-
for (Iterator<Index> iter = createTable.getIndexes().iterator(); iter.hasNext();) {
68-
buffer.append(", ");
69-
Index index = iter.next();
70-
buffer.append(index.toString());
71-
}
72-
}
69+
if (createTable.getIndexes() != null) {
70+
for (Iterator<Index> iter = createTable.getIndexes().iterator(); iter.hasNext();) {
71+
buffer.append(", ");
72+
Index index = iter.next();
73+
buffer.append(index.toString());
74+
}
75+
}
7376

74-
buffer.append(")");
75-
}
76-
}
77+
buffer.append(")");
78+
}
79+
}
80+
}
7781

78-
public StringBuilder getBuffer() {
79-
return buffer;
80-
}
82+
public StringBuilder getBuffer() {
83+
return buffer;
84+
}
8185

82-
public void setBuffer(StringBuilder buffer) {
83-
this.buffer = buffer;
84-
}
86+
public void setBuffer(StringBuilder buffer) {
87+
this.buffer = buffer;
88+
}
8589
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,7 @@ CreateTable CreateTable():
20292029
ForeignKeyIndex fkIndex = null;
20302030
String parameter = null;
20312031
Table fkTable = null;
2032+
SelectBody select = null;
20322033
}
20332034
{
20342035
<K_CREATE>
@@ -2038,7 +2039,7 @@ CreateTable CreateTable():
20382039

20392040
<K_TABLE> table=Table()
20402041
[
2041-
"("
2042+
("("
20422043
(columnName=<S_IDENTIFIER>
20432044
|
20442045
columnName=<S_QUOTED_IDENTIFIER>)
@@ -2147,7 +2148,11 @@ CreateTable CreateTable():
21472148

21482149
")"
21492150
( parameter=CreateParameter() { tableOptions.add(parameter); } )*
2150-
2151+
)
2152+
|
2153+
(
2154+
<K_AS> select = SelectBody() { createTable.setSelectBody(select); }
2155+
)
21512156
]
21522157

21532158
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public void testCreateTable3() throws JSQLParserException {
3535
String statement = "CREATE TABLE testtab (\"test\" varchar (255), \"test2\" varchar (255))";
3636
assertSqlCanBeParsedAndDeparsed(statement);
3737
}
38+
39+
public void testCreateTableAsSelect() throws JSQLParserException {
40+
String statement = "CREATE TABLE a AS SELECT col1, col2 FROM b";
41+
assertSqlCanBeParsedAndDeparsed(statement);
42+
}
3843

3944
public void testCreateTable() throws JSQLParserException {
4045
String statement = "CREATE TABLE mytab (mycol a (10, 20) c nm g, mycol2 mypar1 mypar2 (23,323,3) asdf ('23','123') dasd, "

0 commit comments

Comments
 (0)