Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysql check not enforce语法无法正常识别 #4082

Closed
WillsonYip opened this issue Dec 10, 2020 · 2 comments
Closed

Mysql check not enforce语法无法正常识别 #4082

WillsonYip opened this issue Dec 10, 2020 · 2 comments
Assignees
Milestone

Comments

@WillsonYip
Copy link
Contributor

WillsonYip commented Dec 10, 2020

环境:mysql 8.0.22 , druid 1.2.3

SQL :

建表语句:

CREATE TABLE t12
(
  CHECK (c1 <> c2),
  c1 INT CHECK (c1 > 10),
  c2 INT CONSTRAINT c12_positive CHECK (c2 > 0),
  c3 INT CHECK (c3 < 100),
  CONSTRAINT c21_nonzero CHECK (c1 <> 0),
  CHECK (c1 > c3)
);

alter table t12 ALTER CHECK  c12_positive  NOT ENFORCED;

通过MYSQL的show CREATE table t1 得到的建表语句

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

其中测试代码如下:

 public static void main(String[] args) {
        String sql ="CREATE TABLE `t12` (\n" +
                "  `c1` int DEFAULT NULL,\n" +
                "  `c2` int DEFAULT NULL,\n" +
                "  `c3` int DEFAULT NULL,\n" +
                "  CONSTRAINT `c12_positive` CHECK ((`c2` > 0)) /*!80016 NOT ENFORCED */,\n" +
                "  CONSTRAINT `c21_nonzero` CHECK ((`c1` <> 0)),\n" +
                "  CONSTRAINT `t12_chk_1` CHECK ((`c1` <> `c2`)),\n" +
                "  CONSTRAINT `t12_chk_2` CHECK ((`c1` > 10)),\n" +
                "  CONSTRAINT `t12_chk_3` CHECK ((`c3` < 100)),\n" +
                "  CONSTRAINT `t12_chk_4` CHECK ((`c1` > `c3`))\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement createTableStatement =  parser.parseStatement();
        System.out.println(createTableStatement);
    }

解释报错:

Exception in thread "main" com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'((`c2` > 0)) /*!80016 NOT ENFORCED */,
  CO', expect ), actual null, pos 167, line 5, column 49, token HINT
	at com.alibaba.druid.sql.parser.SQLParser.printError(SQLParser.java:558)
	at com.alibaba.druid.sql.parser.SQLParser.accept(SQLParser.java:566)
	at com.alibaba.druid.sql.dialect.mysql.parser.MySqlCreateTableParser.parseCreateTable(MySqlCreateTableParser.java:362)
	at com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser.parseCreate(MySqlStatementParser.java:301)
	at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:249)
	at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatement(SQLStatementParser.java:4465)
@wenshao wenshao added this to the 1.2.4 milestone Dec 11, 2020
@wenshao wenshao self-assigned this Dec 11, 2020
@wenshao
Copy link
Member

wenshao commented Dec 12, 2020

@WillsonYip
Copy link
Contributor Author

环境:druid 1.2.4

测试代码:

public static void main(String[] args) {
        String sql ="CREATE TABLE `t12` (\n" +
                "  `c1` int DEFAULT NULL,\n" +
                "  `c2` int DEFAULT NULL,\n" +
                "  `c3` int DEFAULT NULL,\n" +
                "  CONSTRAINT `c12_positive` CHECK ((`c2` > 0)) /*!80016 NOT ENFORCED */,\n" +
                "  CONSTRAINT `c21_nonzero` CHECK ((`c1` <> 0)),\n" +
                "  CONSTRAINT `t12_chk_1` CHECK ((`c1` <> `c2`)),\n" +
                "  CONSTRAINT `t12_chk_2` CHECK ((`c1` > 10)),\n" +
                "  CONSTRAINT `t12_chk_3` CHECK ((`c3` < 100)),\n" +
                "  CONSTRAINT `t12_chk_4` CHECK ((`c1` > `c3`))\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
        MySqlStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement createTableStatement =  parser.parseStatement();
        System.out.println(createTableStatement);

        parser = new MySqlStatementParser(createTableStatement.toString());
        createTableStatement = parser.parseStatement();
        System.out.println(createTableStatement);
    }

控制台打印的信息:

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
Exception in thread "main" com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'(`c2` > 0) NOT ENFORCED,
	CONSTRAINT `c21_non', expect ), actual null, pos 149, line 5, column 50, token IDENTIFIER ENFORCED
	at com.alibaba.druid.sql.parser.SQLParser.printError(SQLParser.java:558)
	at com.alibaba.druid.sql.parser.SQLParser.accept(SQLParser.java:566)
	at com.alibaba.druid.sql.dialect.mysql.parser.MySqlCreateTableParser.parseCreateTable(MySqlCreateTableParser.java:362)
	at com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser.parseCreate(MySqlStatementParser.java:301)
	at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:249)
	at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatement(SQLStatementParser.java:4469)
	at org.yzw.Main.main(Main.java:194)

现在能识别/*!80016 NOT ENFORCED */ ,解释并格式化出来的SQL: NOT ENFORCED还不能识别了

@WillsonYip WillsonYip changed the title Mysql check not enforce语法无法正常识别 Mysql 8 DDL check语法无法正常识别 Dec 16, 2020
@WillsonYip WillsonYip changed the title Mysql 8 DDL check语法无法正常识别 Mysql 8 check DDL语法无法正常识别 Dec 16, 2020
@WillsonYip WillsonYip changed the title Mysql 8 check DDL语法无法正常识别 Mysql check not enforce语法无法正常识别 Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants