# HG changeset patch # User casties # Date 1284989998 -7200 # Node ID 54940a99f12d120acb47109d2188ed13808af047 # Parent f5bfcfa97e7ec113d90a12f572b5d72f1e94276e more work on json store diff -r f5bfcfa97e7e -r 54940a99f12d RestDbJsonStore.py --- 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)