Changeset 2681
- Timestamp:
- 01/15/07 07:15:04 (2 years ago)
- Files:
-
- trunk/dabo/db/dCursorMixin.py (modified) (11 diffs)
- trunk/dabo/db/dbMsSQL.py (modified) (1 diff)
- trunk/dabo/db/dbMySQL.py (modified) (1 diff)
- trunk/dabo/db/dbSQLite.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/dabo/db/dCursorMixin.py
r2680 r2681 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: … … 1032 1032 1033 1033 1034 def mkParam(self, parmName):1035 # hack alert:1036 # SQLite uses 'named' too, so I am using that in place of ?1037 # because it means the parameter values can be a dict.1038 # and I havn't coded that part yet. may never need to.1039 # and mysql says 'format' but uses py!1040 # so instead of using what dbapi.paramstyle returns,1041 # each dabo backend has it's own prop to override it.1042 # I hope noone is using 'numeric',1043 # because I am not sure when to reset the counter.1044 marker = {1045 'qmark': '?',1046 'named': ":"+parmName,1047 'format': "%s",1048 'pyformat': "%("+parmName+")s"1049 }[self.__backend.paramstyle]1050 return marker1051 1052 def appeParm(self, parmName, parmValue, parmList):1053 # hack allert (see mkParam())1054 # this func not called,1055 # so more untested code:1056 if self.cursor.paramstyle == 'qmark':1057 if not parmList:1058 parmList=()1059 elif self.cursor.paramstyle == 'pyformat':1060 if not parmList:1061 parmList={}1062 self.cursor.paramstyle == "%("+parmName+")s"1063 return parmList1064 1065 1066 1034 def __saverow(self, row): 1067 1035 rec = self._records[row] … … 1070 1038 if diff or newrec: 1071 1039 if newrec: 1072 flds = [] 1073 valKeys = [] 1074 valVals = {} 1040 flds = "" 1041 vals = "" 1075 1042 kf = self.KeyField 1076 1043 for kk, vv in diff.items(): … … 1087 1054 continue 1088 1055 # Append the field and its value. 1089 flds.append( self.BackendObject.encloseSpaces(kk) ) 1090 key = self.BackendObject.encloseSpaces(kk) 1091 marker = self.mkParam(key) 1092 valKeys.append( marker ) 1093 valVals[key] = vv[1] 1056 flds += ", " + self.BackendObject.encloseSpaces(kk) 1057 # add value to expression 1058 vals += ", %s" % (self.formatForQuery(vv[1]),) 1094 1059 1060 # Trim leading comma-space from the strings 1061 flds = flds[2:] 1062 vals = vals[2:] 1095 1063 if not flds: 1096 1064 # Some backends (sqlite) require non-empty field clauses. We already 1097 1065 # know that we are expecting the backend to generate the PK, so send 1098 1066 # NULL as the PK Value: 1099 flds = (self.KeyField,) 1100 marker = self.mkParam('KeyField') 1101 valKeys.append( marker ) 1102 valVals = {'KeyField':None} 1103 1104 table=self.BackendObject.encloseSpaces(self.Table) 1105 fields=', '.join( flds ) 1106 keys=', '.join(valKeys) 1107 sql = "insert into %(t)s (%(f)s) values (%(v)s)" \ 1108 % {'t':table, 'f':fields, 'v':keys } 1067 flds = self.KeyField 1068 vals = "NULL" 1069 sql = "insert into %s (%s) values (%s) " % ( 1070 self.BackendObject.encloseSpaces(self.Table), flds, vals) 1109 1071 1110 1072 else: 1111 pkWhere = self.makePkWhere(row) # return {'where':exprs, 'parms':vals } 1112 updClause = self.makeUpdClause(diff) # return {'sets':sets,'parms':vals} 1113 1114 # make a dict from the updates vals and the pkWhere vals 1115 valVals = updClause['parms'] 1116 valVals.update(pkWhere['parms']) 1117 1118 table=self.BackendObject.encloseSpaces(self.Table) 1119 sql = "update %(table)s set %(updClause)s where %(w)s" \ 1120 % {'table':table, 'updClause':updClause['sets'], 'w':pkWhere['where'] } 1121 1122 # sql = "update %s set %s where %s" % (self.BackendObject.encloseSpaces(self.Table), 1123 # updClause, pkWhere) 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) 1124 1077 oldPKVal = rec[self.KeyField] 1125 1078 newPKVal = None … … 1136 1089 #run the update 1137 1090 aux = self.AuxCursor 1138 res = aux.execute(sql ,valVals)1091 res = aux.execute(sql) 1139 1092 1140 1093 if newrec and self.AutoPopulatePK and (newPKVal is None): … … 1272 1225 res = True 1273 1226 else: 1274 aux = self.AuxCursor 1275 table=self.BackendObject.encloseSpaces(self.Table) 1276 pkWhere = self.makePkWhere() # return {'where':exprs, 'parms':vals } 1227 pkWhere = self.makePkWhere() 1277 1228 # some backends(PostgreSQL) don't return information about number of deleted rows 1278 1229 # try to fetch it before 1279 sql = "select count(*) as cnt from % (table)s where %(w)s" \1280 % {'table':table, 'w':pkWhere['where'] }1281 aux.execute(sql , pkWhere['parms'])1230 sql = "select count(*) as cnt from %s where %s" % (self.Table, pkWhere) 1231 aux = self.AuxCursor 1232 aux.execute(sql) 1282 1233 res = aux.getFieldVal('cnt') 1283 1234 if res: 1284 sql = "delete from % (table)s where %(w)s" \1285 % {'table':table, 'w':pkWhere['where'] }1286 aux.execute(sql, pkWhere['parms']) 1235 sql = "delete from %s where %s" % (self.Table, pkWhere) 1236 aux.execute(sql) 1237 1287 1238 1288 1239 if not res: … … 1519 1470 return ret 1520 1471 1472 1521 1473 def checkPK(self): 1522 1474 """ Verify that the field(s) specified in the KeyField prop exist.""" … … 1535 1487 raise dException.MissingPKException, _("Primary key field does not exist in the data set: ") + fld 1536 1488 1489 1537 1490 def makePkWhere(self, row=None): 1538 1491 """ Create the WHERE clause used for updates, based on the pk field. … … 1558 1511 return rec[fld] 1559 1512 1560 exprs = [] 1561 vals = {} 1513 ret = "" 1562 1514 for fld in keyFields: 1563 1515 fldSafe = bo.encloseSpaces(fld) 1564 marker = self.mkParam(fldSafe) 1516 if ret: 1517 ret += " AND " 1565 1518 pkVal = getPkVal(fld) 1566 exprs.append( tblPrefix + fldSafe + "=" + marker ) 1567 vals[fldSafe]=pkVal 1568 exprs = " AND ".join(exprs) 1569 1570 return {'where':exprs, 'parms':vals } 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 1571 1527 1572 1528 def makeUpdClause(self, diff): 1573 1529 """ Create the 'set field=val' section of the Update statement. """ 1574 sets = [] 1575 vals = {} 1530 ret = "" 1576 1531 bo = self.BackendObject 1577 1532 tblPrefix = bo.getUpdateTablePrefix(bo.encloseSpaces(self.Table)) … … 1581 1536 if fld in self.getNonUpdateFields(): 1582 1537 continue 1583 fldSafe = bo.encloseSpaces(fld) 1584 sets.append( tblPrefix + bo.encloseSpaces(fld) + " = %("+fldSafe+")s" ) 1585 vals[fldSafe] = new_val 1586 1587 sets = ", ".join(sets) 1588 1589 return {'sets':sets,'parms':vals} 1590 1538 if ret: 1539 ret += ", " 1540 ret += tblPrefix + bo.encloseSpaces(fld) + " = " + self.formatForQuery(new_val) 1541 return ret 1591 1542 1592 1543 … … 2035 1986 def _setKeyField(self, kf): 2036 1987 if "," in kf: 2037 # if True:2038 1988 flds = [f.strip() for f in kf.split(",")] 2039 1989 self._keyField = tuple(flds) trunk/dabo/db/dbMsSQL.py
r2680 r2681 11 11 self.dbModuleName = "pymssql" 12 12 self.useTransactions = True # this does not appear to be required 13 import pymssql 14 self.paramstyle = pymssql.paramstyle 13 import pymssql 15 14 16 15 trunk/dabo/db/dbMySQL.py
r2680 r2681 21 21 def getConnection(self, connectInfo): 22 22 import MySQLdb as dbapi 23 self.dbapi = dbapi 24 # self.paramstyle = self.dbapi.paramstyle 25 self.paramstyle = 'pyformat' 26 23 27 24 port = connectInfo.Port 28 25 if not port: trunk/dabo/db/dbSQLite.py
r2680 r2681 14 14 import sqlite3 as dbapi 15 15 self.dbapi = dbapi 16 # self.paramstyle = self.dbapi.paramstyle 17 self.paramstyle = 'named' 16 18 17 19 18 def getConnection(self, connectInfo):
