Changeset 2681

Show
Ignore:
Timestamp:
01/15/07 07:15:04 (2 years ago)
Author:
ed
Message:

Reverted the changes committed last night until they can be tested and shown to be working for all cases.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/db/dCursorMixin.py

    r2680 r2681  
    3939        ##      which we are mixed-in will take the __init__. 
    4040        dObject.__init__(self, *args, **kwargs) 
    41  
     41         
    4242        # Just in case this is used outside of the context of a bizobj 
    4343        if not hasattr(self, "superCursor") or self.superCursor is None: 
     
    10321032 
    10331033 
    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 marker 
    1051      
    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 parmList 
    1064  
    1065  
    10661034    def __saverow(self, row): 
    10671035        rec = self._records[row] 
     
    10701038        if diff or newrec: 
    10711039            if newrec: 
    1072                 flds = [] 
    1073                 valKeys = [] 
    1074                 valVals = {} 
     1040                flds = "" 
     1041                vals = "" 
    10751042                kf = self.KeyField 
    10761043                for kk, vv in diff.items(): 
     
    10871054                        continue 
    10881055                    # 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]),) 
    10941059                     
     1060                # Trim leading comma-space from the strings 
     1061                flds = flds[2:] 
     1062                vals = vals[2:] 
    10951063                if not flds: 
    10961064                    # Some backends (sqlite) require non-empty field clauses. We already 
    10971065                    # know that we are expecting the backend to generate the PK, so send 
    10981066                    # 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) 
    11091071 
    11101072            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) 
    11241077            oldPKVal = rec[self.KeyField] 
    11251078            newPKVal = None 
     
    11361089            #run the update 
    11371090            aux = self.AuxCursor 
    1138             res = aux.execute(sql,valVals
     1091            res = aux.execute(sql
    11391092 
    11401093            if newrec and self.AutoPopulatePK and (newPKVal is None): 
     
    12721225            res = True 
    12731226        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() 
    12771228            # some backends(PostgreSQL) don't return information about number of deleted rows 
    12781229            # 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
    12821233            res = aux.getFieldVal('cnt') 
    12831234            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 
    12871238 
    12881239        if not res: 
     
    15191470        return ret 
    15201471 
     1472 
    15211473    def checkPK(self): 
    15221474        """ Verify that the field(s) specified in the KeyField prop exist.""" 
     
    15351487            raise dException.MissingPKException, _("Primary key field does not exist in the data set: ") + fld 
    15361488 
     1489 
    15371490    def makePkWhere(self, row=None): 
    15381491        """ Create the WHERE clause used for updates, based on the pk field.  
     
    15581511                return rec[fld] 
    15591512             
    1560         exprs = [] 
    1561         vals = {} 
     1513        ret = "" 
    15621514        for fld in keyFields: 
    15631515            fldSafe = bo.encloseSpaces(fld) 
    1564             marker =  self.mkParam(fldSafe) 
     1516            if ret: 
     1517                ret += " AND " 
    15651518            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 
    15711527 
    15721528    def makeUpdClause(self, diff): 
    15731529        """ Create the 'set field=val' section of the Update statement. """ 
    1574         sets = [] 
    1575         vals = {} 
     1530        ret = "" 
    15761531        bo = self.BackendObject 
    15771532        tblPrefix = bo.getUpdateTablePrefix(bo.encloseSpaces(self.Table)) 
     
    15811536            if fld in self.getNonUpdateFields(): 
    15821537                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 
    15911542 
    15921543 
     
    20351986    def _setKeyField(self, kf): 
    20361987        if "," in kf: 
    2037         # if True: 
    20381988            flds = [f.strip() for f in kf.split(",")] 
    20391989            self._keyField = tuple(flds) 
  • trunk/dabo/db/dbMsSQL.py

    r2680 r2681  
    1111        self.dbModuleName = "pymssql" 
    1212        self.useTransactions = True  # this does not appear to be required 
    13         import pymssql 
    14         self.paramstyle = pymssql.paramstyle 
     13        import pymssql  
    1514 
    1615 
  • trunk/dabo/db/dbMySQL.py

    r2680 r2681  
    2121    def getConnection(self, connectInfo): 
    2222        import MySQLdb as dbapi 
    23         self.dbapi = dbapi 
    24         # self.paramstyle = self.dbapi.paramstyle 
    25         self.paramstyle = 'pyformat'  
    26          
     23 
    2724        port = connectInfo.Port 
    2825        if not port: 
  • trunk/dabo/db/dbSQLite.py

    r2680 r2681  
    1414            import sqlite3 as dbapi 
    1515        self.dbapi = dbapi 
    16         # self.paramstyle = self.dbapi.paramstyle 
    17         self.paramstyle = 'named' 
     16         
    1817 
    1918    def getConnection(self, connectInfo):