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