diff RestDbInterface.py @ 70:9ec7e32e8ad3

working on maps nicer HTML (with xhtml DTD) better unicode handling for psycopg
author casties
date Tue, 23 Nov 2010 17:16:25 +0100
parents e81d034b28a5
children 6f7f8dee6cd2
line wrap: on
line diff
--- a/RestDbInterface.py	Tue Nov 23 12:54:22 2010 +0100
+++ b/RestDbInterface.py	Tue Nov 23 17:16:25 2010 +0100
@@ -10,9 +10,13 @@
 from Products.ZSQLExtend import ZSQLExtend
 import logging
 import re
-import psycopg2
 import json
 import time
+import psycopg2
+# make psycopg use unicode objects
+import psycopg2.extensions
+psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
+psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
 
 from zope.interface import implements
 from zope.publisher.interfaces import IPublishTraverse
@@ -24,6 +28,28 @@
 from Products.ZSQLMethods.SQL import SQLConnectionIDs
 
 
+def unicodify(s,alternate='latin-1'):
+    """decode str (utf-8 or latin-1 representation) into unicode object"""
+    if not s:
+        return u""
+    if isinstance(s, str):
+        try:
+            return s.decode('utf-8')
+        except:
+            return s.decode(alternate)
+    else:
+        return s
+
+def utf8ify(s):
+    """encode unicode object or string into byte string in utf-8 representation.
+       assumes string objects to be utf-8"""
+    if not s:
+        return ""
+    if isinstance(s, str):
+        return s
+    else:
+        return s.encode('utf-8')
+
 def getTextFromNode(node):
     """get the cdata content of a XML node"""
     if node is None:
@@ -52,14 +78,6 @@
     
     return s
 
-gisToSqlTypeMap = {
-                   "text": "text",
-                   "number": "numeric",
-                   "id": "text",
-                   "gis_id": "text",
-                   "coord_lat": "numeric",
-                   "coord_lon": "numeric"
-                   }
 
 class RestDbInterface(Folder):
     """Object for RESTful database queries
@@ -179,8 +197,8 @@
             # get all data in an array
             data = cur.fetchall()
             cur.close()
-            #logging.debug("fields: %s"%repr(fields))
-            #logging.debug("rows: %s"%repr(data))
+            logging.debug("fields: %s"%repr(fields))
+            logging.debug("rows: %s"%repr(data))
             return {"fields":fields, "rows":data}
         else:
             cur.close()
@@ -445,7 +463,10 @@
                 # {name: XX, type: YY}
                 name = sqlName(f['name'])
                 type = f['type']
-                sqltype = gisToSqlTypeMap[type]
+                if hasattr(self, 'toSqlTypeMap'):
+                    sqltype = self.toSqlTypeMap[type]
+                else:
+                    sqltype = 'text'
             
             else:
                 # name only