Author: cito
Date: Fri Feb 5 11:13:54 2016
New Revision: 821
Log:
Support the uuid data type
This is often useful and also supported by SQLAlchemy
Modified:
trunk/docs/contents/changelog.rst
trunk/docs/contents/pg/adaptation.rst
trunk/docs/contents/pgdb/adaptation.rst
trunk/pg.py
trunk/pgdb.py
trunk/tests/test_classic_dbwrapper.py
trunk/tests/test_dbapi20.py
Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst Fri Feb 5 09:04:31 2016 (r820)
+++ trunk/docs/contents/changelog.rst Fri Feb 5 11:13:54 2016 (r821)
@@ -104,6 +104,8 @@
library. In earlier versions of PyGreSQL they had been returned as
strings. You can restore the old behavior by deactivating the respective
typecast functions, e.g. set_typecast('date', None).
+ - PyGreSQL now support the "uuid" data type, converting such columns
+ automatically to and from Python uuid.UUID objects.
- PyGreSQL now supports the "hstore" data type, converting such columns
automatically to and from Python dictionaries. If you want to insert
Python objects as JSON data using DB-API 2, you should wrap them in the
Modified: trunk/docs/contents/pg/adaptation.rst
==============================================================================
--- trunk/docs/contents/pg/adaptation.rst Fri Feb 5 09:04:31 2016
(r820)
+++ trunk/docs/contents/pg/adaptation.rst Fri Feb 5 11:13:54 2016
(r821)
@@ -36,6 +36,7 @@
interval datetime.timedelta
hstore dict
json, jsonb list or dict
+uuid uuid.UUID
array list
record tuple
================================== ==================
Modified: trunk/docs/contents/pgdb/adaptation.rst
==============================================================================
--- trunk/docs/contents/pgdb/adaptation.rst Fri Feb 5 09:04:31 2016
(r820)
+++ trunk/docs/contents/pgdb/adaptation.rst Fri Feb 5 11:13:54 2016
(r821)
@@ -36,6 +36,7 @@
interval datetime.timedelta
hstore dict
json, jsonb list or dict
+uuid uuid.UUID
array list
record tuple
================================== ==================
Modified: trunk/pg.py
==============================================================================
--- trunk/pg.py Fri Feb 5 09:04:31 2016 (r820)
+++ trunk/pg.py Fri Feb 5 11:13:54 2016 (r821)
@@ -42,6 +42,7 @@
from functools import partial
from re import compile as regex
from json import loads as jsondecode, dumps as jsonencode
+from uuid import UUID
try:
long
@@ -179,9 +180,8 @@
' abstime reltime', # these are very old
'float': 'float4 float8',
'int': 'cid int2 int4 int8 oid xid',
- 'hstore': 'hstore', 'json': 'json jsonb',
- 'num': 'numeric',
- 'money': 'money',
+ 'hstore': 'hstore', 'json': 'json jsonb', 'uuid': 'uuid',
+ 'num': 'numeric', 'money': 'money',
'text': 'bpchar char name text varchar'}
def __init__(self):
@@ -863,7 +863,7 @@
'date': cast_date, 'interval': cast_interval,
'time': cast_time, 'timetz': cast_timetz,
'timestamp': cast_timestamp, 'timestamptz': cast_timestamptz,
- 'int2vector': cast_int2vector,
+ 'int2vector': cast_int2vector, 'uuid': UUID,
'anyarray': cast_array, 'record': cast_record}
connection = None # will be set in a connection specific instance
Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py Fri Feb 5 09:04:31 2016 (r820)
+++ trunk/pgdb.py Fri Feb 5 11:13:54 2016 (r821)
@@ -70,6 +70,7 @@
from datetime import date, time, datetime, timedelta
from time import localtime
from decimal import Decimal
+from uuid import UUID
from math import isnan, isinf
from collections import namedtuple
from functools import partial
@@ -393,7 +394,7 @@
'date': cast_date, 'interval': cast_interval,
'time': cast_time, 'timetz': cast_timetz,
'timestamp': cast_timestamp, 'timestamptz': cast_timestamptz,
- 'int2vector': cast_int2vector,
+ 'int2vector': cast_int2vector, 'uuid': UUID,
'anyarray': cast_array, 'record': cast_record}
connection = None # will be set in local connection specific instances
@@ -729,7 +730,8 @@
"""Quote value depending on its type."""
if value is None:
return 'NULL'
- if isinstance(value, (datetime, date, time, timedelta, Hstore, Json)):
+ if isinstance(value,
+ (datetime, date, time, timedelta, Hstore, Json, UUID)):
value = str(value)
if isinstance(value, basestring):
if isinstance(value, Binary):
Modified: trunk/tests/test_classic_dbwrapper.py
==============================================================================
--- trunk/tests/test_classic_dbwrapper.py Fri Feb 5 09:04:31 2016
(r820)
+++ trunk/tests/test_classic_dbwrapper.py Fri Feb 5 11:13:54 2016
(r821)
@@ -24,6 +24,7 @@
from decimal import Decimal
from datetime import date, time, datetime, timedelta
+from uuid import UUID
from time import strftime
from operator import itemgetter
@@ -3702,6 +3703,13 @@
self.assertIsInstance(r, dict)
self.assertEqual(r, d)
+ def testUuid(self):
+ d = UUID('{12345678-1234-5678-1234-567812345678}')
+ q = 'select $1::uuid'
+ r = self.db.query(q, (d,)).getresult()[0][0]
+ self.assertIsInstance(r, UUID)
+ self.assertEqual(r, d)
+
def testDbTypesInfo(self):
dbtypes = self.db.dbtypes
self.assertIsInstance(dbtypes, dict)
Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Fri Feb 5 09:04:31 2016 (r820)
+++ trunk/tests/test_dbapi20.py Fri Feb 5 11:13:54 2016 (r821)
@@ -29,6 +29,7 @@
pass
from datetime import date, time, datetime, timedelta
+from uuid import UUID
try:
from datetime import timezone
@@ -616,6 +617,18 @@
self.assertIsInstance(result, dict)
self.assertEqual(result, d)
+ def test_uuid(self):
+ d = UUID('{12345678-1234-5678-1234-567812345678}')
+ con = self._connect()
+ try:
+ cur = con.cursor()
+ cur.execute("select %s::uuid", (d,))
+ result = cur.fetchone()[0]
+ finally:
+ con.close()
+ self.assertIsInstance(result, UUID)
+ self.assertEqual(result, d)
+
def test_insert_array(self):
values = [(None, None), ([], []), ([None], [[None], ['null']]),
([1, 2, 3], [['a', 'b'], ['c', 'd']]),
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql