[Zope-Checkins] CVS: Zope/lib/python/Shared/DC/ZRDB - DA.py:1.106

Andy McKay andy@agmweb.ca
Fri, 22 Nov 2002 21:07:08 -0500


Update of /cvs-repository/Zope/lib/python/Shared/DC/ZRDB
In directory cvs.zope.org:/tmp/cvs-serv17966/DC/ZRDB

Modified Files:
	DA.py 
Log Message:
Allowing DA to have an option connection hook which allows you to change the database connection at run time for a sqlmethod.


=== Zope/lib/python/Shared/DC/ZRDB/DA.py 1.105 => 1.106 ===
--- Zope/lib/python/Shared/DC/ZRDB/DA.py:1.105	Wed Aug 14 17:50:59 2002
+++ Zope/lib/python/Shared/DC/ZRDB/DA.py	Fri Nov 22 21:07:06 2002
@@ -74,6 +74,7 @@
     _zclass=None
     allow_simple_one_argument_traversal=None
     template_class=SQL
+    connection_hook=None
 
     manage_options=(
         (
@@ -183,7 +184,7 @@
 
     def manage_advanced(self, max_rows, max_cache, cache_time,
                         class_name, class_file, direct=None,
-                        REQUEST=None, zclass=''):
+                        REQUEST=None, zclass='', connection_hook=None):
         """Change advanced properties
 
         The arguments are:
@@ -230,6 +231,8 @@
         self._v_brain=getBrain(self.class_file_, self.class_name_, 1)
         self.allow_simple_one_argument_traversal=direct
 
+        self.connection_hook = connection_hook
+
         if zclass:
             for d in self.aq_acquire('_getProductRegistryData')('zclasses'):
                 if ("%s/%s" % (d.get('product'),d.get('id'))) == zclass:
@@ -341,7 +344,11 @@
     def _searchable_result_columns(self): return self._col
 
     def _cached_result(self, DB__, query):
-
+        pure_query = query
+        # we need to munge the incoming query key in the cache
+        # so that the same request to a different db is returned
+        query += '\nDBConnId: %s' % self.connection_hook
+        
         # Try to fetch from cache
         if hasattr(self,'_v_cache'): cache=self._v_cache
         else: cache=self._v_cache={}, Bucket()
@@ -364,7 +371,8 @@
             k, r = cache[query]
             if k > t: return r
 
-        result=apply(DB__.query, query)
+        # call the pure query
+        result=apply(DB__.query, pure_query)
         if self.cache_time_ > 0:
             tcache[int(now)]=query
             cache[query]= now, result
@@ -389,11 +397,18 @@
                 if hasattr(self, 'REQUEST'): REQUEST=self.REQUEST
                 else: REQUEST={}
 
-        try: dbc=getattr(self, self.connection_id)
+        # connection hook
+        c = self.connection_id
+        # for backwards compatability
+        hk = self.connection_hook
+        # go get the connection hook and call it
+        if hk: c = getattr(self, hk)()
+           
+        try: dbc=getattr(self, c)
         except AttributeError:
             raise AttributeError, (
                 "The database connection <em>%s</em> cannot be found." % (
-                self.connection_id))
+                c))
 
         try: DB__=dbc()
         except: raise 'Database Error', (