changeset 8:17b19345d011

added autocommit option.
author casties
date Thu, 17 Feb 2011 19:25:16 +0100
parents 917e28a08c58
children 87087f4f4059
files DBInterface.py ZDBInterfaceFolder.py zpt/editRestDbInterface.zpt zpt/editZDBInterfaceFolder.zpt
diffstat 4 files changed, 38 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/DBInterface.py	Tue Feb 15 21:14:03 2011 +0100
+++ b/DBInterface.py	Thu Feb 17 19:25:16 2011 +0100
@@ -70,10 +70,11 @@
 class DBInterface:
     """Object for database queries"""
     
-    def __init__(self, connection_id=None):
+    def __init__(self, connection_id=None, autocommit=False):
         """init"""
         # database connection id
-        self.connection_id = connection_id        
+        self.connection_id = connection_id
+        self.autocommit = autocommit
 
     def getConnectionIDs(self):
         """return list of available connection ids"""
@@ -94,6 +95,10 @@
         con = getattr(self, self.connection_id)
         # call to get db object
         db = con()
+        if self.autocommit:
+            # force our transaction isolation level
+            db.tilevel = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
+            
         return db
     
     def executeZSQL(self, query, args=None, max_rows=None):
--- a/ZDBInterfaceFolder.py	Tue Feb 15 21:14:03 2011 +0100
+++ b/ZDBInterfaceFolder.py	Thu Feb 17 19:25:16 2011 +0100
@@ -26,13 +26,13 @@
     manage_editZDBInterfaceFolderForm=PageTemplateFile('zpt/editZDBInterfaceFolder',globals())
 
     
-    def __init__(self, id, title, connection_id=None):
+    def __init__(self, id, title, connection_id=None, autocommit=False):
         """init"""
         self.id = id
         self.title = title
         # database connection id
         self.connection_id = connection_id
-
+        self.autocommit = autocommit
 
     def getSQLQuery(self, table, argv):
         """returns dict with SQL query string and args"""
@@ -42,7 +42,8 @@
         order = None
         wheres = []
         whereOp = "AND"
-        done = {}
+        processed = {}
+        unprocessed = {}
         limit = None
         offset = None
 
@@ -95,7 +96,7 @@
 
         # evaluate argv
         for (key, val) in argv.items():
-            if key in done:
+            if key in processed:
                 # parameter has been processed
                 continue
             
@@ -124,17 +125,19 @@
                 logging.debug("param=%s key=%s val=%s"%(param,key,val))
                 if param in argv:
                     doOp(val, param, argv[param])
-                    done[param] = True
+                    processed[param] = True
                 else:
                     # no corresponding parameter
                     logging.error("getSQLquery: param=%s for op not found!"%param)
+            else:
+                # parameter=value pair
+                unprocessed[key] = val
                 
         # process remaining parameters (without _op)
-        for (key, val) in argv.items():
-            if key not in done and key[0] not in ("_", "-"):
-                param = sqlName(key)
-                # default is begins-with
-                doOp("bw", param, val)
+        for (key, val) in unprocessed.items():
+            param = sqlName(key)
+            # default is begins-with
+            doOp("bw", param, val)
 
         # join it all
         query = "SELECT %s FROM %s"%(select, table)
@@ -158,15 +161,18 @@
         result = self.executeZSQL(query['query'], query['args'])
         return result
     
-    def manage_editZDBInterfaceFolder(self, title=None, connection_id=None,
+    def manage_editZDBInterfaceFolder(self, title=None, connection_id=None, autocommit=None,
                      REQUEST=None):
         """Change the object"""
+        logging.debug("editZDBInterfaceFolder title=%s, connection_id=%s, autocommit=%s"%(title,connection_id,autocommit))
         if title is not None:
             self.title = title
             
         if connection_id is not None:
             self.connection_id = connection_id
                 
+        self.autocommit = (autocommit == "on")
+                
         #checkPermission=getSecurityManager().checkPermission
         REQUEST.RESPONSE.redirect('manage_main')
 
--- a/zpt/editRestDbInterface.zpt	Tue Feb 15 21:14:03 2011 +0100
+++ b/zpt/editRestDbInterface.zpt	Thu Feb 17 19:25:16 2011 +0100
@@ -6,11 +6,15 @@
     <p class="form-label">Connection ID</p>
     <p>
     <select name="connection_id">
-      <tal:block tal:repeat="id python:[i[0] for i in here.getConnectionIDs()]">
-        <option  tal:condition="python:getattr(here,'connection_id','')==id"  tal:attributes="value id" selected tal:content="id"/>
-        <option  tal:condition="not:python:getattr(here,'connection_id','')==id"  tal:attributes="value id" tal:content="id"/>
+      <tal:block tal:repeat="id here/getConnectionIDs">
+        <option  tal:condition="python:getattr(here,'connection_id','')==id[1]"  tal:attributes="value python:id[1]" selected tal:content="python:id[0]"/>
+        <option  tal:condition="not:python:getattr(here,'connection_id','')==id[1]"  tal:attributes="value python:id[1]" tal:content="python:id[0]"/>
       </tal:block>
     </select>
     </p>
+    <p class="form-label">Autocommit</p>
+    <p>
+      <input type="checkbox" name="autocommit" tal:attributes="checked here/autocommit"/> autocommit (don't use when you want transactions)
+    </p>
      <p><input type="submit" value="Change" /></p>
   </form>
--- a/zpt/editZDBInterfaceFolder.zpt	Tue Feb 15 21:14:03 2011 +0100
+++ b/zpt/editZDBInterfaceFolder.zpt	Thu Feb 17 19:25:16 2011 +0100
@@ -4,14 +4,17 @@
     <p class="form-label">Title</p>
         <p class="form-element"><input size="80" name="title" tal:attributes="value here/title"/></p>
     <p class="form-label">Connection ID</p>
-    <pre tal:content="python:repr(here.getConnectionIDs())"/>
     <p>
     <select name="connection_id">
-      <tal:block tal:repeat="id python:[i[0] for i in here.getConnectionIDs()]">
-        <option  tal:condition="python:getattr(here,'connection_id','')==id"  tal:attributes="value id" selected tal:content="id"/>
-        <option  tal:condition="not:python:getattr(here,'connection_id','')==id"  tal:attributes="value id" tal:content="id"/>
+      <tal:block tal:repeat="id here/getConnectionIDs">
+        <option  tal:condition="python:getattr(here,'connection_id','')==id[1]"  tal:attributes="value python:id[1]" selected tal:content="python:id[0]"/>
+        <option  tal:condition="not:python:getattr(here,'connection_id','')==id[1]"  tal:attributes="value python:id[1]" tal:content="python:id[0]"/>
       </tal:block>
     </select>
     </p>
+    <p class="form-label">Autocommit</p>
+    <p>
+      <input type="checkbox" name="autocommit" tal:attributes="checked here/autocommit"/> autocommit (don't use when you want transactions)
+    </p>
      <p><input type="submit" value="Change" /></p>
   </form>