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

Support Parse MYSQL DDL with 'COLLATE' option & fix class typo #266

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Loading