dabo Commit
Revision 1170
Date: 2005-08-15 10:59:44 -0700 (Mon, 15 Aug 2005)
Author: ed

Changed:
U   trunk/dabo/db/dCursorMixin.py

Log:
Added the 'FieldDescription' property that is a wrapper around the cursor's
native 'description' property. We noticed that SQLite returns None in the
description when there is no data, and this was causing errors. This new
property will return an empty tuple instead.

Also cleaned up some type checking in setFieldVal().


Diff:
Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2005-08-14 22:55:49 UTC (rev 1169)
+++ trunk/dabo/db/dCursorMixin.py       2005-08-15 17:59:44 UTC (rev 1170)
@@ -65,6 +65,8 @@
                self.__nonUpdateFields = []
                self.nonUpdateFields = []
                self.__tmpPK = -1               # temp PK value for new records.
+               # Holds the data types for each field
+               self._types = {}
                
                # Holds reference to auxiliary cursor that handles queries that
                # are not supposed to affect the record set.
@@ -157,7 +159,7 @@
                                tmpRows = []
                                # First, get the description property and 
extract the field names from that
                                fldNames = []
-                               for fld in self.description:
+                               for fld in self.FieldDescription:
                                        fldNames.append(fld[0].lower())
                                fldcount = len(fldNames)
                                # Now go through each row, and convert it to a 
dictionary. We will then
@@ -211,6 +213,9 @@
                        self.sql = self.getSQL()
                
                self.execute(self.sql, params)
+               
+               # Store the data types for each field
+               self.storeFieldTypes()
                # Add mementos to each row of the result set
                self.addMemento(-1)
 
@@ -229,6 +234,15 @@
                return True
 
 
+       def storeFieldTypes(self):
+               """Stores the data type for each column in the result set."""
+               self._types = {}
+               if self.RowCount > 0:
+                       rec = self._records[0]
+                       for fname, fval in rec.items():
+                               self._types[fname] = type(fval)
+                       
+               
        def sort(self, col, dir=None, caseSensitive=True):
                """ Sort the result set on the specified column in the 
specified order.
 
@@ -414,19 +428,19 @@
                        # No table specified, so no update checking is possible
                        return
                # This is the current description of the cursor.
-               if not self.description:
+               if not self.FieldDescription:
                        # A query hasn't been run yet; so we need to get one
                        holdWhere = self._whereClause
                        self.addWhere("1 = 0")
                        self.execute(self.getSQL())
                        self._whereClause = holdWhere
-               descFlds = self.description
+               descFlds = self.FieldDescription
                # Get the raw version of the table
                sql = """select * from %s where 1=0 """ % self.Table
                auxCrs = self._getAuxCursor()
                auxCrs.execute( sql )
                # This is the clean version of the table.
-               stdFlds = auxCrs.description
+               stdFlds = auxCrs.FieldDescription
 
                # Get all the fields that are not in the table.
                self.__nonUpdateFields = [d[0] for d in descFlds 
@@ -538,21 +552,19 @@
                        ret = rec[fld]
                else:
                        raise dException.dException, "%s '%s' %s" % (
-                                               _("Field"),
-                                               fld,
-                                               _("does not exist in the data 
set"))
+                                       _("Field"), fld, _("does not exist in 
the data set"))
                return ret
 
 
        def setFieldVal(self, fld, val):
-               """ Set the value of the specified field. 
-               """
+               """ Set the value of the specified field. """
                if self.RowCount <= 0:
                        raise dException.dException, _("No records in the data 
set")
                else:
                        rec = self._records[self.RowNumber]
                        if rec.has_key(fld):
-                               if type(rec[fld]) != type(val):
+                               fldType = self._types[fld]
+                               if fldType != type(val):
                                        if isinstance(val, basestring) and 
isinstance(rec[fld], basestring):
                                                if isinstance(rec[fld], str):
                                                        val = str(val)
@@ -562,15 +574,16 @@
                                                # convert bool to int (original 
field val was int, but UI
                                                # changed to int. 
                                                val = int(val)
-                               if type(rec[fld]) != type(val):
+                               if fldType != type(val):
                                        ignore = False
                                        # Date and DateTime types are handled 
as character, even if the 
-                                       # native field type is not. Ignore these
-                                       dtStrings = ("<type 'DateTime'>", 
"<type 'Date'>")
-                                       if str(type(rec[fld])) in dtStrings:
+                                       # native field type is not. Ignore 
these. NOTE: we have to deal with the 
+                                       # string representation of these 
classes, as there is no primitive for either
+                                       # 'DateTime' or 'Date'.
+                                       dtStrings = ("<type 'DateTime'>", 
"<type 'Date'>", "<type 'datetime.datetime'>")
+                                       if str(fldType) in dtStrings:
                                                if isinstance(val, basestring):
                                                        ignore = True
-                                       
                                        else:
                                                # This can also happen with a 
new record, since we just stuff the
                                                # fields full of empty strings.
@@ -578,7 +591,7 @@
                                        
                                        if not ignore:
                                                msg = "!!! Data Type Mismatch: 
field=%s. Expecting: %s; got: %s" \
-                                                     % (fld, 
str(type(rec[fld])), str(type(val)))
+                                                     % (fld, str(fldType), 
str(type(val)))
                                                dabo.errorLog.write(msg)
                                        
                                rec[fld] = val
@@ -965,7 +978,7 @@
                auxCrs = self._getAuxCursor()
                auxCrs.execute(tmpsql)
 
-               dscrp = auxCrs.description
+               dscrp = auxCrs.FieldDescription
                for fld in dscrp:
                        fldname = fld[0]
 
@@ -1442,7 +1455,13 @@
                
        def _getEncoding(self):
                return self._getBackendObject().Encoding
-               
+       
+       def _getDescrip(self):
+               if self.description is None:
+                       return ()
+               else:
+                       return self.description
+                       
        def _getKeyField(self):
                try:
                        return self._keyField
@@ -1497,6 +1516,9 @@
        Encoding = property(_getEncoding, None, None,
                        _("Encoding type used by the Backend  (string)") )
                        
+       FieldDescription = property(_getDescrip, None, None,
+                       _("Tuple of field names and types, as returned by the 
backend  (tuple)") )
+                       
        IsAdding = property(_isAdding, None, None,
                        _("Returns True if the current record is new and 
unsaved"))
                        




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to