Skip to content

Commit 620d024

Browse files
committed
add support for few statements
1 parent 7fabf55 commit 620d024

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed

CHANGELOG.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
**v0.26.3**
2+
3+
Improvements:
4+
1. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
5+
2. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130
6+
7+
18
**v0.26.2**
29

310
Fixes:

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ In output you will have names like 'dbo' and 'TO_Requests', not '[dbo]' and '[TO
327327

328328
## Supported Statements
329329

330-
- CREATE TABLE [ IF NOT EXISTS ] + columns defenition, columns attributes: column name + type + type size(for example, varchar(255)), UNIQUE, PRIMARY KEY, DEFAULT, CHECK, NULL/NOT NULL, REFERENCES, ON DELETE, ON UPDATE, NOT DEFERRABLE, DEFERRABLE INITIALLY, GENERATED ALWAYS, STORED, COLLATE
330+
- CREATE [OR REPLACE] TABLE [ IF NOT EXISTS ] + columns defenition, columns attributes: column name + type + type size(for example, varchar(255)), UNIQUE, PRIMARY KEY, DEFAULT, CHECK, NULL/NOT NULL, REFERENCES, ON DELETE, ON UPDATE, NOT DEFERRABLE, DEFERRABLE INITIALLY, GENERATED ALWAYS, STORED, COLLATE
331331

332332
- STATEMENTS: PRIMARY KEY, CHECK, FOREIGN KEY in table defenitions (in create table();)
333333

@@ -456,6 +456,24 @@ https://github.com/swiatek25
456456

457457

458458
## Changelog
459+
**v0.26.3**
460+
461+
Improvements:
462+
1. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
463+
2. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130
464+
465+
466+
**v0.26.2**
467+
468+
Fixes:
469+
1. Fixed a huge bug for incorrect parsing lines with 'USE' & 'GO' strings inside.
470+
2. Fixed parsing for CREATE SCHEMA for Snowlake & Oracle DDLs
471+
472+
Improvements:
473+
1. Added COMMENT statement for CREATE TABLE ddl (for SNOWFLAKE dialect support)
474+
2. Added COMMENT statement for CREATE SCHEMA ddl (for SNOWFLAKE dialect support)
475+
476+
459477
**v0.26.1**
460478

461479
Fixes:

docs/README.rst

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ Supported Statements
352352

353353

354354
*
355-
CREATE TABLE [ IF NOT EXISTS ] + columns defenition, columns attributes: column name + type + type size(for example, varchar(255)), UNIQUE, PRIMARY KEY, DEFAULT, CHECK, NULL/NOT NULL, REFERENCES, ON DELETE, ON UPDATE, NOT DEFERRABLE, DEFERRABLE INITIALLY, GENERATED ALWAYS, STORED, COLLATE
355+
CREATE [OR REPLACE] TABLE [ IF NOT EXISTS ] + columns defenition, columns attributes: column name + type + type size(for example, varchar(255)), UNIQUE, PRIMARY KEY, DEFAULT, CHECK, NULL/NOT NULL, REFERENCES, ON DELETE, ON UPDATE, NOT DEFERRABLE, DEFERRABLE INITIALLY, GENERATED ALWAYS, STORED, COLLATE
356356

357357
*
358358
STATEMENTS: PRIMARY KEY, CHECK, FOREIGN KEY in table defenitions (in create table();)
@@ -467,6 +467,7 @@ Snowflake Dialect statements
467467
* CREATE .. CLONE statements for table, database and schema
468468
* CREATE TABLE .. CLUSTER BY ..
469469
* CONSTRAINT .. [NOT] ENFORCED
470+
* COMMENT = in CREATE TABLE & CREATE SCHEMA statements
470471

471472
BigQuery
472473
^^^^^^^^
@@ -522,6 +523,28 @@ https://github.com/swiatek25
522523
Changelog
523524
---------
524525

526+
**v0.26.3**
527+
528+
Improvements:
529+
530+
531+
#. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
532+
#. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130
533+
534+
**v0.26.2**
535+
536+
Fixes:
537+
538+
539+
#. Fixed a huge bug for incorrect parsing lines with 'USE' & 'GO' strings inside.
540+
#. Fixed parsing for CREATE SCHEMA for Snowlake & Oracle DDLs
541+
542+
Improvements:
543+
544+
545+
#. Added COMMENT statement for CREATE TABLE ddl (for SNOWFLAKE dialect support)
546+
#. Added COMMENT statement for CREATE SCHEMA ddl (for SNOWFLAKE dialect support)
547+
525548
**v0.26.1**
526549

