Commit c04f90ce authored by Andy McKay's avatar Andy McKay

Allowing DA to have an option connection hook which allows you to change the...

Allowing DA to have an option connection hook which allows you to change the database connection at run time for a sqlmethod.
parent 31b1e47c
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
__doc__='''Generic Database adapter''' __doc__='''Generic Database adapter'''
__version__='$Revision: 1.105 $'[11:-2] __version__='$Revision: 1.106 $'[11:-2]
import OFS.SimpleItem, Aqueduct, RDB, re import OFS.SimpleItem, Aqueduct, RDB, re
import DocumentTemplate, marshal, md5, base64, Acquisition, os import DocumentTemplate, marshal, md5, base64, Acquisition, os
...@@ -74,6 +74,7 @@ class DA( ...@@ -74,6 +74,7 @@ class DA(
_zclass=None _zclass=None
allow_simple_one_argument_traversal=None allow_simple_one_argument_traversal=None
template_class=SQL template_class=SQL
connection_hook=None
manage_options=( manage_options=(
( (
...@@ -183,7 +184,7 @@ class DA( ...@@ -183,7 +184,7 @@ class DA(
def manage_advanced(self, max_rows, max_cache, cache_time, def manage_advanced(self, max_rows, max_cache, cache_time,
class_name, class_file, direct=None, class_name, class_file, direct=None,
REQUEST=None, zclass=''): REQUEST=None, zclass='', connection_hook=None):
"""Change advanced properties """Change advanced properties
The arguments are: The arguments are:
...@@ -230,6 +231,8 @@ class DA( ...@@ -230,6 +231,8 @@ class DA(
self._v_brain=getBrain(self.class_file_, self.class_name_, 1) self._v_brain=getBrain(self.class_file_, self.class_name_, 1)
self.allow_simple_one_argument_traversal=direct self.allow_simple_one_argument_traversal=direct
self.connection_hook = connection_hook
if zclass: if zclass:
for d in self.aq_acquire('_getProductRegistryData')('zclasses'): for d in self.aq_acquire('_getProductRegistryData')('zclasses'):
if ("%s/%s" % (d.get('product'),d.get('id'))) == zclass: if ("%s/%s" % (d.get('product'),d.get('id'))) == zclass:
...@@ -341,6 +344,10 @@ class DA( ...@@ -341,6 +344,10 @@ class DA(
def _searchable_result_columns(self): return self._col def _searchable_result_columns(self): return self._col
def _cached_result(self, DB__, query): 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 # Try to fetch from cache
if hasattr(self,'_v_cache'): cache=self._v_cache if hasattr(self,'_v_cache'): cache=self._v_cache
...@@ -364,7 +371,8 @@ class DA( ...@@ -364,7 +371,8 @@ class DA(
k, r = cache[query] k, r = cache[query]
if k > t: return r 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: if self.cache_time_ > 0:
tcache[int(now)]=query tcache[int(now)]=query
cache[query]= now, result cache[query]= now, result
...@@ -389,11 +397,18 @@ class DA( ...@@ -389,11 +397,18 @@ class DA(
if hasattr(self, 'REQUEST'): REQUEST=self.REQUEST if hasattr(self, 'REQUEST'): REQUEST=self.REQUEST
else: 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: except AttributeError:
raise AttributeError, ( raise AttributeError, (
"The database connection <em>%s</em> cannot be found." % ( "The database connection <em>%s</em> cannot be found." % (
self.connection_id)) c))
try: DB__=dbc() try: DB__=dbc()
except: raise 'Database Error', ( except: raise 'Database Error', (
......
...@@ -7,6 +7,19 @@ ...@@ -7,6 +7,19 @@
<table cellspacing="0" cellpadding="2" border="0"> <table cellspacing="0" cellpadding="2" border="0">
<tr> <tr>
<td align="left" valign="top"> <td align="left" valign="top">
<div class="form-optional">
Connection Hook
</div>
</td>
<td align="LEFT" valign="TOP">
<input type="TEXT" name="connection_hook" size="40"
value="<dtml-var connection_hook missing="" html_quote>">
</td>
</tr>
<tr>
<td align="left" valign="top">
<div class="form-label"> <div class="form-label">
Maximum rows to retrieve Maximum rows to retrieve
</div> </div>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment