commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7673 - in trunk/gnue-common: src/datasources src/datasources/dri


From: johannes
Subject: [gnue] r7673 - in trunk/gnue-common: src/datasources src/datasources/drivers/DBSIG2 src/datasources/drivers/sql/maxdb src/datasources/drivers/sql/mysql tests
Date: Sat, 2 Jul 2005 11:51:58 -0500 (CDT)

Author: johannes
Date: 2005-07-02 11:51:57 -0500 (Sat, 02 Jul 2005)
New Revision: 7673

Modified:
   trunk/gnue-common/src/datasources/GConditions.py
   trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
   trunk/gnue-common/src/datasources/drivers/sql/maxdb/maxdb.py
   trunk/gnue-common/src/datasources/drivers/sql/mysql/mysqldb.py
   trunk/gnue-common/src/datasources/readgsd.py
   trunk/gnue-common/tests/datasources_GConditions.py
Log:
Added support of python2.3+'s datetime library


Modified: trunk/gnue-common/src/datasources/GConditions.py
===================================================================
--- trunk/gnue-common/src/datasources/GConditions.py    2005-07-02 16:00:14 UTC 
(rev 7672)
+++ trunk/gnue-common/src/datasources/GConditions.py    2005-07-02 16:51:57 UTC 
(rev 7673)
@@ -24,6 +24,7 @@
 import re
 import sys
 import mx.DateTime
+import datetime
 
 from gnue.common.apps import errors
 from gnue.common.definitions import GObjects
@@ -1506,6 +1507,9 @@
   v1 = values [0]
   v2 = values [1]
 
+  print "V1:", repr (v1)
+  print "V2:", repr (v2)
+
   if v1 is None or v2 is None:
     result.append (None)
     values.remove (None)
@@ -1557,11 +1561,29 @@
   
       # String to DateTime
       elif isinstance (chkValue, mx.DateTime.DateTimeType) or \
-           isinstance (chkValue, mx.DateTime.DateTimeDeltaType):
+           isinstance (chkValue, mx.DateTime.DateTimeDeltaType) or \
+           isinstance (chkValue, datetime.datetime) or \
+           isinstance (chkValue, datetime.time) or \
+           isinstance (chkValue, datetime.date):
   
         try:
-          newValue = mx.DateTime.Parser.DateTimeFromString (oldValue)
+          new = mx.DateTime.Parser.DateTimeFromString (oldValue)
 
+          if isinstance (chkValue, datetime.time):
+            micro = int (("%s" % (new.second - int (new.second))) [2:8])
+            newValue = datetime.time (new.hour, new.minute, new.second, micro)
+
+          elif isinstance (chkValue, datetime.date):
+            newValue = datetime.date (new.year, new.month, new.day)
+
+          elif isinstance (chkValue, datetime.datetime):
+            micro = int (("%s" % (new.second - int (new.second))) [2:8])
+            newValue = datetime.datetime (new.year, new.month, new.day,
+                new.hour, new.minute, new.second, micro)
+
+          else:
+            newValue = new
+
         except ValueError:
           raise ConversionError, (oldValue, chkValue)
  
@@ -1628,6 +1650,54 @@
       else:
         raise ConversionRuleError, (oldValue, chkValue)
   
+    elif isinstance (v1, datetime.datetime) or \
+         isinstance (v2, datetime.datetime):
+
+      if isinstance (v1, datetime.datetime):
+        oldValue = v1
+        chkValue = v2
+      else:
+        oldValue = v2
+        chkValue = v1
+
+      if isinstance (chkValue, datetime.date):
+        newValue = oldValue.date ()
+
+      elif isinstance (chkValue, mx.DateTime.DateTimeType):
+        newValue = datetime.date (oldValue.year, oldValue.month, oldValue.day)
+
+    elif isinstance (v1, mx.DateTime.DateTimeType) or \
+         isinstance (v2, mx.DateTime.DateTimeType):
+      if isinstance (v1, mx.DateTime.DateTimeType):
+        oldValue = v1
+        chkValue = v2
+      else:
+        oldValue = v2
+        chkValue = v1
+
+      if isinstance (chkValue, datetime.date):
+        newValue = datetime.date (oldValue.year, oldValue.month, oldValue.day)
+
+      elif isinstance (chkValue, datetime.datetime):
+        micro = int (("%s" % (oldValue.second - int (oldValue.second))) [2:8])
+        newValue = datetime.datetime (oldValue.year, oldValue.month,
+            oldValue.day, oldValue.hour, oldValue.minute, int 
(oldValue.second),
+            micro)
+
+    elif isinstance (v1, mx.DateTime.DateTimeDeltaType) or \
+         isinstance (v2, mx.DateTime.DateTimeDeltaType):
+      if isinstance (v1, mx.DateTime.DateTimeDeltaType):
+        oldValue = v1
+        chkValue = v2
+      else:
+        oldValue = v2
+        chkValue = v1
+
+      if isinstance (chkValue, datetime.time):
+        micro = int (("%s" % (oldValue.second - int (oldValue.second))) [2:8])
+        newValue = datetime.time (oldValue.hour, oldValue.minute,
+                                  int (oldValue.second), micro)
+
     else:
       raise ConversionRuleError, (v1, v2)
   

Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2005-07-02 16:00:14 UTC (rev 7672)
+++ trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py      
2005-07-02 16:51:57 UTC (rev 7673)
@@ -25,6 +25,7 @@
 
 import sys
 import mx.DateTime
+import datetime
 
 from gnue.common.apps import errors, GDebug
 from gnue.common.datasources import Exceptions
@@ -63,13 +64,14 @@
 
   _resultSetClass_ = ResultSet
 
-  _drivername_ = None                   # DBSIG2 compatible driver module
-  _boolean_false_ = False               # value to pass for boolean FALSE
-  _boolean_true_  = True                # value to pass for boolean TRUE
-  _broken_fetchmany_ = False            # Does fetchmany () raise an exception
+  _drivername_        = None            # DBSIG2 compatible driver module
+  _boolean_false_     = False           # value to pass for boolean FALSE
+  _boolean_true_      = True            # value to pass for boolean TRUE
+  _broken_fetchmany_  = False           # Does fetchmany () raise an exception
                                         # when no records are left?
-  _broken_rowcount_ = False             # Is cursor.rowcount unusable?
+  _broken_rowcount_   = False           # Is cursor.rowcount unusable?
   _named_as_sequence_ = False           # Pass 'named' parameters as sequence
+  _std_datetime_      = False
 
 
   # ---------------------------------------------------------------------------
@@ -117,13 +119,40 @@
       else:
         return self._boolean_false_
 
-    elif isinstance (value, mx.DateTime.DateTimeType):
-      # mx.DateTime
-      return self._driver.Timestamp (value.year, value.month, value.day,
-                                     value.hour, value.minute, value.second)
-    elif isinstance (value, mx.DateTime.DateTimeDeltaType):
-      return self._driver.Timestamp (1, 1, 1,
-                                     value.hour, value.minute, value.second)
+    elif isinstance (value, mx.DateTime.DateTimeType) or \
+         isinstance (value, datetime.datetime):
+
+      second = value.second
+      flsecs = hasattr (value, 'microsecond') and value.microsecond or 0
+
+      if isinstance (second, float):
+        flsecs = int (("%s" % (second - int (second))) [2:])
+        second = int (second)
+
+      if self._std_datetime_:
+        flsecs = int (("%s" % flsecs) [:6])
+
+      return self._createTimestamp_ (value.year, value.month, value.day,
+                                      value.hour, value.minute, second, flsecs)
+
+    elif isinstance (value, mx.DateTime.DateTimeDeltaType) or \
+         isinstance (value, datetime.time):
+
+      second = value.second
+      flsecs = hasattr (value, 'microsecond') and value.microsecond or 0
+
+      if isinstance (second, float):
+        flsecs = int (("%s" % (second - int (second))) [2:])
+        second = int (second)
+
+      if self._std_datetime_:
+        flsecs = int (("%s" % flsecs) [:6])
+
+      return self._createTime_ (value.hour, value.minute, second, flsecs)
+
+    elif isinstance (value, datetime.date):
+      return self._driver.Date (value.year, value.month, value.day)
+
     else:
       # Strings, Integers
       return value
@@ -496,3 +525,29 @@
     gDebug (3, 'DBSIG2 Close')
     if self._native:
       self._native.close ()
