本文整理汇总了Python中psycopg2.extensions.new_type函数的典型用法代码示例。如果您正苦于以下问题:Python new_type函数的具体用法?Python new_type怎么用?Python new_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了new_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register_uuid
def register_uuid(oids=None, conn_or_curs=None):
"""Create the UUID type and an uuid.UUID adapter."""
if not oids:
oid1 = 2950
oid2 = 2951
elif type(oids) == list:
oid1, oid2 = oids
else:
oid1 = oids
oid2 = 2951
def parseUUIDARRAY(data, cursor):
if data is None:
return None
elif data == "{}":
return []
else:
return [((len(x) > 0 and x != "NULL") and uuid.UUID(x) or None) for x in data[1:-1].split(",")]
_ext.UUID = _ext.new_type((oid1,), "UUID", lambda data, cursor: data and uuid.UUID(data) or None)
_ext.UUIDARRAY = _ext.new_type((oid2,), "UUID[]", parseUUIDARRAY)
_ext.register_type(_ext.UUID, conn_or_curs)
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
_ext.register_adapter(uuid.UUID, UUID_adapter)
return _ext.UUID
示例2: _make_casters
def _make_casters():
inet = new_type((869,), 'INET', cast_interface)
ainet = new_array_type((1041,), 'INET[]', inet)
cidr = new_type((650,), 'CIDR', cast_network)
acidr = new_array_type((651,), 'CIDR[]', cidr)
return [inet, ainet, cidr, acidr]
示例3: register_inet
def register_inet(oid=None, conn_or_curs=None):
"""Create the INET type and an Inet adapter."""
if not oid:
oid = 869
_ext.INET = _ext.new_type((oid,), "INET", lambda data, cursor: data and Inet(data) or None)
_ext.register_type(_ext.INET, conn_or_curs)
return _ext.INET
示例4: register_extensions
def register_extensions(con):
pgext.register_adapter(Polygon, adapt_wkt)
pgext.register_adapter(Point, adapt_wkt)
pgext.register_adapter(LineString, adapt_wkt)
with con.cursor() as c:
c.execute('select null::point, null::polygon, null::geometry')
point_oid, poly_oid = c.description[0][1], c.description[1][1]
geom_oid = c.description[2][1]
point = pgext.new_type((point_oid,), 'POINT', cast_wkb)
polygon = pgext.new_type((poly_oid,), 'POLYGON', cast_wkb)
geom = pgext.new_type((geom_oid,), 'GEOMETRY', cast_wkb)
pgext.register_type(point)
pgext.register_type(polygon)
pgext.register_type(geom)
示例5: register_ltree
def register_ltree(conn):
oid = _get_ltree_oids(conn)
if not oid[0]:
return False
else:
array_oid = oid[1]
oid = oid[0]
if isinstance(oid, int):
oid = (oid,)
if array_oid is not None:
if isinstance(array_oid, int):
array_oid = (array_oid,)
else:
array_oid = tuple([x for x in array_oid if x])
ltree = extensions.new_type(oid, "LTREE", _cast_fn)
extensions.register_type(ltree, None)
if array_oid:
ltree_array = extensions.new_array_type(array_oid, "LTREEARRAY", ltree)
extensions.register_type(ltree_array, None)
return True
示例6: register_ltree
def register_ltree(conn_or_curs, globally=False, oid=None, array_oid=None):
"""Register the ltree adapter and typecaster on the connection or cursor.
"""
register_adapter()
conn, curs, conn_or_curs = _solve_conn_curs(conn_or_curs)
if oid is None:
oid = get_oids(conn_or_curs, 'ltree')
if oid is None or not oid[0]:
raise psycopg2.ProgrammingError(
"ltree type not found in the database."
)
else:
array_oid = oid[1]
oid = oid[0]
if isinstance(oid, int):
oid = (oid,)
if array_oid is not None:
if isinstance(array_oid, int):
array_oid = (array_oid,)
else:
array_oid = tuple([x for x in array_oid if x])
# create and register the typecaster
LTREE = ext.new_type(oid, "LTREE", cast_ltree)
ext.register_type(LTREE, not globally and conn_or_curs or None)
if array_oid:
LTREEARRAY = ext.new_array_type(array_oid, "LTREEARRAY", LTREE)
ext.register_type(LTREEARRAY, not globally and conn_or_curs or None)
示例7: register_cidr
def register_cidr(oid=None, conn_or_curs=None):
"""Create the CIDR type and an Cidr adapter.
:param oid: oid for the PostgreSQL :sql:`cidr` type, or 2-items sequence
with oids of the type and the array. If not specified, use PostgreSQL
standard oids.
:param conn_or_curs: where to register the typecaster. If not specified,
register it globally.
"""
if not oid:
oid1 = 650
oid2 = 651
elif isinstance(oid, (list, tuple)):
oid1, oid2 = oid
else:
oid1 = oid
oid2 = 651
_ext.CIDR = _ext.new_type((oid1, ), "CIDR",
lambda data, cursor: data and Cidr(data) or None)
_ext.CIDRARRAY = _ext.new_array_type((oid2, ), "CIDRARRAY", _ext.CIDR)
_ext.register_type(_ext.CIDR, conn_or_curs)
_ext.register_type(_ext.CIDRARRAY, conn_or_curs)
return _ext.CIDR
示例8: register_uuid
def register_uuid(oids=None, conn_or_curs=None):
"""Create the UUID type and an uuid.UUID adapter.
:param oids: oid for the PostgreSQL :sql:`uuid` type, or 2-items sequence
with oids of the type and the array. If not specified, use PostgreSQL
standard oids.
:param conn_or_curs: where to register the typecaster. If not specified,
register it globally.
"""
import uuid
if not oids:
oid1 = 2950
oid2 = 2951
elif isinstance(oids, (list, tuple)):
oid1, oid2 = oids
else:
oid1 = oids
oid2 = 2951
_ext.UUID = _ext.new_type((oid1, ), "UUID",
lambda data, cursor: data and uuid.UUID(data) or None)
_ext.UUIDARRAY = _ext.new_array_type((oid2,), "UUID[]", _ext.UUID)
_ext.register_type(_ext.UUID, conn_or_curs)
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
_ext.register_adapter(uuid.UUID, UUID_adapter)
return _ext.UUID
示例9: register_tstz_w_secs
def register_tstz_w_secs(oids=None, conn_or_curs=None):
"""Register alternate type caster for TIMESTAMP WITH TIME ZONE.
The Python datetime module cannot handle time zones with
seconds in the UTC offset. There are, however, historical
"time zones" which contain such offsets, eg. "Asia/Calcutta".
In many cases those offsets represent true local time.
If you encounter "unable to parse time" on a perfectly valid
timestamp you likely want to try this type caster. It truncates
the seconds from the time zone data and retries casting
the timestamp. Note that this will generate timestamps
which are INACCURATE by the number of seconds truncated
(unless the seconds were 00).
<oids>
which OIDs to use this type caster for,
defaults to TIMESTAMP WITH TIME ZONE
<conn_or_curs>
a cursor or connection if you want to attach
this type caster to that only, defaults to
None meaning all connections and cursors
"""
if oids is None:
oids = (1184,) # hardcoded from PostgreSQL headers
_ext.TSTZ_W_SECS = _ext.new_type(oids, "TSTZ_W_SECS", _convert_tstz_w_secs)
_ext.register_type(TSTZ_W_SECS, conn_or_curs)
return _ext.TSTZ_W_SECS
示例10: register_inet
def register_inet(oid=None, conn_or_curs=None):
"""Create the INET type and an Inet adapter.
:param oid: oid for the PostgreSQL :sql:`inet` type, or 2-items sequence
with oids of the type and the array. If not specified, use PostgreSQL
standard oids.
:param conn_or_curs: where to register the typecaster. If not specified,
register it globally.
"""
import warnings
warnings.warn(
"the inet adapter is deprecated, it's not very useful",
DeprecationWarning)
if not oid:
oid1 = 869
oid2 = 1041
elif isinstance(oid, (list, tuple)):
oid1, oid2 = oid
else:
oid1 = oid
oid2 = 1041
_ext.INET = _ext.new_type((oid1, ), "INET",
lambda data, cursor: data and Inet(data) or None)
_ext.INETARRAY = _ext.new_array_type((oid2, ), "INETARRAY", _ext.INET)
_ext.register_type(_ext.INET, conn_or_curs)
_ext.register_type(_ext.INETARRAY, conn_or_curs)
return _ext.INET
示例11: __init__
def __init__(self, name, oid, attrs):
self.name = name
self.oid = oid
self.attnames = [ a[0] for a in attrs ]
self.atttypes = [ a[1] for a in attrs ]
self._create_type(name, self.attnames)
self.typecaster = _ext.new_type((oid,), name, self.parse)
示例12: _register_inet
def _register_inet(oid=None, conn_or_curs=None):
"""Create the INET type and an Inet adapter."""
from psycopg2 import extensions as _ext
if not oid: oid = 869
_ext.INET = _ext.new_type((oid, ), "INET",
lambda data, cursor: data and Inet(data) or None)
_ext.register_type(_ext.INET, conn_or_curs)
return _ext.INET
示例13: register_cast
def register_cast(cls, connection):
cast_function = CAST_MAPPER[cls.type_name()]
cursor = connection.cursor()
cursor.execute(cls.sql_for_oid())
oid = cursor.description[0][1]
cursor.close()
PGTYPE = new_type((oid,), cls.type_name().upper(), cast_function)
register_type(PGTYPE)
示例14: register_range_caster
def register_range_caster(pgrange, pyrange, oid, subtype_oid, array_oid, scope=None):
# Create an adapter for this range type
adapter = partial(cast_raw_range_string, pyrange, subtype_oid=subtype_oid)
# Create types using adapter
range_type = new_type((oid,), pgrange, adapter)
range_array_type = new_array_type((array_oid,), pgrange, range_type)
register_type(range_type, scope)
register_type(range_array_type, scope)
示例15: register_citext_type
def register_citext_type(dbapi_con, connection_record):
def cast_citext(in_str, cursor):
if in_str == None:
return None
return unicode(in_str, cursor.connection.encoding)
with closing(dbapi_con.cursor()) as c:
c.execute(b"SELECT pg_type.oid FROM pg_type WHERE typname = 'citext'")
citext_oid = c.fetchone()
if citext_oid != None:
citext_type = new_type(citext_oid, b"CITEXT", cast_citext)
register_type(citext_type)