Skip to content

Commit 989033d

Browse files
committed
create table implemented version 2
1 parent bbf360e commit 989033d

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
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.Select;
3031
import net.sf.jsqlparser.statement.select.SelectBody;
3132

3233
/**
@@ -38,7 +39,7 @@ public class CreateTable implements Statement {
3839
private List<String> tableOptionsStrings;
3940
private List<ColumnDefinition> columnDefinitions;
4041
private List<Index> indexes;
41-
private SelectBody selectBody;
42+
private Select select;
4243

4344
@Override
4445
public void accept(StatementVisitor statementVisitor) {
@@ -92,12 +93,12 @@ public void setIndexes(List<Index> list) {
9293
indexes = list;
9394
}
9495

95-
public SelectBody getSelectBody() {
96-
return selectBody;
96+
public Select getSelect() {
97+
return select;
9798
}
9899

99-
public void setSelectBody(SelectBody selectBody) {
100-
this.selectBody = selectBody;
100+
public void setSelect(Select select) {
101+
this.select = select;
101102
}
102103

103104
@Override
@@ -106,8 +107,8 @@ public String toString() {
106107

107108
sql = "CREATE TABLE " + table;
108109

109-
if (selectBody != null) {
110-
sql += " AS " + selectBody.toString();
110+
if (select != null) {
111+
sql += " AS " + select.toString();
111112
} else {
112113
sql += " (";
113114

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public CreateTableDeParser(StringBuilder buffer) {
4444

4545
public void deParse(CreateTable createTable) {
4646
buffer.append("CREATE TABLE ").append(createTable.getTable().getFullyQualifiedName());
47-
if (createTable.getSelectBody() != null) {
48-
buffer.append(" AS ").append(createTable.getSelectBody().toString());
47+
if (createTable.getSelect() != null) {
48+
buffer.append(" AS ").append(createTable.getSelect().toString());
4949
} else {
5050
if (createTable.getColumnDefinitions() != null) {
5151
buffer.append(" (");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ CreateTable CreateTable():
20282028
ForeignKeyIndex fkIndex = null;
20292029
String parameter = null;
20302030
Table fkTable = null;
2031-
SelectBody select = null;
2031+
Select select = null;
20322032
}
20332033
{
20342034
<K_CREATE>
@@ -2150,7 +2150,7 @@ CreateTable CreateTable():
21502150
)
21512151
|
21522152
(
2153-
<K_AS> select = SelectBody() { createTable.setSelectBody(select); }
2153+
<K_AS> select = Select() { createTable.setSelect(select); }
21542154
)
21552155
]
21562156

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public void testCreateTableAsSelect() throws JSQLParserException {
4040
String statement = "CREATE TABLE a AS SELECT col1, col2 FROM b";
4141
assertSqlCanBeParsedAndDeparsed(statement);
4242
}
43+
44+
public void testCreateTableAsSelect2() throws JSQLParserException {
45+
String statement = "CREATE TABLE newtable AS WITH a AS (SELECT col1, col3 FROM testtable) SELECT col1, col2, col3 FROM b INNER JOIN a ON b.col1 = a.col1";
46+
assertSqlCanBeParsedAndDeparsed(statement);
47+
}
4348

4449
public void testCreateTable() throws JSQLParserException {
4550
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)