Mercurial > hg > ChinaGisRestApi
comparison RestDbInterface.py @ 28:9e4f9cfd1edc
start adding field structure to xml upload
| author | casties |
|---|---|
| date | Fri, 27 Aug 2010 19:19:38 +0200 |
| parents | a2e4ca3f1cff |
| children | 0b9f8cca6744 |
comparison
equal
deleted
inserted
replaced
| 27:a2e4ca3f1cff | 28:9e4f9cfd1edc |
|---|---|
| 226 file = REQUEST.get("create_table_file",None) | 226 file = REQUEST.get("create_table_file",None) |
| 227 if file is None: | 227 if file is None: |
| 228 RESPONSE.setStatus(400) | 228 RESPONSE.setStatus(400) |
| 229 return | 229 return |
| 230 | 230 |
| 231 logging.debug("put with schema=%s table=%s file=%s"%(schema,tablename,file)) | 231 fields = REQUEST.get("fields",None) |
| 232 ret = self.createTableFromXML(schema, tablename, file) | 232 logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fields))) |
| 233 ret = self.createTableFromXML(schema, tablename, file, fields) | |
| 233 # return the result as JSON | 234 # return the result as JSON |
| 234 format = REQUEST.get("format","JSON") | 235 format = REQUEST.get("format","JSON") |
| 235 if format == "JSON": | 236 if format == "JSON": |
| 236 RESPONSE.setHeader("Content-Type", "application/json") | 237 RESPONSE.setHeader("Content-Type", "application/json") |
| 237 json.dump(ret, RESPONSE) | 238 json.dump(ret, RESPONSE) |
| 416 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString) | 417 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString) |
| 417 logging.debug("createemptytable: SQL=%s"%sqlString) | 418 logging.debug("createemptytable: SQL=%s"%sqlString) |
| 418 self.executeSQL(sqlString,hasResult=False) | 419 self.executeSQL(sqlString,hasResult=False) |
| 419 return sqlFields | 420 return sqlFields |
| 420 | 421 |
| 421 def createTableFromXML(self,schema,table,data): | 422 def createTableFromXML(self,schema,table,data, fields=None): |
| 422 """create or replace a table with the given XML data""" | 423 """create or replace a table with the given XML data""" |
| 423 logging.debug("createTableFromXML schema=%s table=%s data=%s"%(schema,table,data)) | 424 logging.debug("createTableFromXML schema=%s table=%s data=%s fields=%s"%(schema,table,data,fields)) |
| 424 tablename = sqlName(table) | 425 tablename = sqlName(table) |
| 425 self.importExcelXML(schema,tablename, data) | 426 self.importExcelXML(schema, tablename, data, fields) |
| 426 return {"tablename": tablename} | 427 return {"tablename": tablename} |
| 427 | 428 |
| 428 def importExcelXML(self,schema,table,xmldata,fieldsOnly=False): | 429 def importExcelXML(self,schema,table,xmldata,fields=None,fieldsOnly=False): |
| 429 ''' | 430 ''' |
| 430 Import XML file in Excel format into the table | 431 Import XML file in Excel format into the table |
| 431 @param table: name of the table the xml shall be imported into | 432 @param table: name of the table the xml shall be imported into |
| 432 @param containerTagName: XML-Tag which describes a dataset | |
| 433 @param data: data to be imported (string or filehandle) | |
| 434 @param identify: (optional) field res. tag which identifies a entry uniquely for updating purposes. | |
| 435 @param RESPONSE: (optional) | |
| 436 ''' | 433 ''' |
| 437 from xml.dom.pulldom import parseString,parse | 434 from xml.dom.pulldom import parseString,parse |
| 438 | 435 |
| 439 namespace = "urn:schemas-microsoft-com:office:spreadsheet" | 436 namespace = "urn:schemas-microsoft-com:office:spreadsheet" |
| 440 containerTagName = "Table" | 437 containerTagName = "Table" |
| 485 if fieldsOnly: | 482 if fieldsOnly: |
| 486 # return just field names | 483 # return just field names |
| 487 return xmlFields | 484 return xmlFields |
| 488 | 485 |
| 489 # create table | 486 # create table |
| 490 sqlFields = self.createEmptyTable(schema, table, xmlFields) | 487 if fields is None: |
| 488 sqlFields = self.createEmptyTable(schema, table, xmlFields) | |
| 491 numFields = len(sqlFields) | 489 numFields = len(sqlFields) |
| 492 fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields]) | 490 fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields]) |
| 493 valString = ", ".join(["%s" for f in sqlFields]) | 491 valString = ", ".join(["%s" for f in sqlFields]) |
| 494 sqlInsert = 'insert into "%s"."%s" (%s) values (%s)'%(schema,table,fieldString,valString) | 492 sqlInsert = 'insert into "%s"."%s" (%s) values (%s)'%(schema,table,fieldString,valString) |
| 495 #logging.debug("importexcelsql: sqlInsert=%s"%sqlInsert) | 493 #logging.debug("importexcelsql: sqlInsert=%s"%sqlInsert) |
