diff RestDbInterface.py @ 252:efd2469d1722

geometry-column of tables will be displayed as string
author fknauft
date Tue, 20 Sep 2011 11:19:35 +0200
parents 9e34cd7b2e57
children 901c1f745d13
line wrap: on
line diff
--- a/RestDbInterface.py	Mon Sep 19 16:33:02 2011 +0200
+++ b/RestDbInterface.py	Tue Sep 20 11:19:35 2011 +0200
@@ -318,7 +318,12 @@
             # 400 Bad Request
             RESPONSE.setStatus(400)
             return
-        
+    def getAttributeNames(self,schema='public',table=None):   
+        return self.executeSQL("SELECT attname FROM pg_attribute, pg_class WHERE pg_class.oid = attrelid AND attnum>0 AND relname = '%s';"%(table))
+
+    def getAttributeTypes(self,schema='public',table=None):   
+        return self.executeSQL("SELECT field_name, gis_type FROM public.gis_table_meta_rows WHERE table_name = '%s';"%(table))
+         
     def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
         """returns PageTemplate with tables"""
         logging.debug("showtable")
@@ -342,15 +347,36 @@
     def getTable(self,schema='public',table=None,sortBy=1,username='guest'):
         """return table data"""
         logging.debug("gettable")
-        try:
-          if sortBy:
-            data = self.executeSQL('select * from "%s"."%s" order by %s'%(schema,table,sortBy))
-          else:
-            data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
-        except:
+        attrNames=self.getAttributeNames(schema,table)
+        attrTypes=self.getAttributeTypes(schema,table)
+        attrString=""
+ #        try:
+        for name in attrNames['rows']:
+              logging.debug("name: ", name[0])
+              not_added=True
+              if name[0] == "the_geom":                        #FJK: the table column is "the_geom"
+                     attrString=attrString+"ST_AsText("+name[0]+"),"
+                     not_added=False
+                     break
+              for a_iter in attrTypes['rows']:
+                 not_added = True
+                 logging.debug("attrTypes.field_name: ", a_iter[0])
+                 if a_iter[0]==name[0]:            
+                     logging.debug("attrTypes.gis_type: ", a_iter[1])            
+                     if a_iter[1] == "the_geom":                        #FJK: the table column is registered in gis_table_meta_rows as type "the_geom"
+                         attrString=attrString+"ST_AsText("+name[0]+"),"
+                         not_added=False
+              if not_added:
+                  attrString=attrString+name[0]+","
+        attrString=str(attrString).rsplit(",",1)[0] #to remove last ","
+        if sortBy:
+            data = self.executeSQL('select %s from "%s"."%s" order by %s'%(attrString,schema,table,sortBy))
+        else:
+            data = self.executeSQL('select %s from "%s"."%s"'%(attrString,schema,table))
+ #       except:
             """ table does not exist """
-            fields=self.get
-            self.createEmptyTable(schema, table, fields)
+ #           fields=self.get
+  #          self.createEmptyTable(schema, table, fields)
         return data
 
     def hasTable(self,schema='public',table=None,username='guest'):