|
1 | 1 | import contextlib
|
2 | 2 | import functools
|
3 | 3 | import logging
|
| 4 | +import re |
4 | 5 | from typing import Any, Callable, Dict, Optional
|
5 | 6 |
|
6 | 7 | import psycopg2
|
@@ -66,6 +67,9 @@ def wrapped(*args, **kwargs):
|
66 | 67 | return wrapped
|
67 | 68 |
|
68 | 69 |
|
| 70 | +PERCENT_PATTERN = re.compile(r"%(?![\(s])") |
| 71 | + |
| 72 | + |
69 | 73 | class Psycopg2Connector(connector.BaseConnector):
|
70 | 74 | @wrap_exceptions
|
71 | 75 | def __init__(
|
@@ -201,20 +205,26 @@ def _connection(self) -> psycopg2.extensions.connection:
|
201 | 205 | def execute_query(self, query: str, **arguments: Any) -> None:
|
202 | 206 | with self._connection() as connection:
|
203 | 207 | with connection.cursor() as cursor:
|
204 |
| - cursor.execute(query, self._wrap_json(arguments)) |
| 208 | + cursor.execute( |
| 209 | + PERCENT_PATTERN.sub("%%", query), self._wrap_json(arguments) |
| 210 | + ) |
205 | 211 |
|
206 | 212 | @wrap_exceptions
|
207 | 213 | @wrap_query_exceptions
|
208 | 214 | def execute_query_one(self, query: str, **arguments: Any) -> Dict[str, Any]:
|
209 | 215 | with self._connection() as connection:
|
210 | 216 | with connection.cursor() as cursor:
|
211 |
| - cursor.execute(query, self._wrap_json(arguments)) |
| 217 | + cursor.execute( |
| 218 | + PERCENT_PATTERN.sub("%%", query), self._wrap_json(arguments) |
| 219 | + ) |
212 | 220 | return cursor.fetchone()
|
213 | 221 |
|
214 | 222 | @wrap_exceptions
|
215 | 223 | @wrap_query_exceptions
|
216 | 224 | def execute_query_all(self, query: str, **arguments: Any) -> Dict[str, Any]:
|
217 | 225 | with self._connection() as connection:
|
218 | 226 | with connection.cursor() as cursor:
|
219 |
| - cursor.execute(query, self._wrap_json(arguments)) |
| 227 | + cursor.execute( |
| 228 | + PERCENT_PATTERN.sub("%%", query), self._wrap_json(arguments) |
| 229 | + ) |
220 | 230 | return cursor.fetchall()
|
0 commit comments