提交 f249c20b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add support for SQL:2003 WITH [NO] DATA to CREATE TABLE AS

上级 a778d6ee
...@@ -708,7 +708,7 @@ TABLE [ IF NOT EXISTS ] name ...@@ -708,7 +708,7 @@ TABLE [ IF NOT EXISTS ] name
[ ENGINE tableEngineName ] [ ENGINE tableEngineName ]
[ WITH tableEngineParamName [,...] ] [ WITH tableEngineParamName [,...] ]
[ NOT PERSISTENT ] [ TRANSACTIONAL ] [ NOT PERSISTENT ] [ TRANSACTIONAL ]
[ AS select ]"," [ AS select [ WITH [ NO ] DATA ] ]","
Creates a new table. Creates a new table.
Cached tables (the default for regular tables) are persistent, Cached tables (the default for regular tables) are persistent,
...@@ -736,6 +736,7 @@ rows are lost when the database is closed. ...@@ -736,6 +736,7 @@ rows are lost when the database is closed.
The column definition is optional if a query is specified. The column definition is optional if a query is specified.
In that case the column list of the query is used. In that case the column list of the query is used.
If the query is specified its results are inserted into created table unless WITH NO DATA is specified.
This command commits an open transaction, except when using This command commits an open transaction, except when using
TRANSACTIONAL (only supported for temporary tables). TRANSACTIONAL (only supported for temporary tables).
......
...@@ -6679,6 +6679,10 @@ public class Parser { ...@@ -6679,6 +6679,10 @@ public class Parser {
command.setSortedInsertMode(true); command.setSortedInsertMode(true);
} }
command.setQuery(parseSelect()); command.setQuery(parseSelect());
if (readIf("WITH")) {
command.setWithNoData(readIf("NO"));
read("DATA");
}
} }
// for MySQL compatibility // for MySQL compatibility
if (readIf("ROW_FORMAT")) { if (readIf("ROW_FORMAT")) {
......
...@@ -38,6 +38,7 @@ public class CreateTable extends CommandWithColumns { ...@@ -38,6 +38,7 @@ public class CreateTable extends CommandWithColumns {
private Query asQuery; private Query asQuery;
private String comment; private String comment;
private boolean sortedInsertMode; private boolean sortedInsertMode;
private boolean withNoData;
public CreateTable(Session session, Schema schema) { public CreateTable(Session session, Schema schema) {
super(session, schema); super(session, schema);
...@@ -120,7 +121,7 @@ public class CreateTable extends CommandWithColumns { ...@@ -120,7 +121,7 @@ public class CreateTable extends CommandWithColumns {
table.addSequence(sequence); table.addSequence(sequence);
} }
createConstraints(); createConstraints();
if (asQuery != null) { if (asQuery != null && !withNoData) {
boolean old = session.isUndoLogEnabled(); boolean old = session.isUndoLogEnabled();
try { try {
session.setUndoLogEnabled(false); session.setUndoLogEnabled(false);
...@@ -245,6 +246,10 @@ public class CreateTable extends CommandWithColumns { ...@@ -245,6 +246,10 @@ public class CreateTable extends CommandWithColumns {
this.sortedInsertMode = sortedInsertMode; this.sortedInsertMode = sortedInsertMode;
} }
public void setWithNoData(boolean withNoData) {
this.withNoData = withNoData;
}
public void setTableEngine(String tableEngine) { public void setTableEngine(String tableEngine) {
data.tableEngine = tableEngine; data.tableEngine = tableEngine;
} }
......
...@@ -14,3 +14,49 @@ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINT ...@@ -14,3 +14,49 @@ SELECT CONSTRAINT_NAME, CONSTRAINT_TYPE FROM INFORMATION_SCHEMA.TABLE_CONSTRAINT
DROP TABLE TEST; DROP TABLE TEST;
> ok > ok
CREATE TABLE T1(ID INT PRIMARY KEY, COL2 INT);
> ok
INSERT INTO T1 VALUES (1, 2), (11, 22);
> update count: 2
CREATE TABLE T2 AS SELECT * FROM T1;
> ok
SELECT * FROM T2 ORDER BY ID;
> ID COL2
> -- ----
> 1 2
> 11 22
> rows (ordered): 2
DROP TABLE T2;
> ok
CREATE TABLE T2 AS SELECT * FROM T1 WITH DATA;
> ok
SELECT * FROM T2 ORDER BY ID;
> ID COL2
> -- ----
> 1 2
> 11 22
> rows (ordered): 2
DROP TABLE T2;
> ok
CREATE TABLE T2 AS SELECT * FROM T1 WITH NO DATA;
> ok
SELECT * FROM T2 ORDER BY ID;
> ID COL2
> -- ----
> rows (ordered): 0
DROP TABLE T2;
> ok
DROP TABLE T1;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论