mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. add new feature JdbcUtils.getCreateTableScript, for issue #1342
This commit is contained in:
@@ -26,8 +26,10 @@ import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElement {
|
||||
protected String dbType;
|
||||
|
||||
protected SQLName name;
|
||||
protected SQLDataType dataType;
|
||||
@@ -287,6 +289,7 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
|
||||
public SQLColumnDefinition clone() {
|
||||
SQLColumnDefinition x = new SQLColumnDefinition();
|
||||
x.setDbType(dbType);
|
||||
|
||||
if(name != null) {
|
||||
x.setName(name.clone());
|
||||
@@ -341,4 +344,26 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public void simplify() {
|
||||
enable = null;
|
||||
validate = null;
|
||||
rely = null;
|
||||
|
||||
|
||||
if (this.name instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) this.name;
|
||||
String columnName = identExpr.getName();
|
||||
columnName = SQLUtils.normalize(columnName, dbType);
|
||||
identExpr.setName(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,6 @@ public interface SQLConstraint extends SQLObject {
|
||||
SQLName getName();
|
||||
|
||||
void setName(SQLName value);
|
||||
|
||||
void simplify();
|
||||
}
|
||||
|
||||
@@ -15,13 +15,16 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLConstraint {
|
||||
protected String dbType;
|
||||
|
||||
protected SQLName name;
|
||||
private Boolean enable;
|
||||
@@ -90,4 +93,21 @@ public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLCons
|
||||
public void setRely(Boolean rely) {
|
||||
this.rely = rely;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public void simplify() {
|
||||
if (this.name instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) this.name;
|
||||
String columnName = identExpr.getName();
|
||||
columnName = SQLUtils.normalize(columnName, dbType);
|
||||
identExpr.setName(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,13 @@ import java.util.*;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.ListDG;
|
||||
import com.alibaba.druid.util.lang.Consumer;
|
||||
import org.apache.ibatis.jdbc.SQL;
|
||||
|
||||
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
@@ -59,6 +61,10 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
this.setTableSource(new SQLExprTableSource(name));
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.setName(new SQLIdentifierExpr(name));
|
||||
}
|
||||
|
||||
public SQLExprTableSource getTableSource() {
|
||||
return tableSource;
|
||||
}
|
||||
@@ -414,4 +420,37 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void simplify() {
|
||||
SQLName name = getName();
|
||||
if (name instanceof SQLPropertyExpr) {
|
||||
String tableName = ((SQLPropertyExpr) name).getName();
|
||||
tableName = SQLUtils.normalize(tableName);
|
||||
setName(tableName);
|
||||
name = getName();
|
||||
}
|
||||
|
||||
if (name instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) name;
|
||||
String tableName = identExpr.getName();
|
||||
tableName = SQLUtils.normalize(tableName, dbType);
|
||||
identExpr.setName(tableName);
|
||||
}
|
||||
|
||||
if (name instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) name;
|
||||
String tableName = identExpr.getName();
|
||||
tableName = SQLUtils.normalize(tableName);
|
||||
identExpr.setName(tableName);
|
||||
}
|
||||
|
||||
for (SQLTableElement element : this.tableElementList) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
SQLColumnDefinition column = (SQLColumnDefinition) element;
|
||||
column.simplify();
|
||||
} else if (element instanceof SQLConstraint) {
|
||||
((SQLConstraint) element).simplify();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,10 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint, SQLTableElement {
|
||||
@@ -58,4 +60,17 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
|
||||
x.addColumn(columnClone);
|
||||
}
|
||||
}
|
||||
|
||||
public void simplify() {
|
||||
super.simplify();
|
||||
|
||||
for (SQLExpr column : columns) {
|
||||
if (column instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) column;
|
||||
String columnName = identExpr.getName();
|
||||
columnName = SQLUtils.normalize(columnName, dbType);
|
||||
identExpr.setName(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user