Changeset 3125

Show
Ignore:
Timestamp:
05/11/07 10:29:30 (1 year ago)
Author:
paul
Message:

Made changes to dBizobj.scan to keep it working with current code, and to keep
it easy to use. Moved the reverse arg to ScanReverse? property, alongside
ScanRestorePosition?.

Fixed ScanRestorePosition? to default to True instead of None.

Refactored/fixed the case of an exception happening during scan: previously,
another exception would happen because we tried to restore the row number
even if we saved the PK value instead.

See ticket #1064.

Files:

Legend:

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

    r3118 r3125  
    6060        self._caption = "" 
    6161        self._dataSource = "" 
    62         self._scanRestorePosition = None 
     62        self._scanRestorePosition = True 
     63        self._scanReverse = False 
    6364        self._SQL = "" 
    6465        self._requeryOnLoad = False 
     
    542543 
    543544 
    544     def scan(self, func, reverse=False, *args, **kwargs): 
     545    def scan(self, func, *args, **kwargs): 
    545546        """Iterate over all records and apply the passed function to each. 
    546547 
     
    548549 
    549550        If self.ScanRestorePosition is True, the position of the current 
    550         record in the recordset is restored after the iteration. If the 'reverse' 
    551         parameter is True, the records are processed in reverse order. 
    552         """ 
    553         self.scanRows(func, range(self.RowCount), reverse=reverse, *args, **kwargs) 
    554  
    555  
    556     def scanRows(self, func, rows, reverse=False, *args, **kwargs): 
     551        record in the recordset is restored after the iteration. If  
     552        self.ScanReverse is True, the records are processed in reverse order. 
     553        """ 
     554        self.scanRows(func, range(self.RowCount), *args, **kwargs) 
     555 
     556 
     557    def scanRows(self, func, rows, *args, **kwargs): 
    557558        """Iterate over the specified rows and apply the passed function to each. 
    558559 
    559560        Set self.exitScan to True to exit the scan on the next iteration. 
    560561        """ 
     562 
    561563        # Flag that the function can set to prematurely exit the scan 
    562564        self.exitScan = False 
    563565        rows = list(rows) 
    564         if self.ScanRestorePosition: 
    565             try: 
    566                 currPK = self.getPK() 
    567                 currRow = None 
    568             except: 
    569                 # No PK defined 
    570                 currPK = None 
    571                 currRow = self.RowNumber 
    572         try: 
    573             if reverse: 
     566        try: 
     567            currPK = self.getPK() 
     568            currRow = None 
     569        except: 
     570            # No PK defined 
     571            currPK = None 
     572            currRow = self.RowNumber 
     573 
     574        def restorePosition(): 
     575            if self.ScanRestorePosition: 
     576                if currPK is not None: 
     577                    self._positionUsingPK(currPK, updateChildren=False) 
     578                else: 
     579                    try: 
     580                        self.RowNumber = currRow 
     581                    except StandardError, e: 
     582                        # Perhaps the row was deleted; at any rate, leave the pointer 
     583                        # at the end of the data set 
     584                        row = self.RowCount  - 1 
     585                        if row >= 0: 
     586                            self.RowNumber = row 
     587                 
     588        try: 
     589            if self.ScanReverse: 
    574590                rows.reverse() 
    575591            for i in rows: 
     
    578594                if self.exitScan: 
    579595                    break 
    580         except dException.dException, e: 
    581             if self.ScanRestorePosition: 
    582                 self.RowNumber = currRow 
    583             raise dException.dException, e 
    584  
    585         if self.ScanRestorePosition: 
    586             if currPK is not None: 
    587                 self._positionUsingPK(currPK, updateChildren=False) 
    588             else: 
    589                 try: 
    590                     self.RowNumber = currRow 
    591                 except StandardError, e: 
    592                     # Perhaps the row was deleted; at any rate, leave the pointer 
    593                     # at the end of the data set 
    594                     row = self.RowCount  - 1 
    595                     if row >= 0: 
    596                         self.RowNumber = row 
     596        except Exception, e: 
     597            restorePosition() 
     598            raise 
     599        restorePosition() 
     600         
     601 
    597602         
    598603 
     
    10711076            if val is None: 
    10721077                val = self.getParentPK() 
    1073             self.scan(self._setParentFK, val=val
     1078            self.scan(self._setParentFK, val
    10741079 
    10751080    def _setParentFK(self, val): 
     
    17511756 
    17521757 
     1758    def _getScanReverse(self): 
     1759        return self._scanReverse 
     1760 
     1761    def _setScanReverse(self, val): 
     1762        self._scanReverse = val 
     1763 
     1764 
    17531765    def _getSQL(self): 
    17541766        try: 
     
    19111923            at the end of the recordset (False). (bool)""")) 
    19121924     
     1925    ScanReverse = property(_getScanReverse, _setScanReverse, None, 
     1926            _("""Do we scan the records in reverse order? (Default: False) (bool)""")) 
     1927 
    19131928    SQL = property(_getSQL, _setSQL, None, 
    19141929            _("SQL statement used to create the cursor's data. (str)")) 
  • trunk/dabo/biz/dBizobj.py

    r3122 r3125  
    6060        self._caption = "" 
    6161        self._dataSource = "" 
    62         self._scanRestorePosition = None 
     62        self._scanRestorePosition = True 
     63        self._scanReverse = False 
    6364        self._SQL = "" 
    6465        self._requeryOnLoad = False 
     
    557558         
    558559         
    559     def scan(self, func, reverse=False, *args, **kwargs): 
     560    def scan(self, func, *args, **kwargs): 
    560561        """Iterate over all records and apply the passed function to each. 
    561562 
     
    563564 
    564565        If self.ScanRestorePosition is True, the position of the current 
    565         record in the recordset is restored after the iteration. If the 'reverse' 
    566         parameter is True, the records are processed in reverse order. 
    567         """ 
    568         self.scanRows(func, range(self.RowCount), reverse=reverse, *args, **kwargs) 
    569  
    570  
    571     def scanRows(self, func, rows, reverse=False, *args, **kwargs): 
     566        record in the recordset is restored after the iteration. If  
     567        self.ScanReverse is True, the records are processed in reverse order. 
     568        """ 
     569        self.scanRows(func, range(self.RowCount), *args, **kwargs) 
     570 
     571 
     572    def scanRows(self, func, rows, *args, **kwargs): 
    572573        """Iterate over the specified rows and apply the passed function to each. 
    573574 
    574575        Set self.exitScan to True to exit the scan on the next iteration. 
    575576        """ 
     577 
    576578        # Flag that the function can set to prematurely exit the scan 
    577579        self.exitScan = False 
    578580        rows = list(rows) 
    579         if self.ScanRestorePosition: 
    580             try: 
    581                 currPK = self.getPK() 
    582                 currRow = None 
    583             except: 
    584                 # No PK defined 
    585                 currPK = None 
    586                 currRow = self.RowNumber 
    587         try: 
    588             if reverse: 
     581        try: 
     582            currPK = self.getPK() 
     583            currRow = None 
     584        except: 
     585            # No PK defined 
     586            currPK = None 
     587            currRow = self.RowNumber 
     588 
     589        def restorePosition(): 
     590            if self.ScanRestorePosition: 
     591                if currPK is not None: 
     592                    self._positionUsingPK(currPK, updateChildren=False) 
     593                else: 
     594                    try: 
     595                        self.RowNumber = currRow 
     596                    except StandardError, e: 
     597                        # Perhaps the row was deleted; at any rate, leave the pointer 
     598                        # at the end of the data set 
     599                        row = self.RowCount  - 1 
     600                        if row >= 0: 
     601                            self.RowNumber = row 
     602                 
     603        try: 
     604            if self.ScanReverse: 
    589605                rows.reverse() 
    590606            for i in rows: 
     
    593609                if self.exitScan: 
    594610                    break 
    595         except dException.dException, e: 
    596             if self.ScanRestorePosition: 
    597                 self.RowNumber = currRow 
    598             raise dException.dException, e 
    599  
    600         if self.ScanRestorePosition: 
    601             if currPK is not None: 
    602                 self._positionUsingPK(currPK, updateChildren=False) 
    603             else: 
    604                 try: 
    605                     self.RowNumber = currRow 
    606                 except StandardError, e: 
    607                     # Perhaps the row was deleted; at any rate, leave the pointer 
    608                     # at the end of the data set 
    609                     row = self.RowCount  - 1 
    610                     if row >= 0: 
    611                         self.RowNumber = row 
     611        except Exception, e: 
     612            restorePosition() 
     613            raise 
     614        restorePosition() 
    612615         
    613616 
     
    10861089            if val is None: 
    10871090                val = self.getParentPK() 
    1088             self.scan(self._setParentFK, val=val
     1091            self.scan(self._setParentFK, val
    10891092 
    10901093    def _setParentFK(self, val): 
     
    17661769 
    17671770 
     1771    def _getScanReverse(self): 
     1772        return self._scanReverse 
     1773 
     1774    def _setScanReverse(self, val): 
     1775        self._scanReverse = val 
     1776 
     1777 
    17681778    def _getSQL(self): 
    17691779        try: 
     
    19261936            at the end of the recordset (False). (bool)""")) 
    19271937     
     1938    ScanReverse = property(_getScanReverse, _setScanReverse, None, 
     1939            _("""Do we scan the records in reverse order? (Default: False) (bool)""")) 
     1940 
    19281941    SQL = property(_getSQL, _setSQL, None, 
    19291942            _("SQL statement used to create the cursor's data. (str)"))