Skip to content

Commit ac74622

Browse files
chyunchyun
andauthored
Support Create table LIKE (#1066)
* fixes #413 * add coverage Co-authored-by: chyun <[email protected]>
1 parent f1cf0ab commit ac74622

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class CreateTable implements Statement {
2929
private List<ColumnDefinition> columnDefinitions;
3030
private List<Index> indexes;
3131
private Select select;
32+
private Table likeTable;
3233
private boolean selectParenthesis;
3334
private boolean ifNotExists = false;
3435
private RowMovement rowMovement;
@@ -106,6 +107,15 @@ public void setSelect(Select select, boolean parenthesis) {
106107
this.selectParenthesis = parenthesis;
107108
}
108109

110+
public Table getLikeTable() {
111+
return likeTable;
112+
}
113+
114+
public void setLikeTable(Table likeTable, boolean parenthesis) {
115+
this.likeTable = likeTable;
116+
this.selectParenthesis = parenthesis;
117+
}
118+
109119
public boolean isIfNotExists() {
110120
return ifNotExists;
111121
}
@@ -160,6 +170,9 @@ public String toString() {
160170
if (select != null) {
161171
sql += " AS " + (selectParenthesis ? "(" : "") + select.toString() + (selectParenthesis ? ")" : "");
162172
}
173+
if (likeTable != null) {
174+
sql += " LIKE " + (selectParenthesis ? "(" : "") + likeTable.toString() + (selectParenthesis ? ")" : "");
175+
}
163176
return sql;
164177
}
165178

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.util.Iterator;
1313

14+
import net.sf.jsqlparser.schema.Table;
1415
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
1516
import net.sf.jsqlparser.statement.create.table.CreateTable;
1617
import net.sf.jsqlparser.statement.create.table.Index;
@@ -95,6 +96,17 @@ public void deParse(CreateTable createTable) {
9596
buffer.append(")");
9697
}
9798
}
99+
if (createTable.getLikeTable() != null) {
100+
buffer.append(" LIKE ");
101+
if (createTable.isSelectParenthesis()) {
102+
buffer.append("(");
103+
}
104+
Table table = createTable.getLikeTable();
105+
buffer.append(table.getFullyQualifiedName());
106+
if (createTable.isSelectParenthesis()) {
107+
buffer.append(")");
108+
}
109+
}
98110
}
99111

100112
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,6 +4048,7 @@ CreateTable CreateTable():
40484048
List<String> idxSpec = new ArrayList<String>();
40494049
Table fkTable = null;
40504050
Select select = null;
4051+
Table likeTable = null;
40514052
CheckConstraint checkCs = null;
40524053
ExcludeConstraint excludeC = null;
40534054
RowMovement rowMovement = null;
@@ -4201,6 +4202,10 @@ CreateTable CreateTable():
42014202
<K_AS> ( LOOKAHEAD("(" Select() ")") "(" select = Select() { createTable.setSelect(select, true); } ")"
42024203
| select = Select() { createTable.setSelect(select, false); } )
42034204
]
4205+
[
4206+
<K_LIKE> ( LOOKAHEAD("(" Table() ")") "(" likeTable=Table() { createTable.setLikeTable(likeTable, true); } ")"
4207+
| likeTable=Table() { createTable.setLikeTable(likeTable, false); } )
4208+
]
42044209
{
42054210
createTable.setTable(table);
42064211
if (indexes.size() > 0)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,16 @@ public void tableMovementWithAS() throws JSQLParserException {
664664
String sql = "CREATE TABLE test (startdate DATE) DISABLE ROW MOVEMENT AS SELECT 1 FROM dual";
665665
assertSqlCanBeParsedAndDeparsed(sql);
666666
}
667+
668+
@Test
669+
public void testCreateTableWithCommentIssue413() throws JSQLParserException {
670+
String statement = "CREATE TABLE a LIKE b";
671+
assertSqlCanBeParsedAndDeparsed(statement);
672+
}
673+
674+
@Test
675+
public void testCreateTableWithCommentIssue413_2() throws JSQLParserException {
676+
String statement = "CREATE TABLE a LIKE (b)";
677+
assertSqlCanBeParsedAndDeparsed(statement);
678+
}
667679
}

0 commit comments

Comments
 (0)