from . import exceptions as exc
from .pq import Format
from .cursor import BaseCursor
+from .types.oids import type_oid
from .connection import BaseConnection
INVALID_OID = 0
-TEXT_OID = 25
-NUMERIC_OID = 1700
ascii_encode = codecs.lookup("ascii").encode
ascii_decode = codecs.lookup("ascii").decode
oid = data[1]
data = data[0]
else:
- oid = TEXT_OID
+ oid = type_oid["text"]
out.append(data)
types.append(oid)
def adapt(self, obj, fmt):
if obj is None:
- return None, TEXT_OID
+ return None, type_oid["text"]
cls = type(obj)
func = self.get_adapt_function(cls, fmt)
return self.encode(obj)[0]
-@caster(TEXT_OID)
-@binary_caster(TEXT_OID)
+@caster(type_oid["text"])
+@binary_caster(type_oid["text"])
class StringCaster(Typecaster):
def __init__(self, oid, conn):
super().__init__(oid, conn)
@adapter(int)
def adapt_int(obj):
- return ascii_encode(str(obj))[0], NUMERIC_OID
+ return ascii_encode(str(obj))[0], type_oid["numeric"]
-@caster(NUMERIC_OID)
+@caster(type_oid["numeric"])
def cast_int(data):
return int(ascii_decode(data)[0])
--- /dev/null
+"""
+Maps of builtin types and names
+
+You can update this file by executing it, using the PG* env var to connect
+to a Postgres server.
+"""
+
+# Copyright (C) 2020 The Psycopg Team
+
+_oids_table = [
+ # autogenerated start
+ # Generated from PostgreSQL 11
+ ("abstime", 702, 1023),
+ ("aclitem", 1033, 1034),
+ ("any", 2276, 0),
+ ("anyarray", 2277, 0),
+ ("anyelement", 2283, 0),
+ ("anyenum", 3500, 0),
+ ("anynonarray", 2776, 0),
+ ("anyrange", 3831, 0),
+ ("bit", 1560, 1561),
+ ("bool", 16, 1000),
+ ("box", 603, 1020),
+ ("bpchar", 1042, 1014),
+ ("bytea", 17, 1001),
+ ("char", 18, 1002),
+ ("cid", 29, 1012),
+ ("cidr", 650, 651),
+ ("circle", 718, 719),
+ ("cstring", 2275, 1263),
+ ("date", 1082, 1182),
+ ("daterange", 3912, 3913),
+ ("event_trigger", 3838, 0),
+ ("float4", 700, 1021),
+ ("float8", 701, 1022),
+ ("gtsvector", 3642, 3644),
+ ("inet", 869, 1041),
+ ("int2", 21, 1005),
+ ("int2vector", 22, 1006),
+ ("int4", 23, 1007),
+ ("int4range", 3904, 3905),
+ ("int8", 20, 1016),
+ ("int8range", 3926, 3927),
+ ("internal", 2281, 0),
+ ("interval", 1186, 1187),
+ ("json", 114, 199),
+ ("jsonb", 3802, 3807),
+ ("line", 628, 629),
+ ("lseg", 601, 1018),
+ ("macaddr", 829, 1040),
+ ("macaddr8", 774, 775),
+ ("money", 790, 791),
+ ("name", 19, 1003),
+ ("numeric", 1700, 1231),
+ ("numrange", 3906, 3907),
+ ("oid", 26, 1028),
+ ("oidvector", 30, 1013),
+ ("opaque", 2282, 0),
+ ("path", 602, 1019),
+ ("point", 600, 1017),
+ ("polygon", 604, 1027),
+ ("record", 2249, 2287),
+ ("refcursor", 1790, 2201),
+ ("reltime", 703, 1024),
+ ("smgr", 210, 0),
+ ("text", 25, 1009),
+ ("tid", 27, 1010),
+ ("time", 1083, 1183),
+ ("timestamp", 1114, 1115),
+ ("timestamptz", 1184, 1185),
+ ("timetz", 1266, 1270),
+ ("tinterval", 704, 1025),
+ ("trigger", 2279, 0),
+ ("tsquery", 3615, 3645),
+ ("tsrange", 3908, 3909),
+ ("tstzrange", 3910, 3911),
+ ("tsvector", 3614, 3643),
+ ("txid_snapshot", 2970, 2949),
+ ("unknown", 705, 0),
+ ("uuid", 2950, 2951),
+ ("varbit", 1562, 1563),
+ ("varchar", 1043, 1015),
+ ("void", 2278, 0),
+ ("xid", 28, 1011),
+ ("xml", 142, 143),
+ # autogenerated end
+]
+
+type_oid = {name: oid for name, oid, array_oid in _oids_table}
+
+
+def self_update():
+ import subprocess as sp
+
+ queries = [
+ """
+select format(' # Generated from PostgreSQL %s', setting::int / 10000)
+ from pg_settings
+ where name = 'server_version_num';
+""",
+ """
+select format(' (\"%s\", %s, %s),', typname, oid, typarray)
+ from pg_type
+ where oid < 10000
+ and typname !~ all('{^(_|pg_|reg),_handler$}')
+ order by 1;
+""",
+ ]
+
+ with open(__file__, "rb") as f:
+ lines = f.read().splitlines()
+
+ new = []
+ for query in queries:
+ out = sp.run(
+ ["psql", "-AXqt", "-c", query], stdout=sp.PIPE, check=True
+ )
+ new.extend(out.stdout.splitlines())
+
+ istart, = [
+ i for i, l in enumerate(lines) if b"autogenerated " + b"start" in l
+ ]
+ iend, = [i for i, l in enumerate(lines) if b"autogenerated " + b"end" in l]
+ lines[istart + 1 : iend] = new
+
+ with open(__file__, "wb") as f:
+ f.write(b"\n".join(lines))
+ f.write(b"\n")
+
+
+if __name__ == "__main__":
+ self_update()