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