Skip to content

Commit

Permalink
Merge pull request #4095 from WillsonYip/mysql8_check_fix
Browse files Browse the repository at this point in the history
support parsing mysql 8 check related DDL statement  #4089
  • Loading branch information
wenshao authored Dec 25, 2020
2 parents ae9e63f + cb16757 commit 40ad863
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;

public class MysqlAlterTableAlterCheck extends MySqlObjectImpl implements SQLAlterTableItem {

private SQLName name;
private Boolean enforced;

@Override
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
if (getName() != null) {
getName().accept(visitor);
}
}
visitor.endVisit(this);
}

public SQLName getName() {
return name;
}

public void setName(SQLName name) {
this.name = name;
}

public Boolean getEnforced() {
return enforced;
}

public void setEnforced(Boolean enforced) {
this.enforced = enforced;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1546,10 +1546,19 @@ protected SQLTableConstraint parseConstraint() {
lexer.nextToken();
SQLCheck check = new SQLCheck();
check.setName(name);
SQLExpr expr = this.exprParser.expr();
SQLExpr expr = this.exprParser.primary();
check.setExpr(expr);
constraint = check;

boolean enforce = true;
if (Token.NOT.equals(lexer.token())) {
enforce = false;
lexer.nextToken();
}
if (lexer.stringVal().equalsIgnoreCase("ENFORCED")) {
check.setEnforced(enforce);
lexer.nextToken();
}
if (lexer.token() == Token.HINT) {
String hintText = lexer.stringVal();
if (hintText != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6021,13 +6021,14 @@ private boolean parseAlterSpecification(SQLAlterTableStatement stmt) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER) {
constraintSymbol = this.exprParser.name();
if (lexer.token() != Token.PRIMARY && lexer.token() != Token.UNIQUE && lexer.token() != Token.FOREIGN) {
if (lexer.token() != Token.PRIMARY && lexer.token() != Token.UNIQUE && lexer.token() != Token.FOREIGN && lexer.token() != CHECK) {
throw new ParserException("syntax error, expect PRIMARY, UNIQUE or FOREIGN, actual " + lexer.token() + ", " + lexer.info());
}
}
case PRIMARY:
case UNIQUE:
case FOREIGN:
case CHECK:
// Constraint.
if (lexer.token() == Token.FOREIGN) {
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
Expand All @@ -6054,21 +6055,29 @@ private boolean parseAlterSpecification(SQLAlterTableStatement stmt) {
this.exprParser.parseIndex(uk.getIndexDefinition());
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(uk);
stmt.addItem(item);
} else if (lexer.token() == Token.CHECK) { // ADD CHECK (expr)
lexer.nextToken();
accept(Token.LPAREN);
SQLCheck check = new SQLCheck();
if (null != constraintSymbol) {
check.setName(constraintSymbol);
}
check.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
boolean enforce = true;
if (lexer.token() == Token.NOT) {
enforce = false;
lexer.nextToken();
}
if (lexer.stringVal().equalsIgnoreCase("ENFORCED")) {
check.setEnforced(enforce);
lexer.nextToken();
}
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(check);
stmt.addItem(item);
}
return true;

// ADD CHECK (expr)
case CHECK: {
lexer.nextToken();
accept(Token.LPAREN);
SQLCheck check = new SQLCheck();
check.setExpr(this.exprParser.expr());
accept(Token.RPAREN);
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(check);
stmt.addItem(item);
return true;
}

// ADD PARTITION (partition_definition)
case PARTITION: {
lexer.nextToken();
Expand Down Expand Up @@ -6525,6 +6534,20 @@ else if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)) {

alterIndex.setAnalyzerName(this.exprParser.name());
stmt.addItem(alterIndex);
} else if (lexer.token() == Token.CHECK || lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
MysqlAlterTableAlterCheck check = new MysqlAlterTableAlterCheck();
check.setName(this.exprParser.name());
boolean enforce = true;
if (lexer.token() == Token.NOT) {
enforce = false;
lexer.nextToken();
}
if (lexer.stringVal().equalsIgnoreCase("ENFORCED")) {
check.setEnforced(enforce);
lexer.nextToken();
}
stmt.addItem(check);
} else {
// ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
if (lexer.token() == Token.COLUMN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1443,4 +1443,12 @@ default boolean visit(MySqlJSONTableExpr.Column x) {
default void endVisit(MySqlJSONTableExpr.Column x) {

}

default boolean visit(MysqlAlterTableAlterCheck x) {
return true;
}

default void endVisit(MysqlAlterTableAlterCheck x) {

}
} //
Original file line number Diff line number Diff line change
Expand Up @@ -5519,4 +5519,27 @@ public boolean visit(MySqlJSONTableExpr.Column x) {
// return false;
// }

public boolean visit(MysqlAlterTableAlterCheck x) {
print0(ucase ? "ALTER CONSTRAINT " : "alter constraint ");

SQLName name = x.getName();
if (name != null) {
name.accept(this);
print(' ');
}

Boolean enforced = x.getEnforced();
if (enforced != null) {
if (enforced) {
print0(ucase ? " ENFORCED" : " enforced");
} else {
print0(ucase ? " NOT ENFORCED" : " not enforced");
}
}
return false;
}

public void endVisit(MysqlAlterTableAlterCheck x) {

}
} //
Loading

0 comments on commit 40ad863

Please sign in to comment.