dabo Commit
Revision 6894
Date: 2011-10-12 14:13:12 -0700 (Wed, 12 Oct 2011)
Author: Paul
Trac: http://trac.dabodev.com/changeset/6894

Changed:
U   trunk/dabo/db/__init__.py
U   trunk/dabo/db/dBackend.py
U   trunk/dabo/db/dCursorMixin.py
U   trunk/dabo/db/dbSQLite.py

Log:
With this commit, I have BLOB fields saving and updating to/from
my SQLite table. In my case, I'm saving images in various
formats.


Diff:
Modified: trunk/dabo/db/__init__.py
===================================================================
--- trunk/dabo/db/__init__.py   2011-10-11 22:11:05 UTC (rev 6893)
+++ trunk/dabo/db/__init__.py   2011-10-12 21:13:12 UTC (rev 6894)
@@ -46,7 +46,7 @@
                "D": datetime.date,       ## date
                "T": datetime.datetime,   ## datetime
                "N": Decimal,             ## decimal (numeric)
-               "L": str,                 ## BLOB
+               "L": buffer,              ## BLOB
                }
 
 pythonTypes = dict([[v,k] for k,v in daboTypes.iteritems()])

Modified: trunk/dabo/db/dBackend.py
===================================================================
--- trunk/dabo/db/dBackend.py   2011-10-11 22:11:05 UTC (rev 6893)
+++ trunk/dabo/db/dBackend.py   2011-10-12 21:13:12 UTC (rev 6894)
@@ -82,10 +82,18 @@
                        return self.escQuote(val)
 
 
+       def formatBLOB(self, val):
+               """
+               Properly format a BLOB value to be included in an UPDATE
+               or INSERT statement for a specific backend.
+               """
+               return val
+
+
        def formatDateTime(self, val):
                """
-               Properly format a datetime value to be included in an Update
-               or Insert statement. Each backend can have different 
requirements
+               Properly format a datetime value to be included in an UPDATE
+               or INSERT statement. Each backend can have different 
requirements
                for formatting dates, so this is where you encapsulate these 
rules
                in backend-specific subclasses. If nothing special needs to be 
done,
                the default is to return the original value.

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2011-10-11 22:11:05 UTC (rev 6893)
+++ trunk/dabo/db/dCursorMixin.py       2011-10-12 21:13:12 UTC (rev 6894)
@@ -302,9 +302,6 @@
                                                        return ret
                                else:
                                        raise e
-#              elif isinstance(field_val, array.array):
-#                      # Usually blob data
-#                      ret = field_val.tostring()
 
                        rfv = repr(field_val)
                        dabo.log.error(_("%(rfv)s couldn't be converted to 
%(pythonType)s (field %(field_name)s)")
@@ -1660,7 +1657,12 @@
                                        flds += ", " + 
self.BackendObject.encloseNames(kk, aq)
                                        # add value to expression
                                        fieldType = [ds[1] for ds in 
self.DataStructure if ds[0] == kk][0]
-                                       vals.append(vv[1])
+                                       val = vv[1]
+                                       if fieldType == "L" or (isinstance(val, 
basestring) and "\0" in val):
+                                               val = self.formatBLOB(val)
+                                       #elif fieldType in ("D", "T"):
+                                       #       val = self.formatDateTime(val)
+                                       vals.append(val)
 
                                # Trim leading comma-space from the 'flds' 
string
                                flds = flds[2:]
@@ -1694,11 +1696,11 @@
                                # We need to retrieve any new default values
                                aux = self.AuxCursor
                                if not isinstance(self.KeyField, tuple):
-                                       keyFIelds = [self.KeyField]
+                                       keyFields = [self.KeyField]
                                else:
-                                       keyFIelds = self.KeyField
+                                       keyFields = self.KeyField
                                wheres = []
-                               for kf in keyFIelds:
+                               for kf in keyFields:
                                        fld = 
self.BackendObject.encloseNames(kf, self.AutoQuoteNames)
                                        val = self.getFieldVal(kf)
                                        if isinstance(val, basestring):
@@ -2326,9 +2328,14 @@
                        if fld in nonup:
                                continue
                        fieldType = [ds[1] for ds in self.DataStructure if 
ds[0] == fld][0]
+                       val = new_val
+                       if fieldType == "L" or (isinstance(val, basestring) and 
"\0" in val):
+                               val = self.formatBLOB(val)
+                       # elif fieldType in ("D", "T"):
+                       #               val = self.formatDateTime(val)
                        nms = bo.encloseNames(fld, aq)
                        retSql.append("%s%s = %s" % (tblPrefix, nms, 
self.ParamPlaceholder))
-                       retParams.append(new_val)
+                       retParams.append(val)
                return (", ".join(retSql), tuple(retParams))
 
 
@@ -2399,12 +2406,20 @@
                return ret
 
 
+       def formatBLOB(self, val):
+               """Format BLOB values for the backend"""
+               if val is None:
+                       return None
+               if self.BackendObject:
+                       return self.BackendObject.formatBLOB(val)
+               return val
+
+
        def formatDateTime(self, val):
                """Format DateTime values for the backend"""
-               ret = val
                if self.BackendObject:
-                       ret = self.BackendObject.formatDateTime(val)
-               return ret
+                       return self.BackendObject.formatDateTime(val)
+               return val
 
 
        def formatNone(self):

Modified: trunk/dabo/db/dbSQLite.py
===================================================================
--- trunk/dabo/db/dbSQLite.py   2011-10-11 22:11:05 UTC (rev 6893)
+++ trunk/dabo/db/dbSQLite.py   2011-10-12 21:13:12 UTC (rev 6894)
@@ -143,6 +143,10 @@
                self._connection.commit()
 
 
+       def formatBLOB(self, val):
+               return self.dbapi.Binary(val)
+
+
        def formatDateTime(self, val):
                """We need to wrap the value in quotes."""
                sqt = "'"               # single quote



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to