mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-04 19:53:04 +03:00
Added support for UUID arrays
This commit is contained in:
parent
7022269b3d
commit
390a9c2451
|
@ -1,5 +1,7 @@
|
||||||
2009-10-04 Federico Di Gregorio <fog@initd.org>
|
2009-10-04 Federico Di Gregorio <fog@initd.org>
|
||||||
|
|
||||||
|
* lib/extras.py: added support for UUID arrays.
|
||||||
|
|
||||||
* psycopg/connection_int.c: applied patch from Richard Davies to avoid
|
* psycopg/connection_int.c: applied patch from Richard Davies to avoid
|
||||||
deadlocks with multiple threads using the same connection.
|
deadlocks with multiple threads using the same connection.
|
||||||
|
|
||||||
|
|
|
@ -340,13 +340,34 @@ try:
|
||||||
|
|
||||||
__str__ = getquoted
|
__str__ = getquoted
|
||||||
|
|
||||||
def register_uuid(oid=None, conn_or_curs=None):
|
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."""
|
||||||
if not oid: oid = 2950
|
if not oids:
|
||||||
_ext.UUID = _ext.new_type((oid, ), "UUID",
|
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)
|
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.UUID, conn_or_curs)
|
||||||
|
_ext.register_type(_ext.UUIDARRAY, conn_or_curs)
|
||||||
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
_ext.register_adapter(uuid.UUID, UUID_adapter)
|
||||||
|
|
||||||
return _ext.UUID
|
return _ext.UUID
|
||||||
|
|
||||||
except ImportError, e:
|
except ImportError, e:
|
||||||
|
|
|
@ -42,13 +42,33 @@ class TypesExtrasTests(unittest.TestCase):
|
||||||
psycopg2.extras.register_uuid()
|
psycopg2.extras.register_uuid()
|
||||||
except:
|
except:
|
||||||
return
|
return
|
||||||
u = uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350');
|
u = uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350')
|
||||||
s = self.execute("SELECT %s AS foo", (u,))
|
s = self.execute("SELECT %s AS foo", (u,))
|
||||||
self.failUnless(u == s)
|
self.failUnless(u == s)
|
||||||
# must survive NULL cast to a uuid
|
# must survive NULL cast to a uuid
|
||||||
s = self.execute("SELECT NULL::uuid AS foo")
|
s = self.execute("SELECT NULL::uuid AS foo")
|
||||||
self.failUnless(s is None)
|
self.failUnless(s is None)
|
||||||
|
|
||||||
|
def testUUIDARRAY(self):
|
||||||
|
try:
|
||||||
|
import uuid
|
||||||
|
psycopg2.extras.register_uuid()
|
||||||
|
except:
|
||||||
|
return
|
||||||
|
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'), uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e352')]
|
||||||
|
s = self.execute("SELECT %s AS foo", (u,))
|
||||||
|
self.failUnless(u == s)
|
||||||
|
# array with a NULL element
|
||||||
|
u = [uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350'), None]
|
||||||
|
s = self.execute("SELECT %s AS foo", (u,))
|
||||||
|
self.failUnless(u == s)
|
||||||
|
# must survive NULL cast to a uuid[]
|
||||||
|
s = self.execute("SELECT NULL::uuid[] AS foo")
|
||||||
|
self.failUnless(s is None)
|
||||||
|
# what about empty arrays?
|
||||||
|
s = self.execute("SELECT '{}'::uuid[] AS foo")
|
||||||
|
self.failUnless(type(s) == list and len(s) == 0)
|
||||||
|
|
||||||
def testINET(self):
|
def testINET(self):
|
||||||
psycopg2.extras.register_inet()
|
psycopg2.extras.register_inet()
|
||||||
i = "192.168.1.0/24";
|
i = "192.168.1.0/24";
|
||||||
|
|
Loading…
Reference in New Issue
Block a user