Skip to content

Commit 3590e75

Browse files
authored
fix(postgres)!: support ? placeholder (#5455)
* fix(postgres): support ? placehodler * refactor impl * refactor query parameter parsing
1 parent dbef44d commit 3590e75

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

sqlglot/dialects/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class Parser(parser.Parser):
384384

385385
PLACEHOLDER_PARSERS = {
386386
**parser.Parser.PLACEHOLDER_PARSERS,
387+
TokenType.PLACEHOLDER: lambda self: self.expression(exp.Placeholder, jdbc=True),
387388
TokenType.MOD: lambda self: self._parse_query_parameter(),
388389
}
389390

@@ -816,6 +817,9 @@ def interval_sql(self, expression: exp.Interval) -> str:
816817
return super().interval_sql(expression)
817818

818819
def placeholder_sql(self, expression: exp.Placeholder) -> str:
820+
if expression.args.get("jdbc"):
821+
return "?"
822+
819823
this = f"({expression.name})" if expression.this else ""
820824
return f"{self.NAMED_PLACEHOLDER_TOKEN}{this}s"
821825

sqlglot/expressions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4467,8 +4467,9 @@ class SessionParameter(Condition):
44674467

44684468

44694469
# https://www.databricks.com/blog/parameterized-queries-pyspark
4470+
# https://jdbc.postgresql.org/documentation/query/#using-the-statement-or-preparedstatement-interface
44704471
class Placeholder(Condition):
4471-
arg_types = {"this": False, "kind": False, "widget": False}
4472+
arg_types = {"this": False, "kind": False, "widget": False, "jdbc": False}
44724473

44734474
@property
44744475
def name(self) -> str:

tests/dialects/test_postgres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ def test_postgres(self):
923923

924924
self.validate_identity("SELECT * FROM foo WHERE id = %s")
925925
self.validate_identity("SELECT * FROM foo WHERE id = %(id_param)s")
926+
self.validate_identity("SELECT * FROM foo WHERE id = ?")
926927

927928
def test_ddl(self):
928929
# Checks that user-defined types are parsed into DataType instead of Identifier

0 commit comments

Comments
 (0)