+
+
+  # ---------------------------------------------------------------------------
+
+  def _createTimestamp_ (self, year, month, day, hour, minute, secs, fsec = 0):
+
+    psec = float ("%s.%s" % (secs, fsec))
+
+    if self._std_datetime_:
+      return self._driver.Timestamp (year, month, day, hour, minute, secs, 
fsec)
+
+    else:
+      return self._driver.Timestamp (year, month, day, hour, minute, psec)
+
+
+  # ---------------------------------------------------------------------------
+
+  def _createTime_ (self, hour, minute, second, fsec = 0):
+
+    psec = float ("%s.%s" % (second, fsec))
+
+    if self._std_datetime_:
+      return self._driver.Time (hour, minute, second, fsec)
+
+    else:
+      return self._driver.Time (hour, minute, psec)

Modified: trunk/gnue-common/src/datasources/drivers/sql/maxdb/maxdb.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/maxdb/maxdb.py        
2005-07-02 16:00:14 UTC (rev 7672)
+++ trunk/gnue-common/src/datasources/drivers/sql/maxdb/maxdb.py        
2005-07-02 16:51:57 UTC (rev 7673)
@@ -27,6 +27,8 @@
 
 __all__ = ['Connection']
 
+import datetime
+
 from gnue.common.datasources.drivers import DBSIG2
 from gnue.common.datasources.drivers.sql.maxdb import Behavior
 
@@ -106,6 +108,7 @@
   _behavior_   = Behavior.Behavior
 
   _named_as_sequence_ = True
+  _std_datetime_      = True
 
 
   # ---------------------------------------------------------------------------
@@ -132,3 +135,39 @@
         kwargs [sapdbName] = connectData [gnueName]
 
     return (params, kwargs)
+
+  # ---------------------------------------------------------------------------
+
+  def _connect_ (self, connectData):
+
+    DBSIG2.Connection._connect_ (self, connectData)
+    self._native.typeTranslations = {'Date': self.__toDate,
+                                     'Time': self.__toTime,
+                                     'Timestamp': self.__toDateTime}
+
+  # ---------------------------------------------------------------------------
+
+  def __toDate (self, internal):
+    (year, month, day) = (internal [:4], internal [4:6], internal [6:8])
+    return datetime.date (int (year), int (month), int (day))
+
+  # ---------------------------------------------------------------------------
+
+  def __toTime (self, internal):
+    (hour, minute, second) = map (int, (internal [:4], internal [4:6],
+                                        internal [6:8]))
+    return datetime.time (hour, minute, second)
+
+  # ---------------------------------------------------------------------------
+
+  def __toDateTime (self, ival):
+    (year, month, day, hour, minute, second, micros) = \
+        map (int, (ival [:4], ival [4:6], ival [6:8], ival [8:10], ival 
[10:12],
+                   ival [12:14], ival [14:] or '0'))
+    return datetime.datetime (year, month, day, hour, minute, second, micros)
+
+  # ---------------------------------------------------------------------------
+
+  def _createTime_ (self, hour, minute, second, fsecs = 0):
+
+    return self._driver.Time (hour, minute, second)

Modified: trunk/gnue-common/src/datasources/drivers/sql/mysql/mysqldb.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/sql/mysql/mysqldb.py      
2005-07-02 16:00:14 UTC (rev 7672)
+++ trunk/gnue-common/src/datasources/drivers/sql/mysql/mysqldb.py      
2005-07-02 16:51:57 UTC (rev 7673)
@@ -107,6 +107,7 @@
 
   _boolean_true_  = 1
   _boolean_false_ = 0
+  _std_datetime_  = True
 
 
   # ---------------------------------------------------------------------------

Modified: trunk/gnue-common/src/datasources/readgsd.py
===================================================================
--- trunk/gnue-common/src/datasources/readgsd.py        2005-07-02 16:00:14 UTC 
(rev 7672)
+++ trunk/gnue-common/src/datasources/readgsd.py        2005-07-02 16:51:57 UTC 
(rev 7673)
@@ -25,9 +25,10 @@
 import re
 import sets
 import mx.DateTime.ISO
+import datetime
 
 from gnue.common.apps import errors, GClientApp
