changeset 54:54940a99f12d

more work on json store
author casties
date Mon, 20 Sep 2010 15:39:58 +0200
parents f5bfcfa97e7e
children 2f4c427dec44
files RestDbJsonStore.py
diffstat 1 files changed, 31 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbJsonStore.py	Fri Sep 10 09:27:32 2010 +0200
+++ b/RestDbJsonStore.py	Mon Sep 20 15:39:58 2010 +0200
@@ -194,17 +194,32 @@
     
     def showListOfItems(self,resultFormat,schema,table,tag,type):
         """shows the list of existing items"""
-        items = self.getListOfItems(schema, table, tag, type)
-        if resultFormat == 'JSON':
-            self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
-            json.dump(items, self.REQUEST.RESPONSE)        
+        recursive = self.REQUEST.get("recursive", False)
+        if recursive:
+            items = self.getListOfItemsAndValues(schema, table, tag, type)
+            # items contain JSON-strings
+            its = ",".join(['{"key":"%s","val":%s}'%(i[0],i[1]) for i in items])
+            its = "["+its+"]"
+            if resultFormat == 'JSON':
+                self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
+                self.REQUEST.RESPONSE.write(its)       
+            else:
+                self.REQUEST.RESPONSE.write(its)       
+
         else:
-            json.dump(items, self.REQUEST.RESPONSE)        
+            items = self.getListOfItems(schema, table, tag, type)
+            if resultFormat == 'JSON':
+                self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
+                json.dump(items, self.REQUEST.RESPONSE)        
+            else:
+                json.dump(items, self.REQUEST.RESPONSE)
+                
         
     def getListOfItems(self,schema,table,tag,type):
         """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))
         if len(res['rows']) > 0:
             items = [r[0] for r in res['rows']]
@@ -212,6 +227,17 @@
         
         return []
     
+    def getListOfItemsAndValues(self,schema,table,tag,type):
+        """returns the list of existing items and their values"""
+        logging.debug("getlistofitemsandvalues schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type))
+        sql = 'select json_item, json_value from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table)
+            
+        res = self.executeSQL(sql,(tag,type))
+        if len(res['rows']) > 0:
+            return res['rows']
+        
+        return []
+    
     def showItem(self,resultFormat,schema,table,tag,type,item):
         """shows the item"""
         item = self.getItem(schema, table, tag, type, item)