Safely inline arguments to query text.
(conn, query, args)
| 17 | |
| 18 | |
| 19 | async 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 | ) |
nothing calls this directly
no test coverage detected
searching dependent graphs…