mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support alter table add foreign key
This commit is contained in:
+1
-17
@@ -118,23 +118,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
} else if (lexer.token() == (Token.PRIMARY)) {
|
||||
stmt.getTableElementList().add(parseConstraint());
|
||||
} else if (lexer.token() == (Token.FOREIGN)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.KEY);
|
||||
|
||||
MySqlForeignKey fk = new MySqlForeignKey();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.names(fk.getReferencingColumns());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
accept(Token.REFERENCES);
|
||||
|
||||
fk.setReferencedTableName(this.exprParser.name());
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.names(fk.getReferencedColumns());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
MySqlForeignKey fk = this.getExprParser().parseForeignKey();
|
||||
stmt.getTableElementList().add(fk);
|
||||
} else if (lexer.token() == Token.CHECK) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
@@ -692,4 +693,24 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public MySqlForeignKey parseForeignKey() {
|
||||
accept(Token.FOREIGN);
|
||||
accept(Token.KEY);
|
||||
|
||||
MySqlForeignKey fk = new MySqlForeignKey();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.names(fk.getReferencingColumns());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
accept(Token.REFERENCES);
|
||||
|
||||
fk.setReferencedTableName(this.name());
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.names(fk.getReferencedColumns());
|
||||
accept(Token.RPAREN);
|
||||
return fk;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddForeignKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableKeys;
|
||||
@@ -52,6 +53,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddIndex;
|
||||
@@ -2256,6 +2258,12 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
SQLAlterTableAddPrimaryKey item = new SQLAlterTableAddPrimaryKey();
|
||||
item.setPrimaryKey(primaryKey);
|
||||
stmt.getItems().add(item);
|
||||
} else if (lexer.token() == Token.FOREIGN) {
|
||||
MySqlForeignKey fk = this.getExprParser().parseForeignKey();
|
||||
fk.setName(constraintName);
|
||||
|
||||
SQLAlterTableAddForeignKey item = new SQLAlterTableAddForeignKey(fk);
|
||||
stmt.getItems().add(item);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
@@ -2538,4 +2546,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
lexer.nextToken();
|
||||
}
|
||||
}
|
||||
|
||||
public MySqlExprParser getExprParser() {
|
||||
return (MySqlExprParser) exprParser;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2766,21 +2766,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlForeignKey x) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlForeignKey x) {
|
||||
if (x.getName() != null) {
|
||||
print("CONSTRAINT ");
|
||||
x.getName().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
print("FOREIGN KEY ");
|
||||
|
||||
print(" (");
|
||||
print("FOREIGN KEY (");
|
||||
printAndAccept(x.getReferencedColumns(), ", ");
|
||||
print(")");
|
||||
|
||||
@@ -2790,6 +2782,12 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print(" (");
|
||||
printAndAccept(x.getReferencedColumns(), ", ");
|
||||
print(")");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlForeignKey x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user