Re: psycopg2 adaptation for UUIDs

Brian Sutherland <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Fri, Sep 19, 2008 at 09:27:47PM +0200, Federico Di Gregorio wrote:
> Il giorno mar, 16/09/2008 alle 10.51 +0200, Brian Sutherland ha scritto:
> > Seeing as both PostgreSQL 8.3 and python 2.5 support the UUID data type,
> > I thought it might be a good feature if psycopg2 supported them natively
> > as well. (don't know what project policy is on this)
> > 
> > This is the code I am using to be able to put UUID object into and get
> > them out of PostgreSQL:
> 
> Starting from your code I added UUID support in psycopg2 (pushed to bzr
> trunk right now). Just:
> 
> import psycopg2.extras
> psycopg2.extras.register_uuid()
> 
> Note that register_uuid() takes an optional OID parameter for
> non-standard installations where the OID value of the PosgreSQL uuid
> type is not 2950. register_uuid() will raise an exception if Python uuid
> module is not available.

Thanks a lot!

Er, but also, I found a bug in my code (that ended up in your patch).
Below is a patch showing it and my solution (which is probably not
exactly correct).


=== modified file 'lib/extras.py'
--- lib/extras.py	2008-09-19 19:25:16 +0000
+++ lib/extras.py	2008-09-22 12:12:09 +0000
@@ -306,7 +306,7 @@
         """Create the UUID type and an uuid.UUID adapter."""
         if not oid: oid = 2950
         _ext.UUID = _ext.new_type((oid, ), "UUID",
-                                   lambda data, cursor: uuid.UUID(data))
+                                   lambda data, cursor: data and uuid.UUID(data) or None)
         _ext.register_type(_ext.UUID)
         _ext.register_adapter(uuid.UUID, UUID_adapter)
         return _ext.UUID
@@ -321,4 +321,4 @@
         raise e
 
 
-__all__ = [ k for k in locals().keys() if not k.startswith('_') ]
\ No newline at end of file
+__all__ = [ k for k in locals().keys() if not k.startswith('_') ]

=== modified file 'tests/types_extras.py'
--- tests/types_extras.py	2008-09-19 19:25:16 +0000
+++ tests/types_extras.py	2008-09-22 12:10:53 +0000
@@ -45,6 +45,9 @@
         u = uuid.UUID('9c6d5a77-7256-457e-9461-347b4358e350');
         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)
 
 def test_suite():
     return unittest.TestLoader().loadTestsFromName(__name__)


> 
> federico
> 
> -- 
> Federico Di Gregorio                         http://people.initd.org/fog
> Debian GNU/Linux Developer                                [email protected]
> INIT.D Developer                                           fog-NGVKUo/i/[email protected]
>        Se sai che hai un ***** di file così, lo manovri subito. -- vodka



> _______________________________________________
> Psycopg mailing list
> Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
> http://lists.initd.org/mailman/listinfo/psycopg


-- 
Brian Sutherland
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.