Skip to content

Commit

Permalink
release 10.646.24152
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed May 31, 2024
2 parents c840d1c + e12cebb commit 88215af
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
8 changes: 4 additions & 4 deletions mo_sql_parsing/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def isolate(expr, sql, prec):
"where",
"groupby",
"having",
"union_all"
"union_all",
]

ordered_clauses = [
Expand Down Expand Up @@ -274,7 +274,7 @@ def wordy(v):
# WITHIN GROUP (
# ORDER BY public.persentil.sale
# )
ob = self.orderby(json['within'], 100)
ob = self.orderby(json["within"], 100)
parts.append(f"WITHIN GROUP ({ob})")
if "name" in json:
parts.extend(["AS", self.dispatch(json["name"])])
Expand Down Expand Up @@ -590,7 +590,7 @@ def unordered_query(self, json, prec):
part
for clause in unordered_clauses
if clause in json
for part in [getattr(self, clause)(json, precedence[clause]+1)]
for part in [getattr(self, clause)(json, precedence[clause] + 1)]
if part
)
if prec > precedence["from"]:
Expand All @@ -604,7 +604,7 @@ def with_(self, json, prec):
return f"WITH {parts}"

def union_all(self, json, prec):
sql = "\nUNION ALL\n".join(self.dispatch(part) for part in listwrap(json['union_all']))
sql = "\nUNION ALL\n".join(self.dispatch(part) for part in listwrap(json["union_all"]))
return f"{sql}" if prec > precedence["union_all"] else f"({sql})"

def select(self, json, prec):
Expand Down
3 changes: 1 addition & 2 deletions mo_sql_parsing/sql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def parser(literal_string, simple_ident, all_columns=None, sqlserver=False):

# EXPRESSIONS
expression = Forward()
(column_type, column_definition, column_def_references, column_option,) = get_column_type(
(column_type, column_definition, column_def_references, column_option, declare_variable) = get_column_type(
expression, identifier, literal_string
)
proc_param = Group(
Expand Down Expand Up @@ -912,7 +912,6 @@ def mult(tokens):
#############################################################
statement = Forward()
special_ident = keyword("masking policy") | identifier / (lambda t: t[0].lower())
declare_variable = assign("declare", column_definition)
set_one_variable = SET + (
(special_ident + Optional(EQ) + expression)
/ (lambda t: {t[0].lower(): t[1].lower() if isinstance(t[1], str) else t[1]})
Expand Down
10 changes: 8 additions & 2 deletions mo_sql_parsing/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
LEFT_ASSOC,
Keyword,
Combine,
Empty,
)

from mo_sql_parsing.keywords import (
Expand Down Expand Up @@ -266,13 +267,18 @@ def get_column_type(expr, identifier, literal_string):
| assign("check", LB + expr + RB)
| assign("default", expr)
| assign("on update", expr)
| (EQ + expr)("default")
)

column_definition << Group(
identifier("name") + (column_type | identifier("type")) + ZeroOrMore(column_options)
) / to_flat_column_type

variable_options = assign("default", expr) | EQ + expr("default")
declare_variable = assign(
"declare",
delimited_list(Group(identifier("name") + Optional(AS) + simple_types("type") + ZeroOrMore(variable_options))),
)

set_parser_names()

return column_type, column_definition, column_def_references, column_options
return column_type, column_definition, column_def_references, column_options, declare_variable
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.645.24152',
version='10.646.24152',
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.645.24152",
"version": "10.646.24152",
"zip_safe": true
}
44 changes: 41 additions & 3 deletions tests/test_sql_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from unittest import TestCase

from mo_parsing.debug import Debugger

from mo_sql_parsing import parse_sqlserver as parse


Expand Down Expand Up @@ -307,6 +305,46 @@ def test_issue_237_declare_var(self):
result = parse(sql)
expected = {"create_procedure": {
"name": "k",
"body": {"block": {"declare": {"default": 42, "name": "@MYVARIABLE", "type": {"int": {}},}}},
"body": {"block": {"declare": {"default": 42, "name": "@MYVARIABLE", "type": {"int": {}}, }}},
}}
self.assertEqual(result, expected)

def test_issue_237_declare_var_with_as(self):
sql = """create procedure k() BEGIN
DECLARE @MYVARIABLE AS INT = 42;
END"""
result = parse(sql)
expected = {"create_procedure": {
"name": "k",
"body": {"block": {"declare": {"default": 42, "name": "@MYVARIABLE", "type": {"int": {}}}}},
}}
self.assertEqual(result, expected)

def test_issue_237_declare_multiple_vars(self):
sql = """create procedure k() BEGIN
DECLARE @MYVAR INT, @MYOTHERVAR DATE;
END"""
result = parse(sql)
expected = {"create_procedure": {
"name": "k",
"body": {"block": {"declare": [
{"name": "@MYVAR", "type": {"int": {}}},
{"name": "@MYOTHERVAR", "type": {"date": {}}},
]}},
}}
self.assertEqual(result, expected)

def test_issue_237_declare_multiple_vars_with_code(self):
sql = """create procedure k() BEGIN
DECLARE @MYVAR INT, @MYOTHERVAR DATE;
SELECT a FROM rental;
END"""
result = parse(sql)
expected = {"create_procedure": {
"name": "k",
"body": {"block": [
{"declare": [{"name": "@MYVAR", "type": {"int": {}}}, {"name": "@MYOTHERVAR", "type": {"date": {}}}]},
{"select": {"value": "a"}, "from": "rental"},
]},
}}
self.assertEqual(result, expected)

0 comments on commit 88215af

Please sign in to comment.