Skip to content

Commit

Permalink
Merge pull request #266 from bomwo/main
Browse files Browse the repository at this point in the history
Support Parse MYSQL DDL with 'COLLATE' option & fix class typo
  • Loading branch information
xnuinside authored Jul 28, 2024
2 parents 335c627 + db15df8 commit cb4e1b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions simple_ddl_parser/ddl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion simple_ddl_parser/output/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
40 changes: 40 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cb4e1b5

Please sign in to comment.