Changeset 2712
- Timestamp:
- 01/17/07 20:41:49 (2 years ago)
- Files:
-
- branches/carl2/dabo/db/dBackend.py (modified) (3 diffs)
- branches/carl2/dabo/db/dCursorMixin.py (modified) (13 diffs)
- branches/carl2/dabo/db/dbFirebird.py (modified) (2 diffs)
- branches/carl2/dabo/db/dbSQLite.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/carl2/dabo/db/dBackend.py
r2679 r2712 38 38 self._encoding = dabo.defaultEncoding 39 39 40 self.emptyParamValues = {} 41 42 def mkMarker(self, key): 43 return '%('+key+')s' 44 45 def addParam(self, paramList, key, value): 46 paramList[key] = value 47 return paramList 40 48 41 49 def isValidModule(self): … … 268 276 exp = "%(exp)s as %(alias)s" % locals() 269 277 indent = len("select ") * " " 270 # Give the backend-specific code a chance to update the format271 exp = self.processFields(exp)272 278 return self.addWithSep(clause, exp, sep=",\n%s" % indent) 273 279 274 280 275 281 def addFrom(self, clause, exp): 276 282 """ Add a table to the sql statement.""" … … 283 289 """ Add an expression to the where clause.""" 284 290 indent = (len("select ") - len(comp)) * " " 285 exp = self.processFields(exp)286 291 return self.addWithSep(clause, exp, sep="\n%s%s " % (indent, comp)) 287 292 branches/carl2/dabo/db/dCursorMixin.py
r2681 r2712 39 39 ## which we are mixed-in will take the __init__. 40 40 dObject.__init__(self, *args, **kwargs) 41 41 42 42 # Just in case this is used outside of the context of a bizobj 43 43 if not hasattr(self, "superCursor") or self.superCursor is None: … … 218 218 #### NOTE: NEEDS TO BE TESTED THOROUGHLY!!!! #### 219 219 220 # Some backends, notably Firebird, require that fields be specially marked.220 # Some backends, notably Firebird, require that fields be specially marked. 221 221 if not isinstance(sql, unicode): 222 222 sql = unicode(sql, self.Encoding) … … 234 234 res = self.superCursor.execute(self, sqlEX) 235 235 else: 236 res = self.superCursor.execute(self, sqlEX, params)236 res = self.superCursor.execute(self, sqlEX, params) 237 237 except Exception, e: 238 238 # If this is due to a broken connection, let the user know. … … 1030 1030 if useTransaction: 1031 1031 self.commitTransaction() 1032 1033 1032 1034 1033 def __saverow(self, row): 1035 1034 rec = self._records[row] … … 1038 1037 if diff or newrec: 1039 1038 if newrec: 1040 flds = "" 1041 vals = "" 1039 1040 flds = [] ## list of field names 1041 markers = [] ## list of markers 1042 params = self.BackendObject.emptyParamValues ## list of parameters 1043 1042 1044 kf = self.KeyField 1043 1045 for kk, vv in diff.items(): … … 1053 1055 # Skip it. 1054 1056 continue 1055 # Append the field and its value. 1056 flds += ", " + self.BackendObject.encloseSpaces(kk) 1057 # add value to expression 1058 vals += ", %s" % (self.formatForQuery(vv[1]),) 1057 # Append the field, a marker and its value. 1058 fieldName = self.BackendObject.encloseSpaces(kk) 1059 flds.append( fieldName ) 1060 markers.append( self.BackendObject.mkMarker(fieldName) ) 1061 params = self.BackendObject.addParam( params, fieldName, vv[1] ) 1059 1062 1060 # Trim leading comma-space from the strings1061 flds = flds[2:]1062 vals = vals[2:]1063 1063 if not flds: 1064 1064 # Some backends (sqlite) require non-empty field clauses. We already 1065 1065 # know that we are expecting the backend to generate the PK, so send 1066 1066 # NULL as the PK Value: 1067 flds = self.KeyField 1068 vals = "NULL" 1069 sql = "insert into %s (%s) values (%s) " % ( 1070 self.BackendObject.encloseSpaces(self.Table), flds, vals) 1067 flds = (self.KeyField,) 1068 markers.append( self.BackendObject.mkMarker('KeyField')) 1069 params = self.BackendObject.addParam(params, 'KeyField', None) 1070 1071 table=self.BackendObject.encloseSpaces(self.Table) 1072 fields=', '.join( flds ) 1073 markers=', '.join(markers) 1074 sql = "insert into %(t)s (%(f)s) values (%(m)s)" \ 1075 % {'t':table, 'f':fields, 'm':markers } 1071 1076 1072 1077 else: 1073 pkWhere = self.makePkWhere(row) 1074 updClause = self.makeUpdClause(diff) 1075 sql = "update %s set %s where %s" % (self.BackendObject.encloseSpaces(self.Table), 1076 updClause, pkWhere) 1078 updClause, params = self.makeUpdClause(diff) 1079 1080 pkWhere, pkParamsDict = self.makePkWhere(row) 1081 for fieldName, value in pkParamsDict.items(): 1082 params = self.BackendObject.addParam(params, fieldName, value ) 1083 1084 table=self.BackendObject.encloseSpaces(self.Table) 1085 sql = "update %(table)s set %(sets)s where %(w)s" \ 1086 % {'table':table, 'sets':updClause, 'w':pkWhere } 1087 1088 # sql = "update %s set %s where %s" % (self.BackendObject.encloseSpaces(self.Table), 1089 # updClause, pkWhere) 1077 1090 oldPKVal = rec[self.KeyField] 1078 1091 newPKVal = None … … 1089 1102 #run the update 1090 1103 aux = self.AuxCursor 1091 res = aux.execute(sql )1104 res = aux.execute(sql,params) 1092 1105 1093 1106 if newrec and self.AutoPopulatePK and (newPKVal is None): … … 1225 1238 res = True 1226 1239 else: 1227 pkWhere = self.makePkWhere() 1240 aux = self.AuxCursor 1241 table=self.BackendObject.encloseSpaces(self.Table) 1242 pkWhere, pkParamsDict = self.makePkWhere() 1243 params = self.BackendObject.emptyParamValues 1244 for fieldName, value in pkParamsDict.items(): 1245 params = self.BackendObject.addParam(params, fieldName, value ) 1246 1228 1247 # some backends(PostgreSQL) don't return information about number of deleted rows 1229 1248 # try to fetch it before 1230 sql = "select count(*) as cnt from % s where %s" % (self.Table, pkWhere)1231 aux = self.AuxCursor1232 aux.execute(sql )1249 sql = "select count(*) as cnt from %(table)s where %(w)s" \ 1250 % {'table':table, 'w':pkWhere } 1251 aux.execute(sql, params) 1233 1252 res = aux.getFieldVal('cnt') 1234 1253 if res: 1235 sql = "delete from % s where %s" % (self.Table, pkWhere)1236 aux.execute(sql)1237 1254 sql = "delete from %(table)s where %(w)s" \ 1255 % {'table':table, 'w':pkWhere } 1256 aux.execute(sql, params) 1238 1257 1239 1258 if not res: … … 1470 1489 return ret 1471 1490 1472 1473 1491 def checkPK(self): 1474 1492 """ Verify that the field(s) specified in the KeyField prop exist.""" … … 1487 1505 raise dException.MissingPKException, _("Primary key field does not exist in the data set: ") + fld 1488 1506 1489 1490 1507 def makePkWhere(self, row=None): 1491 1508 """ Create the WHERE clause used for updates, based on the pk field. … … 1511 1528 return rec[fld] 1512 1529 1513 ret = "" 1530 exprs = [] 1531 params = {} 1514 1532 for fld in keyFields: 1515 1533 fldSafe = bo.encloseSpaces(fld) 1516 if ret: 1517 ret += " AND " 1534 marker = bo.mkMarker(fldSafe) 1518 1535 pkVal = getPkVal(fld) 1519 if isinstance(pkVal, basestring): 1520 ret += tblPrefix + fldSafe + "='" + pkVal.encode(self.Encoding) + "' " 1521 elif isinstance(pkVal, (datetime.date, datetime.datetime)): 1522 ret += tblPrefix + fldSafe + "=" + self.formatDateTime(pkVal) + " " 1523 else: 1524 ret += tblPrefix + fldSafe + "=" + str(pkVal) + " " 1525 return ret 1526 1536 exprs.append( tblPrefix + fldSafe + "=" + marker ) 1537 params[fldSafe]=pkVal 1538 1539 exprs = " AND ".join(exprs) 1540 1541 return (exprs,params) 1527 1542 1528 1543 def makeUpdClause(self, diff): 1529 1544 """ Create the 'set field=val' section of the Update statement. """ 1530 ret = ""1531 1545 bo = self.BackendObject 1546 sets = [] 1547 params = bo.emptyParamValues 1532 1548 tblPrefix = bo.getUpdateTablePrefix(bo.encloseSpaces(self.Table)) 1533 1549 for fld, val in diff.items(): … … 1536 1552 if fld in self.getNonUpdateFields(): 1537 1553 continue 1538 if ret: 1539 ret += ", " 1540 ret += tblPrefix + bo.encloseSpaces(fld) + " = " + self.formatForQuery(new_val) 1541 return ret 1542 1554 fldSafe = bo.encloseSpaces(fld) 1555 marker = bo.mkMarker( fldSafe ) 1556 sets.append( tblPrefix + bo.encloseSpaces(fld) + " = " + marker ) 1557 params = bo.addParam( params, fldSafe, new_val ) 1558 sets = ", ".join(sets) 1559 return (sets,params) 1543 1560 1544 1561 def processFields(self, txt): … … 1986 2003 def _setKeyField(self, kf): 1987 2004 if "," in kf: 2005 # if True: 1988 2006 flds = [f.strip() for f in kf.split(",")] 1989 2007 self._keyField = tuple(flds) branches/carl2/dabo/db/dbFirebird.py
r2679 r2712 16 16 self.dbapi = kinterbasdb 17 17 18 self.emptyParamValues = () 19 20 def addParam(self, paramList, key, value): 21 paramList = list( paramList) 22 paramList.append(value) 23 parmList = tuple(paramList) 24 marker = '?' 25 return (paramList,marker) 18 26 19 27 def getConnection(self, connectInfo): … … 31 39 return self._connection 32 40 33 34 41 def getDictCursorClass(self): 35 42 return self.dbapi.Cursor 36 37 43 38 44 def noResultsOnSave(self): branches/carl2/dabo/db/dbSQLite.py
r2681 r2712 14 14 import sqlite3 as dbapi 15 15 self.dbapi = dbapi 16 16 17 self.emptyParamValues = {} 18 19 def mkMarker(self, key): 20 return ':'+key 21 22 def addParam(self, paramList, key, value): 23 paramList[key] = value 24 return paramList 17 25 18 26 def getConnection(self, connectInfo):
