From db15df8a6d38a74c95cd500aff54d40ae4ccfd31 Mon Sep 17 00:00:00 2001 From: bomwo Date: Sat, 6 Jul 2024 16:15:48 +0900 Subject: [PATCH] Support Parse MYSQL DDL with 'COLLATE' option & fix class typo --- simple_ddl_parser/ddl_parser.py | 8 ++++++ simple_ddl_parser/output/dialects.py | 2 +- tests/dialects/test_mysql.py | 40 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/simple_ddl_parser/ddl_parser.py b/simple_ddl_parser/ddl_parser.py index a3b2980..202388f 100755 --- a/simple_ddl_parser/ddl_parser.py +++ b/simple_ddl_parser/ddl_parser.py @@ -168,6 +168,14 @@ def exceptional_cases(self, value: str) -> bool: return True return False + def t_COLLATE(self, t: LexToken): + r"(?i:COLLATE|COLLATE)\b" + if not self.lexer.after_columns: + t.type = "COLLATE" + else: + t.type = "ID" + return self.set_last_token(t) + def t_AUTOINCREMENT(self, t: LexToken): r"(?i:AUTO_INCREMENT|AUTOINCREMENT)\b" if not self.lexer.after_columns: diff --git a/simple_ddl_parser/output/dialects.py b/simple_ddl_parser/output/dialects.py index 7024938..d24f826 100644 --- a/simple_ddl_parser/output/dialects.py +++ b/simple_ddl_parser/output/dialects.py @@ -86,7 +86,7 @@ class SparkSQL(Dialect): @dataclass @dialect(name="mysql") -class MySSQL(Dialect): +class MySQL(Dialect): engine: Optional[str] = field( default=None, metadata={"exclude_if_not_provided": True} ) diff --git a/tests/dialects/test_mysql.py b/tests/dialects/test_mysql.py index dc7c585..c659183 100644 --- a/tests/dialects/test_mysql.py +++ b/tests/dialects/test_mysql.py @@ -592,3 +592,43 @@ def test_column_index(): ] assert result == expected + + +def test_table_properties(): + ddl = """CREATE TABLE `posts`( + `integer_column__index` INT NOT NULL INDEX + ) ENGINE=InnoDB AUTO_INCREMENT=4682 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='test';""" + + result = DDLParser(ddl).run(output_mode="mysql") + expected = [ + { + "alter": {}, + "checks": [], + "auto_increment": "4682", + "columns": [ + { + "check": None, + "default": None, + "index": True, + "name": "`integer_column__index`", + "nullable": False, + "references": None, + "size": None, + "type": "INT", + "unique": False, + } + ], + "comment": "'test'", + "default_charset": "utf8mb4", + "engine": "InnoDB", + "index": [], + "partitioned_by": [], + "primary_key": [], + "schema": None, + "table_name": "`posts`", + "tablespace": None, + "table_properties": {"collate": "utf8mb4_unicode_ci"} + } + ] + assert result == expected +