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