Changeset 839 for trunk/pgdb.py


Ignore:
Timestamp:
Feb 8, 2016, 11:00:17 AM (4 years ago)
Author:
cito
Message:

Cast to proper type when adapting datetime values

Otherwise PostgreSQL needs to guess the type from the context
which is not always possible.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgdb.py

    r824 r839  
    733733        if value is None:
    734734            return 'NULL'
    735         if isinstance(value,
    736                 (datetime, date, time, timedelta, Hstore, Json, UUID)):
     735        if isinstance(value, (Hstore, Json, UUID)):
    737736            value = str(value)
    738737        if isinstance(value, basestring):
     
    752751        if isinstance(value, (int, long, Decimal, Literal)):
    753752            return value
     753        if isinstance(value, datetime):
     754            if value.tzinfo:
     755                return "'%s'::timestamptz" % value
     756            return "'%s'::timestamp" % value
     757        if isinstance(value, date):
     758            return "'%s'::date" % value
     759        if isinstance(value, time):
     760            if value.tzinfo:
     761                return "'%s'::timetz" % value
     762            return "'%s'::time" % value
     763        if isinstance(value, timedelta):
     764            return "'%s'::interval" % value
    754765        if isinstance(value, list):
    755766            # Quote value as an ARRAY constructor. This is better than using
     
    15331544
    15341545
    1535 def Time(hour, minute=0, second=0, microsecond=0):
     1546def Time(hour, minute=0, second=0, microsecond=0, tzinfo=None):
    15361547    """Construct an object holding a time value."""
    1537     return time(hour, minute, second, microsecond)
    1538 
    1539 
    1540 def Timestamp(year, month, day, hour=0, minute=0, second=0, microsecond=0):
     1548    return time(hour, minute, second, microsecond, tzinfo)
     1549
     1550
     1551def Timestamp(year, month, day, hour=0, minute=0, second=0, microsecond=0,
     1552        tzinfo=None):
    15411553    """Construct an object holding a time stamp value."""
    1542     return datetime(year, month, day, hour, minute, second, microsecond)
     1554    return datetime(year, month, day, hour, minute, second, microsecond, tzinfo)
    15431555
    15441556
Note: See TracChangeset for help on using the changeset viewer.