author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 09 Apr 2010 15:10:26 +0200 | |
changeset 5201 | 2b4267157f85 |
parent 5174 | 78438ad513ca |
child 5223 | 6abd6e3599f4 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3833
diff
changeset
|
8 |
:copyright: 2001-2010 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 |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
17 |
from warnings import warn |
0 | 18 |
|
1923
3802c2e37e72
handle speaking to an instance using old entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1882
diff
changeset
|
19 |
from logilab.common.logging_ext import set_log_methods |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
20 |
from logilab.common.decorators import monkeypatch |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
21 |
from logilab.common.deprecation import deprecated |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
22 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
23 |
from cubicweb import ETYPE_NAME_MAP, ConnectionError, cwvreg, cwconfig |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
24 |
from cubicweb.req import RequestSessionBase |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
25 |
|
1524 | 26 |
|
0 | 27 |
_MARKER = object() |
28 |
||
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
29 |
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
|
30 |
try: |
4716
55b6a3262071
fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4705
diff
changeset
|
31 |
return super(DBAPIRequest, self).property_value(name) |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
32 |
except KeyError: |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
33 |
return '' |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
34 |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
35 |
def multiple_connections_fix(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
36 |
"""some monkey patching necessary when an application has to deal with |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
37 |
several connections to different repositories. It tries to hide buggy class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
38 |
attributes since classes are not designed to be shared among multiple |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
39 |
registries. |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
40 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
41 |
defaultcls = cwvreg.VRegistry.REGISTRY_FACTORY[None] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
42 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
43 |
etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
44 |
orig_etype_class = etypescls.orig_etype_class = etypescls.etype_class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
45 |
@monkeypatch(defaultcls) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
46 |
def etype_class(self, etype): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
47 |
"""return an entity class for the given entity type. |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
48 |
Try to find out a specific class for this kind of entity or |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
49 |
default to a dump of the class registered for 'Any' |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
50 |
""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
51 |
usercls = orig_etype_class(self, etype) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
52 |
if etype == 'Any': |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
53 |
return usercls |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
54 |
usercls.e_schema = self.schema.eschema(etype) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
55 |
return usercls |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
56 |
|
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
57 |
def multiple_connections_unfix(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
58 |
etypescls = cwvreg.VRegistry.REGISTRY_FACTORY['etypes'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
59 |
etypescls.etype_class = etypescls.orig_etype_class |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
60 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
61 |
|
0 | 62 |
class ConnectionProperties(object): |
63 |
def __init__(self, cnxtype=None, lang=None, close=True, log=False): |
|
64 |
self.cnxtype = cnxtype or 'pyro' |
|
65 |
self.lang = lang |
|
66 |
self.log_queries = log |
|
67 |
self.close_on_del = close |
|
68 |
||
69 |
||
70 |
def get_repository(method, database=None, config=None, vreg=None): |
|
71 |
"""get a proxy object to the CubicWeb repository, using a specific RPC method. |
|
1524 | 72 |
|
0 | 73 |
Only 'in-memory' and 'pyro' are supported for now. Either vreg or config |
74 |
argument should be given |
|
75 |
""" |
|
76 |
assert method in ('pyro', 'inmemory') |
|
77 |
assert vreg or config |
|
78 |
if vreg and not config: |
|
79 |
config = vreg.config |
|
80 |
if method == 'inmemory': |
|
81 |
# get local access to the repository |
|
82 |
from cubicweb.server.repository import Repository |
|
83 |
return Repository(config, vreg=vreg) |
|
84 |
else: # method == 'pyro' |
|
85 |
# resolve the Pyro object |
|
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
86 |
from logilab.common.pyro_ext import ns_get_proxy |
0 | 87 |
try: |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
88 |
return ns_get_proxy(database, |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
89 |
defaultnsgroup=config['pyro-ns-group'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
90 |
nshost=config['pyro-ns-host']) |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
91 |
except Exception, ex: |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
92 |
raise ConnectionError(str(ex)) |
1524 | 93 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
94 |
def repo_connect(repo, login, **kwargs): |
0 | 95 |
"""Constructor to create a new connection to the CubicWeb repository. |
1524 | 96 |
|
0 | 97 |
Returns a Connection instance. |
98 |
""" |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
99 |
if not 'cnxprops' in kwargs: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
100 |
kwargs['cnxprops'] = ConnectionProperties('inmemory') |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
101 |
cnxid = repo.connect(unicode(login), **kwargs) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
102 |
cnx = Connection(repo, cnxid, kwargs['cnxprops']) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
103 |
if kwargs['cnxprops'].cnxtype == 'inmemory': |
0 | 104 |
cnx.vreg = repo.vreg |
105 |
return cnx |
|
1524 | 106 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
107 |
def connect(database=None, login=None, host=None, group=None, |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
108 |
cnxprops=None, setvreg=True, mulcnx=True, initlog=True, **kwargs): |
0 | 109 |
"""Constructor for creating a connection to the CubicWeb repository. |
110 |
Returns a Connection object. |
|
111 |
||
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
112 |
When method is 'pyro', setvreg is True, try to deal with connections to |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
113 |
differents instances in the same process unless specified otherwise by |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
114 |
setting the mulcnx to False. |
0 | 115 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
116 |
config = cwconfig.CubicWebNoAppConfiguration() |
0 | 117 |
if host: |
118 |
config.global_set_option('pyro-ns-host', host) |
|
119 |
if group: |
|
120 |
config.global_set_option('pyro-ns-group', group) |
|
121 |
cnxprops = cnxprops or ConnectionProperties() |
|
122 |
method = cnxprops.cnxtype |
|
123 |
repo = get_repository(method, database, config=config) |
|
124 |
if method == 'inmemory': |
|
125 |
vreg = repo.vreg |
|
126 |
elif setvreg: |
|
127 |
if mulcnx: |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
128 |
multiple_connections_fix() |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
129 |
vreg = cwvreg.CubicWebVRegistry(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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
vreg.set_schema(schema) |
0 | 136 |
else: |
137 |
vreg = None |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
138 |
cnx = repo_connect(repo, login, cnxprops=cnxprops, **kwargs) |
0 | 139 |
cnx.vreg = vreg |
140 |
return cnx |
|
141 |
||
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
142 |
def in_memory_cnx(config, login, **kwargs): |
0 | 143 |
"""usefull method for testing and scripting to get a dbapi.Connection |
1524 | 144 |
object connected to an in-memory repository instance |
0 | 145 |
""" |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
146 |
if isinstance(config, cwvreg.CubicWebVRegistry): |
0 | 147 |
vreg = config |
148 |
config = None |
|
149 |
else: |
|
150 |
vreg = None |
|
151 |
# get local access to the repository |
|
152 |
repo = get_repository('inmemory', config=config, vreg=vreg) |
|
153 |
# connection to the CubicWeb repository |
|
154 |
cnxprops = ConnectionProperties('inmemory') |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
155 |
cnx = repo_connect(repo, login, cnxprops=cnxprops, **kwargs) |
0 | 156 |
return repo, cnx |
157 |
||
158 |
||
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
159 |
class DBAPIRequest(RequestSessionBase): |
1524 | 160 |
|
0 | 161 |
def __init__(self, vreg, cnx=None): |
162 |
super(DBAPIRequest, self).__init__(vreg) |
|
163 |
try: |
|
164 |
# no vreg or config which doesn't handle translations |
|
165 |
self.translations = vreg.config.translations |
|
166 |
except AttributeError: |
|
167 |
self.translations = {} |
|
168 |
self.set_default_language(vreg) |
|
169 |
# cache entities built during the request |
|
170 |
self._eid_cache = {} |
|
171 |
# these args are initialized after a connection is |
|
172 |
# established |
|
173 |
self.cnx = None # connection associated to the request |
|
174 |
self._user = None # request's user, set at authentication |
|
175 |
if cnx is not None: |
|
176 |
self.set_connection(cnx) |
|
177 |
||
178 |
def base_url(self): |
|
179 |
return self.vreg.config['base-url'] |
|
1524 | 180 |
|
0 | 181 |
def from_controller(self): |
182 |
return 'view' |
|
1524 | 183 |
|
0 | 184 |
def set_connection(self, cnx, user=None): |
185 |
"""method called by the session handler when the user is authenticated |
|
186 |
or an anonymous connection is open |
|
187 |
""" |
|
188 |
self.cnx = cnx |
|
189 |
self.cursor = cnx.cursor(self) |
|
190 |
self.set_user(user) |
|
1524 | 191 |
|
0 | 192 |
def set_default_language(self, vreg): |
193 |
try: |
|
194 |
self.lang = vreg.property_value('ui.language') |
|
195 |
except: # property may not be registered |
|
196 |
self.lang = 'en' |
|
197 |
# use req.__ to translate a message without registering it to the catalog |
|
198 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
199 |
gettext, pgettext = self.translations[self.lang] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
200 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3258
diff
changeset
|
201 |
self.pgettext = pgettext |
0 | 202 |
except KeyError: |
203 |
# this occurs usually during test execution |
|
204 |
self._ = self.__ = unicode |
|
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4716
diff
changeset
|
205 |
self.pgettext = lambda x, y: y |
2111
5e142c7a4531
different message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
206 |
self.debug('request default language: %s', self.lang) |
0 | 207 |
|
208 |
def describe(self, eid): |
|
209 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
210 |
return self.cnx.describe(eid) |
|
1524 | 211 |
|
0 | 212 |
def source_defs(self): |
213 |
"""return the definition of sources used by the repository.""" |
|
214 |
return self.cnx.source_defs() |
|
1524 | 215 |
|
0 | 216 |
# entities cache management ############################################### |
1524 | 217 |
|
0 | 218 |
def entity_cache(self, eid): |
219 |
return self._eid_cache[eid] |
|
1524 | 220 |
|
0 | 221 |
def set_entity_cache(self, entity): |
222 |
self._eid_cache[entity.eid] = entity |
|
223 |
||
224 |
def cached_entities(self): |
|
225 |
return self._eid_cache.values() |
|
1524 | 226 |
|
0 | 227 |
def drop_entity_cache(self, eid=None): |
228 |
if eid is None: |
|
229 |
self._eid_cache = {} |
|
230 |
else: |
|
231 |
del self._eid_cache[eid] |
|
232 |
||
233 |
# low level session data management ####################################### |
|
234 |
||
235 |
def session_data(self): |
|
236 |
"""return a dictionnary containing session data""" |
|
237 |
return self.cnx.session_data() |
|
238 |
||
239 |
def get_session_data(self, key, default=None, pop=False): |
|
240 |
"""return value associated to `key` in session data""" |
|
4592
ddbab12acae0
ensure cnx is set first (necessary to introduce of a form object to handle the login form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
241 |
if self.cnx is None: |
4895
7003912cec86
[dbapi] return default argument instead of None when no connection set yet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4850
diff
changeset
|
242 |
return default # before the connection has been established |
0 | 243 |
return self.cnx.get_session_data(key, default, pop) |
1524 | 244 |
|
0 | 245 |
def set_session_data(self, key, value): |
246 |
"""set value associated to `key` in session data""" |
|
247 |
return self.cnx.set_session_data(key, value) |
|
1524 | 248 |
|
0 | 249 |
def del_session_data(self, key): |
250 |
"""remove value associated to `key` in session data""" |
|
251 |
return self.cnx.del_session_data(key) |
|
252 |
||
253 |
def get_shared_data(self, key, default=None, pop=False): |
|
254 |
"""return value associated to `key` in shared data""" |
|
255 |
return self.cnx.get_shared_data(key, default, pop) |
|
1524 | 256 |
|
0 | 257 |
def set_shared_data(self, key, value, querydata=False): |
258 |
"""set value associated to `key` in shared data |
|
259 |
||
260 |
if `querydata` is true, the value will be added to the repository |
|
261 |
session's query data which are cleared on commit/rollback of the current |
|
262 |
transaction, and won't be available through the connexion, only on the |
|
263 |
repository side. |
|
264 |
""" |
|
265 |
return self.cnx.set_shared_data(key, value, querydata) |
|
266 |
||
267 |
# server session compat layer ############################################# |
|
268 |
||
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
269 |
def hijack_user(self, user): |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
270 |
"""return a fake request/session using specified user""" |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
271 |
req = DBAPIRequest(self.vreg) |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
272 |
req.set_connection(self.cnx, user) |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
273 |
return req |
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
274 |
|
0 | 275 |
@property |
276 |
def user(self): |
|
277 |
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
|
278 |
self.set_user(self.cnx.user(self, {'lang': self.lang})) |
0 | 279 |
return self._user |
280 |
||
281 |
def set_user(self, user): |
|
282 |
self._user = user |
|
283 |
if user: |
|
284 |
self.set_entity_cache(user) |
|
1524 | 285 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
286 |
def execute(self, rql, args=None, eid_key=None, build_descr=True): |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
287 |
if eid_key is not None: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
288 |
warn('[3.8] eid_key is deprecated, you can safely remove this argument', |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
289 |
DeprecationWarning, stacklevel=2) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
290 |
return self.cursor.execute(rql, args, build_descr=build_descr) |
0 | 291 |
|
292 |
set_log_methods(DBAPIRequest, getLogger('cubicweb.dbapi')) |
|
1524 | 293 |
|
294 |
||
0 | 295 |
# exceptions ################################################################## |
296 |
||
297 |
class ProgrammingError(Exception): #DatabaseError): |
|
298 |
"""Exception raised for errors that are related to the database's operation |
|
299 |
and not necessarily under the control of the programmer, e.g. an unexpected |
|
300 |
disconnect occurs, the data source name is not found, a transaction could |
|
301 |
not be processed, a memory allocation error occurred during processing, |
|
302 |
etc. |
|
303 |
""" |
|
304 |
||
305 |
# module level objects ######################################################## |
|
306 |
||
307 |
||
308 |
apilevel = '2.0' |
|
309 |
||
310 |
"""Integer constant stating the level of thread safety the interface supports. |
|
311 |
Possible values are: |
|
312 |
||
313 |
0 Threads may not share the module. |
|
314 |
1 Threads may share the module, but not connections. |
|
315 |
2 Threads may share the module and connections. |
|
316 |
3 Threads may share the module, connections and |
|
317 |
cursors. |
|
318 |
||
319 |
Sharing in the above context means that two threads may use a resource without |
|
320 |
wrapping it using a mutex semaphore to implement resource locking. Note that |
|
321 |
you cannot always make external resources thread safe by managing access using |
|
322 |
a mutex: the resource may rely on global variables or other external sources |
|
323 |
that are beyond your control. |
|
324 |
""" |
|
325 |
threadsafety = 1 |
|
326 |
||
327 |
"""String constant stating the type of parameter marker formatting expected by |
|
328 |
the interface. Possible values are : |
|
329 |
||
1524 | 330 |
'qmark' Question mark style, |
0 | 331 |
e.g. '...WHERE name=?' |
1524 | 332 |
'numeric' Numeric, positional style, |
0 | 333 |
e.g. '...WHERE name=:1' |
1524 | 334 |
'named' Named style, |
0 | 335 |
e.g. '...WHERE name=:name' |
1524 | 336 |
'format' ANSI C printf format codes, |
0 | 337 |
e.g. '...WHERE name=%s' |
1524 | 338 |
'pyformat' Python extended format codes, |
0 | 339 |
e.g. '...WHERE name=%(name)s' |
340 |
""" |
|
341 |
paramstyle = 'pyformat' |
|
342 |
||
343 |
||
344 |
# connection object ########################################################### |
|
345 |
||
346 |
class Connection(object): |
|
3258
6536ee4f37f7
update the documentation
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3110
diff
changeset
|
347 |
"""DB-API 2.0 compatible Connection object for CubicWeb |
0 | 348 |
""" |
349 |
# make exceptions available through the connection object |
|
350 |
ProgrammingError = ProgrammingError |
|
351 |
||
352 |
def __init__(self, repo, cnxid, cnxprops=None): |
|
353 |
self._repo = repo |
|
354 |
self.sessionid = cnxid |
|
355 |
self._close_on_del = getattr(cnxprops, 'close_on_del', True) |
|
356 |
self._cnxtype = getattr(cnxprops, 'cnxtype', 'pyro') |
|
357 |
self._closed = None |
|
358 |
if cnxprops and cnxprops.log_queries: |
|
359 |
self.executed_queries = [] |
|
360 |
self.cursor_class = LogCursor |
|
361 |
else: |
|
362 |
self.cursor_class = Cursor |
|
363 |
self.anonymous_connection = False |
|
364 |
self.vreg = None |
|
365 |
# session's data |
|
366 |
self.data = {} |
|
367 |
||
368 |
def __repr__(self): |
|
369 |
if self.anonymous_connection: |
|
370 |
return '<Connection %s (anonymous)>' % self.sessionid |
|
371 |
return '<Connection %s>' % self.sessionid |
|
372 |
||
5100
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
373 |
def __enter__(self): |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
374 |
return self.cursor() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
375 |
|
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
376 |
def __exit__(self, exc_type, exc_val, exc_tb): |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
377 |
if exc_type is None: |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
378 |
self.commit() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
379 |
else: |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
380 |
self.rollback() |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
381 |
return False #propagate the exception |
04c71ebf38a5
Add context management methode to dbapi.Connection
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
5032
diff
changeset
|
382 |
|
0 | 383 |
def request(self): |
384 |
return DBAPIRequest(self.vreg, self) |
|
1524 | 385 |
|
0 | 386 |
def session_data(self): |
387 |
"""return a dictionnary containing session data""" |
|
388 |
return self.data |
|
1524 | 389 |
|
0 | 390 |
def get_session_data(self, key, default=None, pop=False): |
391 |
"""return value associated to `key` in session data""" |
|
392 |
if pop: |
|
393 |
return self.data.pop(key, default) |
|
394 |
else: |
|
395 |
return self.data.get(key, default) |
|
1524 | 396 |
|
0 | 397 |
def set_session_data(self, key, value): |
398 |
"""set value associated to `key` in session data""" |
|
399 |
self.data[key] = value |
|
1524 | 400 |
|
0 | 401 |
def del_session_data(self, key): |
402 |
"""remove value associated to `key` in session data""" |
|
403 |
try: |
|
404 |
del self.data[key] |
|
405 |
except KeyError: |
|
1524 | 406 |
pass |
0 | 407 |
|
408 |
def check(self): |
|
5032 | 409 |
"""raise `BadConnectionId` if the connection is no more valid""" |
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
410 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
411 |
raise ProgrammingError('Closed connection') |
1132 | 412 |
self._repo.check_session(self.sessionid) |
0 | 413 |
|
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
|
414 |
def set_session_props(self, **props): |
5032 | 415 |
"""raise `BadConnectionId` if the connection is no more valid""" |
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
416 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
417 |
raise ProgrammingError('Closed connection') |
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
|
418 |
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
|
419 |
|
0 | 420 |
def get_shared_data(self, key, default=None, pop=False): |
421 |
"""return value associated to `key` in shared data""" |
|
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
422 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
423 |
raise ProgrammingError('Closed connection') |
0 | 424 |
return self._repo.get_shared_data(self.sessionid, key, default, pop) |
1524 | 425 |
|
0 | 426 |
def set_shared_data(self, key, value, querydata=False): |
427 |
"""set value associated to `key` in shared data |
|
428 |
||
429 |
if `querydata` is true, the value will be added to the repository |
|
430 |
session's query data which are cleared on commit/rollback of the current |
|
431 |
transaction, and won't be available through the connexion, only on the |
|
432 |
repository side. |
|
433 |
""" |
|
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
434 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
435 |
raise ProgrammingError('Closed connection') |
0 | 436 |
return self._repo.set_shared_data(self.sessionid, key, value, querydata) |
1524 | 437 |
|
0 | 438 |
def get_schema(self): |
439 |
"""Return the schema currently used by the repository. |
|
1524 | 440 |
|
0 | 441 |
This is NOT part of the DB-API. |
442 |
""" |
|
443 |
if self._closed is not None: |
|
444 |
raise ProgrammingError('Closed connection') |
|
445 |
return self._repo.get_schema() |
|
446 |
||
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
447 |
def load_appobjects(self, cubes=_MARKER, subpath=None, expand=True, |
3707
78596919ede3
[c-c] fixes for shell w/ pyro instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
448 |
force_reload=None): |
0 | 449 |
config = self.vreg.config |
450 |
if cubes is _MARKER: |
|
451 |
cubes = self._repo.get_cubes() |
|
452 |
elif cubes is None: |
|
453 |
cubes = () |
|
454 |
else: |
|
455 |
if not isinstance(cubes, (list, tuple)): |
|
456 |
cubes = (cubes,) |
|
457 |
if expand: |
|
458 |
cubes = config.expand_cubes(cubes) |
|
459 |
if subpath is None: |
|
460 |
subpath = esubpath = ('entities', 'views') |
|
461 |
else: |
|
462 |
esubpath = subpath |
|
463 |
if 'views' in subpath: |
|
464 |
esubpath = list(subpath) |
|
465 |
esubpath.remove('views') |
|
466 |
esubpath.append('web/views') |
|
467 |
cubes = reversed([config.cube_dir(p) for p in cubes]) |
|
468 |
vpath = config.build_vregistry_path(cubes, evobjpath=esubpath, |
|
469 |
tvobjpath=subpath) |
|
470 |
self.vreg.register_objects(vpath, force_reload) |
|
471 |
if self._cnxtype == 'inmemory': |
|
472 |
# should reinit hooks manager as well |
|
473 |
hm, config = self._repo.hm, self._repo.config |
|
474 |
hm.set_schema(hm.schema) # reset structure |
|
475 |
hm.register_system_hooks(config) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
476 |
# instance specific hooks |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2266
diff
changeset
|
477 |
if self._repo.config.instance_hooks: |
0 | 478 |
hm.register_hooks(config.load_hooks(self.vreg)) |
1524 | 479 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
480 |
load_vobjects = deprecated()(load_appobjects) |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
481 |
|
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
482 |
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
|
483 |
"""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
|
484 |
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
|
485 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
486 |
You should call `load_appobjects` at some point to register those views. |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
487 |
""" |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
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
|
493 |
DBAPIRequest.form = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
494 |
DBAPIRequest.data = {} |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
495 |
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
|
496 |
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
|
497 |
DBAPIRequest.url = fake |
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
498 |
DBAPIRequest.next_tabindex = fake |
3832
ce8dd861f442
#473188: missing get_page_data when patching bare db-api request for web compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3707
diff
changeset
|
499 |
DBAPIRequest.get_page_data = fake |
3833
5507aa75f601
add set_page_data as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3832
diff
changeset
|
500 |
DBAPIRequest.set_page_data = fake |
2496
fbd1fd2ca312
#343624: access to ORM and views outside web engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
501 |
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
|
502 |
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
|
503 |
# 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
|
504 |
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
|
505 |
# 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
|
506 |
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
|
507 |
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
|
508 |
|
0 | 509 |
def source_defs(self): |
510 |
"""Return the definition of sources used by the repository. |
|
1524 | 511 |
|
0 | 512 |
This is NOT part of the DB-API. |
513 |
""" |
|
514 |
if self._closed is not None: |
|
515 |
raise ProgrammingError('Closed connection') |
|
516 |
return self._repo.source_defs() |
|
517 |
||
322
0d9aca19b3d0
make req argument optional
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
169
diff
changeset
|
518 |
def user(self, req=None, props=None): |
0 | 519 |
"""return the User object associated to this connection""" |
520 |
# cnx validity is checked by the call to .user_info |
|
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
521 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
522 |
raise ProgrammingError('Closed connection') |
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
|
523 |
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
|
524 |
props) |
0 | 525 |
if req is None: |
526 |
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
|
527 |
rset = req.eid_rset(eid, 'CWUser') |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
528 |
user = self.vreg['etypes'].etype_class('CWUser')(req, rset, row=0, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
529 |
groups=groups, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2496
diff
changeset
|
530 |
properties=properties) |
0 | 531 |
user['login'] = login # cache login |
532 |
return user |
|
533 |
||
534 |
def __del__(self): |
|
535 |
"""close the remote connection if necessary""" |
|
536 |
if self._closed is None and self._close_on_del: |
|
537 |
try: |
|
538 |
self.close() |
|
539 |
except: |
|
540 |
pass |
|
1524 | 541 |
|
0 | 542 |
def describe(self, eid): |
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
543 |
if self._closed is not None: |
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
544 |
raise ProgrammingError('Closed connection') |
0 | 545 |
return self._repo.describe(self.sessionid, eid) |
1524 | 546 |
|
0 | 547 |
def close(self): |
548 |
"""Close the connection now (rather than whenever __del__ is called). |
|
1524 | 549 |
|
0 | 550 |
The connection will be unusable from this point forward; an Error (or |
551 |
subclass) exception will be raised if any operation is attempted with |
|
552 |
the connection. The same applies to all cursor objects trying to use the |
|
553 |
connection. Note that closing a connection without committing the |
|
554 |
changes first will cause an implicit rollback to be performed. |
|
555 |
""" |
|
556 |
if self._closed: |
|
557 |
raise ProgrammingError('Connection is already closed') |
|
558 |
self._repo.close(self.sessionid) |
|
4768
430b89aed996
delete pyro proxy on connection close, properly raise programming error when working on a closed connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
559 |
del self._repo # necessary for proper garbage collection |
0 | 560 |
self._closed = 1 |
561 |
||
562 |
def commit(self): |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
563 |
"""Commit pending transaction for this connection to the repository. |
1524 | 564 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
565 |
may raises `Unauthorized` or `ValidationError` if we attempted to do |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
566 |
something we're not allowed to for security or integrity reason. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
567 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
568 |
If the transaction is undoable, a transaction id will be returned. |
0 | 569 |
""" |
570 |
if not self._closed is None: |
|
571 |
raise ProgrammingError('Connection is already closed') |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
572 |
return self._repo.commit(self.sessionid) |
0 | 573 |
|
574 |
def rollback(self): |
|
575 |
"""This method is optional since not all databases provide transaction |
|
576 |
support. |
|
1524 | 577 |
|
0 | 578 |
In case a database does provide transactions this method causes the the |
579 |
database to roll back to the start of any pending transaction. Closing |
|
580 |
a connection without committing the changes first will cause an implicit |
|
581 |
rollback to be performed. |
|
582 |
""" |
|
583 |
if not self._closed is None: |
|
584 |
raise ProgrammingError('Connection is already closed') |
|
585 |
self._repo.rollback(self.sessionid) |
|
586 |
||
587 |
def cursor(self, req=None): |
|
588 |
"""Return a new Cursor Object using the connection. If the database |
|
589 |
does not provide a direct cursor concept, the module will have to |
|
590 |
emulate cursors using other means to the extent needed by this |
|
591 |
specification. |
|
592 |
""" |
|
593 |
if self._closed is not None: |
|
594 |
raise ProgrammingError('Can\'t get cursor on closed connection') |
|
595 |
if req is None: |
|
596 |
req = self.request() |
|
597 |
return self.cursor_class(self, self._repo, req=req) |
|
598 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
599 |
# undo support ############################################################ |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
600 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
601 |
def undoable_transactions(self, ueid=None, req=None, **actionfilters): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
602 |
"""Return a list of undoable transaction objects by the connection's |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
603 |
user, ordered by descendant transaction time. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
604 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
605 |
Managers may filter according to user (eid) who has done the transaction |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
606 |
using the `ueid` argument. Others will only see their own transactions. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
607 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
608 |
Additional filtering capabilities is provided by using the following |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
609 |
named arguments: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
610 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
611 |
* `etype` to get only transactions creating/updating/deleting entities |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
612 |
of the given type |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
613 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
614 |
* `eid` to get only transactions applied to entity of the given eid |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
615 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
616 |
* `action` to get only transactions doing the given action (action in |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
617 |
'C', 'U', 'D', 'A', 'R'). If `etype`, action can only be 'C', 'U' or |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
618 |
'D'. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
619 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
620 |
* `public`: when additional filtering is provided, their are by default |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
621 |
only searched in 'public' actions, unless a `public` argument is given |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
622 |
and set to false. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
623 |
""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
624 |
txinfos = self._repo.undoable_transactions(self.sessionid, ueid, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
625 |
**actionfilters) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
626 |
if req is None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
627 |
req = self.request() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
628 |
for txinfo in txinfos: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
629 |
txinfo.req = req |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
630 |
return txinfos |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
631 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
632 |
def transaction_info(self, txuuid, req=None): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
633 |
"""Return transaction object for the given uid. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
634 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
635 |
raise `NoSuchTransaction` if not found or if session's user is not |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
636 |
allowed (eg not in managers group and the transaction doesn't belong to |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
637 |
him). |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
638 |
""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
639 |
txinfo = self._repo.transaction_info(self.sessionid, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
640 |
if req is None: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
641 |
req = self.request() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
642 |
txinfo.req = req |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
643 |
return txinfo |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
644 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
645 |
def transaction_actions(self, txuuid, public=True): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
646 |
"""Return an ordered list of action effectued during that transaction. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
647 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
648 |
If public is true, return only 'public' actions, eg not ones triggered |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
649 |
under the cover by hooks, else return all actions. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
650 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
651 |
raise `NoSuchTransaction` if the transaction is not found or if |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
652 |
session's user is not allowed (eg not in managers group and the |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
653 |
transaction doesn't belong to him). |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
654 |
""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
655 |
return self._repo.transaction_actions(self.sessionid, txuuid, public) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
656 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
657 |
def undo_transaction(self, txuuid): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
658 |
"""Undo the given transaction. Return potential restoration errors. |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
659 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
660 |
raise `NoSuchTransaction` if not found or if session's user is not |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
661 |
allowed (eg not in managers group and the transaction doesn't belong to |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
662 |
him). |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
663 |
""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
664 |
return self._repo.undo_transaction(self.sessionid, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4895
diff
changeset
|
665 |
|
0 | 666 |
|
667 |
# cursor object ############################################################### |
|
668 |
||
669 |
class Cursor(object): |
|
670 |
"""These objects represent a database cursor, which is used to manage the |
|
671 |
context of a fetch operation. Cursors created from the same connection are |
|
672 |
not isolated, i.e., any changes done to the database by a cursor are |
|
673 |
immediately visible by the other cursors. Cursors created from different |
|
674 |
connections can or can not be isolated, depending on how the transaction |
|
675 |
support is implemented (see also the connection's rollback() and commit() |
|
676 |
methods.) |
|
677 |
""" |
|
1524 | 678 |
|
0 | 679 |
def __init__(self, connection, repo, req=None): |
680 |
"""This read-only attribute return a reference to the Connection |
|
681 |
object on which the cursor was created. |
|
682 |
""" |
|
683 |
self.connection = connection |
|
684 |
"""optionnal issuing request instance""" |
|
685 |
self.req = req |
|
686 |
||
687 |
"""This read/write attribute specifies the number of rows to fetch at a |
|
688 |
time with fetchmany(). It defaults to 1 meaning to fetch a single row |
|
689 |
at a time. |
|
1524 | 690 |
|
0 | 691 |
Implementations must observe this value with respect to the fetchmany() |
692 |
method, but are free to interact with the database a single row at a |
|
693 |
time. It may also be used in the implementation of executemany(). |
|
694 |
""" |
|
695 |
self.arraysize = 1 |
|
696 |
||
697 |
self._repo = repo |
|
698 |
self._sessid = connection.sessionid |
|
699 |
self._res = None |
|
700 |
self._closed = None |
|
701 |
self._index = 0 |
|
702 |
||
1524 | 703 |
|
0 | 704 |
def close(self): |
705 |
"""Close the cursor now (rather than whenever __del__ is called). The |
|
706 |
cursor will be unusable from this point forward; an Error (or subclass) |
|
707 |
exception will be raised if any operation is attempted with the cursor. |
|
708 |
""" |
|
709 |
self._closed = True |
|
710 |
||
1524 | 711 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
712 |
def execute(self, rql, args=None, eid_key=None, build_descr=True): |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
713 |
"""execute a rql query, return resulting rows and their description in |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
714 |
a :class:`~cubicweb.rset.ResultSet` object |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
715 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
716 |
* `rql` should be an Unicode string or a plain ASCII string, containing |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
717 |
the rql query |
1524 | 718 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
719 |
* `args` the optional args dictionary associated to the query, with key |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
720 |
matching named substitution in `rql` |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
721 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
722 |
* `build_descr` is a boolean flag indicating if the description should |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
723 |
be built on select queries (if false, the description will be en empty |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
724 |
list) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
725 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
726 |
on INSERT queries, there will be one row for each inserted entity, |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
727 |
containing its eid |
1524 | 728 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
729 |
on SET queries, XXX describe |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
730 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
731 |
DELETE queries returns no result. |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
732 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
733 |
.. Note:: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
734 |
to maximize the rql parsing/analyzing cache performance, you should |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
735 |
always use substitute arguments in queries, i.e. avoid query such as:: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
736 |
|
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
737 |
execute('Any X WHERE X eid 123') |
1524 | 738 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
739 |
use:: |
1524 | 740 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
741 |
execute('Any X WHERE X eid %(x)s', {'x': 123}) |
0 | 742 |
""" |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
743 |
if eid_key is not None: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
744 |
warn('[3.8] eid_key is deprecated, you can safely remove this argument', |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
745 |
DeprecationWarning, stacklevel=2) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
746 |
self._res = rset = self._repo.execute(self._sessid, rql, |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5100
diff
changeset
|
747 |
args, build_descr) |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4768
diff
changeset
|
748 |
rset.req = self.req |
0 | 749 |
self._index = 0 |
4850
bd640b137f50
[refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4768
diff
changeset
|
750 |
return rset |
1524 | 751 |
|
0 | 752 |
|
753 |
def executemany(self, operation, seq_of_parameters): |
|
754 |
"""Prepare a database operation (query or command) and then execute it |
|
755 |
against all parameter sequences or mappings found in the sequence |
|
756 |
seq_of_parameters. |
|
1524 | 757 |
|
0 | 758 |
Modules are free to implement this method using multiple calls to the |
759 |
execute() method or by using array operations to have the database |
|
760 |
process the sequence as a whole in one call. |
|
1524 | 761 |
|
0 | 762 |
Use of this method for an operation which produces one or more result |
763 |
sets constitutes undefined behavior, and the implementation is |
|
764 |
permitted (but not required) to raise an exception when it detects that |
|
765 |
a result set has been created by an invocation of the operation. |
|
1524 | 766 |
|
0 | 767 |
The same comments as for execute() also apply accordingly to this |
768 |
method. |
|
1524 | 769 |
|
0 | 770 |
Return values are not defined. |
771 |
""" |
|
772 |
for parameters in seq_of_parameters: |
|
773 |
self.execute(operation, parameters) |
|
774 |
if self._res.rows is not None: |
|
775 |
self._res = None |
|
776 |
raise ProgrammingError('Operation returned a result set') |
|
777 |
||
778 |
||
779 |
def fetchone(self): |
|
780 |
"""Fetch the next row of a query result set, returning a single |
|
781 |
sequence, or None when no more data is available. |
|
1524 | 782 |
|
0 | 783 |
An Error (or subclass) exception is raised if the previous call to |
784 |
execute*() did not produce any result set or no call was issued yet. |
|
785 |
""" |
|
786 |
if self._res is None: |
|
787 |
raise ProgrammingError('No result set') |
|
788 |
row = self._res.rows[self._index] |
|
789 |
self._index += 1 |
|
790 |
return row |
|
791 |
||
1524 | 792 |
|
0 | 793 |
def fetchmany(self, size=None): |
794 |
"""Fetch the next set of rows of a query result, returning a sequence |
|
795 |
of sequences (e.g. a list of tuples). An empty sequence is returned |
|
796 |
when no more rows are available. |
|
1524 | 797 |
|
0 | 798 |
The number of rows to fetch per call is specified by the parameter. If |
799 |
it is not given, the cursor's arraysize determines the number of rows |
|
800 |
to be fetched. The method should try to fetch as many rows as indicated |
|
801 |
by the size parameter. If this is not possible due to the specified |
|
802 |
number of rows not being available, fewer rows may be returned. |
|
1524 | 803 |
|
0 | 804 |
An Error (or subclass) exception is raised if the previous call to |
805 |
execute*() did not produce any result set or no call was issued yet. |
|
1524 | 806 |
|
0 | 807 |
Note there are performance considerations involved with the size |
808 |
parameter. For optimal performance, it is usually best to use the |
|
809 |
arraysize attribute. If the size parameter is used, then it is best |
|
810 |
for it to retain the same value from one fetchmany() call to the next. |
|
811 |
""" |
|
812 |
if self._res is None: |
|
813 |
raise ProgrammingError('No result set') |
|
814 |
if size is None: |
|
815 |
size = self.arraysize |
|
816 |
rows = self._res.rows[self._index:self._index + size] |
|
817 |
self._index += size |
|
818 |
return rows |
|
819 |
||
1524 | 820 |
|
0 | 821 |
def fetchall(self): |
822 |
"""Fetch all (remaining) rows of a query result, returning them as a |
|
823 |
sequence of sequences (e.g. a list of tuples). Note that the cursor's |
|
824 |
arraysize attribute can affect the performance of this operation. |
|
1524 | 825 |
|
0 | 826 |
An Error (or subclass) exception is raised if the previous call to |
827 |
execute*() did not produce any result set or no call was issued yet. |
|
828 |
""" |
|
829 |
if self._res is None: |
|
830 |
raise ProgrammingError('No result set') |
|
831 |
if not self._res.rows: |
|
832 |
return [] |
|
833 |
rows = self._res.rows[self._index:] |
|
834 |
self._index = len(self._res) |
|
835 |
return rows |
|
836 |
||
837 |
||
838 |
def setinputsizes(self, sizes): |
|
839 |
"""This can be used before a call to execute*() to predefine memory |
|
840 |
areas for the operation's parameters. |
|
1524 | 841 |
|
0 | 842 |
sizes is specified as a sequence -- one item for each input parameter. |
843 |
The item should be a Type Object that corresponds to the input that |
|
844 |
will be used, or it should be an integer specifying the maximum length |
|
845 |
of a string parameter. If the item is None, then no predefined memory |
|
846 |
area will be reserved for that column (this is useful to avoid |
|
847 |
predefined areas for large inputs). |
|
1524 | 848 |
|
0 | 849 |
This method would be used before the execute*() method is invoked. |
1524 | 850 |
|
0 | 851 |
Implementations are free to have this method do nothing and users are |
852 |
free to not use it. |
|
853 |
""" |
|
854 |
pass |
|
855 |
||
1524 | 856 |
|
0 | 857 |
def setoutputsize(self, size, column=None): |
858 |
"""Set a column buffer size for fetches of large columns (e.g. LONGs, |
|
859 |
BLOBs, etc.). The column is specified as an index into the result |
|
860 |
sequence. Not specifying the column will set the default size for all |
|
861 |
large columns in the cursor. |
|
1524 | 862 |
|
0 | 863 |
This method would be used before the execute*() method is invoked. |
1524 | 864 |
|
0 | 865 |
Implementations are free to have this method do nothing and users are |
866 |
free to not use it. |
|
1524 | 867 |
""" |
0 | 868 |
pass |
869 |
||
1524 | 870 |
|
0 | 871 |
class LogCursor(Cursor): |
872 |
"""override the standard cursor to log executed queries""" |
|
1524 | 873 |
|
0 | 874 |
def execute(self, operation, parameters=None, eid_key=None, build_descr=True): |
875 |
"""override the standard cursor to log executed queries""" |
|
876 |
tstart, cstart = time(), clock() |
|
877 |
rset = Cursor.execute(self, operation, parameters, eid_key, build_descr) |
|
878 |
self.connection.executed_queries.append((operation, parameters, |
|
879 |
time() - tstart, clock() - cstart)) |
|
880 |
return rset |
|
881 |