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