From 0c337a20290a766c13c0c0674c2ab079be5f6768 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 23 Feb 2012 23:56:55 +0000 Subject: [PATCH] Added support for inet array --- NEWS | 1 + doc/src/extras.rst | 3 ++- lib/extras.py | 31 +++++++++++++++++++++++++------ tests/test_types_extras.py | 9 +++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 5149cf74..cfb88d9b 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,7 @@ What's new in psycopg 2.4.5 to Menno Smits (tickets #94, #95). - Fixed 'rownumber' during iteration on cursor subclasses. Regression introduced in 2.4.4 (ticket #100). + - Added support for 'inet' arrays. What's new in psycopg 2.4.4 diff --git a/doc/src/extras.rst b/doc/src/extras.rst index 710e9b5c..1e0f12f2 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -250,6 +250,7 @@ UUID data type ^^^^^^^^^^^^^^^^^^^^^^ .. versionadded:: 2.0.9 +.. versionchanged:: 2.4.5 added inet array support. .. doctest:: @@ -264,7 +265,7 @@ UUID data type '192.168.0.1/24' -.. autofunction:: register_inet() +.. autofunction:: register_inet .. autoclass:: Inet diff --git a/lib/extras.py b/lib/extras.py index 738af7d2..f53561d2 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 diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 156a2159..36dd77ed 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -89,6 +89,15 @@ class TypesExtrasTests(unittest.TestCase): s = self.execute("SELECT NULL::inet AS foo") self.failUnless(s is None) + def testINETARRAY(self): + psycopg2.extras.register_inet() + i = psycopg2.extras.Inet("192.168.1.0/24") + s = self.execute("SELECT %s AS foo", ([i],)) + self.failUnless(i.addr == s[0].addr) + # must survive NULL cast to inet + s = self.execute("SELECT NULL::inet[] AS foo") + self.failUnless(s is None) + def test_inet_conform(self): from psycopg2.extras import Inet i = Inet("192.168.1.0/24")