Author: cito
Date: Mon Feb  8 15:05:37 2016
New Revision: 841

Log:
Add type helper for UUIDs

Modified:
   trunk/docs/contents/changelog.rst
   trunk/docs/contents/pgdb/types.rst
   trunk/pgdb.py
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/changelog.rst
==============================================================================
--- trunk/docs/contents/changelog.rst   Mon Feb  8 12:32:50 2016        (r840)
+++ trunk/docs/contents/changelog.rst   Mon Feb  8 15:05:37 2016        (r841)
@@ -91,7 +91,7 @@
       composite type, it is passed to Python as a named tuple. PyGreSQL uses
       a new fast built-in parser to achieve this. Anonymous composite types are
       also supported, but yield only an ordinary tuple containing text strings.
-    - A new type helper Interval() has been added.
+    - New type helpers Interval() and Uuid() have been added.
 - Changes concerning both modules:
     - PyGreSQL now tries to raise more specific and appropriate subclasses of
       DatabaseError than just ProgrammingError. Particularly, when database

Modified: trunk/docs/contents/pgdb/types.rst
==============================================================================
--- trunk/docs/contents/pgdb/types.rst  Mon Feb  8 12:32:50 2016        (r840)
+++ trunk/docs/contents/pgdb/types.rst  Mon Feb  8 15:05:37 2016        (r841)
@@ -60,6 +60,12 @@
 
 .. versionadded:: 5.0
 
+.. function:: Uuid([hex], [bytes], [bytes_le], [fields], [int], [version])
+
+    Construct an object holding a UUID value
+
+.. versionadded:: 5.0
+
 .. function:: Hstore(dict)
 
     Construct a wrapper for holding an hstore dictionary
@@ -174,6 +180,10 @@
 
     Used to describe date and time ``interval`` columns
 
+.. object:: UUID
+
+    Used to describe ``uuid`` columns
+
 .. object:: HSTORE
 
     Used to describe ``hstore`` columns

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Mon Feb  8 12:32:50 2016        (r840)
+++ trunk/pgdb.py       Mon Feb  8 15:05:37 2016        (r841)
@@ -72,7 +72,7 @@
 from datetime import date, time, datetime, timedelta
 from time import localtime
 from decimal import Decimal
-from uuid import UUID
+from uuid import UUID as Uuid
 from math import isnan, isinf
 from collections import namedtuple
 from functools import partial
@@ -396,7 +396,7 @@
         'date': cast_date, 'interval': cast_interval,
         'time': cast_time, 'timetz': cast_timetz,
         'timestamp': cast_timestamp, 'timestamptz': cast_timestamptz,
-        'int2vector': cast_int2vector, 'uuid': UUID,
+        'int2vector': cast_int2vector, 'uuid': Uuid,
         'anyarray': cast_array, 'record': cast_record}
 
     connection = None  # will be set in local connection specific instances
@@ -732,7 +732,7 @@
         """Quote value depending on its type."""
         if value is None:
             return 'NULL'
-        if isinstance(value, (Hstore, Json, UUID)):
+        if isinstance(value, (Hstore, Json)):
             value = str(value)
         if isinstance(value, basestring):
             if isinstance(value, Binary):
@@ -762,6 +762,8 @@
             return "'%s'::time" % value
         if isinstance(value, timedelta):
             return "'%s'::interval" % value
+        if isinstance(value, Uuid):
+            return "'%s'::uuid" % value
         if isinstance(value, list):
             # Quote value as an ARRAY constructor. This is better than using
             # an array literal because it carries the information that this is
@@ -1526,6 +1528,7 @@
 TIME = Type('time timetz')
 TIMESTAMP = Type('timestamp timestamptz')
 INTERVAL = Type('interval')
+UUID = Type('uuid')
 HSTORE = Type('hstore')
 JSON = Type('json jsonb')
 
@@ -1583,6 +1586,9 @@
         microseconds=microseconds)
 
 
+Uuid = Uuid  # Construct an object holding a UUID value
+
+
 class Hstore(dict):
     """Wrapper class for marking hstore values."""
 

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Mon Feb  8 12:32:50 2016        (r840)
+++ trunk/tests/test_dbapi20.py Mon Feb  8 15:05:37 2016        (r841)
@@ -29,7 +29,7 @@
         pass
 
 from datetime import date, time, datetime, timedelta
-from uuid import UUID
+from uuid import UUID as Uuid
 
 try:
     from datetime import timezone
@@ -642,7 +642,8 @@
         self.assertEqual(result, d)
 
     def test_uuid(self):
-        d = UUID('{12345678-1234-5678-1234-567812345678}')
+        self.assertIs(Uuid, pgdb.Uuid)
+        d = Uuid('{12345678-1234-5678-1234-567812345678}')
         con = self._connect()
         try:
             cur = con.cursor()
@@ -650,7 +651,7 @@
             result = cur.fetchone()[0]
         finally:
             con.close()
-        self.assertIsInstance(result, UUID)
+        self.assertIsInstance(result, Uuid)
         self.assertEqual(result, d)
 
     def test_insert_array(self):
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to