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