Changeset 841 for trunk


Ignore:
Timestamp:
Feb 8, 2016, 3:05:37 PM (4 years ago)
Author:
cito
Message:

Add type helper for UUIDs

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r823 r841  
    9292      a new fast built-in parser to achieve this. Anonymous composite types are
    9393      also supported, but yield only an ordinary tuple containing text strings.
    94     - A new type helper Interval() has been added.
     94    - New type helpers Interval() and Uuid() have been added.
    9595- Changes concerning both modules:
    9696    - PyGreSQL now tries to raise more specific and appropriate subclasses of
  • trunk/docs/contents/pgdb/types.rst

    r839 r841  
    6161.. versionadded:: 5.0
    6262
     63.. function:: Uuid([hex], [bytes], [bytes_le], [fields], [int], [version])
     64
     65    Construct an object holding a UUID value
     66
     67.. versionadded:: 5.0
     68
    6369.. function:: Hstore(dict)
    6470
     
    174180
    175181    Used to describe date and time ``interval`` columns
     182
     183.. object:: UUID
     184
     185    Used to describe ``uuid`` columns
    176186
    177187.. object:: HSTORE
  • trunk/pgdb.py

    r840 r841  
    7373from time import localtime
    7474from decimal import Decimal
    75 from uuid import UUID
     75from uuid import UUID as Uuid
    7676from math import isnan, isinf
    7777from collections import namedtuple
     
    397397        'time': cast_time, 'timetz': cast_timetz,
    398398        'timestamp': cast_timestamp, 'timestamptz': cast_timestamptz,
    399         'int2vector': cast_int2vector, 'uuid': UUID,
     399        'int2vector': cast_int2vector, 'uuid': Uuid,
    400400        'anyarray': cast_array, 'record': cast_record}
    401401
     
    733733        if value is None:
    734734            return 'NULL'
    735         if isinstance(value, (Hstore, Json, UUID)):
     735        if isinstance(value, (Hstore, Json)):
    736736            value = str(value)
    737737        if isinstance(value, basestring):
     
    763763        if isinstance(value, timedelta):
    764764            return "'%s'::interval" % value
     765        if isinstance(value, Uuid):
     766            return "'%s'::uuid" % value
    765767        if isinstance(value, list):
    766768            # Quote value as an ARRAY constructor. This is better than using
     
    15271529TIMESTAMP = Type('timestamp timestamptz')
    15281530INTERVAL = Type('interval')
     1531UUID = Type('uuid')
    15291532HSTORE = Type('hstore')
    15301533JSON = Type('json jsonb')
     
    15821585    return timedelta(days, hours=hours, minutes=minutes, seconds=seconds,
    15831586        microseconds=microseconds)
     1587
     1588
     1589Uuid = Uuid  # Construct an object holding a UUID value
    15841590
    15851591
  • trunk/tests/test_dbapi20.py

    r840 r841  
    3030
    3131from datetime import date, time, datetime, timedelta
    32 from uuid import UUID
     32from uuid import UUID as Uuid
    3333
    3434try:
     
    643643
    644644    def test_uuid(self):
    645         d = UUID('{12345678-1234-5678-1234-567812345678}')
     645        self.assertIs(Uuid, pgdb.Uuid)
     646        d = Uuid('{12345678-1234-5678-1234-567812345678}')
    646647        con = self._connect()
    647648        try:
     
    651652        finally:
    652653            con.close()
    653         self.assertIsInstance(result, UUID)
     654        self.assertIsInstance(result, Uuid)
    654655        self.assertEqual(result, d)
    655656
Note: See TracChangeset for help on using the changeset viewer.