changeset 31:6d2055f1b4fa

more work on types
author casties
date Tue, 31 Aug 2010 10:54:00 +0200
parents 51db9e78bf98
children c732c2ff61d9
files RestDbInterface.py
diffstat 1 files changed, 23 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbInterface.py	Mon Aug 30 17:29:58 2010 +0200
+++ b/RestDbInterface.py	Tue Aug 31 10:54:00 2010 +0200
@@ -51,6 +51,15 @@
     
     return s
 
+gisToSqlTypeMap = {
+                   "text": "text",
+                   "number": "number",
+                   "id": "text",
+                   "gis_id": "integer",
+                   "coord_lat": "float",
+                   "coord_lon": "float"
+                   }
+
 class RestDbInterface(Folder):
     """Object for RESTful database queries
     path schema: /db/{schema}/{table}/
@@ -155,6 +164,10 @@
             cur.close()
             return None
 
+    def setTableMetaTypes(self,schema,table,fields):
+        """sets the GIS meta information for table"""
+        logging.debug("settablemetatypes schema=%s, table=%s, fields=%s"%(schema,table,fields))
+        # TODO: what now?
     
     def publishTraverse(self,request,name):
         """change the traversal"""
@@ -247,6 +260,7 @@
             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")
@@ -399,26 +413,22 @@
                 # {name: XX, type: YY}
                 name = sqlName(f['name'])
                 type = f['type']
+                sqltype = gisToSqlTypeMap[type]
             
             else:
                 # name only
                 name = sqlName(f)
                 type = 'text'
+                sqltype = 'text'
                 
-            sqlFields.append({'name':name, 'type':type})
+            sqlFields.append({'name':name, 'type':type, 'sqltype':sqltype})
             
-        # drop table if it exists
-        try:
-            res = self.executeSQL('select * from "%s"."%s" where 1=0'%(schema,table))
-            logging.debug("createemptytable: table %s.%s exists"%(schema,table))
-            self.executeSQL('drop table "%s"."%s"'%(schema,table),hasResult=False)
-        except:
-            pass
-        
-        fieldString = ", ".join(['"%s" %s'%(f['name'],f['type']) for f in sqlFields])
+        self.executeSQL('drop table if exists "%s"."%s"'%(schema,table),hasResult=False)
+        fieldString = ", ".join(['"%s" %s'%(f['name'],f['sqltype']) for f in sqlFields])
         sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString)
         logging.debug("createemptytable: SQL=%s"%sqlString)
         self.executeSQL(sqlString,hasResult=False)
+        self.setTableMetaTypes(schema,table,sqlFields)
         return sqlFields
     
     def createTableFromXML(self,schema,table,data, fields=None):
@@ -487,7 +497,9 @@
                         
                         # create table
                         if fields is None:
-                            sqlFields = self.createEmptyTable(schema, table, xmlFields)
+                            fields = xmlFields
+                            
+                        sqlFields = self.createEmptyTable(schema, table, fields)
                         numFields = len(sqlFields)
                         fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields])
                         valString = ", ".join(["%s" for f in sqlFields])