Skip to content

Commit

Permalink
add INDEX to MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed May 22, 2024
1 parent b743e1d commit 402c6e9
Show file tree
Hide file tree
Showing 7 changed files with 18,247 additions and 18,044 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
**v1.5.1**
### Improvements
#### MySQL

1. Added support for INDEX statement in column definition - https://github.com/xnuinside/simple-ddl-parser/issues/253
2.


**v1.5.0**

### Fixes
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@ for help with debugging & testing support for BigQuery dialect DDLs:


## Changelog
**v1.5.1**
### Improvements
#### MySQL

1. Added support for INDEX statement in column definition - https://github.com/xnuinside/simple-ddl-parser/issues/253
2.


**v1.5.0**

### Fixes
Expand Down
12 changes: 12 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,18 @@ for help with debugging & testing support for BigQuery dialect DDLs:
Changelog
---------

**v1.5.1**

Improvements
^^^^^^^^^^^^

MySQL
~~~~~


#. Added support for INDEX statement in column definition - https://github.com/xnuinside/simple-ddl-parser/issues/253
2.

**v1.5.0**

Fixes
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simple-ddl-parser"
version = "1.5.0"
version = "1.5.1"
description = "Simple DDL Parser to parse SQL & dialects like HQL, TSQL (MSSQL), Oracle, AWS Redshift, Snowflake, MySQL, PostgreSQL, etc ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc.; sequences, alters, custom types & other entities from ddl."
authors = ["Iuliia Volkova <[email protected]>"]
license = "MIT"
Expand Down
13 changes: 11 additions & 2 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ def get_column_properties(p_list: List) -> Tuple:
default = None
unique = False
references = None
index = False

if isinstance(p_list[-1], str):
if p_list[-1].upper() == "KEY":
if p_list[-2].upper() == "UNIQUE":
Expand All @@ -432,7 +434,9 @@ def get_column_properties(p_list: List) -> Tuple:
p_list[-1]["references"]["column"] = p_list[-1]["references"]["columns"][0]
del p_list[-1]["references"]["columns"]
references = p_list[-1]["references"]
return pk, default, unique, references, nullable
if p_list[-1] == "INDEX":
index = True
return pk, default, unique, references, nullable, index

def p_autoincrement(self, p: List) -> None:
"""autoincrement : AUTOINCREMENT"""
Expand All @@ -445,6 +449,7 @@ def p_defcolumn(self, p: List) -> None:
| defcolumn PRIMARY KEY
| defcolumn UNIQUE KEY
| defcolumn UNIQUE
| defcolumn INDEX
| defcolumn check_ex
| defcolumn default
| defcolumn collate
Expand All @@ -470,7 +475,9 @@ def p_defcolumn(self, p: List) -> None:
p[0] = p[1]
p_list = list(p)

pk, default, unique, references, nullable = self.get_column_properties(p_list)
pk, default, unique, references, nullable, index = self.get_column_properties(
p_list
)

self.set_property(p)

Expand All @@ -486,6 +493,8 @@ def p_defcolumn(self, p: List) -> None:
if isinstance(p_list[-1], dict) and p_list[-1].get("encode"):
p[0]["encode"] = p[0].get("encode", p_list[-1]["encode"])
p[0]["check"] = self.set_check_in_columm(p[0].get("check"))
if index:
p[0]["index"] = index

@staticmethod
def set_check_in_columm(check: Optional[List]) -> Optional[str]:
Expand Down
36,213 changes: 18,172 additions & 18,041 deletions simple_ddl_parser/parsetab.py

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,38 @@ def test_auto_increment_table_property():

result = DDLParser(ddl).run(output_mode="mysql")
assert result == expected


def test_column_index():
ddl = """CREATE TABLE `posts`(
`integer_column__index` INT NOT NULL INDEX
);"""

result = DDLParser(ddl).run()
expected = [
{
"alter": {},
"checks": [],
"columns": [
{
"check": None,
"default": None,
"index": True,
"name": "`integer_column__index`",
"nullable": False,
"references": None,
"size": None,
"type": "INT",
"unique": False,
}
],
"index": [],
"partitioned_by": [],
"primary_key": [],
"schema": None,
"table_name": "`posts`",
"tablespace": None,
}
]

assert result == expected

0 comments on commit 402c6e9

Please sign in to comment.