improved sql parser.

This commit is contained in:
wenshao
2017-08-01 19:05:31 +08:00
parent c2adc8bfd4
commit 544da04ee4
32 changed files with 489 additions and 4 deletions
@@ -48,7 +48,6 @@ public class SQLPartition extends OracleSegmentAttributesImpl implements OracleS
private SQLObject lobStorage;
public SQLName getName() {
return name;
}
@@ -202,4 +201,53 @@ public class SQLPartition extends OracleSegmentAttributesImpl implements OracleS
public void setSegmentCreationDeferred(boolean segmentCreationDeferred) {
this.segmentCreationDeferred = segmentCreationDeferred;
}
public SQLPartition clone() {
SQLPartition x = new SQLPartition();
if (name != null) {
x.setName(name.clone());
}
if (subPartitionsCount != null) {
x.setSubPartitionsCount(subPartitionsCount.clone());
}
for (SQLSubPartition p : subPartitions) {
SQLSubPartition p2 = p.clone();
p2.setParent(x);
x.subPartitions.add(p2);
}
if (values != null) {
x.setValues(values.clone());
}
if (dataDirectory != null) {
x.setDataDirectory(dataDirectory.clone());
}
if (indexDirectory != null) {
x.setDataDirectory(indexDirectory.clone());
}
if (maxRows != null) {
x.setDataDirectory(maxRows.clone());
}
if (minRows != null) {
x.setDataDirectory(minRows.clone());
}
if (engine != null) {
x.setDataDirectory(engine.clone());
}
if (comment != null) {
x.setDataDirectory(comment.clone());
}
x.segmentCreationImmediate = segmentCreationImmediate;
x.segmentCreationDeferred = segmentCreationDeferred;
if (lobStorage != null) {
x.setLobStorage(lobStorage.clone());
}
return x;
}
}
@@ -74,4 +74,26 @@ public abstract class SQLPartitionBy extends SQLObjectImpl {
public List<SQLName> getStoreIn() {
return storeIn;
}
public void cloneTo(SQLPartitionBy x) {
if (subPartitionBy != null) {
x.setSubPartitionBy(subPartitionBy.clone());
}
if (partitionsCount != null) {
x.setPartitionsCount(partitionsCount.clone());
}
x.linear = linear;
for (SQLPartition p : partitions) {
SQLPartition p2 = p.clone();
p2.setParent(x);
x.partitions.add(p2);
}
for (SQLName name : storeIn) {
SQLName name2 = name.clone();
name2.setParent(x);
x.storeIn.add(name2);
}
}
public abstract SQLPartitionBy clone();
}
@@ -54,4 +54,15 @@ public class SQLPartitionByHash extends SQLPartitionBy {
visitor.endVisit(this);
}
public SQLPartitionByHash clone() {
SQLPartitionByHash x = new SQLPartitionByHash();
if (expr != null) {
x.setExpr(expr.clone());
}
x.key = key;
return x;
}
}
@@ -59,4 +59,20 @@ public class SQLPartitionByList extends SQLPartitionBy {
}
this.columns.add(column);
}
public SQLPartitionByList clone() {
SQLPartitionByList x = new SQLPartitionByList();
if (expr != null) {
x.setExpr(expr.clone());
}
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
return x;
}
}
@@ -79,4 +79,23 @@ public class SQLPartitionByRange extends SQLPartitionBy {
visitor.endVisit(this);
}
public SQLPartitionByRange clone() {
SQLPartitionByRange x = new SQLPartitionByRange();
if (expr != null) {
x.setExpr(expr.clone());
}
if (interval != null) {
x.setInterval(interval.clone());
}
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
return x;
}
}
@@ -58,4 +58,16 @@ public class SQLPartitionValue extends SQLObjectImpl {
}
visitor.endVisit(this);
}
public SQLPartitionValue clone() {
SQLPartitionValue x = new SQLPartitionValue(operator);
for (SQLExpr item : items) {
SQLExpr item2 = item.clone();
item2.setParent(x);
x.items.add(item2);
}
return x;
}
}
@@ -68,4 +68,21 @@ public class SQLSubPartition extends SQLObjectImpl {
visitor.endVisit(this);
}
public SQLSubPartition clone() {
SQLSubPartition x = new SQLSubPartition();
if (name != null) {
x.setName(name.clone());
}
if (values != null) {
x.setValues(values.clone());
}
if (tableSpace != null) {
x.setTableSpace(tableSpace.clone());
}
return x;
}
}
@@ -57,4 +57,23 @@ public abstract class SQLSubPartitionBy extends SQLObjectImpl {
return subPartitionTemplate;
}
public void cloneTo(SQLSubPartitionBy x) {
if (subPartitionsCount != null) {
x.setSubPartitionsCount(subPartitionsCount.clone());
}
x.linear = linear;
for (SQLAssignItem option : options) {
SQLAssignItem option2 = option.clone();
option2.setParent(x);
x.options.add(option2);
}
for (SQLSubPartition p : subPartitionTemplate) {
SQLSubPartition p2 = p.clone();
p2.setParent(x);
x.subPartitionTemplate.add(p2);
}
}
public abstract SQLSubPartitionBy clone();
}
@@ -52,4 +52,13 @@ public class SQLSubPartitionByHash extends SQLSubPartitionBy {
this.key = key;
}
public SQLSubPartitionByHash clone() {
SQLSubPartitionByHash x = new SQLSubPartitionByHash();
if (expr != null) {
x.setExpr(expr.clone());
}
x.key = key;
return x;
}
}
@@ -41,4 +41,11 @@ public class SQLSubPartitionByList extends SQLSubPartitionBy {
this.column = column;
}
public SQLSubPartitionByList clone() {
SQLSubPartitionByList x = new SQLSubPartitionByList();
if (column != null) {
x.setColumn(column.clone());
}
return x;
}
}
@@ -31,4 +31,16 @@ public class SQLSubPartitionByRange extends SQLSubPartitionBy {
protected void accept0(SQLASTVisitor visitor) {
}
public SQLSubPartitionByRange clone() {
SQLSubPartitionByRange x = new SQLSubPartitionByRange();
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
return x;
}
}
@@ -33,6 +33,17 @@ public class SQLAssignItem extends SQLObjectImpl implements SQLReplaceable {
setValue(value);
}
public SQLAssignItem clone() {
SQLAssignItem x = new SQLAssignItem();
if (target != null) {
x.setTarget(target.clone());
}
if (value != null) {
x.setValue(value.clone());
}
return x;
}
public SQLExpr getTarget() {
return target;
}
@@ -57,4 +57,10 @@ public class SQLCheck extends SQLConstraintImpl implements SQLTableElement {
expr = expr.clone();
}
}
public SQLCheck clone() {
SQLCheck x = new SQLCheck();
cloneTo(x);
return x;
}
}
@@ -737,4 +737,29 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return -1;
}
public void cloneTo(SQLCreateTableStatement x) {
x.ifNotExiists = ifNotExiists;
x.type = type;
if (tableSource != null) {
x.setTableSource(tableSource.clone());
}
for (SQLTableElement e : tableElementList) {
SQLTableElement e2 = e.clone();
e2.setParent(x);
x.tableElementList.add(e2);
}
if (inherits != null) {
x.setInherits(inherits.clone());
}
if (select != null) {
x.setSelect(select.clone());
}
}
public SQLCreateTableStatement clone() {
SQLCreateTableStatement x = new SQLCreateTableStatement();
cloneTo(x);
return x;
}
}
@@ -27,4 +27,10 @@ public class SQLPrimaryKeyImpl extends SQLUnique implements SQLPrimaryKey {
}
visitor.endVisit(this);
}
public SQLPrimaryKeyImpl clone() {
SQLPrimaryKeyImpl x = new SQLPrimaryKeyImpl();
cloneTo(x);
return x;
}
}
@@ -18,5 +18,5 @@ package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLObject;
public interface SQLTableElement extends SQLObject {
SQLTableElement clone();
}
@@ -72,6 +72,12 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
}
}
public SQLUnique clone() {
SQLUnique x = new SQLUnique();
cloneTo(x);
return x;
}
public void simplify() {
super.simplify();
@@ -25,7 +25,6 @@ import com.alibaba.druid.util.JdbcConstants;
public class MySqlKey extends SQLUnique implements SQLUniqueConstraint, SQLTableConstraint {
private String indexType;
private boolean hasConstaint;
@@ -66,4 +65,15 @@ public class MySqlKey extends SQLUnique implements SQLUniqueConstraint, SQLTable
this.hasConstaint = hasConstaint;
}
public void cloneTo(MySqlKey x) {
super.cloneTo(x);
x.indexType = indexType;
x.hasConstaint = hasConstaint;
}
public MySqlKey clone() {
MySqlKey x = new MySqlKey();
cloneTo(x);
return x;
}
}
@@ -32,4 +32,10 @@ public class MySqlPrimaryKey extends MySqlKey implements SQLPrimaryKey {
}
visitor.endVisit(this);
}
public MySqlPrimaryKey clone() {
MySqlPrimaryKey x = new MySqlPrimaryKey();
cloneTo(x);
return x;
}
}
@@ -31,4 +31,10 @@ public class MySqlUnique extends MySqlKey {
}
visitor.endVisit(this);
}
public MySqlUnique clone() {
MySqlUnique x = new MySqlUnique();
cloneTo(x);
return x;
}
}
@@ -342,4 +342,38 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
public void output(StringBuffer buf) {
this.accept(new MySqlOutputVisitor(buf));
}
public void cloneTo(MySqlCreateTableStatement x) {
super.cloneTo(x);
for (Map.Entry<String, SQLObject> entry : tableOptions.entrySet()) {
SQLObject obj = entry.getValue().clone();
obj.setParent(x);
x.tableOptions.put(entry.getKey(), obj);
}
if (partitioning != null) {
x.setPartitioning(partitioning.clone());
}
for (SQLCommentHint hint : hints) {
SQLCommentHint h2 = hint.clone();
h2.setParent(x);
x.hints.add(h2);
}
for (SQLCommentHint hint : optionHints) {
SQLCommentHint h2 = hint.clone();
h2.setParent(x);
x.optionHints.add(h2);
}
if (like != null) {
x.setLike(like.clone());
}
if (tableGroup != null) {
x.setTableGroup(tableGroup.clone());
}
}
public MySqlCreateTableStatement clone() {
MySqlCreateTableStatement x = new MySqlCreateTableStatement();
cloneTo(x);
return x;
}
}
@@ -59,4 +59,18 @@ public class MySqlPartitionByKey extends SQLPartitionBy implements MySqlObject {
this.columns.add(column);
}
public void cloneTo(MySqlPartitionByKey x) {
super.cloneTo(x);
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
}
public MySqlPartitionByKey clone() {
MySqlPartitionByKey x = new MySqlPartitionByKey();
cloneTo(x);
return x;
}
}
@@ -57,4 +57,18 @@ public class MySqlSubPartitionByKey extends SQLSubPartitionBy implements MySqlOb
this.columns.add(column);
}
public void cloneTo(MySqlSubPartitionByKey x) {
super.cloneTo(x);
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
}
public MySqlSubPartitionByKey clone() {
MySqlSubPartitionByKey x = new MySqlSubPartitionByKey();
cloneTo(x);
return x;
}
}
@@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLSubPartitionBy;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject;
@@ -72,4 +73,21 @@ public class MySqlSubPartitionByList extends SQLSubPartitionBy implements MySqlO
this.columns.add(column);
}
public void cloneTo(MySqlSubPartitionByList x) {
super.cloneTo(x);
if (expr != null) {
x.setExpr(expr.clone());
}
for (SQLColumnDefinition column : columns) {
SQLColumnDefinition c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
}
public MySqlSubPartitionByList clone() {
MySqlSubPartitionByList x = new MySqlSubPartitionByList();
cloneTo(x);
return x;
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
@@ -69,4 +70,18 @@ public class MySqlTableIndex extends MySqlObjectImpl implements SQLTableElement
}
visitor.endVisit(this);
}
public MySqlTableIndex clone() {
MySqlTableIndex x = new MySqlTableIndex();
if (name != null) {
x.setName(name.clone());
}
x.indexType = indexType;
for (SQLSelectOrderByItem column : columns) {
SQLSelectOrderByItem c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
return x;
}
}
@@ -146,4 +146,28 @@ public abstract class OracleSegmentAttributesImpl extends SQLObjectImpl implemen
public void setCompressForOltp(boolean compressForOltp) {
this.compressForOltp = compressForOltp;
}
public void cloneTo(OracleSegmentAttributesImpl x) {
x.pctfree = pctfree;
x.pctused = pctused;
x.initrans = initrans;
x.maxtrans = maxtrans;
x.pctincrease = pctincrease;
x.freeLists = freeLists;
x.compress = compress;
x.compressLevel = compressLevel;
x.compressForOltp = compressForOltp;
x.pctthreshold = pctthreshold;
x.logging = logging;
if (tablespace != null) {
x.setTablespace(tablespace.clone());
}
if (storage != null) {
x.setStorage(storage.clone());
}
}
}
@@ -96,4 +96,22 @@ public class OraclePrimaryKey extends SQLPrimaryKeyImpl implements OracleConstra
this.initially = initially;
}
public void cloneTo(OraclePrimaryKey x) {
super.cloneTo(x);
if (using != null) {
x.setUsing(using.clone());
}
if (exceptionsInto != null) {
x.setExceptionsInto(exceptionsInto.clone());
}
x.enable = enable;
x.initially = initially;
x.deferrable = deferrable;
}
public OraclePrimaryKey clone() {
OraclePrimaryKey x = new OraclePrimaryKey();
cloneTo(x);
return x;
}
}
@@ -75,4 +75,14 @@ public class OracleSupplementalIdKey extends OracleSQLObjectImpl implements SQLT
public void setUniqueIndex(boolean uniqueIndex) {
this.uniqueIndex = uniqueIndex;
}
public OracleSupplementalIdKey clone() {
OracleSupplementalIdKey x = new OracleSupplementalIdKey();
x.all = all;
x.primaryKey = primaryKey;
x.unique = unique;
x.uniqueIndex = uniqueIndex;
x.foreignKey = foreignKey;
return x;
}
}
@@ -16,6 +16,7 @@
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObjectImpl;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
@@ -69,4 +70,18 @@ public class OracleSupplementalLogGrp extends OracleSQLObjectImpl implements SQL
public void setAlways(boolean always) {
this.always = always;
}
public OracleSupplementalLogGrp clone() {
OracleSupplementalLogGrp x = new OracleSupplementalLogGrp();
if (group != null) {
x.setGroup(group.clone());
}
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
x.always = always;
return x;
}
}
@@ -92,4 +92,26 @@ public class OracleUsingIndexClause extends OracleSegmentAttributesImpl implemen
public List<SQLPartition> getLocalPartitionIndex() {
return localPartitionIndex;
}
public void cloneTo(OracleUsingIndexClause x) {
super.cloneTo(x);
if (index != null) {
x.setIndex(index.clone());
}
x.enable = enable;
x.computeStatistics = computeStatistics;
x.reverse = reverse;
for (SQLPartition p : localPartitionIndex) {
SQLPartition p2 = p.clone();
p2.setParent(x);
x.localPartitionIndex.add(p2);
}
}
public OracleUsingIndexClause clone() {
OracleUsingIndexClause x = new OracleUsingIndexClause();
cloneTo(x);
return x;
}
}
@@ -214,6 +214,16 @@ public class Schema {
}
public boolean visit(MySqlCreateTableStatement x) {
SQLExprTableSource like = x.getLike();
if (like != null) {
SchemaObject table = repository.findTable((SQLName) like.getExpr());
if (table != null) {
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) table.getStatement();
MySqlCreateTableStatement stmtCloned = stmt.clone();
stmtCloned.setName(x.getName().clone());
return visit((SQLCreateTableStatement) stmtCloned);
}
}
visit((SQLCreateTableStatement) x);
return false;
}
@@ -276,7 +276,7 @@ public class MySqlCreateTable_showColumns_repository_test extends MysqlTest {
"\t`c_mediumtext` mediumtext COMMENT 'mediumtext',\n" +
"\t`c_longblob` longblob COMMENT 'longblob',\n" +
"\tKEY `k_c` (`c_tinyint`, `c_int`)\n" +
") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT = '10000000'", createTableStmt.toString());
") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT = '10000000'", repository.console("show create table test1"));
assertEquals("+--------------+--------------+------+-----+---------------------+-----------------------------+\n" +
"| Field | Type | Null | Key | Default | Extra |\n" +
@@ -299,5 +299,28 @@ public class MySqlCreateTable_showColumns_repository_test extends MysqlTest {
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
"| c_longblob | longblob | NO | | NULL | |\n" +
"+--------------+--------------+------+-----+---------------------+-----------------------------+\n", repository.console("show columns from test1"));
repository.console("CREATE TABLE test2 like test1");
assertEquals("CREATE TABLE test2 (\n" +
"\tc_decimal_1 INT(11) NOT NULL DEFAULT NULL,\n" +
"\t`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',\n" +
"\t`c_tinyint` tinyint(4) DEFAULT '1' COMMENT 'tinyint',\n" +
"\t`c_smallint` smallint(6) DEFAULT 0 COMMENT 'smallint',\n" +
"\t`c_mediumint` mediumint(9) DEFAULT NULL COMMENT 'mediumint',\n" +
"\t`c_int` int(11) DEFAULT NULL COMMENT 'int',\n" +
"\t`c_bigint` bigint(20) DEFAULT NULL COMMENT 'bigint',\n" +
"\t`c_date` date DEFAULT '0000-00-00' COMMENT 'date',\n" +
"\t`c_datetime` datetime DEFAULT '0000-00-00 00:00:00' COMMENT 'datetime',\n" +
"\t`c_timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'timestamp',\n" +
"\t`c_time` time DEFAULT NULL COMMENT 'time',\n" +
"\t`c_char` char(10) DEFAULT NULL COMMENT 'char',\n" +
"\t`c_varchar` varchar(10) DEFAULT 'hello' COMMENT 'varchar',\n" +
"\t`c_blob` blob COMMENT 'blob',\n" +
"\t`c_text` text COMMENT 'text',\n" +
"\t`c_mediumtext` mediumtext COMMENT 'mediumtext',\n" +
"\t`c_longblob` longblob COMMENT 'longblob',\n" +
"\tKEY `k_c` (`c_tinyint`, `c_int`)\n" +
") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT = '10000000'", repository.console("show create table test2"));
}
}