Changeset 4046

Show
Ignore:
Timestamp:
04/17/08 08:53:01 (3 months ago)
Author:
ed
Message:

Added 'beginTransaction()', 'commitTransaction()', and 'rollbackTransaction()' methods to dBizobj. Refactored the current transaction-handling code in dBizobj to eliminate duplication.

Updated the database layer version of these methods to return True when successful. They were always supposed to, but no return values were ever created.

Added the 'hasTransactionToken()' method to dApp. This allows a bizobj to see if it currently is the holder of the transaction token.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/biz/dBizobj.py

    r4043 r4046  
    258258 
    259259 
     260    def beginTransaction(self): 
     261        """Attempts to begin a transaction at the database level, and returns 
     262        True/False depending on its success.  
     263        """ 
     264        ret = self._getTransactionToken() 
     265        if ret: 
     266            self._CurrentCursor.beginTransaction() 
     267        return ret 
     268 
     269 
     270    def commitTransaction(self): 
     271        """Attempts to commit a transaction at the database level, and returns 
     272        True/False depending on its success.  
     273        """ 
     274        ret = self._hasTransactionToken() and self._CurrentCursor.commitTransaction() 
     275        if ret: 
     276            self._releaseTransactionToken() 
     277        return ret 
     278 
     279 
     280    def rollbackTransaction(self): 
     281        """Attempts to rollback a transaction at the database level, and returns 
     282        True/False depending on its success.  
     283        """ 
     284        ret = self._hasTransactionToken() and self._CurrentCursor.rollbackTransaction() 
     285        if ret: 
     286            self._releaseTransactionToken() 
     287        return ret 
     288 
     289 
    260290    def _getTransactionToken(self): 
    261         """Ask the Application for the transaction token. 
    262  
    263         If the token is granted, then this bizobj   has the ability to begin and  
    264         end transactions. 
     291        """Ask the Application for the transaction token. If the token is granted,  
     292        then this bizobj has the ability to begin and end transactions. 
    265293        """ 
    266294        try: 
     
    273301 
    274302 
     303    def _hasTransactionToken(self): 
     304        """Returns True/False, depending on whether this bizobj  
     305        currently "holds" the transaction token. 
     306        """ 
     307        try: 
     308            ret = self.Application.hasTransactionToken(self) 
     309        except AttributeError: 
     310            ret = hasattr(dabo, "_bizTransactionToken") and (dabo._bizTransactionToken is self) 
     311        return ret 
     312 
     313 
    275314    def _releaseTransactionToken(self): 
    276         """Ask the Application to give up the transaction token. 
    277  
    278         Once this is done, other bizobjs can receive the token to begin and  
    279         end transactions. 
     315        """Ask the Application to give up the transaction token. Once this is done,  
     316        other bizobjs can receive the token to begin and end transactions. 
    280317        """ 
    281318        try: 
     
    291328        cursor = self._CurrentCursor 
    292329        current_row = self.RowNumber 
    293         isTransactionManager = False 
    294         if startTransaction: 
    295             isTransactionManager = self._getTransactionToken() 
    296             if isTransactionManager: 
    297                 cursor.beginTransaction() 
    298  
     330        startTransaction = startTransaction and self.beginTransaction() 
    299331        try: 
    300332            self.scanChangedRows(self.save, includeNewUnchanged=self.SaveNewUnchanged, 
    301333                    startTransaction=False) 
    302             if isTransactionManager: 
    303                 cursor.commitTransaction() 
    304                 self._releaseTransactionToken() 
     334            if startTransaction: 
     335                self.commitTransaction() 
    305336 
    306337        except dException.ConnectionLostException, e: 
     
    309340        except dException.DBQueryException, e: 
    310341            # Something failed; reset things. 
    311             if isTransactionManager: 
    312                 cursor.rollbackTransaction() 
    313                 self._releaseTransactionToken() 
     342            if startTransaction: 
     343                self.rollbackTransaction() 
    314344            # Pass the exception to the UI 
    315345            self.RowNumber = current_row 
    316346            raise dException.DBQueryException, e 
    317347        except dException.dException, e: 
    318             if isTransactionManager: 
    319                 cursor.rollbackTransaction() 
    320                 self._releaseTransactionToken() 
     348            if startTransaction: 
     349                self.rollbackTransaction() 
    321350            self.RowNumber = current_row 
    322351            raise 
     
    346375        self._validate() 
    347376 
    348         isTransactionManager = False 
    349         if startTransaction: 
    350             isTransactionManager = self._getTransactionToken() 
    351             if isTransactionManager: 
    352                 cursor.beginTransaction() 
    353  
     377        startTransaction = startTransaction and self.beginTransaction() 
    354378        # Save to the Database, but first save the IsAdding flag as the save() call 
    355379        # will reset it to False: 
     
    369393 
    370394            # Finish the transaction, and requery the children if needed. 
    371             if isTransactionManager: 
    372                 cursor.commitTransaction() 
    373                 self._releaseTransactionToken() 
     395            if startTransaction: 
     396                self.commitTransaction() 
    374397            if self.RequeryChildOnSave: 
    375398                self.requeryAllChildren() 
     
    383406        except dException.DBQueryException, e: 
    384407            # Something failed; reset things. 
    385             if isTransactionManager: 
    386                 cursor.rollbackTransaction() 
    387                 self._releaseTransactionToken() 
     408            if startTransaction: 
     409                self.rollbackTransaction() 
    388410            # Pass the exception to the UI 
    389411            raise dException.DBQueryException, e 
     
    391413        except dException.dException, e: 
    392414            # Something failed; reset things. 
    393             if isTransactionManager: 
    394                 cursor.rollbackTransaction() 
    395                 self._releaseTransactionToken() 
     415            if startTransaction: 
     416                self.rollbackTransaction() 
    396417            # Pass the exception to the UI 
    397418            raise 
     
    441462            raise dException.BusinessRuleViolation, errMsg 
    442463 
    443         isTransactionManager = False 
    444         if startTransaction: 
    445             isTransactionManager = self._getTransactionToken() 
    446             if isTransactionManager: 
    447                 cursor.beginTransaction() 
    448  
     464        startTransaction = startTransaction and self.beginTransaction() 
    449465        try: 
    450466            for child in self.__children: 
    451467                child.deleteAll(startTransaction=False) 
    452             if isTransactionManager: 
    453                 cursor.commitTransaction() 
    454                 self._releaseTransactionToken() 
     468            if startTransaction: 
     469                self.commitTransaction() 
    455470 
    456471        except dException.DBQueryException, e: 
    457             if isTransactionManager: 
    458                 cursor.rollbackTransaction() 
    459                 self._releaseTransactionToken() 
     472            if startTransaction: 
     473                self.rollbackTransaction() 
    460474            raise dException.DBQueryException, e 
    461475        except StandardError, e: 
    462             if isTransactionManager: 
    463                 cursor.rollbackTransaction() 
    464                 self._releaseTransactionToken() 
     476            if startTransaction: 
     477                self.rollbackTransaction() 
    465478            raise StandardError, e 
    466479        self.afterDeleteAllChildren() 
     
    485498                    raise dException.dException, _("Deletion prohibited - there are related child records.") 
    486499 
    487         isTransactionManager = False 
    488         if startTransaction: 
    489             isTransactionManager = self._getTransactionToken() 
    490             if isTransactionManager: 
    491                 cursor.beginTransaction() 
    492  
     500        startTransaction = startTransaction and self.beginTransaction() 
    493501        try: 
    494502            cursor.delete() 
     
    505513                    child.cancelAll() 
    506514                    child.requery() 
    507  
    508             if isTransactionManager: 
    509                 cursor.commitTransaction() 
    510                 self._releaseTransactionToken() 
     515            if startTransaction: 
     516                self.commitTransaction() 
    511517 
    512518            if not inLoop: 
     
    515521                self.afterDelete() 
    516522        except dException.DBQueryException, e: 
    517             if isTransactionManager: 
    518                 cursor.rollbackTransaction() 
    519                 self._releaseTransactionToken() 
     523            if startTransaction: 
     524                self.rollbackTransaction() 
    520525            raise dException.DBQueryException, e 
    521526        except StandardError, e: 
    522             if isTransactionManager: 
    523                 cursor.rollbackTransaction() 
    524                 self._releaseTransactionToken() 
     527            if startTransaction: 
     528                self.rollbackTransaction() 
    525529            raise StandardError, e 
    526530 
     
    529533        """ Delete all rows in the data set.""" 
    530534        cursor = self._CurrentCursor 
    531         isTransactionManager = False 
    532         if startTransaction: 
    533             isTransactionManager = self._getTransactionToken() 
    534             if isTransactionManager: 
    535                 cursor.beginTransaction() 
     535        startTransaction = startTransaction and self.beginTransaction() 
    536536        try: 
    537537            while self.RowCount > 0: 
    538538                self.first() 
    539539                ret = self.delete(startTransaction=False, inLoop=True) 
    540             if isTransactionManager: 
    541                 cursor.commitTransaction() 
    542                 self._releaseTransactionToken() 
     540            if startTransaction: 
     541                self.commitTransaction() 
    543542 
    544543            self.afterPointerMove() 
     
    546545            self.afterDelete() 
    547546        except dException.DBQueryException, e: 
    548             if isTransactionManager: 
    549                 cursor.rollbackTransaction() 
    550                 self._releaseTransactionToken() 
     547            if startTransaction: 
     548                self.rollbackTransaction() 
    551549            raise dException.DBQueryException, e 
    552550        except StandardError, e: 
    553             if isTransactionManager: 
    554                 cursor.rollbackTransaction() 
    555                 self._releaseTransactionToken() 
     551            if startTransaction: 
     552                self.rollbackTransaction() 
    556553            raise StandardError, e 
    557554 
  • trunk/dabo/dApp.py

    r4003 r4046  
    902902 
    903903 
     904    def hasTransactionToken(self, biz): 
     905        """Returns True/False, depending on whether the specified 
     906        bizobj currently "holds" the transaction token. 
     907        """ 
     908        cn = biz._connection 
     909        return (self._transactionTokens.get(cn) is biz) 
     910 
     911 
    904912    def releaseTransactionToken(self, biz): 
    905913        """When a process that would normally close a transaction happens, the  
  • trunk/dabo/db/dBackend.py

    r3606 r4046  
    207207        self._connection.begin() 
    208208        dabo.dbActivityLog.write("SQL: begin") 
     209        return True 
    209210 
    210211 
     
    213214        self._connection.commit() 
    214215        dabo.dbActivityLog.write("SQL: commit") 
     216        return True 
    215217 
    216218 
     
    219221        self._connection.rollback() 
    220222        dabo.dbActivityLog.write("SQL: rollback") 
     223        return True 
    221224 
    222225 
  • trunk/dabo/db/dCursorMixin.py

    r4030 r4046  
    466466 
    467467    def sort(self, col, ord=None, caseSensitive=True): 
    468         """ Sort the result set on the specified column in the specified order. 
    469  
    470         If the sort direction is not specified, sort() cycles among Ascending, 
    471         Descending and no sort order. 
     468        """ Sort the result set on the specified column in the specified order. If the sort  
     469        direction is not specified, default to ascending order. If 'cycle' is specified as the 
     470        direction, use the next ordering in the list [None, 'ASC', 'DESC']. The possible  
     471        values for 'ord' are: 
     472            None 
     473            "" (i.e., an empty string) 
     474            ASC 
     475            DESC 
     476            CYCLE 
     477        Only the first three characters are significant; case is ignored. 
    472478        """ 
    473479        currCol = self.sortColumn 
    474480        currOrd = self.sortOrder 
    475481        currCase = self.sortCase 
     482        if ord is None: 
     483            ord = "ASC" 
     484        elif ord == "": 
     485            ord = None 
     486        else: 
     487            ord = ord[:3].upper() 
     488        if ord == "CYC": 
     489            ord = {"ASC": "DES", "DES": None, None: "ASC"}[currOrd] 
     490            col = currCol 
    476491 
    477492        # Make sure that the specified column is a column in the result set 
  • trunk/dabo/db/dbFirebird.py

    r3606 r4046  
    198198    def beginTransaction(self, cursor): 
    199199        """ Begin a SQL transaction.""" 
     200        ret = False 
    200201        if not self._connection._has_transaction(): 
    201202            self._connection.begin() 
    202203            dabo.dbActivityLog.write("SQL: begin") 
     204            ret = True 
     205        return ret 
    203206 
    204207         
  • trunk/dabo/db/dbMySQL.py

    r3606 r4046  
    6666        cursor.execute("START TRANSACTION") 
    6767        dabo.dbActivityLog.write("SQL: begin") 
     68        return True 
    6869 
    6970 
     
    7273        cursor.execute("COMMIT") 
    7374        dabo.dbActivityLog.write("SQL: commit") 
     75        return True 
    7476 
    7577 
     
    7880        cursor.execute("ROLLBACK") 
    7981        dabo.dbActivityLog.write("SQL: rollback") 
     82        return True 
    8083 
    8184 
  • trunk/dabo/db/dbPostgreSQL.py

    r3869 r4046  
    3232    def beginTransaction(self, cursor): 
    3333        dabo.dbActivityLog.write("SQL: begin (implicit, nothing done)") 
    34         pass 
     34        return True 
    3535 
    3636 
     
    6464        else: 
    6565            sqltablestr = (("SELECT schemaname || '.' || tablename AS tablename FROM pg_tables WHERE (schemaname not like 'pg_%s' and schemaname not like 'information%s') and has_table_privilege('%s', schemaname || '.' || tablename, 'SELECT')") % ('%','%',self.conn_user)) 
    66                          
     66 
     67            sqltablestr = (("""SELECT schemaname || '.' || tablename AS tablename  
     68                    FROM pg_tables  
     69                    WHERE (schemaname not like 'pg_%s'  
     70                        and schemaname not like 'information%s')  
     71                    and has_table_privilege('%s', schemaname || '.' || tablename, 'SELECT') 
     72                    """) % ('%','%',self.conn_user)) 
     73#       if includeSystemTables: 
     74#           sqltablestr = ("select relname from pg_class where relkind= 'r' ") 
     75#       else: 
     76#           sqltablestr = ("select relname from pg_class where relkind= 'r' and relname not like 'pg_%' and relname not like 'sql_%' ") 
     77         
     78        print 
     79        print "sqltablestr = ", sqltablestr 
     80        print "conn_user = ", self.conn_user 
     81        print 
    6782        cursor.execute(sqltablestr) 
    6883        rs = cursor.getDataSet() 
     
    7085        for record in rs: 
    7186            tables.append(record["tablename"]) 
     87#           tables.append(record["relname"]) 
    7288        return tuple(tables) 
    7389 
  • trunk/dabo/db/dbSQLite.py

    r4013 r4046  
    8484        cursor.execute("BEGIN") 
    8585        dabo.dbActivityLog.write("SQL: begin") 
     86        return True 
    8687 
    8788 
     
    9293            cursor.execute("COMMIT", errorClass=opError) 
    9394            dabo.dbActivityLog.write("SQL: commit") 
     95            return True 
    9496        except opError, e: 
    9597            # There is no transaction active in this case, so ignore the error. 
     
    104106        cursor.execute("ROLLBACK") 
    105107        dabo.dbActivityLog.write("SQL: rollback") 
     108        return True 
    106109 
    107110