[Zope] calling (SQL) methods dynamically from External methods

Philipp Auersperg zope@philosoft.at
Mon, 21 Aug 2000 04:23:31 +0200


What I want to do is calling a SQL method given its
name as string, so I first have to get the method out of
the self.__dict__ and then call it. I need that for calling
the SQL methods dynamically for example out of an URL
string.

The problem:
------------------
when I call the method like that it does not find its database connection,
it seems that it cannot access other members of 'self'

When I call the SQL Method directly everything works (see full example below after the line)

Interesting fact:
When retrieving the method object with "m=self.dbTest" and then with "m1=self.__dict__['dbTest']" 
"print m,m1" show the same string but "m==m1" results to false!

What am I doing wrong? There seems something mixed up with namespaces, but I am too solly to solve it :(
any help appreciated

Philipp Auersperg (zwork)



---------------------------------------
def callSQLMethod(self,REQUEST):
      
        m1=self.__dict__['dbTest']             #get method via dict , 
				          #dbTest is a normal Z SQL method like 'select * from test'
                                                          # it uses the db connection 'mysql_test'

        
        rs=m1(self)                                    #then the called method 
                                                            #does not find the database
                                                            #connection


------------------------------------------------------------------------------------
full example

def callSQLMethod(self,REQUEST):
      
        rs=self.dbTest()      		#normal call, works - of course

        m=self.dbTest        		#first get method, then call it
        rs=m(self)              		#that works too

        m1=self.__dict__['dbTest'] 		#get method via dict
        print 'Methods',m,m1,m==m1 	#m and m1 print the same 
                                   			#name but are not equal!!
        
        rs=m1(self)                		#then the called method 
                                        		#does not find the database
                                        		#connection



-----------------------------------------------------
Error message:
The database connection mysql_test cannot be found. 

Traceback (innermost last):
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/ZPublisher/Publish.py, line 214, in publish_module
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/ZPublisher/Publish.py, line 179, in publish
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/Zope/__init__.py, line 202, in zpublisher_exception_hook
    (Object: ElementWithAttributes)
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/ZPublisher/Publish.py, line 165, in publish
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/ZPublisher/mapply.py, line 160, in mapply
    (Object: callSQLMethod)
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/ZPublisher/Publish.py, line 102, in call_object
    (Object: callSQLMethod)
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/Products/ExternalMethod/ExternalMethod.py, line 248, in __call__
    (Object: callSQLMethod)
    (Info: ((<Folder instance at 8607e58>, <h3>form</h3><table></table><h3>cookies</h3><table><tr valign="top" align="left"><th>tree-s</th><td>eJyLjlZ3hANPW3UdhWgkEXdXdJFQA1v1WDAAAK2tDwA</td></tr><tr valign="top" align="left"><th>sql_pref__cols</th><td>80</td></tr><tr valign="top" align="left"><th>dtpref_cols</th><td>65</td></tr><tr valign="top" align="left"><th>sql_pref__rows</th><td>20</td></tr><tr valign="top" align="left"><th>dtpref_rows</th><td>15</td></tr></table><h3>other</h3><table><tr valign="top" align="left"><th>BASE5</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>AUTHENTICATION_PATH</th><td></td></tr><tr valign="top" align="left"><th>sql_pref__rows</th><td>20</td></tr><tr valign="top" align="left"><th>dtpref_rows</th><td>15</td></tr><tr valign="top" align="left"><th>BASE3</th><td>http://192.7.2.21:8080/Test/Stream</td></tr><tr valign="top" align="left"><th>URL3</th><td>http://192.7.2.21:8080/Test</td></tr><tr valign="top" align="left"><th>URL0</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>BASE1</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>URL2</th><td>http://192.7.2.21:8080/Test/Stream</td></tr><tr valign="top" align="left"><th>URL4</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>BASE0</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>URL1</th><td>http://192.7.2.21:8080/Test/Stream/getRecords</td></tr><tr valign="top" align="left"><th>SERVER_URL</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>tree-s</th><td>eJyLjlZ3hANPW3UdhWgkEXdXdJFQA1v1WDAAAK2tDwA</td></tr><tr valign="top" align="left"><th>URL</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>AUTHENTICATED_USER</th><td>phil</td></tr><tr valign="top" align="left"><th>BASE2</th><td>http://192.7.2.21:8080/Test</td></tr><tr valign="top" align="left"><th>dtpref_co
 valign="top" align="left"><th>sql_pref__cols</th><td>80</td></tr><tr valign="top" align="left"><th>BASE4</th><td>http://192.7.2.21:8080/Test/Stream/getRecords</td></tr><tr valign="top" align="left"><th>URL0</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>URL1</th><td>http://192.7.2.21:8080/Test/Stream/getRecords</td></tr><tr valign="top" align="left"><th>URL2</th><td>http://192.7.2.21:8080/Test/Stream</td></tr><tr valign="top" align="left"><th>URL3</th><td>http://192.7.2.21:8080/Test</td></tr><tr valign="top" align="left"><th>URL4</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>BASE0</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>BASE1</th><td>http://192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>BASE2</th><td>http://192.7.2.21:8080/Test</td></tr><tr valign="top" align="left"><th>BASE3</th><td>http://192.7.2.21:8080/Test/Stream</td></tr><tr valign="top" align="left"><th>BASE4</th><td>http://192.7.2.21:8080/Test/Stream/getRecords</td></tr><tr valign="top" align="left"><th>BASE5</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod</td></tr></table><h3>environ</h3><table><tr valign="top" align="left"><th>HTTP_ACCEPT_ENCODING</th><td>gzip, deflate</td></tr><tr valign="top" align="left"><th>channel.creation_time</th><td>966817971</td></tr><tr valign="top" align="left"><th>SERVER_PROTOCOL</th><td>1.1</td></tr><tr valign="top" align="left"><th>SERVER_PORT</th><td>8080</td></tr><tr valign="top" align="left"><th>PATH_INFO</th><td>/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>HTTP_HOST</th><td>192.7.2.21:8080</td></tr><tr valign="top" align="left"><th>REQUEST_METHOD</th><td>GET</td></tr><tr valign="top" align="left"><th>PATH_TRANSLATED</th><td>/Test/Stream/getRecords/callSQLMethod</td></tr><tr valign="top" align="left"><th>HTTP_COOKIE</th><td>dtpref_rows="15"; dtpref_cols="65"; sql_pref__rows="20"; sql_pref__cols="80"; tree-s="eJyLjlZ3hANPW3UdhWgk
 valign="top" align="left"><th>SCRIPT_NAME</th><td></td></tr><tr valign="top" align="left"><th>SERVER_SOFTWARE</th><td>Zope/Zope 2.1.4 (binary release, python 1.5.2, linux2-x86) ZServer/1.1b1</td></tr><tr valign="top" align="left"><th>HTTP_ACCEPT_LANGUAGE</th><td>de-at</td></tr><tr valign="top" align="left"><th>HTTP_ACCEPT</th><td>image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */*</td></tr><tr valign="top" align="left"><th>REMOTE_ADDR</th><td>192.7.2.2</td></tr><tr valign="top" align="left"><th>HTTP_REFERER</th><td>http://192.7.2.21:8080/Test/Stream/getRecords/callSQLMethod/manage_main?</td></tr><tr valign="top" align="left"><th>SERVER_NAME</th><td>magnix.philosoft.at</td></tr><tr valign="top" align="left"><th>GATEWAY_INTERFACE</th><td>CGI/1.1</td></tr><tr valign="top" align="left"><th>HTTP_USER_AGENT</th><td>Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)</td></tr><tr valign="top" align="left"><th>CONNECTION_TYPE</th><td>Keep-Alive</td></tr></table>), {}, None))
  File /usr/local/Zope-2.1.4-linux2-x86/Extensions/BlueWire.py, line 105, in callSQLMethod
    (Object: ElementWithAttributes)
  File /usr/local/Zope-2.1.4-linux2-x86/lib/python/Shared/DC/ZRDB/DA.py, line 384, in __call__
    (Object: dbTest)
AttributeError: (see above)