dabo Commit
Revision 4046
Date: 2008-04-17 08:53:01 -0700 (Thu, 17 Apr 2008)
Author: Ed
Trac: http://svn.dabodev.com/trac/dabo/changeset/4046

Changed:
U   trunk/dabo/biz/dBizobj.py
U   trunk/dabo/dApp.py
U   trunk/dabo/db/dBackend.py
U   trunk/dabo/db/dCursorMixin.py
U   trunk/dabo/db/dbFirebird.py
U   trunk/dabo/db/dbMySQL.py
U   trunk/dabo/db/dbPostgreSQL.py
U   trunk/dabo/db/dbSQLite.py

Log:
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.


Diff:
Modified: trunk/dabo/biz/dBizobj.py
===================================================================
--- trunk/dabo/biz/dBizobj.py   2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/biz/dBizobj.py   2008-04-17 15:53:01 UTC (rev 4046)
@@ -257,12 +257,40 @@
                self.afterLast()
 
 
-       def _getTransactionToken(self):
-               """Ask the Application for the transaction token.
+       def beginTransaction(self):
+               """Attempts to begin a transaction at the database level, and 
returns
+               True/False depending on its success. 
+               """
+               ret = self._getTransactionToken()
+               if ret:
+                       self._CurrentCursor.beginTransaction()
+               return ret
 
-               If the token is granted, then this bizobj       has the ability 
to begin and 
-               end transactions.
+
+       def commitTransaction(self):
+               """Attempts to commit a transaction at the database level, and 
returns
+               True/False depending on its success. 
                """
+               ret = self._hasTransactionToken() and 
self._CurrentCursor.commitTransaction()
+               if ret:
+                       self._releaseTransactionToken()
+               return ret
+
+
+       def rollbackTransaction(self):
+               """Attempts to rollback a transaction at the database level, 
and returns
+               True/False depending on its success. 
+               """
+               ret = self._hasTransactionToken() and 
self._CurrentCursor.rollbackTransaction()
+               if ret:
+                       self._releaseTransactionToken()
+               return ret
+
+
+       def _getTransactionToken(self):
+               """Ask the Application for the transaction token. If the token 
is granted, 
+               then this bizobj has the ability to begin and end transactions.
+               """
                try:
                        return self.Application.getTransactionToken(self)
                except AttributeError:
@@ -272,11 +300,20 @@
                        return False
 
 
-       def _releaseTransactionToken(self):
-               """Ask the Application to give up the transaction token.
+       def _hasTransactionToken(self):
+               """Returns True/False, depending on whether this bizobj 
+               currently "holds" the transaction token.
+               """
+               try:
+                       ret = self.Application.hasTransactionToken(self)
+               except AttributeError:
+                       ret = hasattr(dabo, "_bizTransactionToken") and 
(dabo._bizTransactionToken is self)
+               return ret
 
-               Once this is done, other bizobjs can receive the token to begin 
and 
-               end transactions.
+
+       def _releaseTransactionToken(self):
+               """Ask the Application to give up the transaction token. Once 
this is done, 
+               other bizobjs can receive the token to begin and end 
transactions.
                """
                try:
                        self.Application.releaseTransactionToken(self)
@@ -290,34 +327,26 @@
                """Saves all changes to the bizobj and children."""
                cursor = self._CurrentCursor
                current_row = self.RowNumber
-               isTransactionManager = False
-               if startTransaction:
-                       isTransactionManager = self._getTransactionToken()
-                       if isTransactionManager:
-                               cursor.beginTransaction()
-
+               startTransaction = startTransaction and self.beginTransaction()
                try:
                        self.scanChangedRows(self.save, 
includeNewUnchanged=self.SaveNewUnchanged,
                                        startTransaction=False)
-                       if isTransactionManager:
-                               cursor.commitTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.commitTransaction()
 
                except dException.ConnectionLostException, e:
                        self.RowNumber = current_row
                        raise dException.ConnectionLostException, e
                except dException.DBQueryException, e:
                        # Something failed; reset things.
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        # Pass the exception to the UI
                        self.RowNumber = current_row
                        raise dException.DBQueryException, e
                except dException.dException, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        self.RowNumber = current_row
                        raise
 
@@ -345,12 +374,7 @@
                # validation, an Exception will be raised.
                self._validate()
 
-               isTransactionManager = False
-               if startTransaction:
-                       isTransactionManager = self._getTransactionToken()
-                       if isTransactionManager:
-                               cursor.beginTransaction()
-
+               startTransaction = startTransaction and self.beginTransaction()
                # Save to the Database, but first save the IsAdding flag as the 
save() call
                # will reset it to False:
                isAdding = self.IsAdding
@@ -368,9 +392,8 @@
                                        child.saveAll(startTransaction=False)
 
                        # Finish the transaction, and requery the children if 
needed.
-                       if isTransactionManager:
-                               cursor.commitTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.commitTransaction()
                        if self.RequeryChildOnSave:
                                self.requeryAllChildren()
 
