from ._queries import PostgresQuery, PostgresClientQuery
+from . import adapt
from . import errors as e
from .pq import Format
from .abc import ConnectionType, Query, Params
class ClientCursorMixin(BaseCursor[ConnectionType, Row]):
+ def mogrify(self, query: Query, params: Optional[Params] = None) -> str:
+ """
+ Return the query to be executed with parameters merged.
+ """
+ self._tx = adapt.Transformer(self)
+ pgq = self._convert_query(query, params)
+ return pgq.query.decode(self._tx.encoding)
+
def _execute_send(
self,
query: PostgresQuery,
import gc
import pickle
import weakref
+import datetime as dt
from typing import List
import pytest
gc_collect()
n.append(len(gc.get_objects()))
assert n[0] == n[1] == n[2], f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+
+
+def test_mogrify(conn):
+ cur = conn.cursor()
+ q = cur.mogrify("select 'hello'")
+ assert q == "select 'hello'"
+
+ q = cur.mogrify("select %s, %s", [1, dt.date(2020, 1, 1)])
+ assert q == "select 1, '2020-01-01'::date"
+
+ conn.execute("set client_encoding to utf8")
+ q = cur.mogrify("select %(s)s", {"s": "\u20ac"})
+ assert q == "select '\u20ac'"
+
+ conn.execute("set client_encoding to latin9")
+ q = cur.mogrify("select %(s)s", {"s": "\u20ac"})
+ assert q == "select '\u20ac'"
+
+ conn.execute("set client_encoding to latin1")
+ with pytest.raises(UnicodeEncodeError):
+ cur.mogrify("select %(s)s", {"s": "\u20ac"})
import gc
import pytest
import weakref
+import datetime as dt
from typing import List
import psycopg
n.append(len(gc.get_objects()))
assert n[0] == n[1] == n[2], f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}"
+
+
+async def test_mogrify(aconn):
+ cur = aconn.cursor()
+ q = cur.mogrify("select 'hello'")
+ assert q == "select 'hello'"
+
+ q = cur.mogrify("select %s, %s", [1, dt.date(2020, 1, 1)])
+ assert q == "select 1, '2020-01-01'::date"
+
+ await aconn.execute("set client_encoding to utf8")
+ q = cur.mogrify("select %(s)s", {"s": "\u20ac"})
+ assert q == "select '\u20ac'"
+
+ await aconn.execute("set client_encoding to latin9")
+ q = cur.mogrify("select %(s)s", {"s": "\u20ac"})
+ assert q == "select '\u20ac'"
+
+ await aconn.execute("set client_encoding to latin1")
+ with pytest.raises(UnicodeEncodeError):
+ cur.mogrify("select %(s)s", {"s": "\u20ac"})