) -> PQGen[None]:
"""Generator implementing `ServerCursor.execute()`."""
conn = cur._conn
+
+ # If the cursor is being reused, the previous one must be closed.
+ if self.described:
+ yield from self._close_gen(cur)
+ self.described = False
+
yield from cur._start_query(query)
pgq = cur._convert_query(query, params)
cur._execute_send(pgq)
assert ".close()" in str(recwarn.pop(ResourceWarning).message)
+def test_execute_reuse(conn):
+ with conn.cursor("foo") as cur:
+ cur.execute("select generate_series(1, %s) as foo", (3,))
+ assert cur.fetchone() == (1,)
+
+ cur.execute(
+ "select %s::text as bar, %s::text as baz", ("hello", "world")
+ )
+ assert cur.fetchone() == ("hello", "world")
+ assert cur.description[0].name == "bar"
+ assert cur.description[0].type_code == cur.adapters.types["text"].oid
+ assert cur.description[1].name == "baz"
+
+
def test_executemany(conn):
cur = conn.cursor("foo")
with pytest.raises(e.NotSupportedError):
assert ".close()" in str(recwarn.pop(ResourceWarning).message)
+async def test_execute_reuse(aconn):
+ async with aconn.cursor("foo") as cur:
+ await cur.execute("select generate_series(1, %s) as foo", (3,))
+ assert await cur.fetchone() == (1,)
+
+ await cur.execute(
+ "select %s::text as bar, %s::text as baz", ("hello", "world")
+ )
+ assert await cur.fetchone() == ("hello", "world")
+ assert cur.description[0].name == "bar"
+ assert cur.description[0].type_code == cur.adapters.types["text"].oid
+ assert cur.description[1].name == "baz"
+
+
async def test_executemany(aconn):
cur = aconn.cursor("foo")
with pytest.raises(e.NotSupportedError):