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

Few Improvements - V0.26.3 release #132

Merged
merged 3 commits into from
Jun 7, 2022
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
7 changes: 7 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
**v0.26.3**

Improvements:
1. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
2. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130


**v0.26.2**

Fixes:
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ In output you will have names like 'dbo' and 'TO_Requests', not '[dbo]' and '[TO

## Supported Statements

- 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
- 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

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

Expand Down Expand Up @@ -456,6 +456,24 @@ https://github.com/swiatek25


## Changelog
**v0.26.3**

Improvements:
1. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
2. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130


**v0.26.2**

Fixes:
1. Fixed a huge bug for incorrect parsing lines with 'USE' & 'GO' strings inside.
2. Fixed parsing for CREATE SCHEMA for Snowlake & Oracle DDLs

Improvements:
1. Added COMMENT statement for CREATE TABLE ddl (for SNOWFLAKE dialect support)
2. Added COMMENT statement for CREATE SCHEMA ddl (for SNOWFLAKE dialect support)


**v0.26.1**

Fixes:
Expand Down
25 changes: 24 additions & 1 deletion docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ Supported Statements


*
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
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

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

BigQuery
^^^^^^^^
Expand Down Expand Up @@ -522,6 +523,28 @@ https://github.com/swiatek25
Changelog
---------

**v0.26.3**

Improvements:


#. Added support for OR REPLACE in CREATE TABLE: https://github.com/xnuinside/simple-ddl-parser/issues/131
#. Added support for AUTO INCREMENT in column:https://github.com/xnuinside/simple-ddl-parser/issues/130

**v0.26.2**

Fixes:


#. Fixed a huge bug for incorrect parsing lines with 'USE' & 'GO' strings inside.
#. Fixed parsing for CREATE SCHEMA for Snowlake & Oracle DDLs

Improvements:


#. Added COMMENT statement for CREATE TABLE ddl (for SNOWFLAKE dialect support)
#. Added COMMENT statement for CREATE SCHEMA ddl (for SNOWFLAKE dialect support)

**v0.26.1**

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 = "0.26.2"
version = "0.26.3"
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 <xnuinside@gmail.com>"]
license = "MIT"
Expand Down
10 changes: 8 additions & 2 deletions simple_ddl_parser/dialects/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def add_if_not_exists(data: Dict, p_list: List):
def p_create_table(self, p: List):
"""create_table : CREATE TABLE IF NOT EXISTS
| CREATE TABLE
| CREATE OR REPLACE TABLE IF NOT EXISTS
| CREATE OR REPLACE TABLE
| CREATE id TABLE IF NOT EXISTS
| CREATE id TABLE

Expand All @@ -130,7 +132,8 @@ def p_create_table(self, p: List):
p[0] = {}
p_list = list(p)
self.add_if_not_exists(p[0], p_list)

if 'REPLACE' in p_list:
p[0]["replace"] = True
if p[2].upper() == "EXTERNAL":
p[0]["external"] = True
if p[2].upper() == "TEMP" or p[2].upper() == "TEMPORARY":
Expand All @@ -141,7 +144,10 @@ class Column:
def p_column_property(self, p: List):
"""c_property : id id"""
p_list = list(p)
p[0] = {"property": {p_list[1]: p_list[-1]}}
if p[1].lower() == "auto":
p[0] = {"increment": True}
else:
p[0] = {"property": {p_list[1]: p_list[-1]}}

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

Expand Down
89 changes: 89 additions & 0 deletions tests/test_simple_ddl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2617,3 +2617,92 @@ def test_check_that_all_columns_parsed_correctly():
}
]
assert expected == result


def test_create_or_replace():

ddl="""create or replace table someTable (
someField varchar(4)
);
"""

result = DDLParser(ddl,normalize_names=True).run()

expected = [{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'name': 'someField',
'nullable': True,
'references': None,
'size': 4,
'type': 'varchar',
'unique': False}],
'index': [],
'partitioned_by': [],
'primary_key': [],
'replace': True,
'schema': None,
'table_name': 'someTable',
'tablespace': None}]

assert expected == result


def test_increment_column():
expected = [{'alter': {},
'checks': [],
'columns': [{'check': None,
'default': None,
'increment': True,
'name': 'user_id',
'nullable': False,
'references': None,
'size': None,
'type': 'INT',
'unique': False},
{'check': None,
'default': None,
'name': 'username',
'nullable': False,
'references': None,
'size': 100,
'type': 'VARCHAR',
'unique': False},
{'check': None,
'default': None,
'name': 'password',
'nullable': False,
'references': None,
'size': 40,
'type': 'VARCHAR',
'unique': False},
{'check': None,
'default': None,
'name': 'submission_date',
'nullable': True,
'references': None,
'size': None,
'type': 'DATE',
'unique': False}],
'constraints': {'checks': None, 'references': None, 'uniques': None},
'index': [],
'partitioned_by': [],
'primary_key': ['user_id'],
'schema': None,
'table_name': 'Users',
'tablespace': None}]

ddl = """
CREATE TABLE Users (
user_id INT NOT NULL AUTO INCREMENT,
username VARCHAR(100) NOT NULL,
password VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( user_id )
);
"""

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

assert expected == result