bugfix for 'FOREIGN KEY reference'

This commit is contained in:
Yako
2014-08-04 13:37:21 +08:00
parent 238a04d9e2
commit 7d5f0cf8da
4 changed files with 195 additions and 4 deletions
@@ -29,6 +29,12 @@ public class MysqlForeignKey extends SQLForeignKeyImpl {
private boolean hasConstaint;
private Match referenceMatch;
private On referenceOn;
private Option referenceOption;
public SQLName getIndexName() {
return indexName;
}
@@ -63,4 +69,52 @@ public class MysqlForeignKey extends SQLForeignKeyImpl {
}
visitor.endVisit(this);
}
public Match getReferenceMatch() {
return referenceMatch;
}
public void setReferenceMatch(Match referenceMatch) {
this.referenceMatch = referenceMatch;
}
public On getReferenceOn() {
return referenceOn;
}
public void setReferenceOn(On referenceOn) {
this.referenceOn = referenceOn;
}
public Option getReferenceOption() {
return referenceOption;
}
public void setReferenceOption(Option referenceOption) {
this.referenceOption = referenceOption;
}
public static enum Option {
RESTRICT("RESTRICT"), CASCADE("CASCADE"), SET_NULL("SET NULL"), NO_ACTION("NO ACTION");
private String text;
Option(String text){
this.text = text;
}
public String getText() {
return text;
}
}
public static enum Match {
FULL, PARTIAL, SIMPLE;
}
public static enum On {
DELETE, UPDATE;
}
}
@@ -35,6 +35,9 @@ import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
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.MysqlForeignKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Match;
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.On;
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Option;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr;
@@ -249,7 +252,7 @@ public class MySqlExprParser extends SQLExprParser {
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error");
}
@@ -484,7 +487,7 @@ public class MySqlExprParser extends SQLExprParser {
return userName;
}
if(lexer.token() == Token.ERROR) {
if (lexer.token() == Token.ERROR) {
throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : "
+ lexer.pos());
}
@@ -580,7 +583,7 @@ public class MySqlExprParser extends SQLExprParser {
lexer.nextToken();
dataType.getAttributes().put("UNSIGNED", true);
}
if (identifierEquals("ZEROFILL")) {
lexer.nextToken();
dataType.getAttributes().put("ZEROFILL", true);
@@ -782,6 +785,47 @@ public class MySqlExprParser extends SQLExprParser {
accept(Token.LPAREN);
this.names(fk.getReferencedColumns());
accept(Token.RPAREN);
if (identifierEquals("MATCH")) {
if (identifierEquals("FULL")) {
fk.setReferenceMatch(Match.FULL);
} else if (identifierEquals("PARTIAL")) {
fk.setReferenceMatch(Match.PARTIAL);
} else if (identifierEquals("SIMPLE")) {
fk.setReferenceMatch(Match.SIMPLE);
}
}
if (lexer.token() == Token.ON) {
lexer.nextToken();
if (lexer.token() == Token.DELETE) {
fk.setReferenceOn(On.DELETE);
} else if (lexer.token() == Token.UPDATE) {
fk.setReferenceOn(On.UPDATE);
} else {
throw new ParserException("syntax error, expect DELETE or UPDATE, actual " + lexer.token() + " "
+ lexer.stringVal());
}
lexer.nextToken();
if (lexer.token() == Token.RESTRICT) {
fk.setReferenceOption(Option.RESTRICT);
} else if (identifierEquals("CASCADE")) {
fk.setReferenceOption(Option.CASCADE);
} else if (lexer.token() == Token.SET) {
accept(Token.NULL);
fk.setReferenceOption(Option.SET_NULL);
} else if (identifierEquals("ON")) {
lexer.nextToken();
if (identifierEquals("ACTION")) {
fk.setReferenceOption(Option.NO_ACTION);
} else {
throw new ParserException("syntax error, expect ACTION, actual " + lexer.token() + " "
+ lexer.stringVal());
}
}
lexer.nextToken();
}
return fk;
}
@@ -2862,6 +2862,20 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
print(" (");
printAndAccept(x.getReferencedColumns(), ", ");
print(")");
if(x.getReferenceMatch() != null) {
print(" MATCH ");
print(x.getReferenceMatch().name());
}
if(x.getReferenceOn()!= null) {
print(" ON ");
print(x.getReferenceOn().name());
print(" ");
if(x.getReferenceOption() != null) {
print(x.getReferenceOption().getText());
}
}
return false;
}