commit-gnue
[Top][All Lists]
Advanced

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

[gnue] r7213 - in trunk/gnue-common/src/datasources/drivers: Base appser


From: reinhard
Subject: [gnue] r7213 - in trunk/gnue-common/src/datasources/drivers: Base appserver/appserver
Date: Thu, 17 Mar 2005 13:57:57 -0600 (CST)

Author: reinhard
Date: 2005-03-17 13:57:55 -0600 (Thu, 17 Mar 2005)
New Revision: 7213

Removed:
   trunk/gnue-common/src/datasources/drivers/appserver/appserver/RecordSet.py
Modified:
   trunk/gnue-common/src/datasources/drivers/Base/Connection.py
   trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py
   trunk/gnue-common/src/datasources/drivers/appserver/appserver/Connection.py
   trunk/gnue-common/src/datasources/drivers/appserver/appserver/ResultSet.py
   trunk/gnue-common/src/datasources/drivers/appserver/appserver/__init__.py
Log:
Updated record initialization to a more general approach, this obsoletes
appserver's own RecordSet class.


Modified: trunk/gnue-common/src/datasources/drivers/Base/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/Base/Connection.py        
2005-03-17 00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/Base/Connection.py        
2005-03-17 19:57:55 UTC (rev 7213)
@@ -119,6 +119,21 @@
     gLeave (8)
 
   # ---------------------------------------------------------------------------
+  # Initialize a new record with default data
+  # ---------------------------------------------------------------------------
+
+  def initialize (self, table):
+    """
+    Return default values for new records.
+
+    @param table: Table name.
+    @return: Dictionary with fieldname/value pairs.
+    """
+
+    gEnter (8)
+    return gLeave (8, self._initialize (table))
+
+  # ---------------------------------------------------------------------------
   # Insert a new record in the backend
   # ---------------------------------------------------------------------------
 
@@ -463,6 +478,21 @@
 
   # ---------------------------------------------------------------------------
 
+  def _initialize (self, table):
+    """
+    Return default values for new records (to be implemented by descendants).
+
+    Database drivers can overwrite this method to return default data that has
+    to be included in the _insert newfields dictionary.  Appserver uses this to
+    get the gnue_id for new records.
+
+    @param table: Table name.
+    @return: Dictionary with fieldname/value pairs.
+    """
+    return {}
+
+  # ---------------------------------------------------------------------------
+
   def _insert (self, table, newfields, recno):
     """
     Insert a new record in the backend (to be implemented by descendants).

Modified: trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2005-03-17 
00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2005-03-17 
19:57:55 UTC (rev 7213)
@@ -64,17 +64,48 @@
     self._initialData = initialData
 
     if self._initialData and len (self._initialData):
+
+      # Existing record:
+      # Set the current state of all fields as given in the parameter
       for (field, value) in initialData.items ():
         self._fields [field.lower ()] = value
+
     else:
+
+      # New record:
+      # 1. mark as new
       self._insertFlag = True
       self._emptyFlag  = True
+
+      do = self._parent._dataObject
+
+      # 2. Set all fields to None, so that the dictionary at least contains a
+      #    key for every field
+      for field in do._fieldReferences.keys ():
+        self._fields [field.lower ()] = None
+
+      # 3. Get default values from driver (e.g. initialize primary keys)
+      if do._connection:                # There are datasources w/o connection!
+        defaults = do._connection.initialize (do.table)
+        for (field, value) in defaults.items ():
+          self._fields [field.lower ()] = value
+          # Make sure the default values are included in the insert field list
+          self._modifiedFlags [field.lower ()] = True
+      self._initialData = self._fields.copy ()
+
+      # 4. Query current data from the backend.  The requery function only does
+      #    something if the primary key was initialized above.  This is used
+      #    for appserver to retrieve the result of OnInit.
+      self.__requery ()
+
+      # 5. Get default values from DataSource.  This has to be done at the very
+      #    end, as these changes remain local until the first post.
       for (field, value) in defaultData.items ():
         self._fields [field.lower ()] = value
+        # Make sure the default values are included in the insert field list
+        self._modifiedFlags [field.lower ()] = True
 
-    gDebug (8, "%s initialized with %s" % (repr (self), repr (self._fields)))
 
-
   # ---------------------------------------------------------------------------
   # Field access
   # ---------------------------------------------------------------------------
@@ -281,15 +312,23 @@
 
     This method may not be called if the record has unsaved changes; they would
     get lost!
+
+    This method does nothing if no primary key is available.
     """
