psycopg2 adaptation for UUIDs

Brian Sutherland <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Hi,

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:

    try:
        # uuid is included in python2.5
        import uuid
        UUID = psycopg2.extensions.new_type((2950, ), "UUID", lambda s, cursor: uuid.UUID(s))
        psycopg2.extensions.register_type(UUID)
        psycopg2.extensions.register_adapter(uuid.UUID, lambda x: psycopg2.extensions.adapt(str(x)))
    except ImportError:
        pass

-- 
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.