author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 23 Sep 2010 13:10:29 +0200 | |
branch | stable |
changeset 6320 | f2e925ae7122 |
parent 6152 | 6824f8b61098 |
child 6162 | 76bd320c5ace |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5824 | 18 |
"""cubicweb server sources support""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
22 |
from os.path import join, splitext |
1263 | 23 |
from datetime import datetime, timedelta |
0 | 24 |
from logging import getLogger |
25 |
||
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
|
0 | 30 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
31 |
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
|
32 |
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
|
33 |
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
|
34 |
if varmap: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
35 |
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
|
36 |
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
|
37 |
print ' args', args |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
38 |
print ' cache key', cachekey |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
print ' solutions', ','.join(str(s.solutions) |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
40 |
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
|
41 |
# 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
|
42 |
return True |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
43 |
|
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
44 |
def dbg_results(results): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
45 |
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
|
46 |
if len(results) > 10: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
47 |
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
|
48 |
else: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
49 |
print ' -->', results |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
50 |
# 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
|
51 |
return True |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
52 |
|
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
|
53 |
class TimedCache(dict): |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
54 |
def __init__(self, ttl): |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
55 |
# time to live in seconds |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
56 |
if ttl <= 0: |
5637
b72a838aa109
more robust handling of looping task configuration in ldap source
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5424
diff
changeset
|
57 |
raise ValueError('TimedCache initialized with a ttl of %ss' % self.ttl.seconds) |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
58 |
self.ttl = timedelta(seconds=ttl) |
1792 | 59 |
|
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
|
60 |
def __setitem__(self, key, value): |
1263 | 61 |
dict.__setitem__(self, key, (datetime.now(), value)) |
1792 | 62 |
|
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
|
63 |
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
|
64 |
return dict.__getitem__(self, key)[1] |
1792 | 65 |
|
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
|
66 |
def clear_expired(self): |
1263 | 67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
|
0 | 73 |
|
74 |
class AbstractSource(object): |
|
75 |
"""an abstract class for sources""" |
|
76 |
||
77 |
# boolean telling if modification hooks should be called when something is |
|
78 |
# modified in this source |
|
79 |
should_call_hooks = True |
|
80 |
# boolean telling if the repository should connect to this source during |
|
81 |
# migration |
|
82 |
connect_for_migration = True |
|
1792 | 83 |
|
0 | 84 |
# mappings telling which entities and relations are available in the source |
85 |
# keys are supported entity/relation types and values are boolean indicating |
|
86 |
# wether the support is read-only (False) or read-write (True) |
|
87 |
support_entities = {} |
|
88 |
support_relations = {} |
|
89 |
# a global identifier for this source, which has to be set by the source |
|
90 |
# instance |
|
91 |
uri = None |
|
92 |
# a reference to the system information helper |
|
93 |
repo = None |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
94 |
# a reference to the instance'schema (may differs from the source'schema) |
0 | 95 |
schema = None |
1792 | 96 |
|
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
97 |
# multi-sources planning control |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
98 |
dont_cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
99 |
cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
100 |
|
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
101 |
|
0 | 102 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
103 |
self.repo = repo |
|
104 |
self.uri = source_config['uri'] |
|
105 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
|
106 |
self.set_schema(appschema) |
|
107 |
self.support_relations['identity'] = False |
|
1792 | 108 |
|
0 | 109 |
def init_creating(self): |
110 |
"""method called by the repository once ready to create a new instance""" |
|
111 |
pass |
|
1792 | 112 |
|
0 | 113 |
def init(self): |
114 |
"""method called by the repository once ready to handle request""" |
|
115 |
pass |
|
1792 | 116 |
|
5124
d4d0b4b2298a
fix #822890, missing argument on abstract source backup method make backup failed on multi-sources instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
117 |
def backup(self, backupfile, confirm): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
118 |
"""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
|
119 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
120 |
|
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
121 |
def restore(self, backupfile, confirm, drop): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
122 |
"""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
|
123 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
124 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
125 |
def close_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
126 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
127 |
pool._cursors.pop(self.uri, None) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
128 |
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
|
129 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
130 |
def open_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
131 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
132 |
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
|
133 |
|
0 | 134 |
def reset_caches(self): |
135 |
"""method called during test to reset potential source caches""" |
|
136 |
pass |
|
1792 | 137 |
|
0 | 138 |
def clear_eid_cache(self, eid, etype): |
139 |
"""clear potential caches for the given eid""" |
|
140 |
pass |
|
1792 | 141 |
|
0 | 142 |
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
|
143 |
return '<%s source @%#x>' % (self.uri, id(self)) |
0 | 144 |
|
145 |
def __cmp__(self, other): |
|
146 |
"""simple comparison function to get predictable source order, with the |
|
147 |
system source at last |
|
148 |
""" |
|
149 |
if self.uri == other.uri: |
|
150 |
return 0 |
|
151 |
if self.uri == 'system': |
|
152 |
return 1 |
|
153 |
if other.uri == 'system': |
|
154 |
return -1 |
|
155 |
return cmp(self.uri, other.uri) |
|
1792 | 156 |
|
0 | 157 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
158 |
"""set the instance'schema""" |
0 | 159 |
self.schema = schema |
1792 | 160 |
|
0 | 161 |
def support_entity(self, etype, write=False): |
162 |
"""return true if the given entity's type is handled by this adapter |
|
163 |
if write is true, return true only if it's a RW support |
|
164 |
""" |
|
165 |
try: |
|
166 |
wsupport = self.support_entities[etype] |
|
167 |
except KeyError: |
|
168 |
return False |
|
169 |
if write: |
|
170 |
return wsupport |
|
171 |
return True |
|
1792 | 172 |
|
0 | 173 |
def support_relation(self, rtype, write=False): |
174 |
"""return true if the given relation's type is handled by this adapter |
|
175 |
if write is true, return true only if it's a RW support |
|
176 |
||
1792 | 177 |
current implementation return true if the relation is defined into |
178 |
`support_relations` or if it is a final relation of a supported entity |
|
0 | 179 |
type |
180 |
""" |
|
181 |
try: |
|
182 |
wsupport = self.support_relations[rtype] |
|
183 |
except KeyError: |
|
184 |
rschema = self.schema.rschema(rtype) |
|
6057
cb5c8852cbda
[ms] cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5849
diff
changeset
|
185 |
if not rschema.final or rschema.type == 'has_text': |
0 | 186 |
return False |
187 |
for etype in rschema.subjects(): |
|
188 |
try: |
|
189 |
wsupport = self.support_entities[etype] |
|
190 |
break |
|
191 |
except KeyError: |
|
192 |
continue |
|
193 |
else: |
|
194 |
return False |
|
195 |
if write: |
|
196 |
return wsupport |
|
1792 | 197 |
return True |
198 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
199 |
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
|
200 |
"""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
|
201 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
202 |
* 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
|
203 |
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
|
204 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
205 |
* 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
|
206 |
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
|
207 |
""" |
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
|
208 |
# 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
|
209 |
# 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
|
210 |
# 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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
|
0 | 215 |
def eid2extid(self, eid, session=None): |
216 |
return self.repo.eid2extid(self, eid, session) |
|
217 |
||
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
|
218 |
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
|
219 |
return self.repo.extid2eid(self, value, etype, session, **kwargs) |
0 | 220 |
|
221 |
PUBLIC_KEYS = ('adapter', 'uri') |
|
222 |
def remove_sensitive_information(self, sourcedef): |
|
223 |
"""remove sensitive information such as login / password from source |
|
224 |
definition |
|
225 |
""" |
|
226 |
for key in sourcedef.keys(): |
|
227 |
if not key in self.PUBLIC_KEYS: |
|
228 |
sourcedef.pop(key) |
|
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_system_relations(self, session): |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
231 |
"""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
|
232 |
this source |
0 | 233 |
""" |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
234 |
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
|
235 |
{'uri': self.uri}) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
236 |
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
|
237 |
if not myeids: |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
238 |
return |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
239 |
# 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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
253 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
254 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
259 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
260 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
261 |
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
|
262 |
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
|
263 |
sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % ( |
385 | 264 |
rschema.type, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
265 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
266 |
break |
1792 | 267 |
|
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
268 |
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
|
269 |
"""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
|
270 |
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
|
271 |
properly cleanup the database |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
272 |
""" |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
273 |
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
|
274 |
# fti / entities tables cleanup |
0 | 275 |
# 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
|
276 |
dbhelper = session.pool.source('system').dbhelper |
0 | 277 |
session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM ' |
278 |
'entities WHERE entities.source=%%(uri)s)' |
|
279 |
% (dbhelper.fti_table, dbhelper.fti_table, |
|
280 |
dbhelper.fti_uid_attr), |
|
281 |
{'uri': self.uri}) |
|
282 |
session.system_sql('DELETE FROM entities WHERE source=%(uri)s', |
|
283 |
{'uri': self.uri}) |
|
1792 | 284 |
|
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
285 |
# abstract methods to override (at least) in concrete source classes ####### |
1792 | 286 |
|
0 | 287 |
def get_connection(self): |
288 |
"""open and return a connection to the source""" |
|
289 |
raise NotImplementedError() |
|
1792 | 290 |
|
0 | 291 |
def check_connection(self, cnx): |
292 |
"""check connection validity, return None if the connection is still valid |
|
293 |
else a new connection (called when the pool using the given connection is |
|
294 |
being attached to a session) |
|
295 |
||
296 |
do nothing by default |
|
297 |
""" |
|
298 |
pass |
|
1792 | 299 |
|
0 | 300 |
def pool_reset(self, cnx): |
301 |
"""the pool using the given connection is being reseted from its current |
|
302 |
attached session |
|
303 |
||
304 |
do nothing by default |
|
305 |
""" |
|
306 |
pass |
|
1792 | 307 |
|
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
|
308 |
def authenticate(self, session, login, **kwargs): |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6057
diff
changeset
|
309 |
"""if the source support CWUser entity type, it should implement |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
310 |
this method which should return CWUser eid for the given login/password |
0 | 311 |
if this account is defined in this source and valid login / password is |
312 |
given. Else raise `AuthenticationError` |
|
313 |
""" |
|
314 |
raise NotImplementedError() |
|
1792 | 315 |
|
0 | 316 |
def syntax_tree_search(self, session, union, |
317 |
args=None, cachekey=None, varmap=None, debug=0): |
|
1792 | 318 |
"""return result from this source for a rql query (actually from a rql |
319 |
syntax tree and a solution dictionary mapping each used variable to a |
|
0 | 320 |
possible type). If cachekey is given, the query necessary to fetch the |
321 |
results (but not the results themselves) may be cached using this key. |
|
322 |
""" |
|
323 |
raise NotImplementedError() |
|
1792 | 324 |
|
0 | 325 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
326 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
327 |
table (on-the-fly if possible, eg for the system source whose the given |
|
328 |
cursor come from). If not possible, inserts all data by calling |
|
329 |
.executemany(). |
|
330 |
""" |
|
331 |
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
|
332 |
session.pool.source('system').manual_insert(res, table, session) |
1792 | 333 |
|
0 | 334 |
# system source don't have to implement the two methods below |
1792 | 335 |
|
0 | 336 |
def before_entity_insertion(self, session, lid, etype, eid): |
337 |
"""called by the repository when an eid has been attributed for an |
|
338 |
entity stored here but the entity has not been inserted in the system |
|
339 |
table yet. |
|
1792 | 340 |
|
0 | 341 |
This method must return the an Entity instance representation of this |
342 |
entity. |
|
343 |
""" |
|
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
|
344 |
entity = self.repo.vreg['etypes'].etype_class(etype)(session) |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
345 |
entity.eid = eid |
0 | 346 |
return entity |
1792 | 347 |
|
0 | 348 |
def after_entity_insertion(self, session, lid, entity): |
349 |
"""called by the repository after an entity stored here has been |
|
350 |
inserted in the system table. |
|
351 |
""" |
|
352 |
pass |
|
353 |
||
354 |
# read-only sources don't have to implement methods below |
|
355 |
||
356 |
def get_extid(self, entity): |
|
357 |
"""return the external id for the given newly inserted entity""" |
|
358 |
raise NotImplementedError() |
|
1792 | 359 |
|
0 | 360 |
def add_entity(self, session, entity): |
361 |
"""add a new entity to the source""" |
|
362 |
raise NotImplementedError() |
|
1792 | 363 |
|
0 | 364 |
def update_entity(self, session, entity): |
365 |
"""update an entity in the source""" |
|
366 |
raise NotImplementedError() |
|
367 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
368 |
def delete_entity(self, session, entity): |
0 | 369 |
"""delete an entity from the source""" |
370 |
raise NotImplementedError() |
|
371 |
||
372 |
def add_relation(self, session, subject, rtype, object): |
|
373 |
"""add a relation to the source""" |
|
374 |
raise NotImplementedError() |
|
1792 | 375 |
|
0 | 376 |
def delete_relation(self, session, subject, rtype, object): |
377 |
"""delete a relation from the source""" |
|
378 |
raise NotImplementedError() |
|
379 |
||
380 |
# system source interface ################################################# |
|
381 |
||
382 |
def eid_type_source(self, session, eid): |
|
383 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
384 |
raise NotImplementedError() |
|
1792 | 385 |
|
0 | 386 |
def create_eid(self, session): |
387 |
raise NotImplementedError() |
|
388 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
389 |
def add_info(self, session, entity, source, extid): |
0 | 390 |
"""add type and source info for an eid into the system table""" |
391 |
raise NotImplementedError() |
|
392 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
393 |
def update_info(self, session, entity, need_fti_update): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
394 |
"""mark entity as being modified, fulltext reindex if needed""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
395 |
raise NotImplementedError() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
396 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
397 |
def delete_info(self, session, entity, uri, extid, attributes, relations): |
0 | 398 |
"""delete system information on deletion of an entity by transfering |
399 |
record from the entities table to the deleted_entities table |
|
400 |
""" |
|
401 |
raise NotImplementedError() |
|
1792 | 402 |
|
4807
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
403 |
def modified_entities(self, session, etypes, mtime): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
404 |
"""return a 2-uple: |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
405 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
406 |
modified since the given timestamp (actually entities whose full text |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
407 |
index content has changed) |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
408 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
409 |
deleted since the given timestamp |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
410 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
411 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
412 |
|
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
413 |
def index_entity(self, session, entity): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
414 |
"""create an operation to [re]index textual content of the given entity |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
415 |
on commit |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
416 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
417 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
418 |
|
0 | 419 |
def fti_unindex_entity(self, session, eid): |
420 |
"""remove text content for entity with the given eid from the full text |
|
421 |
index |
|
422 |
""" |
|
423 |
raise NotImplementedError() |
|
1792 | 424 |
|
0 | 425 |
def fti_index_entity(self, session, entity): |
426 |
"""add text content of a created/modified entity to the full text index |
|
427 |
""" |
|
428 |
raise NotImplementedError() |
|
1792 | 429 |
|
0 | 430 |
# sql system source interface ############################################# |
431 |
||
432 |
def sqlexec(self, session, sql, args=None): |
|
433 |
"""execute the query and return its result""" |
|
434 |
raise NotImplementedError() |
|
1792 | 435 |
|
0 | 436 |
def temp_table_def(self, selection, solution, table, basemap): |
437 |
raise NotImplementedError() |
|
1792 | 438 |
|
0 | 439 |
def create_index(self, session, table, column, unique=False): |
440 |
raise NotImplementedError() |
|
1792 | 441 |
|
0 | 442 |
def drop_index(self, session, table, column, unique=False): |
443 |
raise NotImplementedError() |
|
444 |
||
445 |
def create_temp_table(self, session, table, schema): |
|
446 |
raise NotImplementedError() |
|
447 |
||
448 |
def clean_temp_data(self, session, temptables): |
|
449 |
"""remove temporary data, usually associated to temporary tables""" |
|
450 |
pass |
|
451 |
||
1792 | 452 |
|
0 | 453 |
class TrFunc(object): |
454 |
"""lower, upper""" |
|
455 |
def __init__(self, trname, index, attrname=None): |
|
456 |
self._tr = trname.lower() |
|
457 |
self.index = index |
|
458 |
self.attrname = attrname |
|
1792 | 459 |
|
0 | 460 |
def apply(self, resdict): |
461 |
value = resdict.get(self.attrname) |
|
462 |
if value is not None: |
|
463 |
return getattr(value, self._tr)() |
|
464 |
return None |
|
465 |
||
466 |
||
467 |
class GlobTrFunc(TrFunc): |
|
468 |
"""count, sum, max, min, avg""" |
|
469 |
funcs = { |
|
470 |
'count': len, |
|
471 |
'sum': sum, |
|
472 |
'max': max, |
|
473 |
'min': min, |
|
474 |
# XXX avg |
|
475 |
} |
|
476 |
def apply(self, result): |
|
477 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
478 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
479 |
[(1, 7), (3, 4)] |
|
480 |
""" |
|
481 |
keys, values = [], {} |
|
482 |
for row in result: |
|
483 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
484 |
value = row[self.index] |
|
485 |
try: |
|
486 |
values[key].append(value) |
|
487 |
except KeyError: |
|
488 |
keys.append(key) |
|
489 |
values[key] = [value] |
|
490 |
result = [] |
|
491 |
trfunc = self.funcs[self._tr] |
|
492 |
for key in keys: |
|
493 |
row = list(key) |
|
494 |
row.insert(self.index, trfunc(values[key])) |
|
495 |
result.append(row) |
|
496 |
return result |
|
497 |
||
498 |
||
499 |
class ConnectionWrapper(object): |
|
500 |
def __init__(self, cnx=None): |
|
501 |
self.cnx = cnx |
|
502 |
def commit(self): |
|
503 |
pass |
|
504 |
def rollback(self): |
|
505 |
pass |
|
506 |
def cursor(self): |
|
507 |
return None # no actual cursor support |
|
508 |
||
509 |
from cubicweb.server import SOURCE_TYPES |
|
510 |
||
511 |
def source_adapter(source_config): |
|
512 |
adapter_type = source_config['adapter'].lower() |
|
513 |
try: |
|
514 |
return SOURCE_TYPES[adapter_type] |
|
515 |
except KeyError: |
|
516 |
raise RuntimeError('Unknown adapter %r' % adapter_type) |
|
1792 | 517 |
|
0 | 518 |
def get_source(source_config, global_schema, repo): |
519 |
"""return a source adapter according to the adapter field in the |
|
520 |
source's configuration |
|
521 |
""" |
|
522 |
return source_adapter(source_config)(repo, global_schema, source_config) |