-from gnue.common.datasources import GSchema, GDataSource
+from gnue.common.datasources import GSchema, GDataSource, GConditions
 from gnue.common.utils.FileUtils import openResource
 from gnue.common.apps.i18n import translate as _            # for epydoc
 
@@ -467,10 +468,13 @@
       if key in existing:
         rs = existing [key]
         changed = 0
+
         for (field, value) in rows [key].items ():
-          if rs.getField (field) != value:
+          (ov, nv) = GConditions.unify ([rs.getField (field), value])
+          if ov != nv:
             rs.setField (field, value)
             changed = 1
+
         upd += changed
 
       else:
@@ -479,7 +483,7 @@
         for (field, value) in rows [key].items ():
           newRec.setField (field, value)
 
-    if new + upd:
+    if new or upd:
       resultSet.post ()
 
     print o (u_("    Rows: %(ins)d inserted, %(upd)d updated, %(kept)d "
@@ -637,7 +641,8 @@
     # Dates must conform with the ISO spec: YYYY-MM-DD
     elif ftype == 'date':
       try:
-        return mx.DateTime.ISO.ParseDate (contents.strip ())
+        val = mx.DateTime.ISO.ParseDate (contents.strip ())
+        return datetime.date (val.year, val.month, val.day)
 
       except ValueError:
         raise InvalidDateError, contents.strip ()
@@ -645,14 +650,23 @@
     # Times must conform with the ISO spec: HH:[MM:[:SS[.ss]]]
     elif ftype == 'time':
       try:
-        return mx.DateTime.ISO.ParseTime (contents.strip ())
+        val = mx.DateTime.ISO.ParseTime (contents.strip ())
+        # Adding the fractional part of seconds to the time instance, renders
+        # all records as 'allways changed' since there's no driver handling
+        # such times properly.
+        micro = int (("%s" % (val.second - int (val.second))) [2:8])
+        return datetime.time (val.hour, val.minute, int (val.second), micro)
 
       except ValueError:
         raise InvalidTimeError, contents.strip ()
 
     elif ftype == 'datetime':
       try:
-        return mx.DateTime.ISO.ParseDateTime (contents.strip ())
+        val = mx.DateTime.ISO.ParseDateTime (contents.strip ())
+        # Fractional part of seconds is problematic
+        micro = int (("%s" % (val.second - int (val.second))) [2:8])
+        return datetime.datetime (val.year, val.month, val.day, val.hour,
+                                  val.minute, int (val.second), micro)
 
       except ValueError:
         raise InvalidDateTimeError, contents.strip ()

Modified: trunk/gnue-common/tests/datasources_GConditions.py
===================================================================
--- trunk/gnue-common/tests/datasources_GConditions.py  2005-07-02 16:00:14 UTC 
(rev 7672)
+++ trunk/gnue-common/tests/datasources_GConditions.py  2005-07-02 16:51:57 UTC 
(rev 7673)
@@ -24,6 +24,7 @@
 import unittest
 import sets
 import mx.DateTime
+import datetime
 
 from gnue.common.datasources.GConditions import *
 
@@ -232,7 +233,13 @@
         [mx.DateTime.now (), '2004-08-15 14:00'],
         ['13.0', 12], ['15', ''], [7L, '12'], [7L, '12.2'], [1, 'True'], [],
         [1], [None], ['5.43', 12, 18L], [None, 'foo'],
-        ['foo', None], [5, None, 2, None, '10']]
+        ['foo', None], [5, None, 2, None, '10'],
+        [datetime.date.today (), '2004-08-15'],
+        [datetime.datetime.today (), '2004-08-15 15:01:32.124'],
+        [datetime.datetime.today ().time (), '2004-08-15 15:01:32.124'],
+        [datetime.datetime.today (), mx.DateTime.now ()],
+        [datetime.datetime.today ().time (), mx.DateTime.Time (1, 2, 3.45)]
+        ]
         
     self.nogos = [['trash', True], ['atext', 5.43]]
 
@@ -251,7 +258,9 @@
     # all sequences listed in self.unifications should work fine
     for sequence in self.unifications:
       tset   = sets.Set ()
+      print "SEQ:", sequence
       result = unify (sequence)
+      print "SEQ:", sequence, "==", result
       allowedTypes = 1
 
       for item in result:





reply via email to

[Prev in Thread] Current Thread [Next in Thread]