MCPcopy Index your code
hub / github.com/MagicStack/asyncpg / _mogrify

Function _mogrify

asyncpg/utils.py:19–52  ·  view source on GitHub ↗

Safely inline arguments to query text.

(conn, query, args)

Source from the content-addressed store, hash-verified

17
18
19async def _mogrify(conn, query, args):
20 """Safely inline arguments to query text."""
21 # Introspect the target query for argument types and
22 # build a list of safely-quoted fully-qualified type names.
23 ps = await conn.prepare(query)
24 paramtypes = []
25 for t in ps.get_parameters():
26 if t.name.endswith('[]'):
27 pname = '_' + t.name[:-2]
28 else:
29 pname = t.name
30
31 paramtypes.append('{}.{}'.format(
32 _quote_ident(t.schema), _quote_ident(pname)))
33 del ps
34
35 # Use Postgres to convert arguments to text representation
36 # by casting each value to text.
37 cols = ['quote_literal(${}::{}::text)'.format(i, t)
38 for i, t in enumerate(paramtypes, start=1)]
39
40 textified = await conn.fetchrow(
41 'SELECT {cols}'.format(cols=', '.join(cols)), *args)
42
43 # Finally, replace $n references with text values.
44 return re.sub(
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 )

Callers

nothing calls this directly

Calls 4

_quote_identFunction · 0.85
prepareMethod · 0.80
get_parametersMethod · 0.80
fetchrowMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…