changeset 52:435d6664ed90

more json store -- stores now
author casties
date Thu, 09 Sep 2010 20:39:03 +0200
parents 17dcf148beb3
children f5bfcfa97e7e
files RestDbJsonStore.py
diffstat 1 files changed, 42 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbJsonStore.py	Thu Sep 09 10:01:16 2010 +0200
+++ b/RestDbJsonStore.py	Thu Sep 09 20:39:03 2010 +0200
@@ -126,37 +126,26 @@
         """
         path = REQUEST.get('restdb_path',[])
         logging.debug("RestDbInterface PUT (path=%s)"%repr(path))
+        logging.debug("PUT request=%s"%REQUEST)
         #logging.debug("req=%s"%REQUEST)
         #self.dav__init(REQUEST, RESPONSE)
         #self.dav__simpleifhandler(REQUEST, RESPONSE)
         # ReST path was stored in request
-        if len(path) == 3:
+        if len(path) == 6:
             schema = path[1]
-            tablename = path[2]
-            file = REQUEST.get("create_table_file",None)
-            if file is None:
-                RESPONSE.setStatus(400)
-                return
+            table = path[2]
+            tag = path[3]
+            type = path[4]
+            item = path[5]
+            # maybe the data was form-encoded
+            value = REQUEST.get('json_string', None)
+            if value is None:
+                # then maybe in the body (the hard way...)
+                REQUEST.stdin.seek(0)
+                value = REQUEST.stdin.read()
 
-            fields = None
-            fieldsStr = REQUEST.get("create_table_fields",None)
-            logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fieldsStr)))
-            if fieldsStr is not None:
-                # unpack fields
-                fields = [{"name":n, "type": t} for (n,t) in [f.split(":") for f in fieldsStr.split(",")]]
-                
-            ret = self.createTableFromXML(schema, tablename, file, fields)
-            # return the result as JSON
-            format = REQUEST.get("format","JSON")
-            if format == "JSON":
-                RESPONSE.setHeader("Content-Type", "application/json")
-                json.dump(ret, RESPONSE)
-                
-            elif format == "JSONHTML":
-                RESPONSE.setHeader("Content-Type", "text/html")
-                RESPONSE.write("<html>\n<body>\n<pre>")
-                json.dump(ret, RESPONSE)
-                RESPONSE.write("</pre>\n</body>\n</html>")
+            logging.debug("put with schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
+            res = self.storeItem(schema,table,tag,type,item,value)
             
         else:
             # 400 Bad Request
@@ -164,7 +153,7 @@
             return
     
     def showListOfTags(self,resultFormat,schema,table):
-        """returns the list of existing tags"""
+        """shows the list of existing tags"""
         tags = self.getListOfTags(schema, table)
         if resultFormat == 'JSON':
             self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
@@ -184,7 +173,7 @@
         return []
     
     def showListOfTypes(self,resultFormat,schema,table,tag):
-        """returns the list of existing types"""
+        """shows the list of existing types"""
         types = self.getListOfTypes(schema, table,tag)
         if resultFormat == 'JSON':
             self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
@@ -193,7 +182,7 @@
             json.dump(types, self.REQUEST.RESPONSE)        
         
     def getListOfTypes(self,schema,table,tag):
-        """returns the list of existing tags"""
+        """returns the list of existing types"""
         logging.debug("getlistoftypes schema=%s table=%s tag=%s"%(schema,table,tag))
         sql = 'select distinct json_type from "%s"."%s" where json_tag = %%s'%(schema,table)
         res = self.executeSQL(sql,(tag,))
@@ -204,7 +193,7 @@
         return []
     
     def showListOfItems(self,resultFormat,schema,table,tag,type):
-        """returns the list of existing items"""
+        """shows the list of existing items"""
         items = self.getListOfItems(schema, table, tag, type)
         if resultFormat == 'JSON':
             self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
@@ -213,7 +202,7 @@
             json.dump(items, self.REQUEST.RESPONSE)        
         
     def getListOfItems(self,schema,table,tag,type):
-        """returns the list of existing tags"""
+        """returns the list of existing items"""
         logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type))
         sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table)
         res = self.executeSQL(sql,(tag,type))
@@ -224,16 +213,17 @@
         return []
     
     def showItem(self,resultFormat,schema,table,tag,type,item):
-        """returns the item"""
+        """shows the item"""
         item = self.getItem(schema, table, tag, type, item)
+        # item is a string
         if resultFormat == 'JSON':
             self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
-            json.dump(item, self.REQUEST.RESPONSE)        
+            self.REQUEST.RESPONSE.write(item)        
         else:
-            json.dump(item, self.REQUEST.RESPONSE)        
+            self.REQUEST.RESPONSE.write(item)        
         
     def getItem(self,schema,table,tag,type,item):
-        """returns the list of existing tags"""
+        """returns the item"""
         logging.debug("getitem schema=%s table=%s tag=%s type=%s item=%s"%(schema,table,tag,type,item))
         sql = 'select json_value from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
         res = self.executeSQL(sql,(tag,type,item))
@@ -242,7 +232,24 @@
             item = res['rows'][0][0]
             return item
         
-        return {}
+        return "{}"
+
+    def storeItem(self,schema,table,tag,type,item,value):
+        """sets the item to value"""
+        logging.debug("storeitem schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
+        # see if the item exists
+        sql = 'select 1 from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
+        res = self.executeSQL(sql,(tag,type,item))
+        if len(res['rows']) > 0:
+            # then update
+            sql = 'update "%s"."%s" set json_value = %%s where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
+        
+        else:
+            # then insert
+            sql = 'insert into "%s"."%s" (json_value, json_tag, json_type, json_item) values (%%s,%%s,%%s,%%s)'%(schema,table)
+        
+        self.executeSQL(sql,(value,tag,type,item), hasResult=False)
+        return "Ok"
     
 manage_addRestDbJsonStoreForm=PageTemplateFile('zpt/addRestDbJsonStore',globals())