-    fields = [field for field in self._fields.keys ()
-              if self._parent.isFieldBound (field)]
+
     do = self._parent._dataObject
-    newfields = do._connection.requery (do.table, self.__wherefields (), 
fields)
-    self._initialData.update (newfields)
-    self._fields.update (newfields)
 
+    if self._initialData.has_key (do._primaryIdField) and \
+       self._initialData [do._primaryIdField] is not None:
 
+      fields = [field for field in self._fields.keys ()
+                if self._parent.isFieldBound (field)]
+      newfields = do._connection.requery (do.table, self.__wherefields (),
+                                          fields)
+      self._initialData.update (newfields)
+      self._fields.update (newfields)
+
+
   # ---------------------------------------------------------------------------
   # Post changes to database
   # ---------------------------------------------------------------------------
@@ -365,8 +404,7 @@
 
     # Now, requery, as the posting of the record and/or of the details could
     # have changed something
-    if not deleting and \
-        self._initialData.has_key (self._parent._dataObject._primaryIdField):
+    if not deleting:
       self.__requery ()
 
 

Modified: 
trunk/gnue-common/src/datasources/drivers/appserver/appserver/Connection.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/appserver/appserver/Connection.py 
2005-03-17 00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/appserver/appserver/Connection.py 
2005-03-17 19:57:55 UTC (rev 7213)
@@ -158,6 +158,12 @@
 
   # ---------------------------------------------------------------------------
 
+  def _initialize (self, table):
+    id = self._sm.store (self._sess_id, table, [None], [], [[]]) [0]
+    return {'gnue_id': id}
+
+  # ---------------------------------------------------------------------------
+
   def _insert (self, table, newfields, recno):
     f = newfields.copy ()
     id = f.pop ('gnue_id')

