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