commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r7113 - trunk/gnue-common/src/datasources/drivers/Base


From: reinhard
Subject: [gnue] r7113 - trunk/gnue-common/src/datasources/drivers/Base
Date: Tue, 8 Mar 2005 09:19:23 -0600 (CST)

Author: reinhard
Date: 2005-03-08 09:19:22 -0600 (Tue, 08 Mar 2005)
New Revision: 7113

Modified:
   trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py
   trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py
Log:
Fixed record status information. Most important of all, this causes empty
records being posted to the database if it's a master to detail records that
have to be posted.


Modified: trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2005-03-08 
15:17:45 UTC (rev 7112)
+++ trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2005-03-08 
15:19:22 UTC (rev 7113)
@@ -23,9 +23,8 @@
 
 __all__ = ['RecordSet']
 
-from gnue.common.apps import errors
-from gnue.common.datasources import GConditions, Exceptions
 import string
+from gnue.common.datasources import Exceptions
 
 # =============================================================================
 # This class implements the basic record set
@@ -33,9 +32,9 @@
 
 class RecordSet:
 
-  # --------------------------------------------------------------------------
+  # ---------------------------------------------------------------------------
   # Constructor
-  # --------------------------------------------------------------------------
+  # ---------------------------------------------------------------------------
 
   def __init__ (self, parent, initialData = {}, dbIdentifier = None,
                 defaultData = {}):
@@ -66,9 +65,10 @@
     gDebug (8, "Initial Data: %s" % self._fields)
 
 
-  # ===========================================================================
+  # ---------------------------------------------------------------------------
   # Dictionary emulation
-  # ===========================================================================
+  # ---------------------------------------------------------------------------
+
   def __setitem__(self, attr, val):
     self.setField (attr, val)
 
@@ -84,54 +84,60 @@
   def items (self):
     return self._fields.items ()
     
-  # Returns 1=Record has uncommitted changes
-  def isPending (self):
+  # ---------------------------------------------------------------------------
+  # Status of this record
+  # ---------------------------------------------------------------------------
 
-    # The _insertFlag and _deleteFlag takes care of records that
-    # were inserted, but then deleted before a save (i.e., nothing to do)
-    if self._emptyFlag or self._insertFlag and self._deleteFlag:
-      result = False
-    else:
-      result = self._insertFlag or self._deleteFlag or self._updateFlag
-
-    if not result:
+  def isEmpty (self):
+    """
+    Returns True if the record is empty, which means that it has been newly
+    inserted, but neither has any field been changed nor has a detail for this
+    record been inserted with a status other than empty.
+    """
+    if self._emptyFlag:
+      result = True
       for child in self._cachedDetailResultSets.values ():
         if child.isPending ():
-          result = True
+          result = False
           break
-
+    else:
+      result = False
     return result
 
+  def isInserted (self):
+    """
+    Returns True if the record has been newly inserted and has either changes
+    or a detail has been inserted. Records with this status will be inserted
+    into the database on post.
+    """
+    return self._insertFlag and not self._deleteFlag and not self.isEmpty ()
 
-  # Returns 1=Record is pending a deletion
-  def isDeleted(self):
-    if self._emptyFlag:
-      return False
-    else:
-      return self._deleteFlag and not self._insertFlag
+  def isModified (self):
+    """
+    Returns True if the record is an existing record with local changes.
+    Records with this status will be updated in the database on post.
+    """
+    return self._updateFlag and not self._insertFlag and not self._deleteFlag
 
+  def isDeleted (self):
+    """
+    Returns True if the record is an existing record that has been deleted.
+    Records with this status will be deleted in the database on post.
+    """
+    return self._deleteFlag and not self._insertFlag
 
-  # Returns 1=Record is pending an update
-  def isModified(self):
-    if self._emptyFlag or self._insertFlag:
-      return False
-    else:
-      return self._updateFlag
+  def isPending (self):
+    """
+    Returns True if the record has any local changes that make it necessary to
+    post it to the database. Equal to isInserted or isModified or isDeleted.
+    """
+    return self.isInserted () or self.isModified () or self.isDeleted ()
 
 
-  # Returns 1=Record is pending an insertion
-  def isInserted(self):
-    if self._emptyFlag:
-      return False
-    else:
-      return self._insertFlag and not self._deleteFlag
+  # ---------------------------------------------------------------------------
+  # Field access
+  # ---------------------------------------------------------------------------
 
-
-  # Returns 1=Record is empty (inserted, but no data set)
-  def isEmpty(self):
-    return self._emptyFlag
-
-
   # Returns current value of "field"
   def getField(self, field):
     try:
