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