Skip to content

Commit 1be5aa4

Browse files
committed
Fix whitespace collapsing breaking -- style SQL comments
The regex r'\s+' collapsed all whitespace including newlines, which broke -- style SQL comments by putting everything on one line. Changed to r'[ \t]+' to only collapse horizontal whitespace while preserving newlines. Bump version to 4.7.1
1 parent d0669f1 commit 1be5aa4

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "t-sql"
7-
version = "4.7.0"
7+
version = "4.7.1"
88
description = "Safe SQL. SQL queries for python t-strings (PEP 750)"
99
readme = "README.md"
1010
requires-python = ">=3.14"

tests/test_deep_nesting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ def test_case_expression_with_subqueries():
311311

312312
sql, params = tsql.render(query, style=styles.QMARK)
313313

314-
assert "CASE WHEN (SELECT COUNT(*)" in sql
314+
assert "CASE" in sql
315+
assert "WHEN (SELECT COUNT(*)" in sql
315316
assert "> ?" in sql
316317
assert "THEN 'active'" in sql
317318
assert params == [10]

tests/test_tsql.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,26 @@ def test_merges_literals_using_exsiting_tstring():
3535
assert result._sql == 'hello there'
3636

3737

38-
def test_strips_literal_whitespace():
39-
result = tsql.render(t"SELECT \n * \n FROM table")
38+
def test_strips_horizontal_whitespace():
39+
# Horizontal whitespace (spaces/tabs) is collapsed, but newlines are preserved
40+
result = tsql.render(t"SELECT * FROM table")
4041
assert result[0] == 'SELECT * FROM table'
4142

4243

44+
def test_preserves_newlines_for_sql_comments():
45+
# Newlines must be preserved so -- style SQL comments work correctly
46+
query = t"""SELECT * FROM users
47+
-- Filter by active status
48+
WHERE active = true"""
49+
result = tsql.render(query)
50+
assert '-- Filter by active status\n' in result[0]
51+
assert 'WHERE active = true' in result[0]
52+
53+
4354
def test_doesnt_strip_whitespace_in_values():
4455
user_input = 'Some string\nWith whitespace. With Formating that is \n just right'
45-
result = tsql.render(t'INSERT \n INTO table (vals) VALUES({user_input})')
46-
assert result[0] == f'INSERT INTO table (vals) VALUES(?)'
56+
result = tsql.render(t'INSERT INTO table (vals) VALUES({user_input})')
57+
assert result[0] == 'INSERT INTO table (vals) VALUES(?)'
4758
assert result[1] == [user_input]
4859

4960

tsql/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
logger = logging.getLogger(__name__)
1111

12-
# Pre-compile regex for whitespace collapsing to avoid cache lookup overhead
13-
_WHITESPACE_RE = re.compile(r'\s+')
12+
# Pre-compile regex for horizontal whitespace collapsing (spaces/tabs only)
13+
# Preserves newlines so that -- style SQL comments work correctly
14+
_WHITESPACE_RE = re.compile(r'[ \t]+')
1415

1516
if TYPE_CHECKING:
1617
from tsql.query_builder import QueryBuilder

0 commit comments

Comments
 (0)