comparison DBInterface.py @ 19:132ae1c0255a

V1.7: new connection caching. some cleanup.
author casties
date Fri, 11 Jan 2013 17:58:56 +0100
parents 60fea3a6c695
children 5f3d6623b71e
comparison
equal deleted inserted replaced
18:60fea3a6c695 19:132ae1c0255a
12 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 12 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
13 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) 13 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
14 14
15 from Products.ZSQLMethods.SQL import SQLConnectionIDs 15 from Products.ZSQLMethods.SQL import SQLConnectionIDs
16 from Shared.DC.ZRDB.Results import Results 16 from Shared.DC.ZRDB.Results import Results
17
18
19 def unicodify(s,alternate='latin-1'):
20 """decode str (utf-8 or latin-1 representation) into unicode object"""
21 if not s:
22 return u""
23 if isinstance(s, str):
24 try:
25 return s.decode('utf-8')
26 except:
27 return s.decode(alternate)
28 else:
29 return s
30
31 def utf8ify(s):
32 """encode unicode object or string into byte string in utf-8 representation.
33 assumes string objects to be utf-8"""
34 if not s:
35 return ""
36 if isinstance(s, str):
37 return s
38 else:
39 return s.encode('utf-8')
40
41 def getTextFromNode(node):
42 """get the cdata content of a XML node"""
43 if node is None:
44 return ""
45
46 if isinstance(node, list):
47 nodelist = node
48 else:
49 nodelist=node.childNodes
50
51 rc = ""
52 for node in nodelist:
53 if node.nodeType == node.TEXT_NODE:
54 rc = rc + node.data
55 return rc
56 17
57 def sqlName(s, lc=True, more=''): 18 def sqlName(s, lc=True, more=''):
58 """returns restricted ASCII-only version of string""" 19 """returns restricted ASCII-only version of string"""
59 if s is None: 20 if s is None:
60 return "" 21 return ""
82 """return list of available connection ids""" 43 """return list of available connection ids"""
83 return SQLConnectionIDs(self) 44 return SQLConnectionIDs(self)
84 45
85 def getDB(self): 46 def getDB(self):
86 """returns DB object""" 47 """returns DB object"""
87 # TODO: can we cache and reuse a DB object? 48 db = None
49
50 # connection caching according to http://pypi.python.org/pypi/alm.solrindex
51 jar = self._p_jar
52 oid = self._p_oid
53 fc = None
54 if jar is not None and oid is not None:
55 fc = getattr(jar, 'foreign_connections', None)
56 if fc is None:
57 jar.foreign_connections = fc = {}
58
59 db = fc.get(oid, None)
60 if db is not None:
61 logging.debug("getDb: using cached db=%s"%repr(db))
62 return db
63
88 if self.connection_id is None: 64 if self.connection_id is None:
89 # try to take the first existing ID 65 # try to take the first existing ID
90 connids = self.getConnectionIDs() 66 connids = self.getConnectionIDs()
91 if len(connids) > 0: 67 if len(connids) > 0:
92 connection_id = connids[0][1] 68 connection_id = connids[0][1]
93 self.connection_id = connection_id 69 self.connection_id = connection_id
94 logging.debug("connection_id: %s"%repr(connection_id)) 70 logging.debug("connection_id: %s"%repr(connection_id))
95 71
96 # get Connection instance 72 # get Connection/DA instance
97 con = getattr(self, self.connection_id) 73 con = getattr(self, self.connection_id)
98 # call to get db object 74 # call to get db object
99 db = con() 75 db = con()
100 if self.autocommit: 76 if self.autocommit:
101 # force our transaction isolation level 77 # force our transaction isolation level
102 db.tilevel = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT 78 db.tilevel = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT
79
80 if fc is not None and oid is not None:
81 # cache db
82 fc[oid] = db
103 83
104 return db 84 return db
105 85
106 def executeZSQL(self, query, args=None, max_rows=None): 86 def executeZSQL(self, query, args=None, max_rows=None):
107 """execute query with args on the database and return all results as Result object.""" 87 """execute query with args on the database and return all results as Result object."""