# HG changeset patch # User casties # Date 1284057543 -7200 # Node ID 435d6664ed90de15b25bbb935eca042422278081 # Parent 17dcf148beb344d914ef9f808b467ad30a7f3ad6 more json store -- stores now diff -r 17dcf148beb3 -r 435d6664ed90 RestDbJsonStore.py --- 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("\n\n
")
-                json.dump(ret, RESPONSE)
-                RESPONSE.write("
\n\n") + 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())