[MOBY-guts] biomoby commit

Yan Wong yanwong at pub.open-bio.org
Tue Apr 19 15:41:50 UTC 2005


yanwong
Tue Apr 19 11:41:50 EDT 2005
Update of /home/repository/moby/moby-live/Python/bioMoby/webservice
In directory pub.open-bio.org:/tmp/cvs-serv16275/bioMoby/webservice

Modified Files:
	Dispatcher.py Invocators.py 
Log Message:
Added a SQL invocator and Dispatcher
this should ease the build of DB based webservices

moby-live/Python/bioMoby/webservice Dispatcher.py,1.4,1.5 Invocators.py,1.3,1.4
===================================================================
RCS file: /home/repository/moby/moby-live/Python/bioMoby/webservice/Dispatcher.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Python/bioMoby/webservice/Dispatcher.py	2005/02/04 12:58:24	1.4
+++ /home/repository/moby/moby-live/Python/bioMoby/webservice/Dispatcher.py	2005/04/19 15:41:50	1.5
@@ -517,3 +517,41 @@
 
         #if nothing works return nothing
         return {}
+
+class SQLDispatcher(AbstractDispatcher):
+    """ A dispatcher for SQL queries
+    """
+    def __init__(self, mobyContentXML, ConnectionObject, sqlCommand, CommandBuilder, formatter):
+        """ To instanciate this object, you need:
+            a DB Connection (as parameters vary from a DB to another, you must use a python DB api 2 Connection object here)
+            the sqlCommand: the sql command as described in the Python DB api
+            the commandBuilder: translate a moby query into a tuple moby query -> (param1, param2, param3)
+            formatter: a function that translate the results into a array of moby objects
+        """
+        from bioMoby.webservice import SQLInvocator
+           
+        AbstractDispatcher.__init__(self, mobyContentXML, SQLInvocator, ConnectionObject, CommandBuilder, formatter)
+        self._sqlCommand = sqlCommand
+    
+    def execute(self):
+        """ execute the sql command
+        """
+        try:
+            for key in self.queryData.keys():
+                query=self.queryData[key]
+                r=self.invocator(key, self._sqlCommand, self.commandBuilder(query), self._invocatorParameters)
+                r.execute()
+                self.answers[key]=self.formatter(r.results)
+        except:
+            import traceback
+            import StringIO
+            from bioMoby import MobyString
+            sio=StringIO.StringIO()
+            traceback.print_exc(file=sio)
+            
+            self.answers['JOBSESSION']=[MobyString(content=sio.getvalue())]
+        
+        self._invocatorParameters.close()
+
+        return self._toMoby()
+

===================================================================
RCS file: /home/repository/moby/moby-live/Python/bioMoby/webservice/Invocators.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- /home/repository/moby/moby-live/Python/bioMoby/webservice/Invocators.py	2005/02/01 08:52:17	1.3
+++ /home/repository/moby/moby-live/Python/bioMoby/webservice/Invocators.py	2005/04/19 15:41:50	1.4
@@ -169,7 +169,35 @@
         self.queueName=queueName
         self.qsubParameters=qsubParameters
         
+class SQLInvocator(GeneralInvocator):
+    """ Invokes SQL commands, 
+        you'll need to setup a connection before using ir
+    """
+    def __init__(self, queryKey, sqlCommand, sqlParameters, sqlConnection):
+        """ the quey of the query, the sql command and its parameters in a tuple
+        """
+        GeneralInvocator.__init__(self, queryKey)
+        
+        self._sqlCommand = sqlCommand
+        self._sqlParameters = sqlParameters
+        self._connection = sqlConnection
+        
+    def setParameters(self, sqlConnection):
+        """ Define a connection for this sql command
+        """
+        self._connection = sqlConnection
         
+    def execute(self):
+        """ execute the sql command
+        """
+        cursor = self._connection.cursor()
+        cursor.prepare(self._sqlCommand%self._sqlParameters)
+        
+        cursor.execute(None)
+        self.results=cursor.fetchall()
+        cursor.close()
+        
+
 class PBSInvocator(LocalInvocator):
     """Invokes a command in a qsub
     """




More information about the MOBY-guts mailing list