.. admonition:: TODO
- pass parameters in binary with ``%b``
- - return parameters in binary with `!cursor(format=BINARY)`
+ - return parameters in binary with `!cursor(binary=True)`
- cannot pass multiple statements in binary
"""Close the database connection."""
self.pgconn.finish()
- def cursor(self, name: str = "", format: Format = Format.TEXT) -> "Cursor":
+ def cursor(self, name: str = "", binary: bool = False) -> "Cursor":
"""
Return a new `Cursor` to send commands and queries to the connection.
"""
if name:
raise NotImplementedError
+ format = Format.BINARY if binary else Format.TEXT
return self.cursor_factory(self, format=format)
def execute(
self.pgconn.finish()
async def cursor(
- self, name: str = "", format: Format = Format.TEXT
+ self, name: str = "", binary: bool = False
) -> "AsyncCursor":
"""
Return a new `AsyncCursor` to send commands and queries to the connection.
if name:
raise NotImplementedError
+ format = Format.BINARY if binary else Format.TEXT
return self.cursor_factory(self, format=format)
async def execute(
) -> Optional["RangeInfo"]:
if isinstance(name, sql.Composable):
name = name.as_string(conn)
- cur = conn.cursor(format=Format.BINARY)
+ cur = conn.cursor(binary=True)
cur.execute(cls._info_query, {"name": name})
recs = cur.fetchall()
return cls._from_records(recs)
) -> Optional["RangeInfo"]:
if isinstance(name, sql.Composable):
name = name.as_string(conn)
- cur = await conn.cursor(format=Format.BINARY)
+ cur = await conn.cursor(binary=True)
await cur.execute(cls._info_query, {"name": name})
recs = await cur.fetchall()
return cls._from_records(recs)
make_loader("t").register(TEXT_OID, conn)
make_bin_loader("b").register(TEXT_OID, conn)
- r = conn.cursor().execute("select 'hello'::text").fetchone()
+ r = conn.cursor(binary=False).execute("select 'hello'::text").fetchone()
assert r == ("hellot",)
- r = conn.cursor(format=1).execute("select 'hello'::text").fetchone()
+ r = conn.cursor(binary=True).execute("select 'hello'::text").fetchone()
assert r == ("hellob",)
)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_cursor_ctx_nested(conn, sql, obj, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out == pq.Format.BINARY)
if fmt_out == pq.Format.TEXT:
make_loader("c").register(TEXT_OID, cur)
else:
import pytest
import psycopg3
-from psycopg3 import pq
from psycopg3.oids import builtins
from psycopg3.adapt import Format
def test_execute_binary_result(conn):
- cur = conn.cursor(format=pq.Format.BINARY)
+ cur = conn.cursor(binary=True)
cur.execute("select %s::text, %s::text", ["foo", None])
assert cur.pgresult.fformat(0) == 1
n = []
for i in range(3):
with psycopg3.connect(dsn) as conn:
- with conn.cursor(format=Format.as_pq(fmt)) as cur:
+ with conn.cursor(binary=Format.as_pq(fmt)) as cur:
cur.execute(faker.drop_stmt)
cur.execute(faker.create_stmt)
cur.executemany(faker.insert_stmt, faker.records)
import weakref
import psycopg3
-from psycopg3 import pq
from psycopg3.adapt import Format
pytestmark = pytest.mark.asyncio
async def test_execute_binary_result(aconn):
- cur = await aconn.cursor(format=pq.Format.BINARY)
+ cur = await aconn.cursor(binary=True)
await cur.execute("select %s::text, %s::text", ["foo", None])
assert cur.pgresult.fformat(0) == 1
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("want, obj", tests_str)
def test_load_list_str(conn, obj, want, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::text[]", (obj,))
assert cur.fetchone()[0] == want
@pytest.mark.parametrize("fmt_in", fmts_in)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_all_chars(conn, fmt_in, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
for i in range(1, 256):
c = chr(i)
cur.execute(f"select %{fmt_in}::text[]", ([c],))
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("want, obj", tests_int)
def test_load_list_int(conn, obj, want, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::int[]", (obj,))
assert cur.fetchone()[0] == want
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_all_chars(conn, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
for i in range(1, 256):
res = cur.execute("select row(chr(%s::int))", (i,)).fetchone()[0]
assert res == (chr(i),)
],
)
def test_load_record_binary(conn, want, rec):
- cur = conn.cursor(format=1)
+ cur = conn.cursor(binary=True)
res = cur.execute(f"select row({rec})").fetchone()[0]
assert res == want
for o1, o2 in zip(res, want):
info = CompositeInfo.fetch(conn, "testcomp")
info.register(conn)
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
res = cur.execute("select row('hello', 10, 20)::testcomp").fetchone()[0]
assert res.foo == "hello"
assert res.bar == 10
info.register(conn, factory=MyThing)
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
res = cur.execute("select row('hello', 10, 20)::testcomp").fetchone()[0]
assert isinstance(res, MyThing)
assert res.baz == 20.0
@pytest.mark.xfail # TODO: binary load
@pytest.mark.parametrize("val, expr", [("2000,1,1", "2000-01-01")])
def test_load_date_binary(conn, val, expr):
- cur = conn.cursor(format=Format.BINARY)
+ cur = conn.cursor(binary=Format.BINARY)
cur.execute(f"select '{expr}'::date")
assert cur.fetchone()[0] == as_date(val)
@pytest.mark.xfail # TODO: binary load
@pytest.mark.parametrize("val, expr", [("0,0", "00:00")])
def test_load_time_binary(conn, val, expr):
- cur = conn.cursor(format=Format.BINARY)
+ cur = conn.cursor(binary=Format.BINARY)
cur.execute(f"select '{expr}'::time")
assert cur.fetchone()[0] == as_time(val)
@pytest.mark.xfail # TODO: binary load
@pytest.mark.parametrize("val, expr, timezone", [("0,0~2", "00:00", "-02:00")])
def test_load_timetz_binary(conn, val, expr, timezone):
- cur = conn.cursor(format=Format.BINARY)
+ cur = conn.cursor(binary=Format.BINARY)
cur.execute(f"set timezone to '{timezone}'")
cur.execute(f"select '{expr}'::time")
assert cur.fetchone()[0] == as_time(val)
@pytest.mark.parametrize("jtype", ["json", "jsonb"])
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_json_load(conn, val, jtype, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute(f"select %s::{jtype}", (val,))
assert cur.fetchone()[0] == json.loads(val)
@pytest.mark.parametrize("val", ["127.0.0.1/32", "::ffff:102:300/128"])
def test_inet_load_address(conn, fmt_out, val):
binary_check(fmt_out)
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::inet", (val,))
addr = ipaddress.ip_address(val.split("/", 1)[0])
assert cur.fetchone()[0] == addr
@pytest.mark.parametrize("val", ["127.0.0.1/24", "::ffff:102:300/127"])
def test_inet_load_network(conn, fmt_out, val):
binary_check(fmt_out)
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::inet", (val,))
assert cur.fetchone()[0] == ipaddress.ip_interface(val)
cur.execute("select array[null, %s::inet]", (val,))
@pytest.mark.parametrize("val", ["127.0.0.0/24", "::ffff:102:300/128"])
def test_cidr_load(conn, fmt_out, val):
binary_check(fmt_out)
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::cidr", (val,))
assert cur.fetchone()[0] == ipaddress.ip_network(val)
cur.execute("select array[null, %s::cidr]", (val,))
)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_int(conn, val, pgtype, want, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute(f"select %s::{pgtype}", (val,))
assert cur.pgresult.fformat(0) == fmt_out
assert cur.pgresult.ftype(0) == builtins[pgtype].oid
)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_float(conn, val, pgtype, want, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute(f"select %s::{pgtype}", (val,))
assert cur.pgresult.fformat(0) == fmt_out
assert cur.pgresult.ftype(0) == builtins[pgtype].oid
)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_float_approx(conn, expr, pgtype, want, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
cur.execute("select %s::%s" % (expr, pgtype))
assert cur.pgresult.fformat(0) == fmt_out
result = cur.fetchone()[0]
@pytest.mark.xfail
def test_load_numeric_binary(conn):
# TODO: numeric binary casting
- cur = conn.cursor(format=1)
+ cur = conn.cursor(binary=1)
res = cur.execute("select 1::numeric").fetchone()[0]
assert res == Decimal(1)
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("b", [True, False])
def test_roundtrip_bool(conn, b, fmt_in, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
result = cur.execute(f"select %{fmt_in}", (b,)).fetchone()[0]
assert cur.pgresult.fformat(0) == fmt_out
if b is not None:
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_1char(conn, typename, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
for i in range(1, 256):
cur.execute(f"select chr(%s::int)::{typename}", (i,))
res = cur.fetchone()[0]
@pytest.mark.parametrize("encoding", ["utf8", "latin9"])
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
def test_load_enc(conn, typename, encoding, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
conn.client_encoding = encoding
(res,) = cur.execute(
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
def test_load_badenc(conn, typename, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
conn.client_encoding = "latin1"
with pytest.raises(psycopg3.DatabaseError):
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
def test_load_ascii(conn, typename, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
conn.client_encoding = "ascii"
(res,) = cur.execute(
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])
def test_text_array(conn, typename, fmt_in, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
a = list(map(chr, range(1, 256))) + [eur]
(res,) = cur.execute(f"select %{fmt_in}::{typename}[]", (a,)).fetchone()
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_text_array_ascii(conn, fmt_in, fmt_out):
conn.client_encoding = "ascii"
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
a = list(map(chr, range(1, 256))) + [eur]
exp = [s.encode("utf8") for s in a]
(res,) = cur.execute(f"select %{fmt_in}::text[]", (a,)).fetchone()
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_load_1byte(conn, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
for i in range(0, 256):
cur.execute("select %s::bytea", (fr"\x{i:02x}",))
assert cur.fetchone()[0] == bytes([i])
@pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY])
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_bytea_array(conn, fmt_in, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
a = [bytes(range(0, 256))]
(res,) = cur.execute(f"select %{fmt_in}::bytea[]", (a,)).fetchone()
assert res == a
@pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])
def test_uuid_load(conn, fmt_out):
- cur = conn.cursor(format=fmt_out)
+ cur = conn.cursor(binary=fmt_out)
val = "12345678123456781234567812345679"
cur.execute("select %s::uuid", (val,))
assert cur.fetchone()[0] == UUID(val)