Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Development Version
-------------------

Nothing yet.
* Escape literal dollar signs in PHP output so SQL text is not interpreted
as PHP string interpolation.


Release 0.6.0 (Aug 13, 2026)
Expand Down
3 changes: 2 additions & 1 deletion sqlparse/filters/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def _process(self, stream, varname, has_nl):
# quote cannot break out of the generated string literal
# (GHSA-3496-9g83-7v6x).
else:
token.value = token.value.replace('\\', '\\\\').replace('"', '\\"')
token.value = (token.value.replace('\\', '\\\\')
.replace('"', '\\"').replace('$', '\\$'))

# Put the token
yield sql.Token(T.Text, token.value)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,14 @@ def test_strip_ws_removes_trailing_ws_in_groups(): # issue782
strip_whitespace=True)
expected = '(where foo = bar) from'
assert formatted == expected


@pytest.mark.parametrize('query, expected', [
("SELECT '$name'", r'''$sql = "SELECT '\$name'";'''),
('SELECT $body$text$body$', r'''$sql = "SELECT \$body\$text\$body\$";'''),
('SELECT "$table"', r'''$sql = "SELECT \"\$table\"";'''),
('SELECT 1 /* $name */', r'''$sql = "SELECT 1 /* \$name */";'''),
("SELECT '\\$name'", r'''$sql = "SELECT '\\\$name'";'''),
])
def test_php_output_preserves_literal_dollar_signs(query, expected):
assert sqlparse.format(query, output_format='php') == expected