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