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