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