Changeset 839 for trunk


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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/pgdb/types.rst

    r819 r839  
    2828    Construct an object holding a date value
    2929
    30 .. function:: Time(hour, minute=0, second=0, microsecond=0)
     30.. function:: Time(hour, [minute], [second], [microsecond], [tzinfo])
    3131
    3232    Construct an object holding a time value
    3333
    34 .. function:: Timestamp(year, month, day, hour=0, minute=0, second=0, microsecond=0)
     34.. function:: Timestamp(year, month, day, [hour], [minute], [second], [microsecond], [tzinfo])
    3535
    3636    Construct an object holding a time stamp value
  • 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
  • trunk/tests/test_dbapi20.py

    r823 r839  
    558558                        'sql, mdy', 'sql, dmy', 'german'):
    559559                    cur.execute("set datestyle to %s" % datestyle)
     560                    if n != 1:
     561                        cur.execute("select %s,%s,%s,%s,%s", params)
     562                        row = cur.fetchone()
     563                        self.assertEqual(row, tuple(values))
    560564                    cur.execute("insert into %s"
    561565                        " values (%%s,%%s,%%s,%%s,%%s)" % table, params)
Note: See TracChangeset for help on using the changeset viewer.