本文整理汇总了Python中psycopg2.extensions.new_array_type函数的典型用法代码示例。如果您正苦于以下问题:Python new_array_type函数的具体用法?Python new_array_type怎么用?Python new_array_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了new_array_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _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]
示例2: 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
示例3: 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
示例4: 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
示例5: 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)
示例6: 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
示例7: 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)
示例8: __init__
def __init__(self, pgrange, pyrange, oid, subtype_oid, array_oid=None):
self.subtype_oid = subtype_oid
self._create_ranges(pgrange, pyrange)
name = self.adapter.name or self.adapter.__class__.__name__
self.typecaster = new_type((oid,), name, self.parse)
if array_oid is not None:
self.array_typecaster = new_array_type((array_oid,), name + "ARRAY", self.typecaster)
else:
self.array_typecaster = None
示例9: register_macaddr_type
def register_macaddr_type():
from psycopg2.extensions import register_adapter, new_type, register_type, new_array_type
import psycopg2
oid = get_type_oid("NULL::macaddr")
PGTYPE = new_type((oid,), "macaddr", cast_macaddr)
register_type(PGTYPE)
register_adapter(MacAddr, adapt_macaddr)
mac_array_oid = get_type_oid("'{}'::macaddr[]")
array_of_mac = new_array_type((mac_array_oid, ), 'macaddr', psycopg2.STRING)
psycopg2.extensions.register_type(array_of_mac)
示例10: __init__
def __init__(self, name, oid, attrs, array_oid=None):
self.name = name
self.oid = oid
self.array_oid = array_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 = new_type((oid,), name, self.parse)
if array_oid:
self.array_typecaster = new_array_type(
(array_oid,), "%sARRAY" % name, self.typecaster)
else:
self.array_typecaster = None
示例11: setup_connection
def setup_connection(conn):
# We want to use unicode everywhere
register_type(UNICODE, conn)
register_type(UNICODEARRAY, conn)
conn.set_client_encoding('UTF8')
cur = conn.cursor()
cur.execute("SELECT NULL::numeric")
oid = cur.description[0][1]
NUMERIC = new_type((oid,), "NUMERIC", numeric_converter)
cur.execute("SELECT NULL::numeric[]")
oid = cur.description[0][1]
NUMERICL = new_array_type((oid,), "NUMERIC[]", NUMERIC)
register_type(NUMERIC, conn)
register_type(NUMERICL, conn)
register_adapter(Integer, AsIs)
register_adapter(RealNumber, RealEncoder)
register_adapter(dict, Json)
register_json(conn, loads=Json.loads)
示例12: register_hstore
def register_hstore(conn_or_curs, globally=False, unicode=False,
oid=None, array_oid=None):
from psycopg2.extras import HstoreAdapter
from psycopg2 import extensions as _ext
import psycopg2
import sys
import re as regex
from .fields import HStoreDict
def cast(s, cur, _bsdec=regex.compile(r"\\(.)")):
if sys.version_info[0] < 3 and unicode:
result = HstoreAdapter.parse_unicode(s, cur)
else:
result = HstoreAdapter.parse(s, cur, _bsdec)
return HStoreDict(result)
if oid is None:
oid = HstoreAdapter.get_oids(conn_or_curs)
if oid is None or not oid[0]:
raise psycopg2.ProgrammingError(
"hstore type not found in the database. "
"please install it from your 'contrib/hstore.sql' file")
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])
HSTORE = _ext.new_type(oid, str("HSTORE"), cast)
_ext.register_type(HSTORE, not globally and conn_or_curs or None)
_ext.register_adapter(dict, HstoreAdapter)
if array_oid:
HSTOREARRAY = _ext.new_array_type(array_oid, str("HSTOREARRAY"), HSTORE)
_ext.register_type(HSTOREARRAY, not globally and conn_or_curs or None)
示例13: register_hstore
def register_hstore(conn_or_curs, globally=False, unicode=False,
oid=None, array_oid=None):
r"""Register adapter and typecaster for `!dict`\-\ |hstore| conversions.
:param conn_or_curs: a connection or cursor: the typecaster will be
registered only on this object unless *globally* is set to `!True`
:param globally: register the adapter globally, not only on *conn_or_curs*
:param unicode: if `!True`, keys and values returned from the database
will be `!unicode` instead of `!str`. The option is not available on
Python 3
:param oid: the OID of the |hstore| type if known. If not, it will be
queried on *conn_or_curs*.
:param array_oid: the OID of the |hstore| array type if known. If not, it
will be queried on *conn_or_curs*.
The connection or cursor passed to the function will be used to query the
database and look for the OID of the |hstore| type (which may be different
across databases). If querying is not desirable (e.g. with
:ref:`asynchronous connections <async-support>`) you may specify it in the
*oid* parameter, which can be found using a query such as :sql:`SELECT
'hstore'::regtype::oid`. Analogously you can obtain a value for *array_oid*
using a query such as :sql:`SELECT 'hstore[]'::regtype::oid`.
Note that, when passing a dictionary from Python to the database, both
strings and unicode keys and values are supported. Dictionaries returned
from the database have keys/values according to the *unicode* parameter.
The |hstore| contrib module must be already installed in the database
(executing the ``hstore.sql`` script in your ``contrib`` directory).
Raise `~psycopg2.ProgrammingError` if the type is not found.
"""
if oid is None:
oid = HstoreAdapter.get_oids(conn_or_curs)
if oid is None or not oid[0]:
raise psycopg2.ProgrammingError(
"hstore type not found in the database. "
"please install it from your 'contrib/hstore.sql' file")
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
if _sys.version_info[0] < 3 and unicode:
cast = HstoreAdapter.parse_unicode
else:
cast = HstoreAdapter.parse
HSTORE = _ext.new_type(oid, "HSTORE", cast)
_ext.register_type(HSTORE, not globally and conn_or_curs or None)
_ext.register_adapter(dict, HstoreAdapter)
if array_oid:
HSTOREARRAY = _ext.new_array_type(array_oid, "HSTOREARRAY", HSTORE)
_ext.register_type(HSTOREARRAY, not globally and conn_or_curs or None)
示例14: cast_interface
except AttributeError:
from psycopg2.extensions import new_type, new_array_type, register_type, \
register_adapter, QuotedString
def cast_interface(s, cur=None):
if s is None:
return None
return ipaddress.ip_interface(unicode(s))
def cast_ipnetwork(s, cur=None):
if s is None:
return None
return ipaddress.ip_network(unicode(s))
inet = new_type((869,), b'INET', cast_interface)
ainet = new_array_type((1041,), b'INET[]', inet)
cidr = new_type((650,), b'CIDR', cast_ipnetwork)
acidr = new_array_type((651,), b'CIDR[]', cidr)
for c in [inet, ainet, cidr, acidr]:
register_type(c, None)
def adapt_ipaddress(obj):
return QuotedString(str(obj))
for t in [ipaddress.IPv4Interface, ipaddress.IPv6Interface,
ipaddress.IPv4Network, ipaddress.IPv6Network]:
register_adapter(t, adapt_ipaddress)
示例15: __init__
def __init__(self, oid = 2249, array_oid = 2287):
self.oid = oid
self.array_oid = array_oid
self.typecaster = new_type((oid,), "RECORD", self.parse)
self.array_typecaster = new_array_type((array_oid,), "RECORD[]", self.typecaster)