Mercurial > hg > ChinaGisRestApi
comparison RestDbJsonStore.py @ 54:54940a99f12d
more work on json store
| author | casties |
|---|---|
| date | Mon, 20 Sep 2010 15:39:58 +0200 |
| parents | 435d6664ed90 |
| children | 9ec7e32e8ad3 |
comparison
equal
deleted
inserted
replaced
| 53:f5bfcfa97e7e | 54:54940a99f12d |
|---|---|
| 192 | 192 |
| 193 return [] | 193 return [] |
| 194 | 194 |
| 195 def showListOfItems(self,resultFormat,schema,table,tag,type): | 195 def showListOfItems(self,resultFormat,schema,table,tag,type): |
| 196 """shows the list of existing items""" | 196 """shows the list of existing items""" |
| 197 items = self.getListOfItems(schema, table, tag, type) | 197 recursive = self.REQUEST.get("recursive", False) |
| 198 if resultFormat == 'JSON': | 198 if recursive: |
| 199 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | 199 items = self.getListOfItemsAndValues(schema, table, tag, type) |
| 200 json.dump(items, self.REQUEST.RESPONSE) | 200 # items contain JSON-strings |
| 201 else: | 201 its = ",".join(['{"key":"%s","val":%s}'%(i[0],i[1]) for i in items]) |
| 202 json.dump(items, self.REQUEST.RESPONSE) | 202 its = "["+its+"]" |
| 203 if resultFormat == 'JSON': | |
| 204 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
| 205 self.REQUEST.RESPONSE.write(its) | |
| 206 else: | |
| 207 self.REQUEST.RESPONSE.write(its) | |
| 208 | |
| 209 else: | |
| 210 items = self.getListOfItems(schema, table, tag, type) | |
| 211 if resultFormat == 'JSON': | |
| 212 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
| 213 json.dump(items, self.REQUEST.RESPONSE) | |
| 214 else: | |
| 215 json.dump(items, self.REQUEST.RESPONSE) | |
| 216 | |
| 203 | 217 |
| 204 def getListOfItems(self,schema,table,tag,type): | 218 def getListOfItems(self,schema,table,tag,type): |
| 205 """returns the list of existing items""" | 219 """returns the list of existing items""" |
| 206 logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type)) | 220 logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type)) |
| 207 sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table) | 221 sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table) |
| 222 | |
| 208 res = self.executeSQL(sql,(tag,type)) | 223 res = self.executeSQL(sql,(tag,type)) |
| 209 if len(res['rows']) > 0: | 224 if len(res['rows']) > 0: |
| 210 items = [r[0] for r in res['rows']] | 225 items = [r[0] for r in res['rows']] |
| 211 return items | 226 return items |
| 227 | |
| 228 return [] | |
| 229 | |
| 230 def getListOfItemsAndValues(self,schema,table,tag,type): | |
| 231 """returns the list of existing items and their values""" | |
| 232 logging.debug("getlistofitemsandvalues schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type)) | |
| 233 sql = 'select json_item, json_value from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table) | |
| 234 | |
| 235 res = self.executeSQL(sql,(tag,type)) | |
| 236 if len(res['rows']) > 0: | |
| 237 return res['rows'] | |
| 212 | 238 |
| 213 return [] | 239 return [] |
| 214 | 240 |
| 215 def showItem(self,resultFormat,schema,table,tag,type,item): | 241 def showItem(self,resultFormat,schema,table,tag,type,item): |
| 216 """shows the item""" | 242 """shows the item""" |