@@ -382,17 +405,15 @@
 
                except dException.DBQueryException, e:
                        # Something failed; reset things.
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        # Pass the exception to the UI
                        raise dException.DBQueryException, e
 
                except dException.dException, e:
                        # Something failed; reset things.
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        # Pass the exception to the UI
                        raise
 
@@ -440,28 +461,20 @@
                if errMsg:
                        raise dException.BusinessRuleViolation, errMsg
 
-               isTransactionManager = False
-               if startTransaction:
-                       isTransactionManager = self._getTransactionToken()
-                       if isTransactionManager:
-                               cursor.beginTransaction()
-
+               startTransaction = startTransaction and self.beginTransaction()
                try:
                        for child in self.__children:
                                child.deleteAll(startTransaction=False)
-                       if isTransactionManager:
-                               cursor.commitTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.commitTransaction()
 
                except dException.DBQueryException, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise dException.DBQueryException, e
                except StandardError, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise StandardError, e
                self.afterDeleteAllChildren()
 
@@ -484,12 +497,7 @@
                                if child.RowCount > 0:
                                        raise dException.dException, 
_("Deletion prohibited - there are related child records.")
 
-               isTransactionManager = False
-               if startTransaction:
-                       isTransactionManager = self._getTransactionToken()
-                       if isTransactionManager:
-                               cursor.beginTransaction()
-
+               startTransaction = startTransaction and self.beginTransaction()
                try:
                        cursor.delete()
                        if self.RowCount == 0:
@@ -504,55 +512,44 @@
                                else:
                                        child.cancelAll()
                                        child.requery()
+                       if startTransaction:
+                               self.commitTransaction()
 
-                       if isTransactionManager:
-                               cursor.commitTransaction()
-                               self._releaseTransactionToken()
-
                        if not inLoop:
                                self.afterPointerMove()
                                self.afterChange()
                                self.afterDelete()
                except dException.DBQueryException, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise dException.DBQueryException, e
                except StandardError, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise StandardError, e
 
 
        def deleteAll(self, startTransaction=True):
                """ Delete all rows in the data set."""
                cursor = self._CurrentCursor
-               isTransactionManager = False
-               if startTransaction:
-                       isTransactionManager = self._getTransactionToken()
-                       if isTransactionManager:
-                               cursor.beginTransaction()
+               startTransaction = startTransaction and self.beginTransaction()
                try:
                        while self.RowCount > 0:
                                self.first()
                                ret = self.delete(startTransaction=False, 
inLoop=True)
-                       if isTransactionManager:
-                               cursor.commitTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.commitTransaction()
 
                        self.afterPointerMove()
                        self.afterChange()
                        self.afterDelete()
                except dException.DBQueryException, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise dException.DBQueryException, e
                except StandardError, e:
-                       if isTransactionManager:
-                               cursor.rollbackTransaction()
-                               self._releaseTransactionToken()
+                       if startTransaction:
+                               self.rollbackTransaction()
                        raise StandardError, e
 
 

Modified: trunk/dabo/dApp.py
===================================================================
--- trunk/dabo/dApp.py  2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/dApp.py  2008-04-17 15:53:01 UTC (rev 4046)
@@ -901,6 +901,14 @@
                        return False
 
 
+       def hasTransactionToken(self, biz):
+               """Returns True/False, depending on whether the specified
+               bizobj currently "holds" the transaction token.
+               """
+               cn = biz._connection
+               return (self._transactionTokens.get(cn) is biz)
+
+
        def releaseTransactionToken(self, biz):
                """When a process that would normally close a transaction 
happens, the 
                bizobj that is holding the transaction token for its connection 
calls this 

Modified: trunk/dabo/db/dBackend.py
===================================================================
--- trunk/dabo/db/dBackend.py   2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dBackend.py   2008-04-17 15:53:01 UTC (rev 4046)
@@ -206,18 +206,21 @@
                """ Begin a SQL transaction. Override in subclasses if 
needed."""
                self._connection.begin()
                dabo.dbActivityLog.write("SQL: begin")
+               return True
 
 
        def commitTransaction(self, cursor):
                """ Commit a SQL transaction."""
                self._connection.commit()
                dabo.dbActivityLog.write("SQL: commit")
+               return True
 
 
        def rollbackTransaction(self, cursor):
                """ Roll back (revert) a SQL transaction."""
                self._connection.rollback()
                dabo.dbActivityLog.write("SQL: rollback")
+               return True
 
 
        def addWithSep(self, base, new, sep=",\n\t"):

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dCursorMixin.py       2008-04-17 15:53:01 UTC (rev 4046)
@@ -465,14 +465,29 @@
 
 
        def sort(self, col, ord=None, caseSensitive=True):
-               """ Sort the result set on the specified column in the 
specified order.
-
-               If the sort direction is not specified, sort() cycles among 
Ascending,
-               Descending and no sort order.
+               """ Sort the result set on the specified column in the 
specified order. If the sort 
+               direction is not specified, default to ascending order. If 
'cycle' is specified as the
+               direction, use the next ordering in the list [None, 'ASC', 
'DESC']. The possible 
+               values for 'ord' are:
+                       None
+                       "" (i.e., an empty string)
+                       ASC
+                       DESC
+                       CYCLE
+               Only the first three characters are significant; case is 
ignored.
                """
                currCol = self.sortColumn
                currOrd = self.sortOrder
                currCase = self.sortCase
