improved sql parser schema repository.

This commit is contained in:
wenshao
2017-08-08 14:59:18 +08:00
parent 9f50882488
commit bcb692f424
15 changed files with 649 additions and 252 deletions
@@ -215,15 +215,16 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
}
public boolean apply(SQLAlterTableAddIndex item) {
if (item.isKey()) {
MySqlPrimaryKey x = new MySqlPrimaryKey();
if (item.isUnique()) {
MySqlUnique x = new MySqlUnique();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
if (item.isUnique()) {
MySqlUnique x = new MySqlUnique();
if (item.isKey()) {
MySqlKey x = new MySqlKey();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(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.dialect.mysql.ast.MySqlObjectImpl;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
@@ -46,22 +47,22 @@ public class MySqlRenameTableStatement extends MySqlStatementImpl {
public static class Item extends MySqlObjectImpl {
private SQLExpr name;
private SQLExpr to;
private SQLName name;
private SQLName to;
public SQLExpr getName() {
public SQLName getName() {
return name;
}
public void setName(SQLExpr name) {
public void setName(SQLName name) {
this.name = name;
}
public SQLExpr getTo() {
public SQLName getTo() {
return to;
}
public void setTo(SQLExpr to) {
public void setTo(SQLName to) {
this.to = to;
}
@@ -21,6 +21,7 @@ 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.SQLMethodInvokeExpr;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
@@ -29,8 +30,8 @@ import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlTableIndex extends MySqlObjectImpl implements SQLTableElement {
private SQLName name;
private String indexType;
private SQLName name;
private String indexType;
private List<SQLSelectOrderByItem> columns = new ArrayList<SQLSelectOrderByItem>();
public MySqlTableIndex(){
@@ -106,6 +107,11 @@ public class MySqlTableIndex extends MySqlObjectImpl implements SQLTableElement
columns.remove(i);
return true;
}
if (expr instanceof SQLMethodInvokeExpr
&& SQLUtils.nameEquals(((SQLMethodInvokeExpr) expr).getMethodName(), columnName.getSimpleName())) {
columns.remove(i);
return true;
}
}
return false;
}
@@ -785,6 +785,11 @@ public class MySqlExprParser extends SQLExprParser {
lexer.nextToken();
}
if (lexer.token() != Token.LPAREN) {
SQLName name = this.name();
primaryKey.setName(name);
}
accept(Token.LPAREN);
for (;;) {
primaryKey.addColumn(this.expr());
@@ -2464,7 +2464,7 @@ public class MySqlStatementParser extends SQLStatementParser {
if (lexer.token() == Token.COLUMN) {
lexer.nextToken();
parseAlterTableAddColumn(stmt);
} else if (lexer.token() == Token.INDEX) {
} else if (lexer.token() == Token.INDEX || identifierEquals("FULLTEXT")) {
SQLAlterTableAddIndex item = parseAlterTableAddIndex();
item.setParent(stmt);
stmt.addItem(item);
@@ -2484,31 +2484,52 @@ public class MySqlStatementParser extends SQLStatementParser {
stmt.addItem(item);
} else if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
SQLName constraintName = this.exprParser.name();
if (lexer.token() == Token.PRIMARY) {
SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
primaryKey.setName(constraintName);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.FOREIGN) {
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
fk.setName(constraintName);
fk.setHasConstraint(true);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
stmt.addItem(item);
} else if (lexer.token() == Token.UNIQUE) {
SQLUnique unique = this.exprParser.parseUnique();
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
stmt.addItem(item);
} else {
throw new ParserException("TODO " + lexer.info());
SQLName constraintName = this.exprParser.name();
if (lexer.token() == Token.PRIMARY) {
SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
primaryKey.setName(constraintName);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
item.setParent(stmt);
stmt.addItem(item);
} else if (lexer.token() == Token.FOREIGN) {
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
fk.setName(constraintName);
fk.setHasConstraint(true);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
stmt.addItem(item);
} else if (lexer.token() == Token.UNIQUE) {
SQLUnique unique = this.exprParser.parseUnique();
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
stmt.addItem(item);
} else {
throw new ParserException("TODO " + lexer.info());
}
}
} else if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
@@ -2647,7 +2668,7 @@ public class MySqlStatementParser extends SQLStatementParser {
}
MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement();
MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item();
item.setName(stmt.getTableSource().getExpr());
item.setName((SQLName) stmt.getTableSource().getExpr());
item.setTo(this.exprParser.name());
renameStmt.addItem(item);
@@ -458,15 +458,21 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public boolean visit(MySqlTableIndex x) {
String indexType = x.getIndexType();
if ("FULLTEXT".equalsIgnoreCase(indexType)) {
print0(ucase ? "FULLTEXT " : "fulltext ");
}
print0(ucase ? "INDEX" : "index");
if (x.getName() != null) {
print(' ');
x.getName().accept(this);
}
if (x.getIndexType() != null) {
if (indexType != null && !"FULLTEXT".equalsIgnoreCase(indexType)) {
print0(ucase ? " USING " : " using ");
print0(x.getIndexType());
print0(indexType);
}
print('(');
@@ -133,9 +133,9 @@ public class MySqlShowColumnOutpuVisitor extends MySqlOutputVisitor {
print(" | ");
if (column.containsNotNullConstaint()) {
print("YES");
} else {
print("NO ");
} else {
print("YES");
}
print(" | ");