Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle None parameters in query, returning NULL #1180

Merged
merged 3 commits into from
Oct 17, 2024
Merged
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
9 changes: 8 additions & 1 deletion asyncpg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ async def _mogrify(conn, query, args):

# Finally, replace $n references with text values.
return re.sub(
r'\$(\d+)\b', lambda m: textified[int(m.group(1)) - 1], query)
r"\$(\d+)\b",
lambda m: (
textified[int(m.group(1)) - 1]
if textified[int(m.group(1)) - 1] is not None
else "NULL"
),
query,
)
8 changes: 5 additions & 3 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,22 @@ async def test_copy_from_query_with_args(self):

res = await self.con.copy_from_query('''
SELECT
i, i * 10
i,
i * 10,
$2::text
FROM
generate_series(1, 5) AS i
WHERE
i = $1
''', 3, output=f)
''', 3, None, output=f)

self.assertEqual(res, 'COPY 1')

output = f.getvalue().decode().split('\n')
self.assertEqual(
output,
[
'3\t30',
'3\t30\t\\N',
''
]
)
Expand Down
Loading