+               if ord is None:
+                       ord = "ASC"
+               elif ord == "":
+                       ord = None
+               else:
+                       ord = ord[:3].upper()
+               if ord == "CYC":
+                       ord = {"ASC": "DES", "DES": None, None: "ASC"}[currOrd]
+                       col = currCol
 
                # Make sure that the specified column is a column in the result 
set
                if not [True for t in self.DataStructure if t[0] == col]  and 
not self.VirtualFields.has_key(col):

Modified: trunk/dabo/db/dbFirebird.py
===================================================================
--- trunk/dabo/db/dbFirebird.py 2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dbFirebird.py 2008-04-17 15:53:01 UTC (rev 4046)
@@ -197,9 +197,12 @@
        
        def beginTransaction(self, cursor):
                """ Begin a SQL transaction."""
+               ret = False
                if not self._connection._has_transaction():
                        self._connection.begin()
                        dabo.dbActivityLog.write("SQL: begin")
+                       ret = True
+               return ret
 
                
        def flush(self, cursor):

Modified: trunk/dabo/db/dbMySQL.py
===================================================================
--- trunk/dabo/db/dbMySQL.py    2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dbMySQL.py    2008-04-17 15:53:01 UTC (rev 4046)
@@ -65,18 +65,21 @@
                """ Begin a SQL transaction."""
                cursor.execute("START TRANSACTION")
                dabo.dbActivityLog.write("SQL: begin")
+               return True
 
 
        def commitTransaction(self, cursor):
                """ Commit a SQL transaction."""
                cursor.execute("COMMIT")
                dabo.dbActivityLog.write("SQL: commit")
+               return True
 
 
        def rollbackTransaction(self, cursor):
                """ Rollback a SQL transaction."""
                cursor.execute("ROLLBACK")
                dabo.dbActivityLog.write("SQL: rollback")
+               return True
 
 
        def escQuote(self, val):

Modified: trunk/dabo/db/dbPostgreSQL.py
===================================================================
--- trunk/dabo/db/dbPostgreSQL.py       2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dbPostgreSQL.py       2008-04-17 15:53:01 UTC (rev 4046)
@@ -31,7 +31,7 @@
        
        def beginTransaction(self, cursor):
                dabo.dbActivityLog.write("SQL: begin (implicit, nothing done)")
-               pass
+               return True
 
 
        def getDictCursorClass(self):
@@ -63,12 +63,28 @@
                        sqltablestr = (("SELECT schemaname || '.' || tablename 
AS tablename FROM pg_tables WHERE has_table_privilege('%s', schemaname || '.' 
|| tablename, 'SELECT')") % self.conn_user)
                else:
                        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))
-                                               
+
+                       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))
+#              if includeSystemTables:
+#                      sqltablestr = ("select relname from pg_class where 
relkind= 'r' ")
+#              else:
+#                      sqltablestr = ("select relname from pg_class where 
relkind= 'r' and relname not like 'pg_%' and relname not like 'sql_%' ")
+               
+               print
+               print "sqltablestr = ", sqltablestr
+               print "conn_user = ", self.conn_user
+               print
                cursor.execute(sqltablestr)
                rs = cursor.getDataSet()
                tables = []
                for record in rs:
                        tables.append(record["tablename"])
+#                      tables.append(record["relname"])
                return tuple(tables)
 
        

Modified: trunk/dabo/db/dbSQLite.py
===================================================================
--- trunk/dabo/db/dbSQLite.py   2008-04-17 00:07:49 UTC (rev 4045)
+++ trunk/dabo/db/dbSQLite.py   2008-04-17 15:53:01 UTC (rev 4046)
@@ -83,6 +83,7 @@
                """
                cursor.execute("BEGIN")
                dabo.dbActivityLog.write("SQL: begin")
+               return True
 
 
        def commitTransaction(self, cursor):
@@ -91,6 +92,7 @@
                try:
                        cursor.execute("COMMIT", errorClass=opError)
                        dabo.dbActivityLog.write("SQL: commit")
+                       return True
                except opError, e:
                        # There is no transaction active in this case, so 
ignore the error.
                        pass
@@ -103,6 +105,7 @@
                """ Rollback a SQL transaction."""
                cursor.execute("ROLLBACK")
                dabo.dbActivityLog.write("SQL: rollback")
+               return True
 
        
        def flush(self, crs):




_______________________________________________
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