Skip to content

Commit 259d16e

Browse files
fobispotcelprans
andauthored
handle None parameters in query, returning NULL (#1180)
--------- Co-authored-by: Elvis Pranskevichus <[email protected]>
1 parent 8f2be4c commit 259d16e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

asyncpg/utils.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ async def _mogrify(conn, query, args):
4242

4343
# Finally, replace $n references with text values.
4444
return re.sub(
45-
r'\$(\d+)\b', lambda m: textified[int(m.group(1)) - 1], query)
45+
r"\$(\d+)\b",
46+
lambda m: (
47+
textified[int(m.group(1)) - 1]
48+
if textified[int(m.group(1)) - 1] is not None
49+
else "NULL"
50+
),
51+
query,
52+
)

tests/test_copy.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,22 @@ async def test_copy_from_query_with_args(self):
148148

149149
res = await self.con.copy_from_query('''
150150
SELECT
151-
i, i * 10
151+
i,
152+
i * 10,
153+
$2::text
152154
FROM
153155
generate_series(1, 5) AS i
154156
WHERE
155157
i = $1
156-
''', 3, output=f)
158+
''', 3, None, output=f)
157159

158160
self.assertEqual(res, 'COPY 1')
159161

160162
output = f.getvalue().decode().split('\n')
161163
self.assertEqual(
162164
output,
163165
[
164-
'3\t30',
166+
'3\t30\t\\N',
165167
''
166168
]
167169
)

0 commit comments

Comments
 (0)