Deleted: 
trunk/gnue-common/src/datasources/drivers/appserver/appserver/RecordSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/appserver/appserver/RecordSet.py  
2005-03-17 00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/appserver/appserver/RecordSet.py  
2005-03-17 19:57:55 UTC (rev 7213)
@@ -1,103 +0,0 @@
-# GNU Enterprise Datasource Library - Driver for GNUe-AppServer
-#
-# Copyright 2000-2005 Free Software Foundation
-#
-# This file is part of GNU Enterprise.
-#
-# GNU Enterprise is free software; you can redistribute it
-# and/or modify it under the terms of the GNU General Public
-# License as published by the Free Software Foundation; either
-# version 2, or (at your option) any later version.
-#
-# GNU Enterprise is distributed in the hope that it will be
-# useful, but WITHOUT ANY WARRANTY; without even the implied
-# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-# PURPOSE. See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public
-# License along with program; see the file COPYING. If not,
-# write to the Free Software Foundation, Inc., 59 Temple Place
-# - Suite 330, Boston, MA 02111-1307, USA.
-#
-# $Id$
-
-from gnue.common.apps import errors
-
-from gnue.common.datasources.drivers import Base
-
-# =============================================================================
-# RecordSet class
-# =============================================================================
-
-class RecordSet (Base.RecordSet):
-  """
-  Handles a record (i.e. an instance) in the GNUe-AppServer backend.
-  """
-
-  # ---------------------------------------------------------------------------
-  # Initialization
-  # ---------------------------------------------------------------------------
-
-  def __init__ (self, parent, sm, session_id, classname, initialData = {}):
-
-    Base.RecordSet.__init__ (self, parent, initialData = initialData)
-
-    self.__sm         = sm
-    self.__session_id = session_id
-    self.__classname  = classname
-
-    if not self._fields.has_key ('gnue_id'):
-      self.__initRecord ()
-
-
-  # ---------------------------------------------------------------------------
-  # Initialize a record 
-  # ---------------------------------------------------------------------------
-
-  def __initRecord (self):
-    """
-    This function creates a new instance of a record and loads all fields from
-    the backend. The store () creates a new gnue_id, calls all OnInit-triggers
-    of the class and loads all fields afterwards, where the state of the record
-    is still 'clean'.
-    """
-
-    self._fields ['gnue_id'] = self.__sm.store (self.__session_id,
-                                                self.__classname,
-                                                [None], [], [[]]) [0]
-    for field in self._parent._dataObject._fieldReferences.keys ():
-      if not self._fields.has_key (field.lower ()):
-        self._fields [field.lower ()] = None
-
-    # Now get default values from the backend
-    self.__updateFields ()
-
-    # They are our initial data, too
-    self._initialData = self._fields.copy ()
-
-    # Make sure that the gnue_id is included in the following insert operation.
-    # This flag does *not* mark the record itself as dirty, so it won't be
-    # included in the next post unless there are further modifications.
-    self._modifiedFlags ['gnue_id'] = True
-
-
-  # ---------------------------------------------------------------------------
-  # Update all fields of a record after update/inserts
-  # ---------------------------------------------------------------------------
-
-  def __updateFields (self):
-    """
-    This function loads all fields from the backend and updates the record's
-    _field dictionary with the new values.
-    """
-
-    propertylist = []
-    for item in self._fields.keys ():
-      if self._parent.isFieldBound (item):
-        propertylist.append (item)
-
-    res = self.__sm.load (self.__session_id, self.__classname,
-                          [self._fields ['gnue_id']], propertylist)
-
-    for ix in range (0, len (propertylist)):
-      self._fields [propertylist [ix]] = res [0][ix]

Modified: 
trunk/gnue-common/src/datasources/drivers/appserver/appserver/ResultSet.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/appserver/appserver/ResultSet.py  
2005-03-17 00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/appserver/appserver/ResultSet.py  
2005-03-17 19:57:55 UTC (rev 7213)
@@ -23,8 +23,6 @@
 
 from gnue.common.datasources.drivers import Base
 
-import RecordSet
-
 # =============================================================================
 # ResultSet class
 # =============================================================================
@@ -33,8 +31,6 @@
   """
   Handles a resultset (i.e. a list) in the GNUe-AppServer backend.
   """
-  # This is actually never used
-  _recordSetClass = RecordSet.RecordSet
 
   # ---------------------------------------------------------------------------
   # Initialization
@@ -91,16 +87,7 @@
         dict [fieldName] = record [j]
         j += 1
 
-      r = RecordSet.RecordSet (self, self.__sm, self.__session_id,
-                               self.__classname, dict)
+      r = self._recordSetClass (parent = self, initialData = dict)
       self._cachedRecords.append (r)
 
     return True
-
-  # ---------------------------------------------------------------------------
-  # Create an empty record
-  # ---------------------------------------------------------------------------
-
-  def _createEmptyRecord (self):
-    return RecordSet.RecordSet (self, self.__sm, self.__session_id,
-                                self.__classname)

Modified: 
trunk/gnue-common/src/datasources/drivers/appserver/appserver/__init__.py
===================================================================
--- trunk/gnue-common/src/datasources/drivers/appserver/appserver/__init__.py   
2005-03-17 00:20:01 UTC (rev 7212)
+++ trunk/gnue-common/src/datasources/drivers/appserver/appserver/__init__.py   
2005-03-17 19:57:55 UTC (rev 7213)
@@ -28,4 +28,3 @@
 from Connection import Connection
 from DataObject import DataObject
 from ResultSet import ResultSet
-from RecordSet import RecordSet





reply via email to

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