]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix(sql): represent array literals correctly
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 20 Mar 2022 18:22:47 +0000 (19:22 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 10 May 2022 17:13:26 +0000 (19:13 +0200)
psycopg/psycopg/sql.py
tests/test_sql.py

index f2b0ac1ccbce3538ecd9e1b634636f2b21cc0eca..9726af8b5f342808e145413ef7c9e722cfdeb32f 100644 (file)
@@ -406,6 +406,8 @@ class Literal(Composable):
                 except KeyError:
                     type_name = ti.regtype.encode(tx.encoding)
                     self._names_cache[ti.regtype, tx.encoding] = type_name
+                if dumper.oid == ti.array_oid:
+                    type_name += b"[]"
                 rv = b"%s::%s" % (rv, type_name)
         return rv
 
index d8ae837035f70de2cab5651daed62501ca2ae115..21d0018c5707ad2c1d297a683a1a8f21edf03bd6 100644 (file)
@@ -339,6 +339,12 @@ class TestLiteral:
         with pytest.raises(ProgrammingError):
             sql.Literal(Foo()).as_string(conn)
 
+    def test_array(self, conn):
+        assert (
+            sql.Literal([dt.date(2000, 1, 1)]).as_string(conn)
+            == "'{2000-01-01}'::date[]"
+        )
+
     @pytest.mark.parametrize("name", ["a-b", f"{eur}", "order"])
     def test_invalid_name(self, conn, name):
         conn.execute(
@@ -368,6 +374,12 @@ class TestLiteral:
         cur = conn.execute(sql.SQL("select {}").format("hello"))
         assert cur.fetchone()[0] == "hello-inv"
 
+        assert (
+            sql.Literal(["hello"]).as_string(conn) == f"'{{hello-inv}}'::\"{name}\"[]"
+        )
+        cur = conn.execute(sql.SQL("select {}").format(["hello"]))
+        assert cur.fetchone()[0] == ["hello-inv"]
+
 
 class TestSQL:
     def test_class(self):