]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor: `TypeInfo.alt_name` renamed to `regtype`
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 20 Mar 2022 15:21:40 +0000 (16:21 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 10 May 2022 17:13:26 +0000 (19:13 +0200)
This has now acquired a precise meaning: it is the name of the type as
SQL snippet (it may contain invalid names, which get quoted, or a schema
name). It can be included literally in a query to make a valid cast.

The previous name wasn't documented and wasn't part of any public API so
we can probably rename it without much problem.

psycopg/psycopg/_typeinfo.py
psycopg/psycopg/postgres.py
psycopg/psycopg/sql.py
tests/test_typeinfo.py
tools/update_oids.py

index c69642a6b09a760c019222534d456f4a984dbf0d..1736913324cf71b820130f5c623960cc49e06cb2 100644 (file)
@@ -37,13 +37,13 @@ class TypeInfo:
         oid: int,
         array_oid: int,
         *,
-        alt_name: str = "",
+        regtype: str = "",
         delimiter: str = ",",
     ):
         self.name = name
         self.oid = oid
         self.array_oid = array_oid
-        self.alt_name = alt_name or name
+        self.regtype = regtype or name
         self.delimiter = delimiter
 
     def __repr__(self) -> str:
@@ -160,7 +160,7 @@ class TypeInfo:
         return """\
 SELECT
     typname AS name, oid, typarray AS array_oid,
-    oid::regtype::text AS alt_name, typdelim AS delimiter
+    oid::regtype::text AS regtype, typdelim AS delimiter
 FROM pg_type t
 WHERE t.oid = %(name)s::regtype
 ORDER BY t.oid
@@ -182,10 +182,10 @@ class RangeInfo(TypeInfo):
         oid: int,
         array_oid: int,
         *,
-        alt_name: str = "",
+        regtype: str = "",
         subtype_oid: int,
     ):
-        super().__init__(name, oid, array_oid, alt_name=alt_name)
+        super().__init__(name, oid, array_oid, regtype=regtype)
         self.subtype_oid = subtype_oid
 
     @classmethod
@@ -194,7 +194,7 @@ class RangeInfo(TypeInfo):
     ) -> str:
         return """\
 SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid,
-    t.oid::regtype::text AS alt_name,
+    t.oid::regtype::text AS regtype,
     r.rngsubtype AS subtype_oid
 FROM pg_type t
 JOIN pg_range r ON t.oid = r.rngtypid
@@ -218,11 +218,11 @@ class MultirangeInfo(TypeInfo):
         oid: int,
         array_oid: int,
         *,
-        alt_name: str = "",
+        regtype: str = "",
         range_oid: int,
         subtype_oid: int,
     ):
-        super().__init__(name, oid, array_oid, alt_name=alt_name)
+        super().__init__(name, oid, array_oid, regtype=regtype)
         self.range_oid = range_oid
         self.subtype_oid = subtype_oid
 
@@ -236,7 +236,7 @@ class MultirangeInfo(TypeInfo):
             )
         return """\
 SELECT t.typname AS name, t.oid AS oid, t.typarray AS array_oid,
-    t.oid::regtype::text AS alt_name,
+    t.oid::regtype::text AS regtype,
     r.rngtypid AS range_oid, r.rngsubtype AS subtype_oid
 FROM pg_type t
 JOIN pg_range r ON t.oid = r.rngmultitypid
@@ -260,11 +260,11 @@ class CompositeInfo(TypeInfo):
         oid: int,
         array_oid: int,
         *,
-        alt_name: str = "",
+        regtype: str = "",
         field_names: Sequence[str],
         field_types: Sequence[int],
     ):
-        super().__init__(name, oid, array_oid, alt_name=alt_name)
+        super().__init__(name, oid, array_oid, regtype=regtype)
         self.field_names = field_names
         self.field_types = field_types
         # Will be set by register() if the `factory` is a type
@@ -277,7 +277,7 @@ class CompositeInfo(TypeInfo):
         return """\
 SELECT
     t.typname AS name, t.oid AS oid, t.typarray AS array_oid,
-    t.oid::regtype::text AS alt_name,
+    t.oid::regtype::text AS regtype,
     coalesce(a.fnames, '{}') AS field_names,
     coalesce(a.ftypes, '{}') AS field_types
 FROM pg_type t
@@ -368,8 +368,8 @@ class TypesRegistry:
             self._registry[info.array_oid] = info
         self._registry[info.name] = info
 
-        if info.alt_name and info.alt_name not in self._registry:
-            self._registry[info.alt_name] = info
+        if info.regtype and info.regtype not in self._registry:
+            self._registry[info.regtype] = info
 
         # Allow info to customise further their relation with the registry
         info._added(self)
