-
Notifications
You must be signed in to change notification settings - Fork 1
introduce pyparsing #27
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
Open
MoonChel
wants to merge
8
commits into
master
Choose a base branch
from
vlado/pyparsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
095e0f3
try pyparsing
c142e18
implement pyparsing
3865f2b
update isort
d116407
remove re
ae3323b
Adjust creatae table parser
OriginalUtkin 618c9de
Fix test
OriginalUtkin 38028e2
Dont use all imports fro enum
OriginalUtkin 384dc36
Fix imports for enum
OriginalUtkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| [settings] | ||
| known_third_party = black,click,jinja2,pytest,setuptools,sql_to_code | ||
| known_third_party = black,click,jinja2,pyparsing,pytest,setuptools,sql_to_code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,33 @@ | ||
| import re | ||
| from pyparsing import CaselessKeyword, QuotedString | ||
|
|
||
| from .models import ForeignKey, Reference | ||
|
|
||
| source_table_name_regex = re.compile('ALTER TABLE "(?P<table_name>\w+)"') | ||
| foreign_key_name_regex = re.compile('ADD FOREIGN KEY \("(?P<foreign_key>\w+)"\)') | ||
| result_table_name_regex = re.compile('REFERENCES "(?P<result_table_name>\w+)"') | ||
| result_table_field_name_regex = re.compile( | ||
| 'REFERENCES "\w+" \("(?P<result_table_field_name>\w+)"\);' | ||
| table_name_schema = CaselessKeyword("alter table") + QuotedString('"')("table_name") | ||
| foreign_key_schema = CaselessKeyword("add foreign key") + QuotedString( | ||
MoonChel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| '("', endQuoteChar='")' | ||
| )("foreign_key") | ||
| reference_table = CaselessKeyword("references") + QuotedString('"')("reference_table") | ||
| reference_table_column_name = QuotedString('("', endQuoteChar='")')( | ||
| "reference_table_column_name" | ||
| ) | ||
|
|
||
| add_foreign_key_schema = ( | ||
| table_name_schema | ||
| + foreign_key_schema | ||
| + reference_table | ||
| + reference_table_column_name | ||
| ) | ||
|
|
||
|
|
||
| def parse(sql_text: str): | ||
| source_table_name = source_table_name_regex.search(sql_text).groupdict()[ | ||
| "table_name" | ||
| ] | ||
| foreign_key_name = foreign_key_name_regex.search(sql_text).groupdict()[ | ||
| "foreign_key" | ||
| ] | ||
| result_table_name = result_table_name_regex.search(sql_text).groupdict()[ | ||
| "result_table_name" | ||
| ] | ||
| result_table_field_name = result_table_field_name_regex.search( | ||
| sql_text | ||
| ).groupdict()["result_table_field_name"] | ||
| result = add_foreign_key_schema.parseString(sql_text) | ||
|
|
||
| return ForeignKey( | ||
| refer_from=Reference(table_name=source_table_name, field_name=foreign_key_name), | ||
| refer_from=Reference( | ||
| table_name=result.table_name, field_name=result.foreign_key | ||
| ), | ||
| refer_to=Reference( | ||
| table_name=result_table_name, field_name=result_table_field_name | ||
| table_name=result.reference_table, | ||
| field_name=result.reference_table_column_name, | ||
| ), | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| import re | ||
| from pyparsing import CaselessKeyword, QuotedString, delimitedList | ||
|
|
||
| from .models import Enumeration | ||
|
|
||
| NAME_REGEX = re.compile('CREATE TYPE "(?P<name>\w+)"') | ||
| VALUE_REGEX = re.compile("'(\w+)'") | ||
| enum_schema = ( | ||
| CaselessKeyword("create type") | ||
| + QuotedString('"')("enum_name") | ||
| + CaselessKeyword("as enum") | ||
| + CaselessKeyword("(") | ||
| + delimitedList(QuotedString("'"))("enum_values") | ||
| + CaselessKeyword(");") | ||
| ) | ||
|
|
||
|
|
||
| def parse(sql_text: str): | ||
| name = NAME_REGEX.search(sql_text).groupdict()["name"] | ||
| values = VALUE_REGEX.findall(sql_text) | ||
| result = enum_schema.parseString(sql_text) | ||
|
|
||
| return Enumeration(name, values) | ||
| return Enumeration(result.enum_name, list(result.enum_values)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,77 @@ | ||
| import re | ||
| from typing import List | ||
| from pyparsing import ( | ||
| CaselessKeyword, | ||
| Group, | ||
| Literal, | ||
| MatchFirst, | ||
| OneOrMore, | ||
| Optional, | ||
| QuotedString, | ||
| Suppress, | ||
| Word, | ||
| alphanums, | ||
| alphas, | ||
| ) | ||
| from pyparsing import pyparsing_common as ppc | ||
| from pyparsing import quotedString | ||
|
|
||
| from .models import Attribute, Table, default_type | ||
| from .models import Attribute, Table | ||
|
|
||
| regex_name_schema: str = r'CREATE TABLE "([\S\d]+)"\s*\(\s*(.+)\s*\);' | ||
| name_and_type_regex = r"(^[\S\d]+)\s([\S]+)\s*" | ||
| default_regex = r"DEFAULT\s+(.?)+\s*" | ||
| # table schema | ||
| create_table = CaselessKeyword("create table") | ||
| table_name = MatchFirst(QuotedString('"'))("table_name") | ||
|
|
||
| # field schema | ||
| column_name = QuotedString('"')("column_name") | ||
|
|
||
| def parse(sql_text: str) -> Table: | ||
| table_name, schema = re.findall(regex_name_schema, sql_text)[0] | ||
| all_attributes: List[str] = schema.split(",") | ||
| schema: List[str] = map( | ||
| lambda attribute: attribute.strip().replace('"', ""), all_attributes | ||
| ) | ||
| attributes = parse_attributes(schema) | ||
| # column type: | ||
| # "(" ")" - because of varchar(40) | ||
| # "_" - because of enums like process_type | ||
| column_type = Word(alphas, alphanums + "(" + ")" + "_")("column_type") | ||
MoonChel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| is_primary_key = CaselessKeyword("primary key") | ||
| is_unique = CaselessKeyword("unique")("is_unique") | ||
|
|
||
| return Table(table_name, attributes) | ||
| date_time_now = Word("now()") | ||
| default_value = quotedString | ppc.real | ppc.signed_integer | date_time_now | ||
| has_default = CaselessKeyword("default") + default_value("default_value") | ||
|
|
||
| is_not_null = CaselessKeyword("not null")("is_not_null") | ||
|
|
||
| def parse_attributes(schema) -> List[Attribute]: | ||
| attributes = list() | ||
|
|
||
| for attribute in schema: | ||
| name, a_type = re.findall(pattern=name_and_type_regex, string=attribute)[0] | ||
| table_columns = OneOrMore( | ||
| Group( | ||
| column_name | ||
| + column_type | ||
| + ( | ||
| Optional(is_not_null) | ||
| & Optional(has_default) | ||
| & Optional(is_unique) | ||
| & Optional(is_primary_key) | ||
| ) | ||
| + Optional(Suppress(",")) | ||
| ) | ||
| )("table_columns") | ||
|
|
||
| is_pk = "PRIMARY KEY" in attribute.upper() | ||
| is_default = not is_pk and "DEFAULT" in attribute.upper() | ||
| create_table_schema = ( | ||
| create_table + table_name + Literal("(") + table_columns + Literal(");") | ||
| ) | ||
|
|
||
| attributes.append( | ||
| Attribute( | ||
| name=name, | ||
| type=a_type, | ||
| primary_key=is_pk, | ||
| nullable="NOT NULL" not in attribute.upper() and not is_pk, | ||
| is_default=is_default, | ||
| default=parse_default(attribute) if is_default else None, | ||
| ) | ||
| ) | ||
|
|
||
| return attributes | ||
| def parse(sql_text: str) -> Table: | ||
| result = create_table_schema.parseString(sql_text) | ||
|
|
||
| table = Table( | ||
| result.table_name, | ||
| [ | ||
| Attribute( | ||
| name=column.column_name, | ||
| type=column.column_type, | ||
| default=column.default_value if column.default_value else None, | ||
| is_unique=bool(column.is_unique), | ||
| is_nullable=not bool(column.is_not_null), | ||
| is_primary_key=bool(column.is_primary_key), | ||
| ) | ||
| for column in result.table_columns | ||
| ], | ||
| ) | ||
|
|
||
| def parse_default(sql_command: str) -> default_type: | ||
| return re.findall(default_regex, sql_command)[0] | ||
| return table | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| ALTER TABLE "issue" | ||
| ADD FOREIGN KEY ("process_id") | ||
| ADD FOREIGN KEY ("process_id_test") | ||
| REFERENCES "process" ("process_id"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| CREATE TABLE "process" ( | ||
| "process_id" int PRIMARY KEY, | ||
| "booking_id" int, | ||
| "ticket_id" int, | ||
| "created_at" timestamp, | ||
| "updated_at" timestamp | ||
| "booking_id" int DEFAULT 10, | ||
| "state" process_state DEFAULT "verification" NOT NULL, | ||
| "created_at" timestamp DEFAULT (now()), | ||
| "updated_at" timestamp, | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.