From 835d45f07c7b757fd999effdd2d9b8fa62abf0cc Mon Sep 17 00:00:00 2001 From: taozhi8833998 Date: Mon, 22 Jan 2024 09:41:49 +0800 Subject: [PATCH] fix: check constraint support and expr in mysql --- package.json | 2 +- pegjs/mariadb.pegjs | 2 +- pegjs/mysql.pegjs | 2 +- test/mysql-mariadb.spec.js | 9 +++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3a0a2c15..391a509e 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "ast", "sql-ast" ], - "author": "taozhi8833998 ", + "author": "taozhi8833998 ", "files": [ "ast/", "build", diff --git a/pegjs/mariadb.pegjs b/pegjs/mariadb.pegjs index 9ac3f0b5..c3901de7 100644 --- a/pegjs/mariadb.pegjs +++ b/pegjs/mariadb.pegjs @@ -1295,7 +1295,7 @@ create_constraint_foreign } check_constraint_definition - = kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? { + = kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:or_and_expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? { const enforced = [] if (ne) enforced.push(ne[0], ne[2]) return { diff --git a/pegjs/mysql.pegjs b/pegjs/mysql.pegjs index 3e5d3e18..3c1d10fd 100644 --- a/pegjs/mysql.pegjs +++ b/pegjs/mysql.pegjs @@ -1530,7 +1530,7 @@ create_constraint_foreign } check_constraint_definition - = kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? { + = kc:constraint_name? __ u:'CHECK'i __ LPAREN __ c:or_and_expr __ RPAREN __ ne:(KW_NOT? __ 'ENFORCED'i)? { const enforced = [] if (ne) enforced.push(ne[0], ne[2]) return { diff --git a/test/mysql-mariadb.spec.js b/test/mysql-mariadb.spec.js index a62650c0..92f3edf1 100644 --- a/test/mysql-mariadb.spec.js +++ b/test/mysql-mariadb.spec.js @@ -927,6 +927,15 @@ describe('mysql', () => { `CREATE USER 'jeffrey'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new_password' DEFAULT ROLE 'administrator', 'developer' REQUIRE SSL AND X509 WITH MAX_QUERIES_PER_HOUR 100 PASSWORD EXPIRE INTERVAL 180 DAY FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 2 ACCOUNT LOCK COMMENT 'test comment' ATTRIBUTE '{"fname": "James", "lname": "Scott", "phone": "123-456-7890"}'` ] }, + { + title: 'check constraint', + sql: [ + 'CREATE TABLE `table` (\n' + + '`name` VARCHAR(255) CHECK(`name` LIKE \'ABC%\' AND LENGTH(`name`) >= 5)\n' + + ');', + "CREATE TABLE `table` (`name` VARCHAR(255) CHECK (`name` LIKE 'ABC%' AND LENGTH(`name`) >= 5))" + ] + } ] SQL_LIST.forEach(sqlInfo => { const { title, sql } = sqlInfo