527550
Fixes:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "simple-ddl-parser"
3-
version = "0.26.2"
3+
version = "0.26.3"
44
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."
55
authors = ["Iuliia Volkova <[email protected]>"]
66
license = "MIT"

simple_ddl_parser/dialects/sql.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def add_if_not_exists(data: Dict, p_list: List):
121121
def p_create_table(self, p: List):
122122
"""create_table : CREATE TABLE IF NOT EXISTS
123123
| CREATE TABLE
124+
| CREATE OR REPLACE TABLE IF NOT EXISTS
125+
| CREATE OR REPLACE TABLE
124126
| CREATE id TABLE IF NOT EXISTS
125127
| CREATE id TABLE
126128
@@ -130,7 +132,8 @@ def p_create_table(self, p: List):
130132
p[0] = {}
131133
p_list = list(p)
132134
self.add_if_not_exists(p[0], p_list)
133-
135+
if 'REPLACE' in p_list:
136+
p[0]["replace"] = True
134137
if p[2].upper() == "EXTERNAL":
135138
p[0]["external"] = True
136139
if p[2].upper() == "TEMP" or p[2].upper() == "TEMPORARY":
@@ -141,7 +144,10 @@ class Column:
141144
def p_column_property(self, p: List):
142145
"""c_property : id id"""
143146
p_list = list(p)
144-
p[0] = {"property": {p_list[1]: p_list[-1]}}
147+
if p[1].lower() == "auto":
148+
p[0] = {"increment": True}
149+
else:
150+
p[0] = {"property": {p_list[1]: p_list[-1]}}
145151

146152
def set_base_column_propery(self, p: List) -> Dict:
147153

tests/test_simple_ddl_parser.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,3 +2617,92 @@ def test_check_that_all_columns_parsed_correctly():
26172617
}
26182618
]
26192619
assert expected == result
2620+
2621+
2622+
def test_create_or_replace():
2623+
2624+
ddl="""create or replace table someTable (
2625+
someField varchar(4)
2626+
);
2627+
"""
2628+
2629+
result = DDLParser(ddl,normalize_names=True).run()
2630+
2631+
expected = [{'alter': {},
2632+
'checks': [],
2633+
'columns': [{'check': None,
2634+
'default': None,
2635+
'name': 'someField',
2636+
'nullable': True,
2637+
'references': None,
2638+
'size': 4,
2639+
'type': 'varchar',
2640+
'unique': False}],
2641+
'index': [],
2642+
'partitioned_by': [],
2643+
'primary_key': [],
2644+
'replace': True,
2645+
'schema': None,
2646+
'table_name': 'someTable',
2647+
'tablespace': None}]
2648+
2649+
assert expected == result
2650+
2651+
2652+
def test_increment_column():
2653+
expected = [{'alter': {},
2654+
'checks': [],
2655+
'columns': [{'check': None,
2656+
'default': None,
2657+
'increment': True,
2658+
'name': 'user_id',
2659+
'nullable': False,
2660+
'references': None,
2661+
'size': None,
2662+
'type': 'INT',
2663+
'unique': False},
2664+
{'check': None,
2665+
'default': None,
2666+
'name': 'username',
2667+
'nullable': False,
2668+
'references': None,
2669+
'size': 100,
2670+
'type': 'VARCHAR',
2671+
'unique': False},
2672+
{'check': None,
2673+
'default': None,
2674+
'name': 'password',
2675+
'nullable': False,
2676+
'references': None,
2677+
'size': 40,
2678+
'type': 'VARCHAR',
2679+
'unique': False},
2680+
{'check': None,
2681+
'default': None,
2682+
'name': 'submission_date',
2683+
'nullable': True,
2684+
'references': None,
2685+
'size': None,
2686+
'type': 'DATE',
2687+
'unique': False}],
2688+
'constraints': {'checks': None, 'references': None, 'uniques': None},
2689+
'index': [],
2690+
'partitioned_by': [],
2691+
'primary_key': ['user_id'],
2692+
'schema': None,
2693+
'table_name': 'Users',
2694+
'tablespace': None}]
2695+
2696+
ddl = """
2697+
CREATE TABLE Users (
2698+
user_id INT NOT NULL AUTO INCREMENT,
2699+
username VARCHAR(100) NOT NULL,
2700+
password VARCHAR(40) NOT NULL,
2701+
submission_date DATE,
2702+
PRIMARY KEY ( user_id )
2703+
);
2704+
"""
2705+
2706+
result = DDLParser(ddl).run(output_mode="mysql")
2707+
2708+
assert expected == result

0 commit comments

Comments
 (0)