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