Author: cito
Date: Mon Feb  8 11:00:17 2016
New Revision: 839

Log:
Cast to proper type when adapting datetime values

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

Modified:
   trunk/docs/contents/pgdb/types.rst
   trunk/pgdb.py
   trunk/tests/test_dbapi20.py

Modified: trunk/docs/contents/pgdb/types.rst
==============================================================================
--- trunk/docs/contents/pgdb/types.rst  Sun Feb  7 10:50:01 2016        (r838)
+++ trunk/docs/contents/pgdb/types.rst  Mon Feb  8 11:00:17 2016        (r839)
@@ -27,11 +27,11 @@
 
     Construct an object holding a date value
 
-.. function:: Time(hour, minute=0, second=0, microsecond=0)
+.. function:: Time(hour, [minute], [second], [microsecond], [tzinfo])
 
     Construct an object holding a time value
 
-.. function:: Timestamp(year, month, day, hour=0, minute=0, second=0, 
microsecond=0)
+.. function:: Timestamp(year, month, day, [hour], [minute], [second], 
[microsecond], [tzinfo])
 
     Construct an object holding a time stamp value
 

Modified: trunk/pgdb.py
==============================================================================
--- trunk/pgdb.py       Sun Feb  7 10:50:01 2016        (r838)
+++ trunk/pgdb.py       Mon Feb  8 11:00:17 2016        (r839)
@@ -732,8 +732,7 @@
         """Quote value depending on its type."""
         if value is None:
             return 'NULL'
-        if isinstance(value,
-                (datetime, date, time, timedelta, Hstore, Json, UUID)):
+        if isinstance(value, (Hstore, Json, UUID)):
             value = str(value)
         if isinstance(value, basestring):
             if isinstance(value, Binary):
@@ -751,6 +750,18 @@
             return value
         if isinstance(value, (int, long, Decimal, Literal)):
             return value
+        if isinstance(value, datetime):
+            if value.tzinfo:
+                return "'%s'::timestamptz" % value
+            return "'%s'::timestamp" % value
+        if isinstance(value, date):
+            return "'%s'::date" % value
+        if isinstance(value, time):
+            if value.tzinfo:
+                return "'%s'::timetz" % value
+            return "'%s'::time" % value
+        if isinstance(value, timedelta):
+            return "'%s'::interval" % value
         if isinstance(value, list):
             # Quote value as an ARRAY constructor. This is better than using
             # an array literal because it carries the information that this is
@@ -1532,14 +1543,15 @@
     return date(year, month, day)
 
 
-def Time(hour, minute=0, second=0, microsecond=0):
+def Time(hour, minute=0, second=0, microsecond=0, tzinfo=None):
     """Construct an object holding a time value."""
-    return time(hour, minute, second, microsecond)
+    return time(hour, minute, second, microsecond, tzinfo)
 
 
-def Timestamp(year, month, day, hour=0, minute=0, second=0, microsecond=0):
+def Timestamp(year, month, day, hour=0, minute=0, second=0, microsecond=0,
+        tzinfo=None):
     """Construct an object holding a time stamp value."""
-    return datetime(year, month, day, hour, minute, second, microsecond)
+    return datetime(year, month, day, hour, minute, second, microsecond, 
tzinfo)
 
 
 def DateFromTicks(ticks):

Modified: trunk/tests/test_dbapi20.py
==============================================================================
--- trunk/tests/test_dbapi20.py Sun Feb  7 10:50:01 2016        (r838)
+++ trunk/tests/test_dbapi20.py Mon Feb  8 11:00:17 2016        (r839)
@@ -557,6 +557,10 @@
                 for datestyle in ('iso', 'postgres, mdy', 'postgres, dmy',
                         'sql, mdy', 'sql, dmy', 'german'):
                     cur.execute("set datestyle to %s" % datestyle)
+                    if n != 1:
+                        cur.execute("select %s,%s,%s,%s,%s", params)
+                        row = cur.fetchone()
+                        self.assertEqual(row, tuple(values))
                     cur.execute("insert into %s"
                         " values (%%s,%%s,%%s,%%s,%%s)" % table, params)
                     cur.execute("select * from %s" % table)
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to