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