Skip to content

Commit

Permalink
release 10.641.24143
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed May 22, 2024
2 parents 2ba9737 + 4bc0c21 commit df3ba1d
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 12 deletions.
58 changes: 50 additions & 8 deletions mo_sql_parsing/sql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,9 @@ def mult(tokens):
)

use_schema = assign("use", identifier)

open_cursor = assign("open", identifier)
close_cursor = assign("close", identifier)
fetch_cursor = assign("fetch", identifier) + INTO + delimited_list(ident)
cache_options = Optional((
keyword("options").suppress()
+ LB
Expand All @@ -766,7 +768,8 @@ def mult(tokens):

drops = assign(
"drop",
MatchFirst([
temporary
+MatchFirst([
keyword(item).suppress() + Optional(flag("if exists")) + Group(identifier)(item)
for item in ["table", "view", "index", "schema"]
]),
Expand All @@ -777,6 +780,7 @@ def mult(tokens):
insert = (
Optional(assign("with", with_clause))
+ keyword("insert").suppress()
+ Optional(flag("ignore"))
+ (
flag("overwrite") + keyword("table").suppress()
| keyword("into").suppress() + Optional(keyword("table").suppress())
Expand All @@ -788,6 +792,15 @@ def mult(tokens):
+ returning
) / to_insert_call

replace = (
Optional(assign("with", with_clause))
+ keyword("replace").suppress()
+ Optional(keyword("into").suppress())
+ identifier("table")
+ Optional(LB + delimited_list(identifier)("columns") + RB)
+ (values | query)("query")
) / to_replace_call

update = (
keyword("update")("op")
+ (delimited_list(table_source) + ZeroOrMore(join))("params")
Expand Down Expand Up @@ -892,6 +905,7 @@ def mult(tokens):
#############################################################
# GET/SET
#############################################################
statement = Forward()
special_ident = keyword("masking policy") | identifier / (lambda t: t[0].lower())
declare_variable = assign("declare", column_definition)
set_one_variable = SET + (
Expand Down Expand Up @@ -951,7 +965,6 @@ def mult(tokens):
))

# EXPLAIN
statement = Forward()
explain_option = MatchFirst([
(
Keyword(option, caseless=True)
Expand Down Expand Up @@ -1034,14 +1047,24 @@ def mult(tokens):
#############################################################

many_command = Forward()
block = Group(Optional(identifier("label") + ":") + BEGIN + Group(many_command)("block") + END)
block = BEGIN + Group(many_command)("block") + END
if_block = (
assign("if", expression)
+ assign("then", many_command)
+ Optional(assign("else", many_command))
+ keyword("end if").suppress()
)
leave = assign("leave", identifier)
while_block = (
assign("while", expression)
+ assign("do", many_command)
+ keyword("end while").suppress()
)
loop_block = (
keyword("loop").suppress()
+ many_command("loop")
+ keyword("end loop").suppress()
)

create_trigger = assign(
"create trigger",
Expand Down Expand Up @@ -1074,7 +1097,7 @@ def mult(tokens):
+ keyword("procedure")
+ identifier("name")
+ LB
+ Group(delimited_list(proc_param))("params")
+ Group(Optional(delimited_list(proc_param)))("params")
+ RB
+ characteristic
+ statement("body")
Expand Down Expand Up @@ -1110,13 +1133,26 @@ def mult(tokens):
+ statement("body")
)("declare_handler")

declare_cursor = Group(
keyword("declare").suppress()
+ identifier("name")
+ keyword("cursor for").suppress()
+ query("query")
)("declare_cursor")


transact = (
Group(keyword("start transaction")("op")) / to_json_call
| Group(keyword("commit")("op")) / to_json_call
| Group(keyword("rollback")("op")) / to_json_call
)

flow = block | if_block | leave | assign("return", expression)
blocks = Group(Optional(identifier("label") + ":") + (
block
| if_block
| while_block
| loop_block
))

#############################################################
# FINALLY ASSEMBLE THE PARSER
Expand All @@ -1131,7 +1167,7 @@ def mult(tokens):

statement << Group(
query
| (insert | update | delete | merge | truncate | use_schema)
| (insert | replace | update | delete | merge | truncate | use_schema)
| (create_table | create_view | create_cache | create_index | create_schema)
| drops
| (copy | alter)
Expand All @@ -1141,8 +1177,14 @@ def mult(tokens):
| explain
| delimiter_command
| declare_hanlder
| flow
| declare_cursor
| leave
| assign("return", expression)
| transact
| open_cursor
| close_cursor
| fetch_cursor
| blocks
| (Optional(keyword("alter session")).suppress() + (set_variables | unset_one_variable | declare_variable))
)

Expand Down
18 changes: 18 additions & 0 deletions mo_sql_parsing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,24 @@ def to_insert_call(tokens):
return Call("insert", [tokens["table"]], {"columns": columns, "query": query, **options})


def to_replace_call(tokens):
options = {k: v for k, v in tokens.items() if k not in ["columns", "table", "query"]}
query = tokens["query"]
columns = tokens["columns"]
try:
values = query["from"]["literal"]
if values:
if columns:
data = [dict(zip(columns, row)) for row in values]
return Call("replace", [tokens["table"]], {"values": data, **options})
else:
return Call("replace", [tokens["table"]], {"values": values, **options})
except Exception:
pass

return Call("replace", [tokens["table"]], {"columns": columns, "query": query, **options})


def to_update_call(tokens):
value = tokens["value"]
name = tokens["name"]
Expand Down
2 changes: 1 addition & 1 deletion packaging/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
name='mo-sql-parsing',
packages=["mo_sql_parsing"],
url='https://github.com/klahnakoski/mo-sql-parsing',
version='10.640.24140',
version='10.641.24143',
zip_safe=True
)
2 changes: 1 addition & 1 deletion packaging/setuptools.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,6 @@
"name": "mo-sql-parsing",
"packages": ["mo_sql_parsing"],
"url": "https://github.com/klahnakoski/mo-sql-parsing",
"version": "10.640.24140",
"version": "10.641.24143",
"zip_safe": true
}
Loading

0 comments on commit df3ba1d

Please sign in to comment.