Skip to content

Commit

Permalink
Fix postgres throwing an error when a non parameter is not a string
Browse files Browse the repository at this point in the history
  • Loading branch information
baluyotraf committed Aug 2, 2024
1 parent b046fc7 commit d8c3a0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/altqq/translators/psycopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _resolve_value(self, query: Query, field: dc.Field[T]) -> PsycopgStatement:

field_type = common.get_parameter_type(field.type)
if field_type == QueryValueTypes.NON_PARAMETER:
return PsycopgStatement(value.replace("%", "%%"), ())
return PsycopgStatement(str(value).replace("%", "%%"), ())
elif field_type == QueryValueTypes.LIST_PARAMETER:
return PsycopgStatement(
common.create_list_markers(self.MARKER, len(value)), value
Expand Down
31 changes: 31 additions & 0 deletions tests/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ class SelectTableByFilter(altqq.Query):
filter_value: Any


class SelectTableByFilterNonParameter(altqq.Query):
"""Test query that selects and filters a table."""

__query__ = """
SELECT *, (15 % 10) AS t FROM "{table}"
WHERE "{filter_column}" = {filter_value}
"""

table: altqq.NonParameter[str]
filter_column: altqq.NonParameter[str]
filter_value: altqq.NonParameter[int]


class OrderQuery(altqq.Query):
"""Test query that orders a query."""

Expand Down Expand Up @@ -113,6 +126,24 @@ class UnionAllQuery(altqq.Query):
),
plain_text="""SELECT * FROM table WHERE A IN (10,20,30)""",
),
SampleQuery(
SelectTableByFilterNonParameter("Users", "age", 25),
pyodbc=altqq.PyODBCQuery(
query="""
SELECT *, (15 % 10) AS t FROM "Users" WHERE "age" = 25
""",
parameters=[],
),
psycopg=altqq.PsycopgQuery(
query="""
SELECT *, (15 % 10) AS t FROM "Users" WHERE "age" = 25
""",
parameters=(),
),
plain_text="""
SELECT *, (15 % 10) AS t FROM "Users" WHERE "age" = 25
""",
),
SampleQuery(
SelectTableByFilter("Users", "age", 25),
pyodbc=altqq.PyODBCQuery(
Expand Down

0 comments on commit d8c3a0e

Please sign in to comment.