author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 28 Apr 2010 14:05:50 +0200 | |
branch | oldstable |
changeset 5428 | 98e0b7421e51 |
parent 5424 | 8ecbcbff9777 |
child 5557 | 1a534c596bff |
child 5637 | b72a838aa109 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""cubicweb server sources support |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
23 |
from os.path import join, splitext |
1263 | 24 |
from datetime import datetime, timedelta |
0 | 25 |
from logging import getLogger |
26 |
||
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
27 |
from cubicweb import set_log_methods, server |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
28 |
from cubicweb.schema import VIRTUAL_RTYPES |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
29 |
from cubicweb.server.sqlutils import SQL_PREFIX |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
30 |
|
0 | 31 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
32 |
def dbg_st_search(uri, union, varmap, args, cachekey=None, prefix='rql for'): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
33 |
if server.DEBUG & server.DBG_RQL: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
34 |
print ' %s %s source: %s' % (prefix, uri, union.as_string()) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
35 |
if varmap: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
36 |
print ' using varmap', varmap |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
37 |
if server.DEBUG & server.DBG_MORE: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
38 |
print ' args', args |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
print ' cache key', cachekey |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
40 |
print ' solutions', ','.join(str(s.solutions) |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
41 |
for s in union.children) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
42 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
43 |
return True |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
44 |
|
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
45 |
def dbg_results(results): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
46 |
if server.DEBUG & server.DBG_RQL: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
47 |
if len(results) > 10: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
48 |
print ' -->', results[:10], '...', len(results) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
49 |
else: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
50 |
print ' -->', results |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
51 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
52 |
return True |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
53 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
54 |
class TimedCache(dict): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
55 |
def __init__(self, ttlm, ttls=0): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
56 |
# time to live in minutes |
1263 | 57 |
self.ttl = timedelta(0, ttlm*60 + ttls, 0) |
1792 | 58 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
59 |
def __setitem__(self, key, value): |
1263 | 60 |
dict.__setitem__(self, key, (datetime.now(), value)) |
1792 | 61 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
62 |
def __getitem__(self, key): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
63 |
return dict.__getitem__(self, key)[1] |
1792 | 64 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
65 |
def clear_expired(self): |
1263 | 66 |
now_ = datetime.now() |
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
67 |
ttl = self.ttl |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
68 |
for key, (timestamp, value) in self.items(): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
69 |
if now_ - timestamp > ttl: |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
70 |
del self[key] |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
71 |
|
0 | 72 |
|
73 |
class AbstractSource(object): |
|
74 |
"""an abstract class for sources""" |
|
75 |
||
76 |
# boolean telling if modification hooks should be called when something is |
|
77 |
# modified in this source |
|
78 |
should_call_hooks = True |
|
79 |
# boolean telling if the repository should connect to this source during |
|
80 |
# migration |
|
81 |
connect_for_migration = True |
|
1792 | 82 |
|
0 | 83 |
# mappings telling which entities and relations are available in the source |
84 |
# keys are supported entity/relation types and values are boolean indicating |
|
85 |
# wether the support is read-only (False) or read-write (True) |
|
86 |
support_entities = {} |
|
87 |
support_relations = {} |
|
88 |
# a global identifier for this source, which has to be set by the source |
|
89 |
# instance |
|
90 |
uri = None |
|
91 |
# a reference to the system information helper |
|
92 |
repo = None |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
93 |
# a reference to the instance'schema (may differs from the source'schema) |
0 | 94 |
schema = None |
1792 | 95 |
|
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
96 |
# multi-sources planning control |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
97 |
dont_cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
98 |
cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
99 |
|
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
100 |
|
0 | 101 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
102 |
self.repo = repo |
|
103 |
self.uri = source_config['uri'] |
|
104 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
|
105 |
self.set_schema(appschema) |
|
106 |
self.support_relations['identity'] = False |
|
1792 | 107 |
|
0 | 108 |
def init_creating(self): |
109 |
"""method called by the repository once ready to create a new instance""" |
|
110 |
pass |
|
1792 | 111 |
|
0 | 112 |
def init(self): |
113 |
"""method called by the repository once ready to handle request""" |
|
114 |
pass |
|
1792 | 115 |
|
5124
d4d0b4b2298a
fix #822890, missing argument on abstract source backup method make backup failed on multi-sources instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
116 |
def backup(self, backupfile, confirm): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
117 |
"""method called to create a backup of source's data""" |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
118 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
119 |
|
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
120 |
def restore(self, backupfile, confirm, drop): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
121 |
"""method called to restore a backup of source's data""" |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
122 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
123 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
124 |
def close_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
125 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
126 |
pool._cursors.pop(self.uri, None) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
127 |
pool.source_cnxs[self.uri][1].close() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
128 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
129 |
def open_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
130 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
131 |
pool.source_cnxs[self.uri] = (self, self.get_connection()) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
132 |
|
0 | 133 |
def reset_caches(self): |
134 |
"""method called during test to reset potential source caches""" |
|
135 |
pass |
|
1792 | 136 |
|
0 | 137 |
def clear_eid_cache(self, eid, etype): |
138 |
"""clear potential caches for the given eid""" |
|
139 |
pass |
|
1792 | 140 |
|
0 | 141 |
def __repr__(self): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
142 |
return '<%s source @%#x>' % (self.uri, id(self)) |
0 | 143 |
|
144 |
def __cmp__(self, other): |
|
145 |
"""simple comparison function to get predictable source order, with the |
|
146 |
system source at last |
|
147 |
""" |
|
148 |
if self.uri == other.uri: |
|
149 |
return 0 |
|
150 |
if self.uri == 'system': |
|
151 |
return 1 |
|
152 |
if other.uri == 'system': |
|
153 |
return -1 |
|
154 |
return cmp(self.uri, other.uri) |
|
1792 | 155 |
|
0 | 156 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
157 |
"""set the instance'schema""" |
0 | 158 |
self.schema = schema |
1792 | 159 |
|
0 | 160 |
def support_entity(self, etype, write=False): |
161 |
"""return true if the given entity's type is handled by this adapter |
|
162 |
if write is true, return true only if it's a RW support |
|
163 |
""" |
|
164 |
try: |
|
165 |
wsupport = self.support_entities[etype] |
|
166 |
except KeyError: |
|
167 |
return False |
|
168 |
if write: |
|
169 |
return wsupport |
|
170 |
return True |
|
1792 | 171 |
|
0 | 172 |
def support_relation(self, rtype, write=False): |
173 |
"""return true if the given relation's type is handled by this adapter |
|
174 |
if write is true, return true only if it's a RW support |
|
175 |
||
1792 | 176 |
current implementation return true if the relation is defined into |
177 |
`support_relations` or if it is a final relation of a supported entity |
|
0 | 178 |
type |
179 |
""" |
|
180 |
try: |
|
181 |
wsupport = self.support_relations[rtype] |
|
182 |
except KeyError: |
|
183 |
rschema = self.schema.rschema(rtype) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3633
diff
changeset
|
184 |
if not rschema.final or rschema == 'has_text': |
0 | 185 |
return False |
186 |
for etype in rschema.subjects(): |
|
187 |
try: |
|
188 |
wsupport = self.support_entities[etype] |
|
189 |
break |
|
190 |
except KeyError: |
|
191 |
continue |
|
192 |
else: |
|
193 |
return False |
|
194 |
if write: |
|
195 |
return wsupport |
|
1792 | 196 |
return True |
197 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
198 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
199 |
"""return True if the relation may be crossed among sources. Rules are: |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
200 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
201 |
* if this source support the relation, can't be crossed unless explicitly |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
202 |
specified in .cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
203 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
204 |
* if this source doesn't support the relation, can be crossed unless |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
205 |
explicitly specified in .dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
206 |
""" |
3633
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
207 |
# XXX find a way to have relation such as state_of in dont cross |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
208 |
# relation (eg composite relation without both end type available? |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
209 |
# card 1 relation ? ...) |
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
210 |
if self.support_relation(rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
211 |
return rtype in self.cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
212 |
return rtype not in self.dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
213 |
|
0 | 214 |
def eid2extid(self, eid, session=None): |
215 |
return self.repo.eid2extid(self, eid, session) |
|
216 |
||
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1238
diff
changeset
|
217 |
def extid2eid(self, value, etype, session=None, **kwargs): |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1238
diff
changeset
|
218 |
return self.repo.extid2eid(self, value, etype, session, **kwargs) |
0 | 219 |
|
220 |
PUBLIC_KEYS = ('adapter', 'uri') |
|
221 |
def remove_sensitive_information(self, sourcedef): |
|
222 |
"""remove sensitive information such as login / password from source |
|
223 |
definition |
|
224 |
""" |
|
225 |
for key in sourcedef.keys(): |
|
226 |
if not key in self.PUBLIC_KEYS: |
|
227 |
sourcedef.pop(key) |
|
228 |
||
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
229 |
def _cleanup_system_relations(self, session): |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
230 |
"""remove relation in the system source referencing entities coming from |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
231 |
this source |
0 | 232 |
""" |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
233 |
cu = session.system_sql('SELECT eid FROM entities WHERE source=%(uri)s', |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
234 |
{'uri': self.uri}) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
235 |
myeids = ','.join(str(r[0]) for r in cu.fetchall()) |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
236 |
if not myeids: |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
237 |
return |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
238 |
# delete relations referencing one of those eids |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
239 |
eidcolum = SQL_PREFIX + 'eid' |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
240 |
for rschema in self.schema.relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3633
diff
changeset
|
241 |
if rschema.final or rschema.type in VIRTUAL_RTYPES: |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
242 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
243 |
if rschema.inlined: |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
244 |
column = SQL_PREFIX + rschema.type |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
245 |
for subjtype in rschema.subjects(): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
246 |
table = SQL_PREFIX + str(subjtype) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
247 |
for objtype in rschema.objects(subjtype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
248 |
if self.support_entity(objtype): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
249 |
sql = 'UPDATE %s SET %s=NULL WHERE %s IN (%s);' % ( |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
250 |
table, column, eidcolum, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
251 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
252 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
253 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
254 |
for etype in rschema.subjects(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
255 |
if self.support_entity(etype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
256 |
sql = 'DELETE FROM %s_relation WHERE eid_from IN (%s);' % ( |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
257 |
rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
258 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
259 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
260 |
for etype in rschema.objects(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
261 |
if self.support_entity(etype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
262 |
sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % ( |
385 | 263 |
rschema.type, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
264 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
265 |
break |
1792 | 266 |
|
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
267 |
def cleanup_entities_info(self, session): |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
268 |
"""cleanup system tables from information for entities coming from |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
269 |
this source. This should be called when a source is removed to |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
270 |
properly cleanup the database |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
271 |
""" |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
272 |
self._cleanup_system_relations(session) |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
273 |
# fti / entities tables cleanup |
0 | 274 |
# sqlite doesn't support DELETE FROM xxx USING yyy |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
275 |
dbhelper = session.pool.source('system').dbhelper |
0 | 276 |
session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM ' |
277 |
'entities WHERE entities.source=%%(uri)s)' |
|
278 |
% (dbhelper.fti_table, dbhelper.fti_table, |
|
279 |
dbhelper.fti_uid_attr), |
|
280 |
{'uri': self.uri}) |
|
281 |
session.system_sql('DELETE FROM entities WHERE source=%(uri)s', |
|
282 |
{'uri': self.uri}) |
|
1792 | 283 |
|
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
284 |
# abstract methods to override (at least) in concrete source classes ####### |
1792 | 285 |
|
0 | 286 |
def get_connection(self): |
287 |
"""open and return a connection to the source""" |
|
288 |
raise NotImplementedError() |
|
1792 | 289 |
|
0 | 290 |
def check_connection(self, cnx): |
291 |
"""check connection validity, return None if the connection is still valid |
|
292 |
else a new connection (called when the pool using the given connection is |
|
293 |
being attached to a session) |
|
294 |
||
295 |
do nothing by default |
|
296 |
""" |
|
297 |
pass |
|
1792 | 298 |
|
0 | 299 |
def pool_reset(self, cnx): |
300 |
"""the pool using the given connection is being reseted from its current |
|
301 |
attached session |
|
302 |
||
303 |
do nothing by default |
|
304 |
""" |
|
305 |
pass |
|
1792 | 306 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
307 |
def authenticate(self, session, login, **kwargs): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
308 |
"""if the source support CWUser entity type, it should implements |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
309 |
this method which should return CWUser eid for the given login/password |
0 | 310 |
if this account is defined in this source and valid login / password is |
311 |
given. Else raise `AuthenticationError` |
|
312 |
""" |
|
313 |
raise NotImplementedError() |
|
1792 | 314 |
|
0 | 315 |
def syntax_tree_search(self, session, union, |
316 |
args=None, cachekey=None, varmap=None, debug=0): |
|
1792 | 317 |
"""return result from this source for a rql query (actually from a rql |
318 |
syntax tree and a solution dictionary mapping each used variable to a |
|
0 | 319 |
possible type). If cachekey is given, the query necessary to fetch the |
320 |
results (but not the results themselves) may be cached using this key. |
|
321 |
""" |
|
322 |
raise NotImplementedError() |
|
1792 | 323 |
|
0 | 324 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
325 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
326 |
table (on-the-fly if possible, eg for the system source whose the given |
|
327 |
cursor come from). If not possible, inserts all data by calling |
|
328 |
.executemany(). |
|
329 |
""" |
|
330 |
res = self.syntax_tree_search(session, union, args, varmap=varmap) |
|
2627
d710278e0c1c
manual_insert is a public method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2625
diff
changeset
|
331 |
session.pool.source('system').manual_insert(res, table, session) |
1792 | 332 |
|
0 | 333 |
# system source don't have to implement the two methods below |
1792 | 334 |
|
0 | 335 |
def before_entity_insertion(self, session, lid, etype, eid): |
336 |
"""called by the repository when an eid has been attributed for an |
|
337 |
entity stored here but the entity has not been inserted in the system |
|
338 |
table yet. |
|
1792 | 339 |
|
0 | 340 |
This method must return the an Entity instance representation of this |
341 |
entity. |
|
342 |
""" |
|
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:
2627
diff
changeset
|
343 |
entity = self.repo.vreg['etypes'].etype_class(etype)(session) |
0 | 344 |
entity.set_eid(eid) |
345 |
return entity |
|
1792 | 346 |
|
0 | 347 |
def after_entity_insertion(self, session, lid, entity): |
348 |
"""called by the repository after an entity stored here has been |
|
349 |
inserted in the system table. |
|
350 |
""" |
|
351 |
pass |
|
352 |
||
353 |
# read-only sources don't have to implement methods below |
|
354 |
||
355 |
def get_extid(self, entity): |
|
356 |
"""return the external id for the given newly inserted entity""" |
|
357 |
raise NotImplementedError() |
|
1792 | 358 |
|
0 | 359 |
def add_entity(self, session, entity): |
360 |
"""add a new entity to the source""" |
|
361 |
raise NotImplementedError() |
|
1792 | 362 |
|
0 | 363 |
def update_entity(self, session, entity): |
364 |
"""update an entity in the source""" |
|
365 |
raise NotImplementedError() |
|
366 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
367 |
def delete_entity(self, session, entity): |
0 | 368 |
"""delete an entity from the source""" |
369 |
raise NotImplementedError() |
|
370 |
||
371 |
def add_relation(self, session, subject, rtype, object): |
|
372 |
"""add a relation to the source""" |
|
373 |
raise NotImplementedError() |
|
1792 | 374 |
|
0 | 375 |
def delete_relation(self, session, subject, rtype, object): |
376 |
"""delete a relation from the source""" |
|
377 |
raise NotImplementedError() |
|
378 |
||
379 |
# system source interface ################################################# |
|
380 |
||
381 |
def eid_type_source(self, session, eid): |
|
382 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
383 |
raise NotImplementedError() |
|
1792 | 384 |
|
0 | 385 |
def create_eid(self, session): |
386 |
raise NotImplementedError() |
|
387 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
388 |
def add_info(self, session, entity, source, extid): |
0 | 389 |
"""add type and source info for an eid into the system table""" |
390 |
raise NotImplementedError() |
|
391 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
392 |
def update_info(self, session, entity, need_fti_update): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
393 |
"""mark entity as being modified, fulltext reindex if needed""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
394 |
raise NotImplementedError() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
395 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
396 |
def delete_info(self, session, entity, uri, extid, attributes, relations): |
0 | 397 |
"""delete system information on deletion of an entity by transfering |
398 |
record from the entities table to the deleted_entities table |
|
399 |
""" |
|
400 |
raise NotImplementedError() |
|
1792 | 401 |
|
4807
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
402 |
def modified_entities(self, session, etypes, mtime): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
403 |
"""return a 2-uple: |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
404 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
405 |
modified since the given timestamp (actually entities whose full text |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
406 |
index content has changed) |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
407 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
408 |
deleted since the given timestamp |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
409 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
410 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
411 |
|
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
412 |
def index_entity(self, session, entity): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
413 |
"""create an operation to [re]index textual content of the given entity |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
414 |
on commit |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
415 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
416 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
417 |
|
0 | 418 |
def fti_unindex_entity(self, session, eid): |
419 |
"""remove text content for entity with the given eid from the full text |
|
420 |
index |
|
421 |
""" |
|
422 |
raise NotImplementedError() |
|
1792 | 423 |
|
0 | 424 |
def fti_index_entity(self, session, entity): |
425 |
"""add text content of a created/modified entity to the full text index |
|
426 |
""" |
|
427 |
raise NotImplementedError() |
|
1792 | 428 |
|
0 | 429 |
# sql system source interface ############################################# |
430 |
||
431 |
def sqlexec(self, session, sql, args=None): |
|
432 |
"""execute the query and return its result""" |
|
433 |
raise NotImplementedError() |
|
1792 | 434 |
|
0 | 435 |
def temp_table_def(self, selection, solution, table, basemap): |
436 |
raise NotImplementedError() |
|
1792 | 437 |
|
0 | 438 |
def create_index(self, session, table, column, unique=False): |
439 |
raise NotImplementedError() |
|
1792 | 440 |
|
0 | 441 |
def drop_index(self, session, table, column, unique=False): |
442 |
raise NotImplementedError() |
|
443 |
||
444 |
def create_temp_table(self, session, table, schema): |
|
445 |
raise NotImplementedError() |
|
446 |
||
447 |
def clean_temp_data(self, session, temptables): |
|
448 |
"""remove temporary data, usually associated to temporary tables""" |
|
449 |
pass |
|
450 |
||
1792 | 451 |
|
0 | 452 |
class TrFunc(object): |
453 |
"""lower, upper""" |
|
454 |
def __init__(self, trname, index, attrname=None): |
|
455 |
self._tr = trname.lower() |
|
456 |
self.index = index |
|
457 |
self.attrname = attrname |
|
1792 | 458 |
|
0 | 459 |
def apply(self, resdict): |
460 |
value = resdict.get(self.attrname) |
|
461 |
if value is not None: |
|
462 |
return getattr(value, self._tr)() |
|
463 |
return None |
|
464 |
||
465 |
||
466 |
class GlobTrFunc(TrFunc): |
|
467 |
"""count, sum, max, min, avg""" |
|
468 |
funcs = { |
|
469 |
'count': len, |
|
470 |
'sum': sum, |
|
471 |
'max': max, |
|
472 |
'min': min, |
|
473 |
# XXX avg |
|
474 |
} |
|
475 |
def apply(self, result): |
|
476 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
477 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
478 |
[(1, 7), (3, 4)] |
|
479 |
""" |
|
480 |
keys, values = [], {} |
|
481 |
for row in result: |
|
482 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
483 |
value = row[self.index] |
|
484 |
try: |
|
485 |
values[key].append(value) |
|
486 |
except KeyError: |
|
487 |
keys.append(key) |
|
488 |
values[key] = [value] |
|
489 |
result = [] |
|
490 |
trfunc = self.funcs[self._tr] |
|
491 |
for key in keys: |
|
492 |
row = list(key) |
|
493 |
row.insert(self.index, trfunc(values[key])) |
|
494 |
result.append(row) |
|
495 |
return result |
|
496 |
||
497 |
||
498 |
class ConnectionWrapper(object): |
|
499 |
def __init__(self, cnx=None): |
|
500 |
self.cnx = cnx |
|
501 |
def commit(self): |
|
502 |
pass |
|
503 |
def rollback(self): |
|
504 |
pass |
|
505 |
def cursor(self): |
|
506 |
return None # no actual cursor support |
|
507 |
||
508 |
from cubicweb.server import SOURCE_TYPES |
|
509 |
||
510 |
def source_adapter(source_config): |
|
511 |
adapter_type = source_config['adapter'].lower() |
|
512 |
try: |
|
513 |
return SOURCE_TYPES[adapter_type] |
|
514 |
except KeyError: |
|
515 |
raise RuntimeError('Unknown adapter %r' % adapter_type) |
|
1792 | 516 |
|
0 | 517 |
def get_source(source_config, global_schema, repo): |
518 |
"""return a source adapter according to the adapter field in the |
|
519 |
source's configuration |
|
520 |
""" |
|
521 |
return source_adapter(source_config)(repo, global_schema, source_config) |