Mercurial > hg > ChinaGisRestApi
comparison 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 |
comparison
equal
deleted
inserted
replaced
69:752ce3dfd23e | 70:9ec7e32e8ad3 |
---|---|
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | 8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
9 from AccessControl import getSecurityManager, Unauthorized | 9 from AccessControl import getSecurityManager, Unauthorized |
10 from Products.ZSQLExtend import ZSQLExtend | 10 from Products.ZSQLExtend import ZSQLExtend |
11 import logging | 11 import logging |
12 import re | 12 import re |
13 import psycopg2 | |
14 import json | 13 import json |
15 import time | 14 import time |
15 import psycopg2 | |
16 # make psycopg use unicode objects | |
17 import psycopg2.extensions | |
18 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | |
19 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) | |
16 | 20 |
17 from zope.interface import implements | 21 from zope.interface import implements |
18 from zope.publisher.interfaces import IPublishTraverse | 22 from zope.publisher.interfaces import IPublishTraverse |
19 from ZPublisher.BaseRequest import DefaultPublishTraverse | 23 from ZPublisher.BaseRequest import DefaultPublishTraverse |
20 #from zope.publisher.interfaces import NotFound | 24 #from zope.publisher.interfaces import NotFound |
22 #from zope.component import queryMultiAdapter | 26 #from zope.component import queryMultiAdapter |
23 import Shared.DC.ZRDB.DA | 27 import Shared.DC.ZRDB.DA |
24 from Products.ZSQLMethods.SQL import SQLConnectionIDs | 28 from Products.ZSQLMethods.SQL import SQLConnectionIDs |
25 | 29 |
26 | 30 |
31 def unicodify(s,alternate='latin-1'): | |
32 """decode str (utf-8 or latin-1 representation) into unicode object""" | |
33 if not s: | |
34 return u"" | |
35 if isinstance(s, str): | |
36 try: | |
37 return s.decode('utf-8') | |
38 except: | |
39 return s.decode(alternate) | |
40 else: | |
41 return s | |
42 | |
43 def utf8ify(s): | |
44 """encode unicode object or string into byte string in utf-8 representation. | |
45 assumes string objects to be utf-8""" | |
46 if not s: | |
47 return "" | |
48 if isinstance(s, str): | |
49 return s | |
50 else: | |
51 return s.encode('utf-8') | |
52 | |
27 def getTextFromNode(node): | 53 def getTextFromNode(node): |
28 """get the cdata content of a XML node""" | 54 """get the cdata content of a XML node""" |
29 if node is None: | 55 if node is None: |
30 return "" | 56 return "" |
31 | 57 |
50 if lc: | 76 if lc: |
51 return s.lower() | 77 return s.lower() |
52 | 78 |
53 return s | 79 return s |
54 | 80 |
55 gisToSqlTypeMap = { | |
56 "text": "text", | |
57 "number": "numeric", | |
58 "id": "text", | |
59 "gis_id": "text", | |
60 "coord_lat": "numeric", | |
61 "coord_lon": "numeric" | |
62 } | |
63 | 81 |
64 class RestDbInterface(Folder): | 82 class RestDbInterface(Folder): |
65 """Object for RESTful database queries | 83 """Object for RESTful database queries |
66 path schema: /db/{schema}/{table}/ | 84 path schema: /db/{schema}/{table}/ |
67 omitting table gives a list of schemas | 85 omitting table gives a list of schemas |
177 fields = cur.description | 195 fields = cur.description |
178 if hasResult: | 196 if hasResult: |
179 # get all data in an array | 197 # get all data in an array |
180 data = cur.fetchall() | 198 data = cur.fetchall() |
181 cur.close() | 199 cur.close() |
182 #logging.debug("fields: %s"%repr(fields)) | 200 logging.debug("fields: %s"%repr(fields)) |
183 #logging.debug("rows: %s"%repr(data)) | 201 logging.debug("rows: %s"%repr(data)) |
184 return {"fields":fields, "rows":data} | 202 return {"fields":fields, "rows":data} |
185 else: | 203 else: |
186 cur.close() | 204 cur.close() |
187 return None | 205 return None |
188 | 206 |
443 for f in fields: | 461 for f in fields: |
444 if isinstance(f,dict): | 462 if isinstance(f,dict): |
445 # {name: XX, type: YY} | 463 # {name: XX, type: YY} |
446 name = sqlName(f['name']) | 464 name = sqlName(f['name']) |
447 type = f['type'] | 465 type = f['type'] |
448 sqltype = gisToSqlTypeMap[type] | 466 if hasattr(self, 'toSqlTypeMap'): |
467 sqltype = self.toSqlTypeMap[type] | |
468 else: | |
469 sqltype = 'text' | |
449 | 470 |
450 else: | 471 else: |
451 # name only | 472 # name only |
452 name = sqlName(f) | 473 name = sqlName(f) |
453 type = 'text' | 474 type = 'text' |