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