improved mysql 8.16 support, sql parser support check not enforced option, fix #4082

This commit is contained in:
高铁
2020-12-11 10:49:03 +08:00
parent b1e8264b3a
commit b04ff9b889
5 changed files with 63 additions and 0 deletions
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLCheck extends SQLConstraintImpl implements SQLTableElement, SQLTableConstraint, SQLReplaceable {
private SQLExpr expr;
private Boolean enforced;
public SQLCheck(){
@@ -63,6 +64,8 @@ public class SQLCheck extends SQLConstraintImpl implements SQLTableElement, SQLT
if (expr != null) {
expr = expr.clone();
}
x.enforced = enforced;
}
public SQLCheck clone() {
@@ -89,4 +92,12 @@ public class SQLCheck extends SQLConstraintImpl implements SQLTableElement, SQLT
}
return false;
}
public Boolean getEnforced() {
return enforced;
}
public void setEnforced(Boolean enforced) {
this.enforced = enforced;
}
}
@@ -1549,6 +1549,22 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
SQLExpr expr = this.exprParser.expr();
check.setExpr(expr);
constraint = check;
if (lexer.token() == Token.HINT) {
String hintText = lexer.stringVal();
if (hintText != null) {
hintText = hintText.trim();
}
if (hintText.startsWith("!")) {
if (hintText.endsWith("NOT ENFORCED")) {
check.setEnforced(false);
} else if (hintText.endsWith(" ENFORCED")) {
check.setEnforced(true);
}
lexer.nextToken();
}
}
}
if (constraint != null) {
@@ -5273,6 +5273,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
x.getExpr().accept(this);
this.indentCount--;
print(')');
Boolean enforced = x.getEnforced();
if (enforced != null) {
if (enforced.booleanValue()) {
print0(ucase ? " ENFORCED" : " enforced");
} else {
print0(ucase ? " NOT ENFORCED" : " not enforced");
}
}
return false;
}
@@ -58,6 +58,9 @@ public class MySqlResourceTest extends TestCase {
public void test_16() throws Exception {
exec_test("bvt/parser/mysql-16.txt");
}
public void test_17() throws Exception {
exec_test("bvt/parser/mysql-17.txt");
}
public void exec_test(String resource) throws Exception {
// System.out.println(resource);
@@ -0,0 +1,23 @@
CREATE TABLE `t12` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
CONSTRAINT `c12_positive` CHECK ((`c2` > 0)) /*!80016 NOT ENFORCED */,
CONSTRAINT `c21_nonzero` CHECK ((`c1` <> 0)),
CONSTRAINT `t12_chk_1` CHECK ((`c1` <> `c2`)),
CONSTRAINT `t12_chk_2` CHECK ((`c1` > 10)),
CONSTRAINT `t12_chk_3` CHECK ((`c3` < 100)),
CONSTRAINT `t12_chk_4` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
---------------------------
CREATE TABLE `t12` (
`c1` int DEFAULT NULL,
`c2` int DEFAULT NULL,
`c3` int DEFAULT NULL,
CONSTRAINT `c12_positive` CHECK (`c2` > 0) NOT ENFORCED,
CONSTRAINT `c21_nonzero` CHECK (`c1` <> 0),
CONSTRAINT `t12_chk_1` CHECK (`c1` <> `c2`),
CONSTRAINT `t12_chk_2` CHECK (`c1` > 10),
CONSTRAINT `t12_chk_3` CHECK (`c3` < 100),
CONSTRAINT `t12_chk_4` CHECK (`c1` > `c3`)
) ENGINE = InnoDB CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci