public MysqlForeignKey parseForeignKey() {
accept(Token.FOREIGN);
accept(Token.KEY);
MysqlForeignKey fk = new MysqlForeignKey();
if (lexer.token() != Token.LPAREN) {
SQLName indexName = name();
fk.setIndexName(indexName);
}
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);
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());
}
}