summaryrefslogtreecommitdiff
path: root/lib/extras.py
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-02-23 23:56:55 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2012-02-23 23:56:55 +0000
commit0c337a20290a766c13c0c0674c2ab079be5f6768 (patch)
tree52cbf714be59c43364061b9b10567a61f246eaaf /lib/extras.py
parent36b6c80ed1b3bcea78b52cd88050a905927c35b3 (diff)
downloadpsycopg2-0c337a20290a766c13c0c0674c2ab079be5f6768.tar.gz
Added support for inet array
Diffstat (limited to 'lib/extras.py')
-rw-r--r--lib/extras.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 738af7d..f53561d 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -498,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'):
@@ -517,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