index 29816c24817c7ac9464dcb160f1a611cbca2e145..713bba7018b8bbe7259dcc5ce4d83d74ddebbe5e 100644 (file)
@@ -20,23 +20,23 @@ for t in [
     # Generated from PostgreSQL 14.0
     TypeInfo("aclitem", 1033, 1034),
     TypeInfo("bit", 1560, 1561),
-    TypeInfo("bool", 16, 1000, alt_name="boolean"),
+    TypeInfo("bool", 16, 1000, regtype="boolean"),
     TypeInfo("box", 603, 1020, delimiter=";"),
-    TypeInfo("bpchar", 1042, 1014, alt_name="character"),
+    TypeInfo("bpchar", 1042, 1014, regtype="character"),
     TypeInfo("bytea", 17, 1001),
-    TypeInfo("char", 18, 1002, alt_name='"char"'),
+    TypeInfo("char", 18, 1002, regtype='"char"'),
     TypeInfo("cid", 29, 1012),
     TypeInfo("cidr", 650, 651),
     TypeInfo("circle", 718, 719),
     TypeInfo("date", 1082, 1182),
-    TypeInfo("float4", 700, 1021, alt_name="real"),
-    TypeInfo("float8", 701, 1022, alt_name="double precision"),
+    TypeInfo("float4", 700, 1021, regtype="real"),
+    TypeInfo("float8", 701, 1022, regtype="double precision"),
     TypeInfo("gtsvector", 3642, 3644),
     TypeInfo("inet", 869, 1041),
-    TypeInfo("int2", 21, 1005, alt_name="smallint"),
+    TypeInfo("int2", 21, 1005, regtype="smallint"),
     TypeInfo("int2vector", 22, 1006),
-    TypeInfo("int4", 23, 1007, alt_name="integer"),
-    TypeInfo("int8", 20, 1016, alt_name="bigint"),
+    TypeInfo("int4", 23, 1007, regtype="integer"),
+    TypeInfo("int8", 20, 1016, regtype="bigint"),
     TypeInfo("interval", 1186, 1187),
     TypeInfo("json", 114, 199),
     TypeInfo("jsonb", 3802, 3807),
@@ -69,16 +69,16 @@ for t in [
     TypeInfo("regtype", 2206, 2211),
     TypeInfo("text", 25, 1009),
     TypeInfo("tid", 27, 1010),
-    TypeInfo("time", 1083, 1183, alt_name="time without time zone"),
-    TypeInfo("timestamp", 1114, 1115, alt_name="timestamp without time zone"),
-    TypeInfo("timestamptz", 1184, 1185, alt_name="timestamp with time zone"),
-    TypeInfo("timetz", 1266, 1270, alt_name="time with time zone"),
+    TypeInfo("time", 1083, 1183, regtype="time without time zone"),
+    TypeInfo("timestamp", 1114, 1115, regtype="timestamp without time zone"),
+    TypeInfo("timestamptz", 1184, 1185, regtype="timestamp with time zone"),
+    TypeInfo("timetz", 1266, 1270, regtype="time with time zone"),
     TypeInfo("tsquery", 3615, 3645),
     TypeInfo("tsvector", 3614, 3643),
     TypeInfo("txid_snapshot", 2970, 2949),
     TypeInfo("uuid", 2950, 2951),
-    TypeInfo("varbit", 1562, 1563, alt_name="bit varying"),
-    TypeInfo("varchar", 1043, 1015, alt_name="character varying"),
+    TypeInfo("varbit", 1562, 1563, regtype="bit varying"),
+    TypeInfo("varchar", 1043, 1015, regtype="character varying"),
     TypeInfo("xid", 28, 1011),
     TypeInfo("xid8", 5069, 271),
     TypeInfo("xml", 142, 143),
index bcf604bb0e01394adb29027d50e229a3a28779b8..f2b0ac1ccbce3538ecd9e1b634636f2b21cc0eca 100644 (file)
@@ -402,10 +402,10 @@ class Literal(Composable):
             ti = tx.adapters.types.get(dumper.oid)
             if ti:
                 try:
-                    type_name = self._names_cache[ti.alt_name, tx.encoding]
+                    type_name = self._names_cache[ti.regtype, tx.encoding]
                 except KeyError:
-                    type_name = ti.alt_name.encode(tx.encoding)
-                    self._names_cache[ti.alt_name, tx.encoding] = type_name
+                    type_name = ti.regtype.encode(tx.encoding)
+                    self._names_cache[ti.regtype, tx.encoding] = type_name
                 rv = b"%s::%s" % (rv, type_name)
         return rv
 
index fd1367e3769c77dffafd7c5e2731e0547130cb81..d0316ad3abb5690553ed936aacf8391ec9b89d0b 100644 (file)
@@ -23,7 +23,7 @@ def test_fetch(conn, name, status):
 
     assert info.oid == psycopg.adapters.types["text"].oid
     assert info.array_oid == psycopg.adapters.types["text"].array_oid
-    assert info.alt_name == "text"
+    assert info.regtype == "text"
 
 
 @pytest.mark.asyncio
index 743ad91adce5f59d2804614fcaf6f336d51bee03..98d8fce6c7225e100a05bc278aefaa978f209e83 100755 (executable)
@@ -32,7 +32,7 @@ select
         oid::text,
         typarray::text,
         case when oid::regtype::text != typname
-            then format('alt_name=%L', oid::regtype)
+            then format('regtype=%L', oid::regtype)
         end,
         case when typdelim != ','
             then format('delimiter=%L', typdelim)