diff DBInterface.py @ 3:d70e57193731

new executeZSQL method that returns Zope Results. new ZDBInterfaceFolder that doesn't do much yet.
author casties
date Mon, 14 Feb 2011 23:20:43 +0100
parents 881fcea6a57d
children 0ade331198de
line wrap: on
line diff
--- a/DBInterface.py	Mon Feb 14 11:11:34 2011 +0100
+++ b/DBInterface.py	Mon Feb 14 23:20:43 2011 +0100
@@ -12,6 +12,7 @@
 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
 
 from Products.ZSQLMethods.SQL import SQLConnectionIDs
+from Shared.DC.ZRDB.Results import Results
 
 
 def unicodify(s,alternate='latin-1'):
@@ -65,46 +66,80 @@
     return s
 
 
-class DbInterface:
-    """Object for database queries
-    """
-    def __init__(self, id, title, connection_id=None):
+class DBInterface:
+    """Object for database queries"""
+    
+    def __init__(self, connection_id=None):
         """init"""
-        self.id = id
-        self.title = title
         # database connection id
         self.connection_id = connection_id        
 
+    def getConnectionIDs(self):
+        """return list of available connection ids"""
+        return SQLConnectionIDs(self)
+
+    def getDB(self):
+        """returns DB object"""
+        # TODO: can we cache and reuse a DB object?
+        if self.connection_id is None:
+            # try to take the first existing ID
+            connids = self.getConnectionIDs()
+            if len(connids) > 0:
+                connection_id = connids[0][1]
+                self.connection_id = connection_id
+                logging.debug("connection_id: %s"%repr(connection_id))
+
+        # get Connection instance
+        con = getattr(self, self.connection_id)
+        # call to get db object
+        db = con()
+        return db
+    
+    def executeZSQL(self, query, args=None, max_rows=None):
+        """execute query with args on the database and return all results as Result object."""
+        logging.debug("executeZSQL query=%s args=%s"%(query,args))
+        dbc = self.getDB()
+        res = dbc.query(query, max_rows=max_rows, query_data=args)
+        # return result set as Result object with Brains
+        return Results(res)
+
+
+    #
+    # Old way using cursor from DA
+    # 
+    
     def getCursor(self,autocommit=True):
         """returns fresh DB cursor"""
-        conn = getattr(self,"_v_database_connection",None)
+        conn = getattr(self,"_v_database_connection", None)
         if conn is None:
             # create a new connection object
             try:
                 if self.connection_id is None:
                     # try to take the first existing ID
-                    connids = SQLConnectionIDs(self)
+                    connids = self.getConnectionIDs()
                     if len(connids) > 0:
-                        connection_id = connids[0][0]
+                        connection_id = connids[0][1]
                         self.connection_id = connection_id
                         logging.debug("connection_id: %s"%repr(connection_id))
 
                 da = getattr(self, self.connection_id)
+                logging.debug('da=%s'%da)
                 da.connect('')
                 # we copy the DAs database connection
                 conn = da._v_database_connection
-                #conn._register() # register with the Zope transaction system
+                #conn._register() # register with the Zope transaction system(?)
                 self._v_database_connection = conn
+                
             except Exception, e:
                 raise IOError("No database connection! (%s)"%str(e))
         
         cursor = conn.getcursor()
         if autocommit:
-            # is there a better version to get to the connection?
+            # TODO: is there a better version to get to the connection?
             cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
             
         return cursor
-    
+
     def getFieldNameMap(self,fields):
         """returns a dict mapping field names to row indexes"""
         map = {}