author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 09 Jul 2009 19:40:45 +0200 | |
branch | stable |
changeset 2364 | fa16d36d60cb |
parent 2266 | efc6de279644 |
child 2476 | 1294a6bdf3bf |
permissions | -rw-r--r-- |
0 | 1 |
"""DB-API 2.0 compliant module |
2 |
||
3 |
Take a look at http://www.python.org/peps/pep-0249.html |
|
4 |
||
5 |
(most parts of this document are reported here in docstrings) |
|
6 |
||
7 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1923
diff
changeset
|
8 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 9 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1923
diff
changeset
|
10 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 11 |
""" |
12 |
__docformat__ = "restructuredtext en" |
|
13 |
||
1132 | 14 |
from logging import getLogger |
0 | 15 |
from time import time, clock |
16 |
||
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
17 |
from logilab.common.logging_ext import set_log_methods |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
18 |
from cubicweb import ETYPE_NAME_MAP, ConnectionError, RequestSessionMixIn |
0 | 19 |
from cubicweb.cwvreg import CubicWebRegistry, MulCnxCubicWebRegistry |
20 |
from cubicweb.cwconfig import CubicWebNoAppConfiguration |
|
1524 | 21 |
|
0 | 22 |
_MARKER = object() |
23 |
||
24 |
class ConnectionProperties(object): |
|
25 |
def __init__(self, cnxtype=None, lang=None, close=True, log=False): |
|
26 |
self.cnxtype = cnxtype or 'pyro' |
|
27 |
self.lang = lang |
|
28 |
self.log_queries = log |
|
29 |
self.close_on_del = close |
|
30 |
||
31 |
||
32 |
def get_repository(method, database=None, config=None, vreg=None): |
|
33 |
"""get a proxy object to the CubicWeb repository, using a specific RPC method. |
|
1524 | 34 |
|
0 | 35 |
Only 'in-memory' and 'pyro' are supported for now. Either vreg or config |
36 |
argument should be given |
|
37 |
""" |
|
38 |
assert method in ('pyro', 'inmemory') |
|
39 |
assert vreg or config |
|
40 |
if vreg and not config: |
|
41 |
config = vreg.config |
|
42 |
if method == 'inmemory': |
|
43 |
# get local access to the repository |
|
44 |
from cubicweb.server.repository import Repository |
|
45 |
return Repository(config, vreg=vreg) |
|
46 |
else: # method == 'pyro' |
|
1132 | 47 |
from Pyro import core, naming |
0 | 48 |
from Pyro.errors import NamingError, ProtocolError |
49 |
core.initClient(banner=0) |
|
167
b726c12af78f
don't change Pyro.config here
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
50 |
nsid = ':%s.%s' % (config['pyro-ns-group'], database) |
0 | 51 |
locator = naming.NameServerLocator() |
52 |
# resolve the Pyro object |
|
53 |
try: |
|
54 |
nshost, nsport = config['pyro-ns-host'], config['pyro-ns-port'] |
|
1882 | 55 |
uri = locator.getNS(nshost, nsport).resolve(nsid) |
0 | 56 |
except ProtocolError: |
57 |
raise ConnectionError('Could not connect to the Pyro name server ' |
|
58 |
'(host: %s:%i)' % (nshost, nsport)) |
|
1132 | 59 |
except NamingError: |
0 | 60 |
raise ConnectionError('Could not get repository for %s ' |
167
b726c12af78f
don't change Pyro.config here
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
61 |
'(not registered in Pyro), ' |
0 | 62 |
'you may have to restart your server-side ' |
167
b726c12af78f
don't change Pyro.config here
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
63 |
'application' % nsid) |
0 | 64 |
return core.getProxyForURI(uri) |
1524 | 65 |
|
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
66 |
def repo_connect(repo, login, password, cnxprops=None): |
0 | 67 |
"""Constructor to create a new connection to the CubicWeb repository. |
1524 | 68 |
|
0 | 69 |
Returns a Connection instance. |
70 |
""" |
|
71 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
72 |
cnxid = repo.connect(unicode(login), password, cnxprops=cnxprops) |
0 | 73 |
cnx = Connection(repo, cnxid, cnxprops) |
74 |
if cnxprops.cnxtype == 'inmemory': |
|
75 |
cnx.vreg = repo.vreg |
|
76 |
return cnx |
|
1524 | 77 |
|
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
78 |
def connect(database=None, login=None, password=None, host=None, |
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
167
diff
changeset
|
79 |
group=None, cnxprops=None, port=None, setvreg=True, mulcnx=True, |
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
167
diff
changeset
|
80 |
initlog=True): |
0 | 81 |
"""Constructor for creating a connection to the CubicWeb repository. |
82 |
Returns a Connection object. |
|
83 |
||
84 |
When method is 'pyro' and setvreg is True, use a special registry class |
|
85 |
(MulCnxCubicWebRegistry) made to deal with connections to differents instances |
|
86 |
in the same process unless specified otherwise by setting the mulcnx to |
|
87 |
False. |
|
88 |
""" |
|
89 |
config = CubicWebNoAppConfiguration() |
|
90 |
if host: |
|
91 |
config.global_set_option('pyro-ns-host', host) |
|
92 |
if port: |
|
93 |
config.global_set_option('pyro-ns-port', port) |
|
94 |
if group: |
|
95 |
config.global_set_option('pyro-ns-group', group) |
|
96 |
cnxprops = cnxprops or ConnectionProperties() |
|
97 |
method = cnxprops.cnxtype |
|
98 |
repo = get_repository(method, database, config=config) |
|
99 |
if method == 'inmemory': |
|
100 |
vreg = repo.vreg |
|
101 |
elif setvreg: |
|
102 |
if mulcnx: |
|
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
167
diff
changeset
|
103 |
vreg = MulCnxCubicWebRegistry(config, initlog=initlog) |
0 | 104 |
else: |
169
0e031b66cb0b
don't systematically init_log, it may breaks client log configuration
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
167
diff
changeset
|
105 |
vreg = CubicWebRegistry(config, initlog=initlog) |
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
106 |
schema = repo.get_schema() |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
107 |
for oldetype, newetype in ETYPE_NAME_MAP.items(): |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
108 |
if oldetype in schema: |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
109 |
print 'aliasing', newetype, 'to', oldetype |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
110 |
schema._entities[newetype] = schema._entities[oldetype] |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
111 |
vreg.set_schema(schema) |
0 | 112 |
else: |
113 |
vreg = None |
|
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
114 |
cnx = repo_connect(repo, login, password, cnxprops) |
0 | 115 |
cnx.vreg = vreg |
116 |
return cnx |
|
117 |
||
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
118 |
def in_memory_cnx(config, login, password): |
0 | 119 |
"""usefull method for testing and scripting to get a dbapi.Connection |
1524 | 120 |
object connected to an in-memory repository instance |
0 | 121 |
""" |
122 |
if isinstance(config, CubicWebRegistry): |
|
123 |
vreg = config |
|
124 |
config = None |
|
125 |
else: |
|
126 |
vreg = None |
|
127 |
# get local access to the repository |
|
128 |
repo = get_repository('inmemory', config=config, vreg=vreg) |
|
129 |
# connection to the CubicWeb repository |
|
130 |
cnxprops = ConnectionProperties('inmemory') |
|
2266
efc6de279644
rename user to login where this is not a user object
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
131 |
cnx = repo_connect(repo, login, password, cnxprops=cnxprops) |
0 | 132 |
return repo, cnx |
133 |
||
134 |
||
135 |
class DBAPIRequest(RequestSessionMixIn): |
|
1524 | 136 |
|
0 | 137 |
def __init__(self, vreg, cnx=None): |
138 |
super(DBAPIRequest, self).__init__(vreg) |
|
139 |
try: |
|
140 |
# no vreg or config which doesn't handle translations |
|
141 |
self.translations = vreg.config.translations |
|
142 |
except AttributeError: |
|
143 |
self.translations = {} |
|
144 |
self.set_default_language(vreg) |
|
145 |
# cache entities built during the request |
|
146 |
self._eid_cache = {} |
|
147 |
# these args are initialized after a connection is |
|
148 |
# established |
|
149 |
self.cnx = None # connection associated to the request |
|
150 |
self._user = None # request's user, set at authentication |
|
151 |
if cnx is not None: |
|
152 |
self.set_connection(cnx) |
|
153 |
||
154 |
def base_url(self): |
|
155 |
return self.vreg.config['base-url'] |
|
1524 | 156 |
|
0 | 157 |
def from_controller(self): |
158 |
return 'view' |
|
1524 | 159 |
|
0 | 160 |
def set_connection(self, cnx, user=None): |
161 |
"""method called by the session handler when the user is authenticated |
|
162 |
or an anonymous connection is open |
|
163 |
""" |
|
164 |
self.cnx = cnx |
|
165 |
self.cursor = cnx.cursor(self) |
|
166 |
self.set_user(user) |
|
1524 | 167 |
|
0 | 168 |
def set_default_language(self, vreg): |
169 |
try: |
|
170 |
self.lang = vreg.property_value('ui.language') |
|
171 |
except: # property may not be registered |
|
172 |
self.lang = 'en' |
|
173 |
# use req.__ to translate a message without registering it to the catalog |
|
174 |
try: |
|
175 |
self._ = self.__ = self.translations[self.lang] |
|
176 |
except KeyError: |
|
177 |
# this occurs usually during test execution |
|
178 |
self._ = self.__ = unicode |
|
2111
5e142c7a4531
different message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
179 |
self.debug('request default language: %s', self.lang) |
0 | 180 |
|
181 |
def decorate_rset(self, rset): |
|
182 |
rset.vreg = self.vreg |
|
183 |
rset.req = self |
|
184 |
return rset |
|
1524 | 185 |
|
0 | 186 |
def describe(self, eid): |
187 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
188 |
return self.cnx.describe(eid) |
|
1524 | 189 |
|
0 | 190 |
def source_defs(self): |
191 |
"""return the definition of sources used by the repository.""" |
|
192 |
return self.cnx.source_defs() |
|
1524 | 193 |
|
0 | 194 |
# entities cache management ############################################### |
1524 | 195 |
|
0 | 196 |
def entity_cache(self, eid): |
197 |
return self._eid_cache[eid] |
|
1524 | 198 |
|
0 | 199 |
def set_entity_cache(self, entity): |
200 |
self._eid_cache[entity.eid] = entity |
|
201 |
||
202 |
def cached_entities(self): |
|
203 |
return self._eid_cache.values() |
|
1524 | 204 |
|
0 | 205 |
def drop_entity_cache(self, eid=None): |
206 |
if eid is None: |
|
207 |
self._eid_cache = {} |
|
208 |
else: |
|
209 |
del self._eid_cache[eid] |
|
210 |
||
211 |
# low level session data management ####################################### |
|
212 |
||
213 |
def session_data(self): |
|
214 |
"""return a dictionnary containing session data""" |
|
215 |
return self.cnx.session_data() |
|
216 |
||
217 |
def get_session_data(self, key, default=None, pop=False): |
|
218 |
"""return value associated to `key` in session data""" |
|
219 |
return self.cnx.get_session_data(key, default, pop) |
|
1524 | 220 |
|
0 | 221 |
def set_session_data(self, key, value): |
222 |
"""set value associated to `key` in session data""" |
|
223 |
return self.cnx.set_session_data(key, value) |
|
1524 | 224 |
|
0 | 225 |
def del_session_data(self, key): |
226 |
"""remove value associated to `key` in session data""" |
|
227 |
return self.cnx.del_session_data(key) |
|
228 |
||
229 |
def get_shared_data(self, key, default=None, pop=False): |
|
230 |
"""return value associated to `key` in shared data""" |
|
231 |
return self.cnx.get_shared_data(key, default, pop) |
|
1524 | 232 |
|
0 | 233 |
def set_shared_data(self, key, value, querydata=False): |
234 |
"""set value associated to `key` in shared data |
|
235 |
||
236 |
if `querydata` is true, the value will be added to the repository |
|
237 |
session's query data which are cleared on commit/rollback of the current |
|
238 |
transaction, and won't be available through the connexion, only on the |
|
239 |
repository side. |
|
240 |
""" |
|
241 |
return self.cnx.set_shared_data(key, value, querydata) |
|
242 |
||
243 |
# server session compat layer ############################################# |
|
244 |
||
245 |
@property |
|
246 |
def user(self): |
|
247 |
if self._user is None and self.cnx: |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
248 |
self.set_user(self.cnx.user(self, {'lang': self.lang})) |
0 | 249 |
return self._user |
250 |
||
251 |
def set_user(self, user): |
|
252 |
self._user = user |
|
253 |
if user: |
|
254 |
self.set_entity_cache(user) |
|
1524 | 255 |
|
0 | 256 |
def execute(self, *args, **kwargs): |
257 |
"""Session interface compatibility""" |
|
258 |
return self.cursor.execute(*args, **kwargs) |
|
259 |
||
260 |
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi')) |
|
1524 | 261 |
|
262 |
||
0 | 263 |
# exceptions ################################################################## |
264 |
||
265 |
class ProgrammingError(Exception): #DatabaseError): |
|
266 |
"""Exception raised for errors that are related to the database's operation |
|
267 |
and not necessarily under the control of the programmer, e.g. an unexpected |
|
268 |
disconnect occurs, the data source name is not found, a transaction could |
|
269 |
not be processed, a memory allocation error occurred during processing, |
|
270 |
etc. |
|
271 |
""" |
|
272 |
||
273 |
# module level objects ######################################################## |
|
274 |
||
275 |
||
276 |
apilevel = '2.0' |
|
277 |
||
278 |
"""Integer constant stating the level of thread safety the interface supports. |
|
279 |
Possible values are: |
|
280 |
||
281 |
0 Threads may not share the module. |
|
282 |
1 Threads may share the module, but not connections. |
|
283 |
2 Threads may share the module and connections. |
|
284 |
3 Threads may share the module, connections and |
|
285 |
cursors. |
|
286 |
||
287 |
Sharing in the above context means that two threads may use a resource without |
|
288 |
wrapping it using a mutex semaphore to implement resource locking. Note that |
|
289 |
you cannot always make external resources thread safe by managing access using |
|
290 |
a mutex: the resource may rely on global variables or other external sources |
|
291 |
that are beyond your control. |
|
292 |
""" |
|
293 |
threadsafety = 1 |
|
294 |
||
295 |
"""String constant stating the type of parameter marker formatting expected by |
|
296 |
the interface. Possible values are : |
|
297 |
||
1524 | 298 |
'qmark' Question mark style, |
0 | 299 |
e.g. '...WHERE name=?' |
1524 | 300 |
'numeric' Numeric, positional style, |
0 | 301 |
e.g. '...WHERE name=:1' |
1524 | 302 |
'named' Named style, |
0 | 303 |
e.g. '...WHERE name=:name' |
1524 | 304 |
'format' ANSI C printf format codes, |
0 | 305 |
e.g. '...WHERE name=%s' |
1524 | 306 |
'pyformat' Python extended format codes, |
0 | 307 |
e.g. '...WHERE name=%(name)s' |
308 |
""" |
|
309 |
paramstyle = 'pyformat' |
|
310 |
||
311 |
||
312 |
# connection object ########################################################### |
|
313 |
||
314 |
class Connection(object): |
|
315 |
"""DB-API 2.0 compatible Connection object for CubicWebt |
|
316 |
""" |
|
317 |
# make exceptions available through the connection object |
|
318 |
ProgrammingError = ProgrammingError |
|
319 |
||
320 |
def __init__(self, repo, cnxid, cnxprops=None): |
|
321 |
self._repo = repo |
|
322 |
self.sessionid = cnxid |
|
323 |
self._close_on_del = getattr(cnxprops, 'close_on_del', True) |
|
324 |
self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro') |
|
325 |
self._closed = None |
|
326 |
if cnxprops and cnxprops.log_queries: |
|
327 |
self.executed_queries = [] |
|
328 |
self.cursor_class = LogCursor |
|
329 |
else: |
|
330 |
self.cursor_class = Cursor |
|
331 |
self.anonymous_connection = False |
|
332 |
self.vreg = None |
|
333 |
# session's data |
|
334 |
self.data = {} |
|
335 |
||
336 |
def __repr__(self): |
|
337 |
if self.anonymous_connection: |
|
338 |
return '<Connection %s (anonymous)>' % self.sessionid |
|
339 |
return '<Connection %s>' % self.sessionid |
|
340 |
||
341 |
def request(self): |
|
342 |
return DBAPIRequest(self.vreg, self) |
|
1524 | 343 |
|
0 | 344 |
def session_data(self): |
345 |
"""return a dictionnary containing session data""" |
|
346 |
return self.data |
|
1524 | 347 |
|
0 | 348 |
def get_session_data(self, key, default=None, pop=False): |
349 |
"""return value associated to `key` in session data""" |
|
350 |
if pop: |
|
351 |
return self.data.pop(key, default) |
|
352 |
else: |
|
353 |
return self.data.get(key, default) |
|
1524 | 354 |
|
0 | 355 |
def set_session_data(self, key, value): |
356 |
"""set value associated to `key` in session data""" |
|
357 |
self.data[key] = value |
|
1524 | 358 |
|
0 | 359 |
def del_session_data(self, key): |
360 |
"""remove value associated to `key` in session data""" |
|
361 |
try: |
|
362 |
del self.data[key] |
|
363 |
except KeyError: |
|
1524 | 364 |
pass |
0 | 365 |
|
366 |
def check(self): |
|
367 |
"""raise `BadSessionId` if the connection is no more valid""" |
|
1132 | 368 |
self._repo.check_session(self.sessionid) |
0 | 369 |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
370 |
def set_session_props(self, **props): |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
371 |
"""raise `BadSessionId` if the connection is no more valid""" |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
372 |
self._repo.set_session_props(self.sessionid, props) |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
373 |
|
0 | 374 |
def get_shared_data(self, key, default=None, pop=False): |
375 |
"""return value associated to `key` in shared data""" |
|
376 |
return self._repo.get_shared_data(self.sessionid, key, default, pop) |
|
1524 | 377 |
|
0 | 378 |
def set_shared_data(self, key, value, querydata=False): |
379 |
"""set value associated to `key` in shared data |
|
380 |
||
381 |
if `querydata` is true, the value will be added to the repository |
|
382 |
session's query data which are cleared on commit/rollback of the current |
|
383 |
transaction, and won't be available through the connexion, only on the |
|
384 |
repository side. |
|
385 |
""" |
|
386 |
return self._repo.set_shared_data(self.sessionid, key, value, querydata) |
|
1524 | 387 |
|
0 | 388 |
def get_schema(self): |
389 |
"""Return the schema currently used by the repository. |
|
1524 | 390 |
|
0 | 391 |
This is NOT part of the DB-API. |
392 |
""" |
|
393 |
if self._closed is not None: |
|
394 |
raise ProgrammingError('Closed connection') |
|
395 |
return self._repo.get_schema() |
|
396 |
||
397 |
def load_vobjects(self, cubes=_MARKER, subpath=None, expand=True, force_reload=None): |
|
398 |
config = self.vreg.config |
|
399 |
if cubes is _MARKER: |
|
400 |
cubes = self._repo.get_cubes() |
|
401 |
elif cubes is None: |
|
402 |
cubes = () |
|
403 |
else: |
|
404 |
if not isinstance(cubes, (list, tuple)): |
|
405 |
cubes = (cubes,) |
|
406 |
if expand: |
|
407 |
cubes = config.expand_cubes(cubes) |
|
408 |
if subpath is None: |
|
409 |
subpath = esubpath = ('entities', 'views') |
|
410 |
else: |
|
411 |
esubpath = subpath |
|
412 |
if 'views' in subpath: |
|
413 |
esubpath = list(subpath) |
|
414 |
esubpath.remove('views') |
|
415 |
esubpath.append('web/views') |
|
416 |
cubes = reversed([config.cube_dir(p) for p in cubes]) |
|
417 |
vpath = config.build_vregistry_path(cubes, evobjpath=esubpath, |
|
418 |
tvobjpath=subpath) |
|
419 |
self.vreg.register_objects(vpath, force_reload) |
|
420 |
if self._cnxtype == 'inmemory': |
|
421 |
# should reinit hooks manager as well |
|
422 |
hm, config = self._repo.hm, self._repo.config |
|
423 |
hm.set_schema(hm.schema) # reset structure |
|
424 |
hm.register_system_hooks(config) |
|
425 |
# application specific hooks |
|
426 |
if self._repo.config.application_hooks: |
|
427 |
hm.register_hooks(config.load_hooks(self.vreg)) |
|
1524 | 428 |
|
0 | 429 |
def source_defs(self): |
430 |
"""Return the definition of sources used by the repository. |
|
1524 | 431 |
|
0 | 432 |
This is NOT part of the DB-API. |
433 |
""" |
|
434 |
if self._closed is not None: |
|
435 |
raise ProgrammingError('Closed connection') |
|
436 |
return self._repo.source_defs() |
|
437 |
||
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
438 |
def user(self, req=None, props=None): |
0 | 439 |
"""return the User object associated to this connection""" |
440 |
# cnx validity is checked by the call to .user_info |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
441 |
eid, login, groups, properties = self._repo.user_info(self.sessionid, |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2111
diff
changeset
|
442 |
props) |
0 | 443 |
if req is None: |
444 |
req = self.request() |
|
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
445 |
rset = req.eid_rset(eid, 'CWUser') |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
446 |
user = self.vreg.etype_class('CWUser')(req, rset, row=0, groups=groups, |
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
447 |
properties=properties) |
0 | 448 |
user['login'] = login # cache login |
449 |
return user |
|
450 |
||
451 |
def __del__(self): |
|
452 |
"""close the remote connection if necessary""" |
|
453 |
if self._closed is None and self._close_on_del: |
|
454 |
try: |
|
455 |
self.close() |
|
456 |
except: |
|
457 |
pass |
|
1524 | 458 |
|
0 | 459 |
def describe(self, eid): |
460 |
return self._repo.describe(self.sessionid, eid) |
|
1524 | 461 |
|
0 | 462 |
def close(self): |
463 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 464 |
|
0 | 465 |
The connection will be unusable from this point forward; an Error (or |
466 |
subclass) exception will be raised if any operation is attempted with |
|
467 |
the connection. The same applies to all cursor objects trying to use the |
|
468 |
connection. Note that closing a connection without committing the |
|
469 |
changes first will cause an implicit rollback to be performed. |
|
470 |
""" |
|
471 |
if self._closed: |
|
472 |
raise ProgrammingError('Connection is already closed') |
|
473 |
self._repo.close(self.sessionid) |
|
474 |
self._closed = 1 |
|
475 |
||
476 |
def commit(self): |
|
477 |
"""Commit any pending transaction to the database. Note that if the |
|
478 |
database supports an auto-commit feature, this must be initially off. An |
|
479 |
interface method may be provided to turn it back on. |
|
1524 | 480 |
|
0 | 481 |
Database modules that do not support transactions should implement this |
482 |
method with void functionality. |
|
483 |
""" |
|
484 |
if not self._closed is None: |
|
485 |
raise ProgrammingError('Connection is already closed') |
|
486 |
self._repo.commit(self.sessionid) |
|
487 |
||
488 |
def rollback(self): |
|
489 |
"""This method is optional since not all databases provide transaction |
|
490 |
support. |
|
1524 | 491 |
|
0 | 492 |
In case a database does provide transactions this method causes the the |
493 |
database to roll back to the start of any pending transaction. Closing |
|
494 |
a connection without committing the changes first will cause an implicit |
|
495 |
rollback to be performed. |
|
496 |
""" |
|
497 |
if not self._closed is None: |
|
498 |
raise ProgrammingError('Connection is already closed') |
|
499 |
self._repo.rollback(self.sessionid) |
|
500 |
||
501 |
def cursor(self, req=None): |
|
502 |
"""Return a new Cursor Object using the connection. If the database |
|
503 |
does not provide a direct cursor concept, the module will have to |
|
504 |
emulate cursors using other means to the extent needed by this |
|
505 |
specification. |
|
506 |
""" |
|
507 |
if self._closed is not None: |
|
508 |
raise ProgrammingError('Can\'t get cursor on closed connection') |
|
509 |
if req is None: |
|
510 |
req = self.request() |
|
511 |
return self.cursor_class(self, self._repo, req=req) |
|
512 |
||
513 |
||
514 |
# cursor object ############################################################### |
|
515 |
||
516 |
class Cursor(object): |
|
517 |
"""These objects represent a database cursor, which is used to manage the |
|
518 |
context of a fetch operation. Cursors created from the same connection are |
|
519 |
not isolated, i.e., any changes done to the database by a cursor are |
|
520 |
immediately visible by the other cursors. Cursors created from different |
|
521 |
connections can or can not be isolated, depending on how the transaction |
|
522 |
support is implemented (see also the connection's rollback() and commit() |
|
523 |
methods.) |
|
524 |
""" |
|
1524 | 525 |
|
0 | 526 |
def __init__(self, connection, repo, req=None): |
527 |
"""This read-only attribute return a reference to the Connection |
|
528 |
object on which the cursor was created. |
|
529 |
""" |
|
530 |
self.connection = connection |
|
531 |
"""optionnal issuing request instance""" |
|
532 |
self.req = req |
|
533 |
||
534 |
"""This read/write attribute specifies the number of rows to fetch at a |
|
535 |
time with fetchmany(). It defaults to 1 meaning to fetch a single row |
|
536 |
at a time. |
|
1524 | 537 |
|
0 | 538 |
Implementations must observe this value with respect to the fetchmany() |
539 |
method, but are free to interact with the database a single row at a |
|
540 |
time. It may also be used in the implementation of executemany(). |
|
541 |
""" |
|
542 |
self.arraysize = 1 |
|
543 |
||
544 |
self._repo = repo |
|
545 |
self._sessid = connection.sessionid |
|
546 |
self._res = None |
|
547 |
self._closed = None |
|
548 |
self._index = 0 |
|
549 |
||
1524 | 550 |
|
0 | 551 |
def close(self): |
552 |
"""Close the cursor now (rather than whenever __del__ is called). The |
|
553 |
cursor will be unusable from this point forward; an Error (or subclass) |
|
554 |
exception will be raised if any operation is attempted with the cursor. |
|
555 |
""" |
|
556 |
self._closed = True |
|
557 |
||
1524 | 558 |
|
0 | 559 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
560 |
"""Prepare and execute a database operation (query or command). |
|
561 |
Parameters may be provided as sequence or mapping and will be bound to |
|
562 |
variables in the operation. Variables are specified in a |
|
563 |
database-specific notation (see the module's paramstyle attribute for |
|
564 |
details). |
|
1524 | 565 |
|
0 | 566 |
A reference to the operation will be retained by the cursor. If the |
567 |
same operation object is passed in again, then the cursor can optimize |
|
568 |
its behavior. This is most effective for algorithms where the same |
|
569 |
operation is used, but different parameters are bound to it (many |
|
570 |
times). |
|
1524 | 571 |
|
0 | 572 |
For maximum efficiency when reusing an operation, it is best to use the |
573 |
setinputsizes() method to specify the parameter types and sizes ahead |
|
574 |
of time. It is legal for a parameter to not match the predefined |
|
575 |
information; the implementation should compensate, possibly with a loss |
|
576 |
of efficiency. |
|
1524 | 577 |
|
0 | 578 |
The parameters may also be specified as list of tuples to e.g. insert |
579 |
multiple rows in a single operation, but this kind of usage is |
|
580 |
depreciated: executemany() should be used instead. |
|
1524 | 581 |
|
0 | 582 |
Return values are not defined by the DB-API, but this here it returns a |
583 |
ResultSet object. |
|
584 |
""" |
|
585 |
self._res = res = self._repo.execute(self._sessid, operation, |
|
586 |
parameters, eid_key, build_descr) |
|
587 |
self.req.decorate_rset(res) |
|
588 |
self._index = 0 |
|
589 |
return res |
|
1524 | 590 |
|
0 | 591 |
|
592 |
def executemany(self, operation, seq_of_parameters): |
|
593 |
"""Prepare a database operation (query or command) and then execute it |
|
594 |
against all parameter sequences or mappings found in the sequence |
|
595 |
seq_of_parameters. |
|
1524 | 596 |
|
0 | 597 |
Modules are free to implement this method using multiple calls to the |
598 |
execute() method or by using array operations to have the database |
|
599 |
process the sequence as a whole in one call. |
|
1524 | 600 |
|
0 | 601 |
Use of this method for an operation which produces one or more result |
602 |
sets constitutes undefined behavior, and the implementation is |
|
603 |
permitted (but not required) to raise an exception when it detects that |
|
604 |
a result set has been created by an invocation of the operation. |
|
1524 | 605 |
|
0 | 606 |
The same comments as for execute() also apply accordingly to this |
607 |
method. |
|
1524 | 608 |
|
0 | 609 |
Return values are not defined. |
610 |
""" |
|
611 |
for parameters in seq_of_parameters: |
|
612 |
self.execute(operation, parameters) |
|
613 |
if self._res.rows is not None: |
|
614 |
self._res = None |
|
615 |
raise ProgrammingError('Operation returned a result set') |
|
616 |
||
617 |
||
618 |
def fetchone(self): |
|
619 |
"""Fetch the next row of a query result set, returning a single |
|
620 |
sequence, or None when no more data is available. |
|
1524 | 621 |
|
0 | 622 |
An Error (or subclass) exception is raised if the previous call to |
623 |
execute*() did not produce any result set or no call was issued yet. |
|
624 |
""" |
|
625 |
if self._res is None: |
|
626 |
raise ProgrammingError('No result set') |
|
627 |
row = self._res.rows[self._index] |
|
628 |
self._index += 1 |
|
629 |
return row |
|
630 |
||
1524 | 631 |
|
0 | 632 |
def fetchmany(self, size=None): |
633 |
"""Fetch the next set of rows of a query result, returning a sequence |
|
634 |
of sequences (e.g. a list of tuples). An empty sequence is returned |
|
635 |
when no more rows are available. |
|
1524 | 636 |
|
0 | 637 |
The number of rows to fetch per call is specified by the parameter. If |
638 |
it is not given, the cursor's arraysize determines the number of rows |
|
639 |
to be fetched. The method should try to fetch as many rows as indicated |
|
640 |
by the size parameter. If this is not possible due to the specified |
|
641 |
number of rows not being available, fewer rows may be returned. |
|
1524 | 642 |
|
0 | 643 |
An Error (or subclass) exception is raised if the previous call to |
644 |
execute*() did not produce any result set or no call was issued yet. |
|
1524 | 645 |
|
0 | 646 |
Note there are performance considerations involved with the size |
647 |
parameter. For optimal performance, it is usually best to use the |
|
648 |
arraysize attribute. If the size parameter is used, then it is best |
|
649 |
for it to retain the same value from one fetchmany() call to the next. |
|
650 |
""" |
|
651 |
if self._res is None: |
|
652 |
raise ProgrammingError('No result set') |
|
653 |
if size is None: |
|
654 |
size = self.arraysize |
|
655 |
rows = self._res.rows[self._index:self._index + size] |
|
656 |
self._index += size |
|
657 |
return rows |
|
658 |
||
1524 | 659 |
|
0 | 660 |
def fetchall(self): |
661 |
"""Fetch all (remaining) rows of a query result, returning them as a |
|
662 |
sequence of sequences (e.g. a list of tuples). Note that the cursor's |
|
663 |
arraysize attribute can affect the performance of this operation. |
|
1524 | 664 |
|
0 | 665 |
An Error (or subclass) exception is raised if the previous call to |
666 |
execute*() did not produce any result set or no call was issued yet. |
|
667 |
""" |
|
668 |
if self._res is None: |
|
669 |
raise ProgrammingError('No result set') |
|
670 |
if not self._res.rows: |
|
671 |
return [] |
|
672 |
rows = self._res.rows[self._index:] |
|
673 |
self._index = len(self._res) |
|
674 |
return rows |
|
675 |
||
676 |
||
677 |
def setinputsizes(self, sizes): |
|
678 |
"""This can be used before a call to execute*() to predefine memory |
|
679 |
areas for the operation's parameters. |
|
1524 | 680 |
|
0 | 681 |
sizes is specified as a sequence -- one item for each input parameter. |
682 |
The item should be a Type Object that corresponds to the input that |
|
683 |
will be used, or it should be an integer specifying the maximum length |
|
684 |
of a string parameter. If the item is None, then no predefined memory |
|
685 |
area will be reserved for that column (this is useful to avoid |
|
686 |
predefined areas for large inputs). |
|
1524 | 687 |
|
0 | 688 |
This method would be used before the execute*() method is invoked. |
1524 | 689 |
|
0 | 690 |
Implementations are free to have this method do nothing and users are |
691 |
free to not use it. |
|
692 |
""" |
|
693 |
pass |
|
694 |
||
1524 | 695 |
|
0 | 696 |
def setoutputsize(self, size, column=None): |
697 |
"""Set a column buffer size for fetches of large columns (e.g. LONGs, |
|
698 |
BLOBs, etc.). The column is specified as an index into the result |
|
699 |
sequence. Not specifying the column will set the default size for all |
|
700 |
large columns in the cursor. |
|
1524 | 701 |
|
0 | 702 |
This method would be used before the execute*() method is invoked. |
1524 | 703 |
|
0 | 704 |
Implementations are free to have this method do nothing and users are |
705 |
free to not use it. |
|
1524 | 706 |
""" |
0 | 707 |
pass |
708 |
||
1524 | 709 |
|
0 | 710 |
class LogCursor(Cursor): |
711 |
"""override the standard cursor to log executed queries""" |
|
1524 | 712 |
|
0 | 713 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
714 |
"""override the standard cursor to log executed queries""" |
|
715 |
tstart, cstart = time(), clock() |
|
716 |
rset = Cursor.execute(self, operation, parameters, eid_key, build_descr) |
|
717 |
self.connection.executed_queries.append((operation, parameters, |
|
718 |
time() - tstart, clock() - cstart)) |
|
719 |
return rset |
|
720 |