summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2008-09-19 21:25:16 +0200
committerFederico Di Gregorio <fog@initd.org>2008-09-19 21:25:16 +0200
commitb92601306e9bf7627ee7a19382492fa4bf511d57 (patch)
tree6722a7e880d45e23bf4493d4f749c0fc2b2fdaaa /lib
parent758eaf23d10c434f8dab0167d0bd3f343cafa60f (diff)
downloadpsycopg2-b92601306e9bf7627ee7a19382492fa4bf511d57.tar.gz
Added suppport for UUID and related test.
Diffstat (limited to 'lib')
-rw-r--r--lib/extras.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/lib/extras.py b/lib/extras.py
index 2db28fd..91ab9ab 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -24,7 +24,8 @@ try:
import logging
except:
logging = None
-
+
+from psycopg2 import extensions as _ext
from psycopg2.extensions import cursor as _cursor
from psycopg2.extensions import connection as _connection
from psycopg2.extensions import adapt as _A
@@ -281,3 +282,43 @@ class MinTimeLoggingCursor(LoggingCursor):
self.timestamp = time.time()
return LoggingCursor.execute(self, procname, vars)
+
+# a dbtype and adapter for Python UUID type
+
+try:
+ import uuid
+
+ class UUID_adapter(object):
+ """Adapt Python's uuid.UUID type to PostgreSQL's uuid."""
+
+ def __init__(self, uuid):
+ self._uuid = uuid
+
+ def prepare(self, conn):
+ pass
+
+ def getquoted(self):
+ return "'"+str(self._uuid)+"'::uuid"
+
+ __str__ = getquoted
+
+ def register_uuid(oid=None):
+ """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))
+ _ext.register_type(_ext.UUID)
+ _ext.register_adapter(uuid.UUID, UUID_adapter)
+ return _ext.UUID
+
+except ImportError, e:
+ def register_uuid(oid=None):
+ """Create the UUID type and an uuid.UUID adapter.
+
+ This is a fake function that will always raise an error because the
+ import of the uuid module failed.
+ """
+ raise e
+
+
+__all__ = [ k for k in locals().keys() if not k.startswith('_') ] \ No newline at end of file