author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 19 Oct 2009 20:01:34 +0200 | |
changeset 3738 | 89871767fa82 |
parent 3720 | 5376aaadd16b |
child 4204 | 60256056bda6 |
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 |
|
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
83 |
# multi-sources planning control |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
84 |
dont_cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
85 |
cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
86 |
|
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
87 |
|
0 | 88 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
89 |
self.repo = repo |
|
90 |
self.uri = source_config['uri'] |
|
91 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
|
92 |
self.set_schema(appschema) |
|
93 |
self.support_relations['identity'] = False |
|
1792 | 94 |
|
0 | 95 |
def init_creating(self): |
96 |
"""method called by the repository once ready to create a new instance""" |
|
97 |
pass |
|
1792 | 98 |
|
0 | 99 |
def init(self): |
100 |
"""method called by the repository once ready to handle request""" |
|
101 |
pass |
|
1792 | 102 |
|
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2719
diff
changeset
|
103 |
def backup(self, backupfile): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
104 |
"""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
|
105 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
106 |
|
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2719
diff
changeset
|
107 |
def restore(self, backupfile): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
108 |
"""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
|
109 |
pass |
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 close_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._cursors.pop(self.uri, None) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
114 |
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
|
115 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
116 |
def open_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
117 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
118 |
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
|
119 |
|
0 | 120 |
def reset_caches(self): |
121 |
"""method called during test to reset potential source caches""" |
|
122 |
pass |
|
1792 | 123 |
|
0 | 124 |
def clear_eid_cache(self, eid, etype): |
125 |
"""clear potential caches for the given eid""" |
|
126 |
pass |
|
1792 | 127 |
|
0 | 128 |
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
|
129 |
return '<%s source @%#x>' % (self.uri, id(self)) |
0 | 130 |
|
131 |
def __cmp__(self, other): |
|
132 |
"""simple comparison function to get predictable source order, with the |
|
133 |
system source at last |
|
134 |
""" |
|
135 |
if self.uri == other.uri: |
|
136 |
return 0 |
|
137 |
if self.uri == 'system': |
|
138 |
return 1 |
|
139 |
if other.uri == 'system': |
|
140 |
return -1 |
|
141 |
return cmp(self.uri, other.uri) |
|
1792 | 142 |
|
0 | 143 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
144 |
"""set the instance'schema""" |
0 | 145 |
self.schema = schema |
1792 | 146 |
|
0 | 147 |
def support_entity(self, etype, write=False): |
148 |
"""return true if the given entity's type is handled by this adapter |
|
149 |
if write is true, return true only if it's a RW support |
|
150 |
""" |
|
151 |
try: |
|
152 |
wsupport = self.support_entities[etype] |
|
153 |
except KeyError: |
|
154 |
return False |
|
155 |
if write: |
|
156 |
return wsupport |
|
157 |
return True |
|
1792 | 158 |
|
0 | 159 |
def support_relation(self, rtype, write=False): |
160 |
"""return true if the given relation's type is handled by this adapter |
|
161 |
if write is true, return true only if it's a RW support |
|
162 |
||
1792 | 163 |
current implementation return true if the relation is defined into |
164 |
`support_relations` or if it is a final relation of a supported entity |
|
0 | 165 |
type |
166 |
""" |
|
167 |
try: |
|
168 |
wsupport = self.support_relations[rtype] |
|
169 |
except KeyError: |
|
170 |
rschema = self.schema.rschema(rtype) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3633
diff
changeset
|
171 |
if not rschema.final or rschema == 'has_text': |
0 | 172 |
return False |
173 |
for etype in rschema.subjects(): |
|
174 |
try: |
|
175 |
wsupport = self.support_entities[etype] |
|
176 |
break |
|
177 |
except KeyError: |
|
178 |
continue |
|
179 |
else: |
|
180 |
return False |
|
181 |
if write: |
|
182 |
return wsupport |
|
1792 | 183 |
return True |
184 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
185 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
186 |
"""return True if the relation may be crossed among sources. Rules are: |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
187 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
188 |
* if this source support the relation, can't be crossed unless explicitly |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
189 |
specified in .cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
190 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
191 |
* if this source doesn't support the relation, can be crossed unless |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
192 |
explicitly specified in .dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
193 |
""" |
3633
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
194 |
# XXX find a way to have relation such as state_of in dont cross |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
195 |
# relation (eg composite relation without both end type available? |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
196 |
# card 1 relation ? ...) |
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
197 |
if self.support_relation(rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
198 |
return rtype in self.cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
199 |
return rtype not in self.dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
200 |
|
0 | 201 |
def eid2extid(self, eid, session=None): |
202 |
return self.repo.eid2extid(self, eid, session) |
|
203 |
||
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
|
204 |
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
|
205 |
return self.repo.extid2eid(self, value, etype, session, **kwargs) |
0 | 206 |
|
207 |
PUBLIC_KEYS = ('adapter', 'uri') |
|
208 |
def remove_sensitive_information(self, sourcedef): |
|
209 |
"""remove sensitive information such as login / password from source |
|
210 |
definition |
|
211 |
""" |
|
212 |
for key in sourcedef.keys(): |
|
213 |
if not key in self.PUBLIC_KEYS: |
|
214 |
sourcedef.pop(key) |
|
215 |
||
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
216 |
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
|
217 |
"""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
|
218 |
this source |
0 | 219 |
""" |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
220 |
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
|
221 |
{'uri': self.uri}) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
222 |
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
|
223 |
if not myeids: |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
224 |
return |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
225 |
# 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
|
226 |
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
|
227 |
for rschema in self.schema.relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3633
diff
changeset
|
228 |
if rschema.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
|
229 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
230 |
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
|
231 |
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
|
232 |
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
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
239 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
240 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
241 |
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
|
242 |
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
|
243 |
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
|
244 |
rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
245 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
246 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
247 |
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
|
248 |
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
|
249 |
sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % ( |
385 | 250 |
rschema.type, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
251 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
252 |
break |
1792 | 253 |
|
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
254 |
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
|
255 |
"""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
|
256 |
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
|
257 |
properly cleanup the database |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
258 |
""" |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
259 |
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
|
260 |
# fti / entities tables cleanup |
0 | 261 |
# 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
|
262 |
dbhelper = session.pool.source('system').dbhelper |
0 | 263 |
session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM ' |
264 |
'entities WHERE entities.source=%%(uri)s)' |
|
265 |
% (dbhelper.fti_table, dbhelper.fti_table, |
|
266 |
dbhelper.fti_uid_attr), |
|
267 |
{'uri': self.uri}) |
|
268 |
session.system_sql('DELETE FROM entities WHERE source=%(uri)s', |
|
269 |
{'uri': self.uri}) |
|
1792 | 270 |
|
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
271 |
# abstract methods to override (at least) in concrete source classes ####### |
1792 | 272 |
|
0 | 273 |
def get_connection(self): |
274 |
"""open and return a connection to the source""" |
|
275 |
raise NotImplementedError() |
|
1792 | 276 |
|
0 | 277 |
def check_connection(self, cnx): |
278 |
"""check connection validity, return None if the connection is still valid |
|
279 |
else a new connection (called when the pool using the given connection is |
|
280 |
being attached to a session) |
|
281 |
||
282 |
do nothing by default |
|
283 |
""" |
|
284 |
pass |
|
1792 | 285 |
|
0 | 286 |
def pool_reset(self, cnx): |
287 |
"""the pool using the given connection is being reseted from its current |
|
288 |
attached session |
|
289 |
||
290 |
do nothing by default |
|
291 |
""" |
|
292 |
pass |
|
1792 | 293 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
294 |
def authenticate(self, session, login, **kwargs): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
295 |
"""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
|
296 |
this method which should return CWUser eid for the given login/password |
0 | 297 |
if this account is defined in this source and valid login / password is |
298 |
given. Else raise `AuthenticationError` |
|
299 |
""" |
|
300 |
raise NotImplementedError() |
|
1792 | 301 |
|
0 | 302 |
def syntax_tree_search(self, session, union, |
303 |
args=None, cachekey=None, varmap=None, debug=0): |
|
1792 | 304 |
"""return result from this source for a rql query (actually from a rql |
305 |
syntax tree and a solution dictionary mapping each used variable to a |
|
0 | 306 |
possible type). If cachekey is given, the query necessary to fetch the |
307 |
results (but not the results themselves) may be cached using this key. |
|
308 |
""" |
|
309 |
raise NotImplementedError() |
|
1792 | 310 |
|
0 | 311 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
312 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
313 |
table (on-the-fly if possible, eg for the system source whose the given |
|
314 |
cursor come from). If not possible, inserts all data by calling |
|
315 |
.executemany(). |
|
316 |
""" |
|
317 |
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
|
318 |
session.pool.source('system').manual_insert(res, table, session) |
1792 | 319 |
|
0 | 320 |
# system source don't have to implement the two methods below |
1792 | 321 |
|
0 | 322 |
def before_entity_insertion(self, session, lid, etype, eid): |
323 |
"""called by the repository when an eid has been attributed for an |
|
324 |
entity stored here but the entity has not been inserted in the system |
|
325 |
table yet. |
|
1792 | 326 |
|
0 | 327 |
This method must return the an Entity instance representation of this |
328 |
entity. |
|
329 |
""" |
|
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
|
330 |
entity = self.repo.vreg['etypes'].etype_class(etype)(session) |
0 | 331 |
entity.set_eid(eid) |
332 |
return entity |
|
1792 | 333 |
|
0 | 334 |
def after_entity_insertion(self, session, lid, entity): |
335 |
"""called by the repository after an entity stored here has been |
|
336 |
inserted in the system table. |
|
337 |
""" |
|
338 |
pass |
|
339 |
||
340 |
# read-only sources don't have to implement methods below |
|
341 |
||
342 |
def get_extid(self, entity): |
|
343 |
"""return the external id for the given newly inserted entity""" |
|
344 |
raise NotImplementedError() |
|
1792 | 345 |
|
0 | 346 |
def add_entity(self, session, entity): |
347 |
"""add a new entity to the source""" |
|
348 |
raise NotImplementedError() |
|
1792 | 349 |
|
0 | 350 |
def update_entity(self, session, entity): |
351 |
"""update an entity in the source""" |
|
352 |
raise NotImplementedError() |
|
353 |
||
354 |
def delete_entity(self, session, etype, eid): |
|
355 |
"""delete an entity from the source""" |
|
356 |
raise NotImplementedError() |
|
357 |
||
358 |
def add_relation(self, session, subject, rtype, object): |
|
359 |
"""add a relation to the source""" |
|
360 |
raise NotImplementedError() |
|
1792 | 361 |
|
0 | 362 |
def delete_relation(self, session, subject, rtype, object): |
363 |
"""delete a relation from the source""" |
|
364 |
raise NotImplementedError() |
|
365 |
||
366 |
# system source interface ################################################# |
|
367 |
||
368 |
def eid_type_source(self, session, eid): |
|
369 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
370 |
raise NotImplementedError() |
|
1792 | 371 |
|
0 | 372 |
def create_eid(self, session): |
373 |
raise NotImplementedError() |
|
374 |
||
375 |
def add_info(self, session, entity, source, extid=None): |
|
376 |
"""add type and source info for an eid into the system table""" |
|
377 |
raise NotImplementedError() |
|
378 |
||
379 |
def delete_info(self, session, eid, etype, uri, extid): |
|
380 |
"""delete system information on deletion of an entity by transfering |
|
381 |
record from the entities table to the deleted_entities table |
|
382 |
""" |
|
383 |
raise NotImplementedError() |
|
1792 | 384 |
|
0 | 385 |
def fti_unindex_entity(self, session, eid): |
386 |
"""remove text content for entity with the given eid from the full text |
|
387 |
index |
|
388 |
""" |
|
389 |
raise NotImplementedError() |
|
1792 | 390 |
|
0 | 391 |
def fti_index_entity(self, session, entity): |
392 |
"""add text content of a created/modified entity to the full text index |
|
393 |
""" |
|
394 |
raise NotImplementedError() |
|
1792 | 395 |
|
0 | 396 |
def modified_entities(self, session, etypes, mtime): |
397 |
"""return a 2-uple: |
|
398 |
* list of (etype, eid) of entities of the given types which have been |
|
399 |
modified since the given timestamp (actually entities whose full text |
|
400 |
index content has changed) |
|
401 |
* list of (etype, eid) of entities of the given types which have been |
|
402 |
deleted since the given timestamp |
|
403 |
""" |
|
404 |
raise NotImplementedError() |
|
405 |
||
406 |
# sql system source interface ############################################# |
|
407 |
||
408 |
def sqlexec(self, session, sql, args=None): |
|
409 |
"""execute the query and return its result""" |
|
410 |
raise NotImplementedError() |
|
1792 | 411 |
|
0 | 412 |
def temp_table_def(self, selection, solution, table, basemap): |
413 |
raise NotImplementedError() |
|
1792 | 414 |
|
0 | 415 |
def create_index(self, session, table, column, unique=False): |
416 |
raise NotImplementedError() |
|
1792 | 417 |
|
0 | 418 |
def drop_index(self, session, table, column, unique=False): |
419 |
raise NotImplementedError() |
|
420 |
||
421 |
def create_temp_table(self, session, table, schema): |
|
422 |
raise NotImplementedError() |
|
423 |
||
424 |
def clean_temp_data(self, session, temptables): |
|
425 |
"""remove temporary data, usually associated to temporary tables""" |
|
426 |
pass |
|
427 |
||
1792 | 428 |
|
0 | 429 |
class TrFunc(object): |
430 |
"""lower, upper""" |
|
431 |
def __init__(self, trname, index, attrname=None): |
|
432 |
self._tr = trname.lower() |
|
433 |
self.index = index |
|
434 |
self.attrname = attrname |
|
1792 | 435 |
|
0 | 436 |
def apply(self, resdict): |
437 |
value = resdict.get(self.attrname) |
|
438 |
if value is not None: |
|
439 |
return getattr(value, self._tr)() |
|
440 |
return None |
|
441 |
||
442 |
||
443 |
class GlobTrFunc(TrFunc): |
|
444 |
"""count, sum, max, min, avg""" |
|
445 |
funcs = { |
|
446 |
'count': len, |
|
447 |
'sum': sum, |
|
448 |
'max': max, |
|
449 |
'min': min, |
|
450 |
# XXX avg |
|
451 |
} |
|
452 |
def apply(self, result): |
|
453 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
454 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
455 |
[(1, 7), (3, 4)] |
|
456 |
""" |
|
457 |
keys, values = [], {} |
|
458 |
for row in result: |
|
459 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
460 |
value = row[self.index] |
|
461 |
try: |
|
462 |
values[key].append(value) |
|
463 |
except KeyError: |
|
464 |
keys.append(key) |
|
465 |
values[key] = [value] |
|
466 |
result = [] |
|
467 |
trfunc = self.funcs[self._tr] |
|
468 |
for key in keys: |
|
469 |
row = list(key) |
|
470 |
row.insert(self.index, trfunc(values[key])) |
|
471 |
result.append(row) |
|
472 |
return result |
|
473 |
||
474 |
||
475 |
class ConnectionWrapper(object): |
|
476 |
def __init__(self, cnx=None): |
|
477 |
self.cnx = cnx |
|
478 |
def commit(self): |
|
479 |
pass |
|
480 |
def rollback(self): |
|
481 |
pass |
|
482 |
def cursor(self): |
|
483 |
return None # no actual cursor support |
|
484 |
||
485 |
from cubicweb.server import SOURCE_TYPES |
|
486 |
||
487 |
def source_adapter(source_config): |
|
488 |
adapter_type = source_config['adapter'].lower() |
|
489 |
try: |
|
490 |
return SOURCE_TYPES[adapter_type] |
|
491 |
except KeyError: |
|
492 |
raise RuntimeError('Unknown adapter %r' % adapter_type) |
|
1792 | 493 |
|
0 | 494 |
def get_source(source_config, global_schema, repo): |
495 |
"""return a source adapter according to the adapter field in the |
|
496 |
source's configuration |
|
497 |
""" |
|
498 |
return source_adapter(source_config)(repo, global_schema, source_config) |