Skip to content

Commit

Permalink
Fix spelling.
Browse files Browse the repository at this point in the history
  • Loading branch information
kklein committed Dec 2, 2024
1 parent d1aaa4c commit 537fe64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/pytsql/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def _process_replacement(line: str, parameters: dict[str, Any]) -> str:
return new_line


def _parametrize(
def _parameterize(
source: str,
parameters: dict[str, Any],
start: str = _REPLACE_START,
end: str = _REPLACE_END,
) -> str:
"""Replace all {start} and {end} statements, i.e. parametrizes the SQL script.
"""Replace all {start} and {end} statements, i.e. parameterizes the SQL script.
Parameters
----------
Expand All @@ -57,19 +57,19 @@ def _parametrize(
the input source code, separated by newlines, with parameter replacements
"""

def parametrization_replacer(match: Match) -> str:
def parameterization_replacer(match: Match) -> str:
return _process_replacement(match.group(1), parameters)

# The pattern matches all parametrization patterns, including those within a string literal.
# The pattern matches all parameterization patterns, including those within a string literal.
pattern = re.compile(
rf"/\* {re.escape(start)} \*/.*?/\* {re.escape(end)}(.*?) \*/",
re.DOTALL | re.MULTILINE,
)

parametrized = re.sub(pattern, parametrization_replacer, source)
parameterized = re.sub(pattern, parameterization_replacer, source)

non_empty_stripped_lines = [
x.strip() for x in parametrized.split("\n") if x.strip() != ""
x.strip() for x in parameterized.split("\n") if x.strip() != ""
]

return "\n".join(non_empty_stripped_lines)
Expand Down Expand Up @@ -235,13 +235,13 @@ def executes(
None
"""
parametrized_code = _parametrize(code, parameters) if parameters else code
parameterized_code = _parameterize(code, parameters) if parameters else code
with engine.connect().execution_options(isolation_level="AUTOCOMMIT") as conn:
# Since the prints table is a temporary one, it will be local to the connection and it will be dropped once the
# connection is closed. Caveat: sqlalchemy engines can pool connections, so we still have to drop it preemtively.
conn.execute(_text(f"DROP TABLE IF EXISTS {_PRINTS_TABLE}"))
conn.execute(_text(f"CREATE TABLE {_PRINTS_TABLE} (p NVARCHAR(4000))"))
for batch in _split(parametrized_code, isolate_top_level_statements):
for batch in _split(parameterized_code, isolate_top_level_statements):
sql_batch = _text(batch)
conn.execute(sql_batch)
_fetch_and_clear_prints(conn)
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/test_parametrize.py → tests/unit/test_parameterize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from pytsql.tsql import _parametrize
from pytsql.tsql import _parameterize


def test_replace_comment():
Expand All @@ -14,7 +14,7 @@ def test_replace_comment():
/* </replace> select {alpha},b,{charlie} from x */
""" # noqa: W291
expected = """select first,b,second from x"""
assert _parametrize(seed, {"alpha": "first", "charlie": "second"}) == expected
assert _parameterize(seed, {"alpha": "first", "charlie": "second"}) == expected


def test_replace_comment_second():
Expand All @@ -26,7 +26,7 @@ def test_replace_comment_second():
"""
expected = """USE master;
select * from [master].[dbo].[new_table];"""
assert _parametrize(seed, {"tableName": "new_table"}) == expected
assert _parameterize(seed, {"tableName": "new_table"}) == expected


def test_replace_comment_int():
Expand All @@ -38,7 +38,7 @@ def test_replace_comment_int():
"""
expected = """USE master;
select top 1337 * from [master].[dbo].[table];"""
assert _parametrize(seed, {"n_rows": 1337}) == expected
assert _parameterize(seed, {"n_rows": 1337}) == expected


def test_replace_comment_class():
Expand All @@ -57,7 +57,7 @@ def __str__(self):
"""
expected = """USE master;
select * from master.dbo.table;"""
assert _parametrize(seed, {"table_qualifier": MyClass("table")}) == expected
assert _parameterize(seed, {"table_qualifier": MyClass("table")}) == expected


def test_none_replacement():
Expand All @@ -68,7 +68,7 @@ def test_none_replacement():
/* </replace> select * from [master].[dbo].[{tableName}]; */
"""
with pytest.raises(ValueError):
_parametrize(seed, {"tableName": None})
_parameterize(seed, {"tableName": None})


def test_double_replacement():
Expand All @@ -84,7 +84,7 @@ def test_double_replacement():
FROM dbo.new_table t1
JOIN dbo.new_table t2
ON t1.id = t2.id;""" # noqa: W291
assert _parametrize(seed, {"tableName": "new_table"}) == expected
assert _parameterize(seed, {"tableName": "new_table"}) == expected


def test_multiline_replacement():
Expand All @@ -96,7 +96,7 @@ def test_multiline_replacement():
"""
expected = """USE master;
select * from [master].[dbo].[new_table];"""
assert _parametrize(seed, {"tableName": "new_table"}) == expected
assert _parameterize(seed, {"tableName": "new_table"}) == expected


def test_multi_multiline_replacements():
Expand All @@ -115,7 +115,7 @@ def test_multi_multiline_replacements():
USE newTable;
select * from [master].[dbo].[second_table];""" # noqa: W291
assert (
_parametrize(seed, {"tableName": "new_table", "otherTable": "second_table"})
_parameterize(seed, {"tableName": "new_table", "otherTable": "second_table"})
== expected
)

Expand All @@ -127,7 +127,7 @@ def test_regular_comment_stays():
"""
expected = """USE master; /* this is a regular comment */
select * from [master].[dbo].[table];"""
assert _parametrize(seed, {"branch": "new_master"}) == expected
assert _parameterize(seed, {"branch": "new_master"}) == expected


def test_unknown_parameter_exception():
Expand All @@ -138,15 +138,15 @@ def test_unknown_parameter_exception():
select * from [master].[dbo].[table];
"""
with pytest.raises(KeyError):
_parametrize(seed, {"key": "value"})
_parameterize(seed, {"key": "value"})


def test_same_line_replacement():
seed = """
select a,b,/* <replace> */c/* </replace>{otherC} */ from x
"""
expected = """select a,b,newC from x"""
assert _parametrize(seed, {"otherC": "newC"}) == expected
assert _parameterize(seed, {"otherC": "newC"}) == expected


def test_multi_same_line_replacements():
Expand All @@ -156,12 +156,12 @@ def test_multi_same_line_replacements():
"""
expected = """SELECT newA, newB
FROM table;"""
assert _parametrize(seed, {"newA": "newA", "newB": "newB"}) == expected
assert _parameterize(seed, {"newA": "newA", "newB": "newB"}) == expected


def test_replace_in_string_literal():
seed = """SELECT '/* <replace> */a/* </replace>{newA} */'"""
assert _parametrize(seed, {"newA": "newA"}) == "SELECT 'newA'"
assert _parameterize(seed, {"newA": "newA"}) == "SELECT 'newA'"


def test_custom_replace_keywords():
Expand All @@ -170,7 +170,7 @@ def test_custom_replace_keywords():
"""
expected = """SELECT a,/* <replace> */b/* </replace>{otherB} */,newC FROM x"""
assert (
_parametrize(
_parameterize(
seed,
{"otherB": "newB", "otherC": "newC"},
start="*repl_start*",
Expand Down

0 comments on commit 537fe64

Please sign in to comment.