@@ -211,8 +217,10 @@
     return self._modifiedFlags.has_key(fieldName) or \
            self._modifiedFlags.has_key (string.lower(fieldName))
 
+  # ---------------------------------------------------------------------------
+  # Mark record as deleted
+  # ---------------------------------------------------------------------------
 
-  # Mark the current record as deleted
   def delete(self):
     if self._parent.isReadOnly():
       # Provide better feedback??
@@ -221,8 +229,10 @@
     else:
       self._deleteFlag = True
 
+  # ---------------------------------------------------------------------------
+  # Post changes to database
+  # ---------------------------------------------------------------------------
 
-  # Posts changes to database
   def post (self, recordNumber = None):
     # Should a post() to a read only datasource cause a ReadOnlyError?
     # It does no harm to attempt to post since nothing will be posted,
@@ -235,7 +245,7 @@
     status = (self._insertFlag, self._deleteFlag, self._updateFlag)
 
     # Call the hooks for commit-level hooks
-    if not self._emptyFlag and hasattr(self._parent._dataObject,'_dataSource'):
+    if not self.isEmpty() and hasattr(self._parent._dataObject,'_dataSource'):
 
       if self._insertFlag and not self._deleteFlag:
         self._parent._dataObject._dataSource._beforeCommitInsert(self)
@@ -269,16 +279,29 @@
       self._cachedDetailResultSets[child].post(foreign_keys=fk)
 
 
+  # ---------------------------------------------------------------------------
   # Sets the ResultSet associated with this master record
+  # ---------------------------------------------------------------------------
+
   def addDetailResultSet(self, resultSet):
     self._cachedDetailResultSets[resultSet._dataObject] = resultSet
 
 
-  ###
-  ### Methods below should be over-written by Vendor Specific functions
-  ###
+  # ---------------------------------------------------------------------------
+  # Nice string representation
+  # ---------------------------------------------------------------------------
 
-  # Post any changes to database
+  def __repr__ (self):
+    do = self._parent._dataObject
+    if hasattr (do, 'table'):
+      return "<RecordSet for %s>" % do.table
+    else:
+      return "<NIL-Table RecordSet>"
+
+  # ---------------------------------------------------------------------------
+  # Virtual methods to be overwritten by the drivers
+  # ---------------------------------------------------------------------------
+
   def _postChanges (self, recordNumber = None):
     """
     Post any changes (deletes, inserts, and updates) to the database.
@@ -309,7 +332,6 @@
     self._insertFlag = False
     self._updateFlag = False
 
-
   def _postDelete (self):
     """
     Post a deletion to the backend. Descendants should override this function

Modified: trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py 2005-03-08 
15:17:45 UTC (rev 7112)
+++ trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py 2005-03-08 
15:19:22 UTC (rev 7113)
@@ -348,12 +348,6 @@
     for rec in (self._cachedRecords):
       if rec.isPending ():
         return True
-
-      else:
-        for detail in rec._cachedDetailResultSets.values():
-          if detail.isPending ():
-            return True
-
     return False
 
 
@@ -376,7 +370,7 @@
     while recordPosition < len(self._cachedRecords):
       self._postingRecord = self._cachedRecords[recordPosition]
       postingRecordset = self._postingRecord
-      delete = self._postingRecord._emptyFlag or 
self._postingRecord._deleteFlag
+      delete = self._postingRecord.isEmpty() or self._postingRecord.isDeleted()
       if not delete:
         # Flip the flag for 'default' values to true so that hidden
         # default fields are included in insert statements
@@ -388,7 +382,7 @@
           self._postingRecord._fields[field] = foreign_keys[field]
           # Some DBs will throw an exception if you update a Primary Key
           # (even if you are updating to the same value)
-          if self._postingRecord._insertFlag:
+          if self._postingRecord.isInserted():
             self._postingRecord._modifiedFlags[field] = True
 
         recordPosition += 1
@@ -460,7 +454,18 @@
     return rs
 
 
+  # ---------------------------------------------------------------------------
+  # Nice string representation
+  # ---------------------------------------------------------------------------
 
+  def __repr__ (self):
+    do = self._dataObject
+    if hasattr (do, 'table'):
+      return "<ResultSet for %s>" % do.table
+    else:
+      return "<NIL-Table RecordSet>"
+
+
   ###
   ### Methods below should be overridden by Vendor Specific functions
   ### (_createEmptyRecord may not need to be overridden in all cases)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]