changeset 17:ed997e639cfd

more upload table
author casties
date Tue, 17 Aug 2010 11:16:16 +0200
parents cbb73d103152
children 060797795a4d
files RestDbInterface.py
diffstat 1 files changed, 23 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbInterface.py	Mon Aug 16 21:24:00 2010 +0200
+++ b/RestDbInterface.py	Tue Aug 17 11:16:16 2010 +0200
@@ -85,7 +85,7 @@
         self.manage_addFolder('template')
 
 
-    def getCursor(self):
+    def getCursor(self,autocommit=True):
         """returns fresh DB cursor"""
         conn = getattr(self,"_v_database_connection",None)
         if conn is None:
@@ -109,30 +109,28 @@
                 raise IOError("No database connection! (%s)"%str(e))
         
         cursor = conn.getcursor()
-        cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
+        if autocommit:
+            # is there a better version to get to the connection?
+            cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
+            
         return cursor
     
-    def executeSQL(self, query, *args):
+    def executeSQL(self, query, args=None, hasResult=True, autocommit=True):
         """execute query with args on database and return all results.
         result format: {"fields":fields, "rows":data}"""
         logging.debug("executeSQL query=%s args=%s"%(query,args))
-        cur = self.getCursor()
+        cur = self.getCursor(autocommit=autocommit)
         cur.execute(query, args)
         # description of returned fields 
         fields = cur.description
-        # get all data in an array
-        data = cur.fetchall()
-        cur.close()
-        return {"fields":fields, "rows":data}
-
-    def executeOnlySQL(self, query, *args):
-        """execute query with args on database that has no results.
-        result format: {"fields":fields, "rows":data}"""
-        logging.debug("executeOnlySQL query=%s args=%s"%(query,args))
-        cur = self.getCursor()
-        cur.execute(query, args)
-        cur.close()
-        return None
+        if hasResult:
+            # get all data in an array
+            data = cur.fetchall()
+            cur.close()
+            return {"fields":fields, "rows":data}
+        else:
+            cur.close()
+            return None
 
     
     def publishTraverse(self,request,name):
@@ -215,7 +213,8 @@
                 return
 
             logging.debug("put with schema=%s table=%s file=%s"%(schema,tablename,file))
-            self.createTableFromXML(schema, tablename, file)
+            ret = self.createTableFromXML(schema, tablename, file)
+            
             
         else:
             # 400 Bad Request
@@ -301,23 +300,23 @@
         try:
             res = self.executeSQL('select * from "%s"."%s" where 1=0'%(schema,table))
             logging.debug("createemptytable: table %s.%s exists"%(schema,table))
-            self.executeOnlySQL('drop table "%s"."%s"'%(schema,table))
+            self.executeSQL('drop table "%s"."%s"'%(schema,table),hasResult=False)
         except:
             pass
         
         fieldString = ", ".join(['"%s" %s'%(f['name'],f['type']) for f in sqlFields])
         sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString)
         logging.debug("createemptytable: SQL=%s"%sqlString)
-        ret = self.executeOnlySQL(sqlString)
+        ret = self.executeSQL(sqlString,hasResult=False)
         
         return sqlFields
-        
-        
 
     def createTableFromXML(self,schema,table,data):
         """create or replace a table with the given XML data"""
         logging.debug("createTableFromXML schema=%s table=%s data=%s"%(schema,table,data))
-        self.importExcelXML(schema,table, data)
+        tablename = sqlName(table)
+        self.importExcelXML(schema,tablename, data)
+        return {"tablename": tablename}
         
     def importExcelXML(self,schema,table,xmldata,fieldNamesOnly=False):
         '''
@@ -415,7 +414,7 @@
                             data.extend(missFields * [None,])
                             
                         logging.debug("importexcel sqlinsert=%s data=%s"%(sqlInsert,data))
-                        self.executeOnlySQL(sqlInsert, *data)
+                        self.executeSQL(sqlInsert, data, hasResult=False)
                       
         return cnt