diff options
author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2012-02-24 00:35:21 +0000 |
---|---|---|
committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2012-02-24 00:35:21 +0000 |
commit | b2c61eaa1895b06b717600c8de16d79e71dabf97 (patch) | |
tree | 3a60f45b9e949928231f568f13f9dddc6b54a6fa /lib/extras.py | |
parent | 98d6d96ee30bd54311f1644faf1b625602092606 (diff) | |
parent | a165f861270994da0435e42e568f8e4327bd8251 (diff) | |
download | psycopg2-b2c61eaa1895b06b717600c8de16d79e71dabf97.tar.gz |
Merge branch 'types-arrays' into devel
Diffstat (limited to 'lib/extras.py')
-rw-r--r-- | lib/extras.py | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/lib/extras.py b/lib/extras.py index 870b5ca..f53561d 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -455,14 +455,21 @@ class UUID_adapter(object): __str__ = getquoted def register_uuid(oids=None, conn_or_curs=None): - """Create the UUID type and an uuid.UUID adapter.""" + """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 type(oids) == list: + elif isinstance(oids, (list, tuple)): oid1, oid2 = oids else: oid1 = oids @@ -491,13 +498,13 @@ class Inet(object): """ def __init__(self, addr): self.addr = addr - + def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.addr) def prepare(self, conn): self._conn = conn - + def getquoted(self): obj = _A(self.addr) if hasattr(obj, 'prepare'): @@ -510,13 +517,32 @@ class Inet(object): def __str__(self): return str(self.addr) - + 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", + """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. + """ + 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 |