author | sylvain.thenault@logilab.fr |
Mon, 06 Apr 2009 09:54:40 +0200 | |
changeset 1242 | 59e04ffe0dcf |
parent 1238 | fa29b5b60107 |
child 1250 | 5c20a7f13c84 |
permissions | -rw-r--r-- |
0 | 1 |
"""cubicweb server sources support |
2 |
||
3 |
:organization: Logilab |
|
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
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from logging import getLogger |
|
10 |
||
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
|
11 |
from mx.DateTime import now, DateTimeDelta |
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
|
12 |
|
0 | 13 |
from cubicweb import set_log_methods |
14 |
||
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
|
15 |
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
|
16 |
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
|
17 |
# time to live in minutes |
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
|
18 |
self.ttl = DateTimeDelta(0, 0, ttlm, ttls) |
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
|
19 |
|
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
|
20 |
def __setitem__(self, key, value): |
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
|
21 |
dict.__setitem__(self, key, (now(), value)) |
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
|
22 |
|
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
|
23 |
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
|
24 |
return dict.__getitem__(self, key)[1] |
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
|
25 |
|
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
|
26 |
def clear_expired(self): |
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
|
27 |
now_ = now() |
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
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
|
0 | 33 |
|
34 |
class AbstractSource(object): |
|
35 |
"""an abstract class for sources""" |
|
36 |
||
37 |
# boolean telling if modification hooks should be called when something is |
|
38 |
# modified in this source |
|
39 |
should_call_hooks = True |
|
40 |
# boolean telling if the repository should connect to this source during |
|
41 |
# migration |
|
42 |
connect_for_migration = True |
|
43 |
||
44 |
# mappings telling which entities and relations are available in the source |
|
45 |
# keys are supported entity/relation types and values are boolean indicating |
|
46 |
# wether the support is read-only (False) or read-write (True) |
|
47 |
support_entities = {} |
|
48 |
support_relations = {} |
|
49 |
# a global identifier for this source, which has to be set by the source |
|
50 |
# instance |
|
51 |
uri = None |
|
52 |
# a reference to the system information helper |
|
53 |
repo = None |
|
54 |
# a reference to the application'schema (may differs from the source'schema) |
|
55 |
schema = None |
|
56 |
||
57 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
|
58 |
self.repo = repo |
|
59 |
self.uri = source_config['uri'] |
|
60 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
|
61 |
self.set_schema(appschema) |
|
62 |
self.support_relations['identity'] = False |
|
63 |
||
64 |
def init_creating(self): |
|
65 |
"""method called by the repository once ready to create a new instance""" |
|
66 |
pass |
|
67 |
||
68 |
def init(self): |
|
69 |
"""method called by the repository once ready to handle request""" |
|
70 |
pass |
|
71 |
||
72 |
def reset_caches(self): |
|
73 |
"""method called during test to reset potential source caches""" |
|
74 |
pass |
|
75 |
||
76 |
def clear_eid_cache(self, eid, etype): |
|
77 |
"""clear potential caches for the given eid""" |
|
78 |
pass |
|
79 |
||
80 |
def __repr__(self): |
|
81 |
return '<%s source>' % self.uri |
|
82 |
||
83 |
def __cmp__(self, other): |
|
84 |
"""simple comparison function to get predictable source order, with the |
|
85 |
system source at last |
|
86 |
""" |
|
87 |
if self.uri == other.uri: |
|
88 |
return 0 |
|
89 |
if self.uri == 'system': |
|
90 |
return 1 |
|
91 |
if other.uri == 'system': |
|
92 |
return -1 |
|
93 |
return cmp(self.uri, other.uri) |
|
94 |
||
95 |
def set_schema(self, schema): |
|
96 |
"""set the application'schema""" |
|
97 |
self.schema = schema |
|
98 |
||
99 |
def support_entity(self, etype, write=False): |
|
100 |
"""return true if the given entity's type is handled by this adapter |
|
101 |
if write is true, return true only if it's a RW support |
|
102 |
""" |
|
103 |
try: |
|
104 |
wsupport = self.support_entities[etype] |
|
105 |
except KeyError: |
|
106 |
return False |
|
107 |
if write: |
|
108 |
return wsupport |
|
109 |
return True |
|
110 |
||
111 |
def support_relation(self, rtype, write=False): |
|
112 |
"""return true if the given relation's type is handled by this adapter |
|
113 |
if write is true, return true only if it's a RW support |
|
114 |
||
115 |
current implementation return true if the relation is defined into |
|
116 |
`support_relations` or if it is a final relation of a supported entity |
|
117 |
type |
|
118 |
""" |
|
119 |
try: |
|
120 |
wsupport = self.support_relations[rtype] |
|
121 |
except KeyError: |
|
122 |
rschema = self.schema.rschema(rtype) |
|
123 |
if not rschema.is_final() or rschema == 'has_text': |
|
124 |
return False |
|
125 |
for etype in rschema.subjects(): |
|
126 |
try: |
|
127 |
wsupport = self.support_entities[etype] |
|
128 |
break |
|
129 |
except KeyError: |
|
130 |
continue |
|
131 |
else: |
|
132 |
return False |
|
133 |
if write: |
|
134 |
return wsupport |
|
135 |
return True |
|
136 |
||
137 |
def eid2extid(self, eid, session=None): |
|
138 |
return self.repo.eid2extid(self, eid, session) |
|
139 |
||
140 |
def extid2eid(self, value, etype, session=None, insert=True): |
|
141 |
return self.repo.extid2eid(self, value, etype, session, insert) |
|
142 |
||
143 |
PUBLIC_KEYS = ('adapter', 'uri') |
|
144 |
def remove_sensitive_information(self, sourcedef): |
|
145 |
"""remove sensitive information such as login / password from source |
|
146 |
definition |
|
147 |
""" |
|
148 |
for key in sourcedef.keys(): |
|
149 |
if not key in self.PUBLIC_KEYS: |
|
150 |
sourcedef.pop(key) |
|
151 |
||
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
152 |
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
|
153 |
"""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
|
154 |
this source |
0 | 155 |
""" |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
156 |
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
|
157 |
{'uri': self.uri}) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
158 |
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
|
159 |
if not myeids: |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
160 |
return |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
161 |
# delete relations referencing one of those eids |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
162 |
for rschema in self.schema.relations(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
163 |
if rschema.is_final() or rschema.type == 'identity': |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
164 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
165 |
if rschema.inlined: |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
166 |
for subjtype in rschema.subjects(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
167 |
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
|
168 |
if self.support_entity(objtype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
169 |
sql = 'UPDATE %s SET %s = NULL WHERE eid IN (%s);' % ( |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
170 |
subjtype, rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
171 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
172 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
173 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
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
|
177 |
rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
178 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
179 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % ( |
385 | 183 |
rschema.type, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
184 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
185 |
break |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
186 |
|
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
187 |
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
|
188 |
"""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
|
189 |
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
|
190 |
properly cleanup the database |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
191 |
""" |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
192 |
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
|
193 |
# fti / entities tables cleanup |
0 | 194 |
# 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
|
195 |
dbhelper = session.pool.source('system').dbhelper |
0 | 196 |
session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM ' |
197 |
'entities WHERE entities.source=%%(uri)s)' |
|
198 |
% (dbhelper.fti_table, dbhelper.fti_table, |
|
199 |
dbhelper.fti_uid_attr), |
|
200 |
{'uri': self.uri}) |
|
201 |
session.system_sql('DELETE FROM entities WHERE source=%(uri)s', |
|
202 |
{'uri': self.uri}) |
|
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
203 |
|
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
204 |
# abstract methods to override (at least) in concrete source classes ####### |
0 | 205 |
|
206 |
def get_connection(self): |
|
207 |
"""open and return a connection to the source""" |
|
208 |
raise NotImplementedError() |
|
209 |
||
210 |
def check_connection(self, cnx): |
|
211 |
"""check connection validity, return None if the connection is still valid |
|
212 |
else a new connection (called when the pool using the given connection is |
|
213 |
being attached to a session) |
|
214 |
||
215 |
do nothing by default |
|
216 |
""" |
|
217 |
pass |
|
218 |
||
219 |
def pool_reset(self, cnx): |
|
220 |
"""the pool using the given connection is being reseted from its current |
|
221 |
attached session |
|
222 |
||
223 |
do nothing by default |
|
224 |
""" |
|
225 |
pass |
|
226 |
||
227 |
def authenticate(self, session, login, password): |
|
228 |
"""if the source support EUser entity type, it should implements |
|
229 |
this method which should return EUser eid for the given login/password |
|
230 |
if this account is defined in this source and valid login / password is |
|
231 |
given. Else raise `AuthenticationError` |
|
232 |
""" |
|
233 |
raise NotImplementedError() |
|
234 |
||
235 |
def syntax_tree_search(self, session, union, |
|
236 |
args=None, cachekey=None, varmap=None, debug=0): |
|
237 |
"""return result from this source for a rql query (actually from a rql |
|
238 |
syntax tree and a solution dictionary mapping each used variable to a |
|
239 |
possible type). If cachekey is given, the query necessary to fetch the |
|
240 |
results (but not the results themselves) may be cached using this key. |
|
241 |
""" |
|
242 |
raise NotImplementedError() |
|
243 |
||
244 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
|
245 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
246 |
table (on-the-fly if possible, eg for the system source whose the given |
|
247 |
cursor come from). If not possible, inserts all data by calling |
|
248 |
.executemany(). |
|
249 |
""" |
|
250 |
res = self.syntax_tree_search(session, union, args, varmap=varmap) |
|
251 |
session.pool.source('system')._manual_insert(res, table, session) |
|
252 |
||
253 |
||
254 |
# system source don't have to implement the two methods below |
|
255 |
||
256 |
def before_entity_insertion(self, session, lid, etype, eid): |
|
257 |
"""called by the repository when an eid has been attributed for an |
|
258 |
entity stored here but the entity has not been inserted in the system |
|
259 |
table yet. |
|
260 |
|
|
261 |
This method must return the an Entity instance representation of this |
|
262 |
entity. |
|
263 |
""" |
|
264 |
entity = self.repo.vreg.etype_class(etype)(session, None) |
|
265 |
entity.set_eid(eid) |
|
266 |
return entity |
|
267 |
||
268 |
def after_entity_insertion(self, session, lid, entity): |
|
269 |
"""called by the repository after an entity stored here has been |
|
270 |
inserted in the system table. |
|
271 |
""" |
|
272 |
pass |
|
273 |
||
274 |
# read-only sources don't have to implement methods below |
|
275 |
||
276 |
def get_extid(self, entity): |
|
277 |
"""return the external id for the given newly inserted entity""" |
|
278 |
raise NotImplementedError() |
|
279 |
||
280 |
def add_entity(self, session, entity): |
|
281 |
"""add a new entity to the source""" |
|
282 |
raise NotImplementedError() |
|
283 |
||
284 |
def update_entity(self, session, entity): |
|
285 |
"""update an entity in the source""" |
|
286 |
raise NotImplementedError() |
|
287 |
||
288 |
def delete_entity(self, session, etype, eid): |
|
289 |
"""delete an entity from the source""" |
|
290 |
raise NotImplementedError() |
|
291 |
||
292 |
def add_relation(self, session, subject, rtype, object): |
|
293 |
"""add a relation to the source""" |
|
294 |
raise NotImplementedError() |
|
295 |
||
296 |
def delete_relation(self, session, subject, rtype, object): |
|
297 |
"""delete a relation from the source""" |
|
298 |
raise NotImplementedError() |
|
299 |
||
300 |
# system source interface ################################################# |
|
301 |
||
302 |
def eid_type_source(self, session, eid): |
|
303 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
304 |
raise NotImplementedError() |
|
305 |
||
306 |
def create_eid(self, session): |
|
307 |
raise NotImplementedError() |
|
308 |
||
309 |
def add_info(self, session, entity, source, extid=None): |
|
310 |
"""add type and source info for an eid into the system table""" |
|
311 |
raise NotImplementedError() |
|
312 |
||
313 |
def delete_info(self, session, eid, etype, uri, extid): |
|
314 |
"""delete system information on deletion of an entity by transfering |
|
315 |
record from the entities table to the deleted_entities table |
|
316 |
""" |
|
317 |
raise NotImplementedError() |
|
318 |
||
319 |
def fti_unindex_entity(self, session, eid): |
|
320 |
"""remove text content for entity with the given eid from the full text |
|
321 |
index |
|
322 |
""" |
|
323 |
raise NotImplementedError() |
|
324 |
||
325 |
def fti_index_entity(self, session, entity): |
|
326 |
"""add text content of a created/modified entity to the full text index |
|
327 |
""" |
|
328 |
raise NotImplementedError() |
|
329 |
||
330 |
def modified_entities(self, session, etypes, mtime): |
|
331 |
"""return a 2-uple: |
|
332 |
* list of (etype, eid) of entities of the given types which have been |
|
333 |
modified since the given timestamp (actually entities whose full text |
|
334 |
index content has changed) |
|
335 |
* list of (etype, eid) of entities of the given types which have been |
|
336 |
deleted since the given timestamp |
|
337 |
""" |
|
338 |
raise NotImplementedError() |
|
339 |
||
340 |
# sql system source interface ############################################# |
|
341 |
||
342 |
def sqlexec(self, session, sql, args=None): |
|
343 |
"""execute the query and return its result""" |
|
344 |
raise NotImplementedError() |
|
345 |
||
346 |
def temp_table_def(self, selection, solution, table, basemap): |
|
347 |
raise NotImplementedError() |
|
348 |
||
349 |
def create_index(self, session, table, column, unique=False): |
|
350 |
raise NotImplementedError() |
|
351 |
||
352 |
def drop_index(self, session, table, column, unique=False): |
|
353 |
raise NotImplementedError() |
|
354 |
||
355 |
def create_temp_table(self, session, table, schema): |
|
356 |
raise NotImplementedError() |
|
357 |
||
358 |
def clean_temp_data(self, session, temptables): |
|
359 |
"""remove temporary data, usually associated to temporary tables""" |
|
360 |
pass |
|
361 |
||
362 |
||
363 |
class TrFunc(object): |
|
364 |
"""lower, upper""" |
|
365 |
def __init__(self, trname, index, attrname=None): |
|
366 |
self._tr = trname.lower() |
|
367 |
self.index = index |
|
368 |
self.attrname = attrname |
|
369 |
||
370 |
def apply(self, resdict): |
|
371 |
value = resdict.get(self.attrname) |
|
372 |
if value is not None: |
|
373 |
return getattr(value, self._tr)() |
|
374 |
return None |
|
375 |
||
376 |
||
377 |
class GlobTrFunc(TrFunc): |
|
378 |
"""count, sum, max, min, avg""" |
|
379 |
funcs = { |
|
380 |
'count': len, |
|
381 |
'sum': sum, |
|
382 |
'max': max, |
|
383 |
'min': min, |
|
384 |
# XXX avg |
|
385 |
} |
|
386 |
def apply(self, result): |
|
387 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
388 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
389 |
[(1, 7), (3, 4)] |
|
390 |
""" |
|
391 |
keys, values = [], {} |
|
392 |
for row in result: |
|
393 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
394 |
value = row[self.index] |
|
395 |
try: |
|
396 |
values[key].append(value) |
|
397 |
except KeyError: |
|
398 |
keys.append(key) |
|
399 |
values[key] = [value] |
|
400 |
result = [] |
|
401 |
trfunc = self.funcs[self._tr] |
|
402 |
for key in keys: |
|
403 |
row = list(key) |
|
404 |
row.insert(self.index, trfunc(values[key])) |
|
405 |
result.append(row) |
|
406 |
return result |
|
407 |
||
408 |
||
409 |
class ConnectionWrapper(object): |
|
410 |
def __init__(self, cnx=None): |
|
411 |
self.cnx = cnx |
|
412 |
def commit(self): |
|
413 |
pass |
|
414 |
def rollback(self): |
|
415 |
pass |
|
416 |
def cursor(self): |
|
417 |
return None # no actual cursor support |
|
418 |
||
419 |
from cubicweb.server import SOURCE_TYPES |
|
420 |
||
421 |
def source_adapter(source_config): |
|
422 |
adapter_type = source_config['adapter'].lower() |
|
423 |
try: |
|
424 |
return SOURCE_TYPES[adapter_type] |
|
425 |
except KeyError: |
|
426 |
raise RuntimeError('Unknown adapter %r' % adapter_type) |
|
427 |
||
428 |
def get_source(source_config, global_schema, repo): |
|
429 |
"""return a source adapter according to the adapter field in the |
|
430 |
source's configuration |
|
431 |
""" |
|
432 |
return source_adapter(source_config)(repo, global_schema, source_config) |