Skip to content

Commit

Permalink
fix references not null issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed Mar 25, 2024
1 parent 365e520 commit e64d821
Show file tree
Hide file tree
Showing 5 changed files with 402 additions and 327 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**v1.0.4**
### Improvements
1. Support functions with schema prefix in `DEFAULT` and `CHECK` statements.
1. Support functions with schema prefix in `DEFAULT` and `CHECK` statements. https://github.com/xnuinside/simple-ddl-parser/issues/240
2. Fix for REFERENCES NOT NULL - https://github.com/xnuinside/simple-ddl-parser/issues/239

**v1.0.3**
### Improvements
Expand Down
17 changes: 10 additions & 7 deletions simple_ddl_parser/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Snowflake:

def p_clone(self, p: List) -> None:
"""clone : CLONE id"""
p_list = list(p)
Expand Down Expand Up @@ -34,11 +33,16 @@ def p_multi_id_or_string(self, p: List) -> None:
p[0] = value

def p_fmt_equals(self, p: List) -> None:
"""fmt_equals : id LP multi_id_or_string RP
"""
fmt_split = re.compile(r"\w+\s*=\s*\w+|\w+\s*=\s*'.'|\w+\s*=\s*'..'|\w+\s*=\s*\('.+'\)|\w+\s*=\(\)")
"""fmt_equals : id LP multi_id_or_string RP"""
fmt_split = re.compile(
r"\w+\s*=\s*\w+|\w+\s*=\s*'.'|\w+\s*=\s*'..'|\w+\s*=\s*\('.+'\)|\w+\s*=\(\)"
)
p_list = list(p)
p[0] = {f.split('=')[0].strip(): f.split('=')[1].strip() for f in fmt_split.findall(p_list[3]) if '=' in f}
p[0] = {
f.split("=")[0].strip(): f.split("=")[1].strip()
for f in fmt_split.findall(p_list[3])
if "=" in f
}

def p_table_property_equals(self, p: List) -> None:
"""table_property_equals : id id id_or_string
Expand Down Expand Up @@ -92,8 +96,7 @@ def p_expression_change_tracking(self, p: List) -> None:
p[0]["change_tracking"] = p_list[-1]

def p_comment_equals(self, p: List) -> None:
"""expr : expr option_comment
"""
"""expr : expr option_comment"""
p[0] = p[1]
if p[2]:
p[0].update(p[2])
Expand Down
3 changes: 2 additions & 1 deletion simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ def p_autoincrement(self, p: List) -> None:
def p_defcolumn(self, p: List) -> None:
"""defcolumn : column
| defcolumn comment
| defcolumn null
| defcolumn encode
| defcolumn PRIMARY KEY
| defcolumn UNIQUE KEY
Expand All @@ -405,6 +404,8 @@ def p_defcolumn(self, p: List) -> None:
| defcolumn collate
| defcolumn enforced
| defcolumn ref
| defcolumn null
| defcolumn ref null
| defcolumn foreign ref
| defcolumn encrypt
| defcolumn generated
Expand Down
637 changes: 319 additions & 318 deletions simple_ddl_parser/parsetab.py

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions tests/test_simple_ddl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3335,3 +3335,72 @@ def test_create_empty_table_with_parentheses():
"types": [],
}
assert result == expected


def test_reference_not_null():

ddl = """CREATE TABLE a
(
id UUID PRIMARY KEY
);
CREATE TABLE b
(
id UUID PRIMARY KEY,
a_id UUID REFERENCES a(id) NOT NULL
);
"""
result = DDLParser(ddl).run(group_by_type=True)
expected = {'ddl_properties': [],
'domains': [],
'schemas': [],
'sequences': [],
'tables': [{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'name': 'id',
'nullable': False,
'references': None,
'size': None,
'type': 'UUID',
'unique': False}],
'index': [],
'partitioned_by': [],
'primary_key': ['id'],
'schema': None,
'table_name': 'a',
'tablespace': None},
{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'name': 'id',
'nullable': False,
'references': None,
'size': None,
'type': 'UUID',
'unique': False},
{'check': None,
'default': None,
'name': 'a_id',
'nullable': False,
'references': {'columns': ['id'],
'deferrable_initially': None,
'on_delete': None,
'on_update': None,
'schema': None,
'table': 'a'},
'size': None,
'type': 'UUID',
'unique': False}],
'index': [],
'partitioned_by': [],
'primary_key': ['id'],
'schema': None,
'table_name': 'b',
'tablespace': None}],
'types': []}

assert expected == result

0 comments on commit e64d821

Please sign in to comment.