changeset 4473:fccf7e09af0c

- optimisation for date: if the database provides us with a datetime object, use that for creation of the roundup Date object -- don't convert to string first
author Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
date Mon, 21 Mar 2011 20:49:56 +0000
parents 34dce76bb202
children 9b4cf6c96ee2
files roundup/backends/rdbms_common.py
diffstat 1 files changed, 12 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/backends/rdbms_common.py	Mon Mar 21 20:44:39 2011 +0000
+++ b/roundup/backends/rdbms_common.py	Mon Mar 21 20:49:56 2011 +0000
@@ -52,7 +52,7 @@
 __docformat__ = 'restructuredtext'
 
 # standard python modules
-import sys, os, time, re, errno, weakref, copy, logging
+import sys, os, time, re, errno, weakref, copy, logging, datetime
 
 # roundup modules
 from roundup import hyperdb, date, password, roundupdb, security, support
@@ -62,6 +62,7 @@
 from roundup.support import reversed
 from roundup.i18n import _
 
+
 # support
 from roundup.backends.blobfiles import FileStorage
 try:
@@ -90,6 +91,13 @@
     # assume it's a number returned from the db API
     return int(value)
 
+def date_to_hyperdb_value(d):
+    """ convert date d to a roundup date """
+    if isinstance (d, datetime.datetime):
+        return date.Date(d)
+    return date.Date (str(d).replace(' ', '.'))
+
+
 def connection_dict(config, dbnamestr=None):
     """ Used by Postgresql and MySQL to detemine the keyword args for
     opening the database connection."""
@@ -1039,7 +1047,7 @@
 
     sql_to_hyperdb_value = {
         hyperdb.String : str,
-        hyperdb.Date   : lambda x:date.Date(str(x).replace(' ', '.')),
+        hyperdb.Date   : date_to_hyperdb_value,
 #        hyperdb.Link   : int,      # XXX numeric ids
         hyperdb.Link   : str,
         hyperdb.Interval  : date.Interval,
@@ -2660,6 +2668,7 @@
                 name = p.name
                 assert (name)
                 classes[key][name] = p
+                p.to_hyperdb = self.db.to_hyperdb_value(p.propclass.__class__)
         while True:
             row = cursor.fetchone()
             if not row: break
@@ -2674,8 +2683,7 @@
                 for propname, p in pt.iteritems():
                     value = row[p.sql_idx]
                     if value is not None:
-                        cls = p.propclass.__class__
-                        value = self.db.to_hyperdb_value(cls)(value)
+                        value = p.to_hyperdb(value)
                     node[propname] = value
                 self.db._cache_save(key, node)
             yield str(row[0])

Roundup Issue Tracker: http://roundup-tracker.org/