From 7effd977416a1b4a3191065c6075f716154cd00f Mon Sep 17 00:00:00 2001 From: Iuliia Volkova Date: Tue, 7 Jun 2022 13:37:38 +0400 Subject: [PATCH] Few Improvements - V0.26.3 release (#132) * parse validaly comment statement after schema & table * add support for few statements --- CHANGELOG.txt | 7 +++ README.md | 20 ++++++- docs/README.rst | 25 ++++++++- pyproject.toml | 2 +- simple_ddl_parser/dialects/sql.py | 10 +++- tests/test_simple_ddl_parser.py | 89 +++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index fc3a1cd..f1836ce 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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: diff --git a/README.md b/README.md index 97d7f84..51f2fef 100644 --- a/README.md +++ b/README.md @@ -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();) @@ -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: diff --git a/docs/README.rst b/docs/README.rst index 696fa1b..d946939 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -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();) @@ -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 ^^^^^^^^ @@ -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: diff --git a/pyproject.toml b/pyproject.toml index 8270451..8f57a61 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT" diff --git a/simple_ddl_parser/dialects/sql.py b/simple_ddl_parser/dialects/sql.py index 88399df..bc8b9ed 100644 --- a/simple_ddl_parser/dialects/sql.py +++ b/simple_ddl_parser/dialects/sql.py @@ -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 @@ -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": @@ -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: diff --git a/tests/test_simple_ddl_parser.py b/tests/test_simple_ddl_parser.py index f07a48a..d2d84d1 100644 --- a/tests/test_simple_ddl_parser.py +++ b/tests/test_simple_ddl_parser.py @@ -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