On 5/21/06, Alex Greif <[EMAIL PROTECTED]> wrote:
> First, does it work when using the date, time and datetime objects
> from Python's builtin datetime module? As I understand it, mx.DateTime
> and the Python datetime module do not have the same API, and I believe
> SQLAlchemy is designed for the Python datetime module rather than for
> mx.DateTime.

on linux using the datetime classes works fine with
t.date = datetime.date(2004,12,30)
t.last_entry = datetime.time(23,55,56,345)
but it does not work on windows. On win it seems to need the mx
classes. So its really weird because on linux only datetime works and
on windows only mx works :(
On Windows I get the following stacktrace:
Traceback (most recent call last):
  File "test2.py", line 33, in ?
    objectstore.flush()
  File "build\bdist.win32\egg\sqlalchemy\mapping\objectstore.py", line
260, in flush
  File "build\bdist.win32\egg\sqlalchemy\mapping\objectstore.py", line
81, in flush
  File "build\bdist.win32\egg\sqlalchemy\mapping\unitofwork.py", line
249, in flush
  File "build\bdist.win32\egg\sqlalchemy\mapping\unitofwork.py", line
374, in execute
  File "build\bdist.win32\egg\sqlalchemy\mapping\unitofwork.py", line
527, in execute
  File "build\bdist.win32\egg\sqlalchemy\mapping\mapper.py", line 616,
in save_obj
  File "build\bdist.win32\egg\sqlalchemy\sql.py", line 473, in execute
  File "build\bdist.win32\egg\sqlalchemy\sql.py", line 378, in execute
  File "build\bdist.win32\egg\sqlalchemy\sql.py", line 355, in execute
  File "build\bdist.win32\egg\sqlalchemy\engine.py", line 645, in
execute_compiled
  File "build\bdist.win32\egg\sqlalchemy\engine.py", line 542, in
_process_defaults
  File "build\bdist.win32\egg\sqlalchemy\sql.py", line 271, in __getitem__
  File "build\bdist.win32\egg\sqlalchemy\sql.py", line 692, in typeprocess
  File "build\bdist.win32\egg\sqlalchemy\databases\postgres.py", line
82, in convert_bind_param
TypeError: argument 1 must be DateTime, not datetime.date

Are you using psycopg1 on Windows and psycopg2 on Linux? That could
account for the behavior you're seeing, where Windows requires
mx.DateTime and Linux requires the builtin datetime module.

I also notice that the datetime-to-mx.DateTime conversion in
databases/postgres.py is incomplete. It converts DateTime types, but
not Date or Time. Try the attached patch and see if it solves your
problem on Windows. Note that I haven't yet written a test case for
it, so if it fails, please tell me. (Or rather, tell the
sqlalchemy-users list).

--
Robin Munn
[EMAIL PROTECTED]
GPG key 0xD6497014
=== postgres.py
==================================================================
--- postgres.py	(revision 1453)
+++ postgres.py	(local)
@@ -76,15 +76,17 @@
         return "DATE"
 class PG1Date(sqltypes.Date):
     def convert_bind_param(self, value, engine):
-        # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime
-        # this one doesnt seem to work with the "emulation" mode
         if value is not None:
+            if isinstance(value, datetime.datetime) or isintace(value, datetime.date):
+                mx_date = mxDateTime(value.year, value.month, value.day)
+                return psycopg.DateFromMx(mx_date)
             return psycopg.DateFromMx(value)
         else:
             return None
     def convert_result_value(self, value, engine):
-        # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime
-        return value
+        if value is None:
+            return None
+        return datetime.date(value.year, value.month, value.day)
     def get_col_spec(self):
         return "DATE"
 class PG2Time(sqltypes.Time):
@@ -92,14 +94,25 @@
         return "TIME"
 class PG1Time(sqltypes.Time):
     def convert_bind_param(self, value, engine):
-        # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime
-        # this one doesnt seem to work with the "emulation" mode
         if value is not None:
+            if isinstance(value, datetime.datetime) or isintace(value, datetime.time):
+                seconds = float(str(value.second) + "."
+                                + str(value.microsecond))
+                mx_time = mxDateTime(1, 1, 1,
+                                     value.hour, value.minute,
+                                     seconds)
+                return psycopg.TimeFromMx(mx_time)
             return psycopg.TimeFromMx(value)
         else:
             return None
     def convert_result_value(self, value, engine):
-        # TODO: perform appropriate postgres1 conversion between Python DateTime/MXDateTime
+        if value is None:
+            return None
+        second_parts = str(value.second).split(".")
+        seconds = int(second_parts[0])
+        microseconds = int(second_parts[1])
+        return datetime.time(value.hour, value.minute, seconds,
+                             microseconds)
         return value
     def get_col_spec(self):
         return "TIME"

Reply via email to