author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 31 Jul 2009 22:37:28 +0200 | |
changeset 2613 | 5e19c2bb370e |
parent 2493 | 9806571ea790 |
child 2589 | 92f2bc945261 |
permissions | -rw-r--r-- |
0 | 1 |
"""Adapters for native cubicweb sources. |
2 |
||
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
3 |
Notes: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
4 |
* extid (aka external id, the primary key of an entity in the external source |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
5 |
from which it comes from) are stored in a varchar column encoded as a base64 |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
6 |
string. This is because it should actually be Bytes but we want an index on |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
7 |
it for fast querying. |
2056 | 8 |
|
0 | 9 |
:organization: Logilab |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
10 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 11 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
12 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 13 |
""" |
14 |
__docformat__ = "restructuredtext en" |
|
15 |
||
16 |
from threading import Lock |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
17 |
from datetime import datetime |
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
18 |
from base64 import b64decode, b64encode |
0 | 19 |
|
20 |
from logilab.common.cache import Cache |
|
21 |
from logilab.common.configuration import REQUIRED |
|
22 |
from logilab.common.adbh import get_adv_func_helper |
|
23 |
||
24 |
from indexer import get_indexer |
|
25 |
||
26 |
from cubicweb import UnknownEid, AuthenticationError, Binary, server |
|
27 |
from cubicweb.server.utils import crypt_password |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
28 |
from cubicweb.server.sqlutils import (SQL_PREFIX, SQLAdapterMixIn, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
29 |
sql_source_backup, sql_source_restore) |
0 | 30 |
from cubicweb.server.rqlannotation import set_qdata |
31 |
from cubicweb.server.sources import AbstractSource |
|
32 |
from cubicweb.server.sources.rql2sql import SQLGenerator |
|
33 |
||
34 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
35 |
ATTR_MAP = {} |
0 | 36 |
NONSYSTEM_ETYPES = set() |
37 |
NONSYSTEM_RELATIONS = set() |
|
38 |
||
39 |
class LogCursor(object): |
|
40 |
def __init__(self, cursor): |
|
41 |
self.cu = cursor |
|
1792 | 42 |
|
0 | 43 |
def execute(self, query, args=None): |
44 |
"""Execute a query. |
|
45 |
it's a function just so that it shows up in profiling |
|
46 |
""" |
|
47 |
if server.DEBUG: |
|
48 |
print 'exec', query, args |
|
49 |
try: |
|
50 |
self.cu.execute(str(query), args) |
|
51 |
except Exception, ex: |
|
52 |
print "sql: %r\n args: %s\ndbms message: %r" % ( |
|
53 |
query, args, ex.args[0]) |
|
54 |
raise |
|
1792 | 55 |
|
0 | 56 |
def fetchall(self): |
57 |
return self.cu.fetchall() |
|
1792 | 58 |
|
0 | 59 |
def fetchone(self): |
60 |
return self.cu.fetchone() |
|
1792 | 61 |
|
0 | 62 |
def make_schema(selected, solution, table, typemap): |
63 |
"""return a sql schema to store RQL query result""" |
|
64 |
sql = [] |
|
65 |
varmap = {} |
|
66 |
for i, term in enumerate(selected): |
|
67 |
name = 'C%s' % i |
|
68 |
key = term.as_string() |
|
69 |
varmap[key] = '%s.%s' % (table, name) |
|
70 |
ttype = term.get_type(solution) |
|
71 |
try: |
|
72 |
sql.append('%s %s' % (name, typemap[ttype])) |
|
73 |
except KeyError: |
|
74 |
# assert not schema(ttype).is_final() |
|
75 |
sql.append('%s %s' % (name, typemap['Int'])) |
|
76 |
return ','.join(sql), varmap |
|
77 |
||
78 |
def _modified_sql(table, etypes): |
|
79 |
# XXX protect against sql injection |
|
80 |
if len(etypes) > 1: |
|
81 |
restr = 'type IN (%s)' % ','.join("'%s'" % etype for etype in etypes) |
|
82 |
else: |
|
83 |
restr = "type='%s'" % etypes[0] |
|
84 |
if table == 'entities': |
|
85 |
attr = 'mtime' |
|
86 |
else: |
|
87 |
attr = 'dtime' |
|
88 |
return 'SELECT type, eid FROM %s WHERE %s AND %s > %%(time)s' % ( |
|
89 |
table, restr, attr) |
|
90 |
||
91 |
||
92 |
class NativeSQLSource(SQLAdapterMixIn, AbstractSource): |
|
93 |
"""adapter for source using the native cubicweb schema (see below) |
|
94 |
""" |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
95 |
sqlgen_class = SQLGenerator |
0 | 96 |
# need default value on class since migration doesn't call init method |
97 |
has_deleted_entitites_table = True |
|
1792 | 98 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
99 |
passwd_rql = "Any P WHERE X is CWUser, X login %(login)s, X upassword P" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
100 |
auth_rql = "Any X WHERE X is CWUser, X login %(login)s, X upassword %(pwd)s" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
101 |
_sols = ({'X': 'CWUser', 'P': 'Password'},) |
1792 | 102 |
|
0 | 103 |
options = ( |
104 |
('db-driver', |
|
105 |
{'type' : 'string', |
|
106 |
'default': 'postgres', |
|
107 |
'help': 'database driver (postgres or sqlite)', |
|
108 |
'group': 'native-source', 'inputlevel': 1, |
|
109 |
}), |
|
110 |
('db-host', |
|
111 |
{'type' : 'string', |
|
112 |
'default': '', |
|
113 |
'help': 'database host', |
|
114 |
'group': 'native-source', 'inputlevel': 1, |
|
115 |
}), |
|
116 |
('db-name', |
|
117 |
{'type' : 'string', |
|
118 |
'default': REQUIRED, |
|
119 |
'help': 'database name', |
|
120 |
'group': 'native-source', 'inputlevel': 0, |
|
121 |
}), |
|
122 |
('db-user', |
|
123 |
{'type' : 'string', |
|
124 |
'default': 'cubicweb', |
|
125 |
'help': 'database user', |
|
126 |
'group': 'native-source', 'inputlevel': 0, |
|
127 |
}), |
|
128 |
('db-password', |
|
129 |
{'type' : 'password', |
|
130 |
'default': '', |
|
131 |
'help': 'database password', |
|
132 |
'group': 'native-source', 'inputlevel': 0, |
|
133 |
}), |
|
134 |
('db-encoding', |
|
135 |
{'type' : 'string', |
|
136 |
'default': 'utf8', |
|
137 |
'help': 'database encoding', |
|
138 |
'group': 'native-source', 'inputlevel': 1, |
|
139 |
}), |
|
140 |
) |
|
1792 | 141 |
|
0 | 142 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
143 |
SQLAdapterMixIn.__init__(self, source_config) |
|
144 |
AbstractSource.__init__(self, repo, appschema, source_config, |
|
145 |
*args, **kwargs) |
|
146 |
# sql generator |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
147 |
self._rql_sqlgen = self.sqlgen_class(appschema, self.dbhelper, |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
148 |
self.encoding, ATTR_MAP.copy()) |
0 | 149 |
# full text index helper |
150 |
self.indexer = get_indexer(self.dbdriver, self.encoding) |
|
151 |
# advanced functionality helper |
|
152 |
self.dbhelper.fti_uid_attr = self.indexer.uid_attr |
|
153 |
self.dbhelper.fti_table = self.indexer.table |
|
154 |
self.dbhelper.fti_restriction_sql = self.indexer.restriction_sql |
|
155 |
self.dbhelper.fti_need_distinct_query = self.indexer.need_distinct |
|
156 |
# sql queries cache |
|
157 |
self._cache = Cache(repo.config['rql-cache-size']) |
|
158 |
self._temp_table_data = {} |
|
159 |
self._eid_creation_lock = Lock() |
|
2072
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
160 |
# XXX no_sqlite_wrap trick since we've a sqlite locking pb when |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
161 |
# running unittest_multisources with the wrapping below |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
162 |
if self.dbdriver == 'sqlite' and \ |
8008e8812d76
deactivate sqlite connection wrapping for unittest_multisources for now
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2066
diff
changeset
|
163 |
not getattr(repo.config, 'no_sqlite_wrap', False): |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
164 |
from cubicweb.server.sources.extlite import ConnectionWrapper |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
165 |
self.get_connection = lambda: ConnectionWrapper(self) |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
166 |
self.check_connection = lambda cnx: cnx |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
167 |
def pool_reset(cnx): |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
168 |
if cnx._cnx is not None: |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
169 |
cnx._cnx.close() |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
170 |
cnx._cnx = None |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
171 |
self.pool_reset = pool_reset |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
172 |
|
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
173 |
@property |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
174 |
def _sqlcnx(self): |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
175 |
# XXX: sqlite connections can only be used in the same thread, so |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
176 |
# create a new one each time necessary. If it appears to be time |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
177 |
# consuming, find another way |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
178 |
return SQLAdapterMixIn.get_connection(self) |
0 | 179 |
|
180 |
def reset_caches(self): |
|
181 |
"""method called during test to reset potential source caches""" |
|
182 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
|
1792 | 183 |
|
0 | 184 |
def clear_eid_cache(self, eid, etype): |
185 |
"""clear potential caches for the given eid""" |
|
186 |
self._cache.pop('%s X WHERE X eid %s' % (etype, eid), None) |
|
187 |
self._cache.pop('Any X WHERE X eid %s' % eid, None) |
|
1792 | 188 |
|
0 | 189 |
def sqlexec(self, session, sql, args=None): |
190 |
"""execute the query and return its result""" |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
191 |
return self.process_result(self.doexec(session, sql, args)) |
1792 | 192 |
|
0 | 193 |
def init_creating(self): |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
194 |
pool = self.repo._get_pool() |
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
195 |
pool.pool_set() |
0 | 196 |
# check full text index availibility |
197 |
if not self.indexer.has_fti_table(pool['system']): |
|
198 |
self.error('no text index table') |
|
199 |
self.indexer = None |
|
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
200 |
pool.pool_reset() |
0 | 201 |
self.repo._free_pool(pool) |
202 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
203 |
def backup(self, confirm, backupfile=None, timestamp=None, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
204 |
askconfirm=False): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
205 |
"""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
|
206 |
backupfile = self.backup_file(backupfile, timestamp) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
207 |
sql_source_backup(self, self, confirm, backupfile, askconfirm) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
208 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
209 |
def restore(self, confirm, backupfile=None, timestamp=None, drop=True, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
210 |
askconfirm=False): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
211 |
"""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
|
212 |
backupfile = self.backup_file(backupfile, timestamp) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
213 |
sql_source_restore(self, self, confirm, backupfile, drop, askconfirm) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
214 |
|
0 | 215 |
def init(self): |
1792 | 216 |
self.init_creating() |
0 | 217 |
pool = self.repo._get_pool() |
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
218 |
pool.pool_set() |
0 | 219 |
# XXX cubicweb < 2.42 compat |
220 |
if 'deleted_entities' in self.dbhelper.list_tables(pool['system']): |
|
221 |
self.has_deleted_entitites_table = True |
|
222 |
else: |
|
223 |
self.has_deleted_entitites_table = False |
|
2064
a5cd3a92314a
properly call [re]set_pool, fix connection handling so we have a change to get cw running on top of a sqlite dabase with threads activated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2056
diff
changeset
|
224 |
pool.pool_reset() |
0 | 225 |
self.repo._free_pool(pool) |
1792 | 226 |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
227 |
def map_attribute(self, etype, attr, cb): |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
228 |
self._rql_sqlgen.attr_map['%s.%s' % (etype, attr)] = cb |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
229 |
|
0 | 230 |
# ISource interface ####################################################### |
231 |
||
232 |
def compile_rql(self, rql): |
|
233 |
rqlst = self.repo.querier._rqlhelper.parse(rql) |
|
234 |
rqlst.restricted_vars = () |
|
235 |
rqlst.children[0].solutions = self._sols |
|
236 |
self.repo.querier.sqlgen_annotate(rqlst) |
|
438 | 237 |
set_qdata(self.schema.rschema, rqlst, ()) |
0 | 238 |
return rqlst |
1792 | 239 |
|
0 | 240 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
241 |
"""set the instance'schema""" |
0 | 242 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
243 |
self.cache_hit, self.cache_miss, self.no_cache = 0, 0, 0 |
|
244 |
self.schema = schema |
|
245 |
try: |
|
246 |
self._rql_sqlgen.schema = schema |
|
247 |
except AttributeError: |
|
248 |
pass # __init__ |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
249 |
if 'CWUser' in schema: # probably an empty schema if not true... |
0 | 250 |
# rql syntax trees used to authenticate users |
251 |
self._passwd_rqlst = self.compile_rql(self.passwd_rql) |
|
252 |
self._auth_rqlst = self.compile_rql(self.auth_rql) |
|
1792 | 253 |
|
0 | 254 |
def support_entity(self, etype, write=False): |
255 |
"""return true if the given entity's type is handled by this adapter |
|
256 |
if write is true, return true only if it's a RW support |
|
257 |
""" |
|
258 |
return not etype in NONSYSTEM_ETYPES |
|
1792 | 259 |
|
0 | 260 |
def support_relation(self, rtype, write=False): |
261 |
"""return true if the given relation's type is handled by this adapter |
|
262 |
if write is true, return true only if it's a RW support |
|
263 |
""" |
|
264 |
if write: |
|
265 |
return not rtype in NONSYSTEM_RELATIONS |
|
266 |
# due to current multi-sources implementation, the system source |
|
1792 | 267 |
# can't claim not supporting a relation |
0 | 268 |
return True #not rtype == 'content_for' |
269 |
||
270 |
def authenticate(self, session, login, password): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
271 |
"""return CWUser eid for the given login/password if this account is |
0 | 272 |
defined in this source, else raise `AuthenticationError` |
273 |
||
274 |
two queries are needed since passwords are stored crypted, so we have |
|
275 |
to fetch the salt first |
|
276 |
""" |
|
277 |
args = {'login': login, 'pwd' : password} |
|
278 |
if password is not None: |
|
279 |
rset = self.syntax_tree_search(session, self._passwd_rqlst, args) |
|
280 |
try: |
|
281 |
pwd = rset[0][0] |
|
282 |
except IndexError: |
|
283 |
raise AuthenticationError('bad login') |
|
1954 | 284 |
# passwords are stored using the Bytes type, so we get a StringIO |
0 | 285 |
if pwd is not None: |
286 |
args['pwd'] = crypt_password(password, pwd.getvalue()[:2]) |
|
287 |
# get eid from login and (crypted) password |
|
288 |
rset = self.syntax_tree_search(session, self._auth_rqlst, args) |
|
289 |
try: |
|
290 |
return rset[0][0] |
|
291 |
except IndexError: |
|
292 |
raise AuthenticationError('bad password') |
|
1792 | 293 |
|
294 |
def syntax_tree_search(self, session, union, args=None, cachekey=None, |
|
0 | 295 |
varmap=None): |
296 |
"""return result from this source for a rql query (actually from |
|
297 |
a rql syntax tree and a solution dictionary mapping each used |
|
298 |
variable to a possible type). If cachekey is given, the query |
|
299 |
necessary to fetch the results (but not the results themselves) |
|
300 |
may be cached using this key. |
|
301 |
""" |
|
302 |
if server.DEBUG: |
|
303 |
print 'RQL FOR NATIVE SOURCE', self.uri, cachekey |
|
304 |
if varmap: |
|
305 |
print 'USING VARMAP', varmap |
|
306 |
print union.as_string() |
|
307 |
if args: print 'ARGS', args |
|
308 |
print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children) |
|
309 |
# remember number of actually selected term (sql generation may append some) |
|
310 |
if cachekey is None: |
|
311 |
self.no_cache += 1 |
|
312 |
# generate sql query if we are able to do so (not supported types...) |
|
313 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
314 |
else: |
|
315 |
# sql may be cached |
|
316 |
try: |
|
317 |
sql, query_args = self._cache[cachekey] |
|
318 |
self.cache_hit += 1 |
|
319 |
except KeyError: |
|
320 |
self.cache_miss += 1 |
|
321 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
322 |
self._cache[cachekey] = sql, query_args |
|
323 |
args = self.merge_args(args, query_args) |
|
324 |
assert isinstance(sql, basestring), repr(sql) |
|
325 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
326 |
cursor = self.doexec(session, sql, args) |
0 | 327 |
except (self.dbapi_module.OperationalError, |
328 |
self.dbapi_module.InterfaceError): |
|
329 |
# FIXME: better detection of deconnection pb |
|
330 |
self.info("request failed '%s' ... retry with a new cursor", sql) |
|
331 |
session.pool.reconnect(self) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
332 |
cursor = self.doexec(session, sql, args) |
0 | 333 |
res = self.process_result(cursor) |
334 |
if server.DEBUG: |
|
335 |
print '------>', res |
|
336 |
return res |
|
1792 | 337 |
|
0 | 338 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
339 |
"""similar as .syntax_tree_search, but inserts data in the |
|
340 |
temporary table (on-the-fly if possible, eg for the system |
|
341 |
source whose the given cursor come from). If not possible, |
|
342 |
inserts all data by calling .executemany(). |
|
343 |
""" |
|
344 |
if self.uri == 'system': |
|
345 |
if server.DEBUG: |
|
346 |
print 'FLYING RQL FOR SOURCE', self.uri |
|
347 |
if varmap: |
|
348 |
print 'USING VARMAP', varmap |
|
349 |
print union.as_string() |
|
350 |
print 'SOLUTIONS', ','.join(str(s.solutions) for s in union.children) |
|
351 |
# generate sql queries if we are able to do so |
|
352 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
353 |
query = 'INSERT INTO %s %s' % (table, sql.encode(self.encoding)) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
354 |
self.doexec(session, query, self.merge_args(args, query_args)) |
0 | 355 |
else: |
356 |
super(NativeSQLSource, self).flying_insert(table, session, union, |
|
357 |
args, varmap) |
|
1792 | 358 |
|
0 | 359 |
def _manual_insert(self, results, table, session): |
360 |
"""insert given result into a temporary table on the system source""" |
|
361 |
if not results: |
|
362 |
return |
|
363 |
query_args = ['%%(%s)s' % i for i in xrange(len(results[0]))] |
|
364 |
query = 'INSERT INTO %s VALUES(%s)' % (table, ','.join(query_args)) |
|
365 |
kwargs_list = [] |
|
366 |
for row in results: |
|
367 |
kwargs = {} |
|
368 |
row = tuple(row) |
|
369 |
for index, cell in enumerate(row): |
|
2066
2c4bf4ee88a2
cleanup, stop encoding unicode string in manual_insert, no more necessary and make crash recent sqlite w/ 8bit string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2064
diff
changeset
|
370 |
if isinstance(cell, Binary): |
0 | 371 |
cell = self.binary(cell.getvalue()) |
372 |
kwargs[str(index)] = cell |
|
373 |
kwargs_list.append(kwargs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
374 |
self.doexecmany(session, query, kwargs_list) |
0 | 375 |
|
376 |
def clean_temp_data(self, session, temptables): |
|
377 |
"""remove temporary data, usually associated to temporary tables""" |
|
378 |
if temptables: |
|
379 |
for table in temptables: |
|
380 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
381 |
self.doexec(session,'DROP TABLE %s' % table) |
0 | 382 |
except: |
383 |
pass |
|
384 |
try: |
|
385 |
del self._temp_table_data[table] |
|
386 |
except KeyError: |
|
387 |
continue |
|
1792 | 388 |
|
0 | 389 |
def add_entity(self, session, entity): |
390 |
"""add a new entity to the source""" |
|
391 |
attrs = self.preprocess_entity(entity) |
|
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:
1079
diff
changeset
|
392 |
sql = self.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
393 |
self.doexec(session, sql, attrs) |
1792 | 394 |
|
0 | 395 |
def update_entity(self, session, entity): |
396 |
"""replace an entity in the source""" |
|
397 |
attrs = self.preprocess_entity(entity) |
|
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:
1079
diff
changeset
|
398 |
sql = self.sqlgen.update(SQL_PREFIX + str(entity.e_schema), attrs, [SQL_PREFIX + 'eid']) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
399 |
self.doexec(session, sql, attrs) |
0 | 400 |
|
401 |
def delete_entity(self, session, etype, eid): |
|
402 |
"""delete an entity from the source""" |
|
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:
1079
diff
changeset
|
403 |
attrs = {SQL_PREFIX + 'eid': eid} |
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:
1079
diff
changeset
|
404 |
sql = self.sqlgen.delete(SQL_PREFIX + etype, attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
405 |
self.doexec(session, sql, attrs) |
0 | 406 |
|
407 |
def add_relation(self, session, subject, rtype, object): |
|
408 |
"""add a relation to the source""" |
|
409 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
410 |
sql = self.sqlgen.insert('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
411 |
self.doexec(session, sql, attrs) |
1792 | 412 |
|
0 | 413 |
def delete_relation(self, session, subject, rtype, object): |
414 |
"""delete a relation from the source""" |
|
415 |
rschema = self.schema.rschema(rtype) |
|
416 |
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:
1079
diff
changeset
|
417 |
table = SQL_PREFIX + session.describe(subject)[0] |
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:
1079
diff
changeset
|
418 |
column = SQL_PREFIX + rtype |
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:
1079
diff
changeset
|
419 |
sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, |
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:
1079
diff
changeset
|
420 |
SQL_PREFIX) |
0 | 421 |
attrs = {'eid' : subject} |
422 |
else: |
|
423 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
424 |
sql = self.sqlgen.delete('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
425 |
self.doexec(session, sql, attrs) |
0 | 426 |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
427 |
def doexec(self, session, query, args=None): |
0 | 428 |
"""Execute a query. |
429 |
it's a function just so that it shows up in profiling |
|
430 |
""" |
|
431 |
if server.DEBUG: |
|
432 |
print 'exec', query, args |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
433 |
cursor = session.pool[self.uri] |
0 | 434 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
435 |
# str(query) to avoid error if it's an unicode string |
0 | 436 |
cursor.execute(str(query), args) |
437 |
except Exception, ex: |
|
438 |
self.critical("sql: %r\n args: %s\ndbms message: %r", |
|
439 |
query, args, ex.args[0]) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
440 |
try: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
441 |
session.pool.connection(self.uri).rollback() |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
442 |
self.critical('transaction has been rollbacked') |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
443 |
except: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
444 |
pass |
0 | 445 |
raise |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
446 |
return cursor |
1792 | 447 |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
448 |
def doexecmany(self, session, query, args): |
0 | 449 |
"""Execute a query. |
450 |
it's a function just so that it shows up in profiling |
|
451 |
""" |
|
452 |
if server.DEBUG: |
|
453 |
print 'execmany', query, 'with', len(args), 'arguments' |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
454 |
cursor = session.pool[self.uri] |
0 | 455 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
456 |
# str(query) to avoid error if it's an unicode string |
0 | 457 |
cursor.executemany(str(query), args) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
458 |
except Exception, ex: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
459 |
self.critical("sql many: %r\n args: %s\ndbms message: %r", |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
460 |
query, args, ex.args[0]) |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
461 |
try: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
462 |
session.pool.connection(self.uri).rollback() |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
463 |
self.critical('transaction has been rollbacked') |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
464 |
except: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
465 |
pass |
0 | 466 |
raise |
1792 | 467 |
|
0 | 468 |
# short cut to method requiring advanced db helper usage ################## |
1792 | 469 |
|
0 | 470 |
def create_index(self, session, table, column, unique=False): |
471 |
cursor = LogCursor(session.pool[self.uri]) |
|
472 |
self.dbhelper.create_index(cursor, table, column, unique) |
|
1792 | 473 |
|
0 | 474 |
def drop_index(self, session, table, column, unique=False): |
475 |
cursor = LogCursor(session.pool[self.uri]) |
|
476 |
self.dbhelper.drop_index(cursor, table, column, unique) |
|
477 |
||
478 |
# system source interface ################################################# |
|
479 |
||
480 |
def eid_type_source(self, session, eid): |
|
481 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
482 |
sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
|
483 |
try: |
|
484 |
res = session.system_sql(sql).fetchone() |
|
485 |
except: |
|
1079
452cb76fe07a
backport typo fix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
973
diff
changeset
|
486 |
assert session.pool, 'session has no pool set' |
0 | 487 |
raise UnknownEid(eid) |
488 |
if res is None: |
|
489 |
raise UnknownEid(eid) |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
490 |
if res[-1] is not None: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
491 |
if not isinstance(res, list): |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
492 |
res = list(res) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
493 |
res[-1] = b64decode(res[-1]) |
0 | 494 |
return res |
495 |
||
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
496 |
def extid2eid(self, session, source, extid): |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
497 |
"""get eid from an external id. Return None if no record found.""" |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
498 |
assert isinstance(extid, str) |
0 | 499 |
cursor = session.system_sql('SELECT eid FROM entities WHERE ' |
500 |
'extid=%(x)s AND source=%(s)s', |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
501 |
{'x': b64encode(extid), 's': source.uri}) |
0 | 502 |
# XXX testing rowcount cause strange bug with sqlite, results are there |
503 |
# but rowcount is 0 |
|
1792 | 504 |
#if cursor.rowcount > 0: |
0 | 505 |
try: |
506 |
result = cursor.fetchone() |
|
507 |
if result: |
|
1954 | 508 |
return result[0] |
0 | 509 |
except: |
510 |
pass |
|
511 |
return None |
|
1792 | 512 |
|
0 | 513 |
def temp_table_def(self, selected, sol, table): |
514 |
return make_schema(selected, sol, table, self.dbhelper.TYPE_MAPPING) |
|
515 |
||
516 |
def create_temp_table(self, session, table, schema): |
|
517 |
# we don't want on commit drop, this may cause problem when |
|
518 |
# running with an ldap source, and table will be deleted manually any way |
|
519 |
# on commit |
|
520 |
sql = self.dbhelper.sql_temporary_table(table, schema, False) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
521 |
self.doexec(session, sql) |
1792 | 522 |
|
0 | 523 |
def create_eid(self, session): |
524 |
self._eid_creation_lock.acquire() |
|
525 |
try: |
|
526 |
for sql in self.dbhelper.sqls_increment_sequence('entities_id_seq'): |
|
2310
80fcdfbb8eed
oops, needs the cursor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
527 |
cursor = self.doexec(session, sql) |
0 | 528 |
return cursor.fetchone()[0] |
529 |
finally: |
|
530 |
self._eid_creation_lock.release() |
|
531 |
||
532 |
def add_info(self, session, entity, source, extid=None): |
|
533 |
"""add type and source info for an eid into the system table""" |
|
534 |
# begin by inserting eid/type/source/extid into the entities table |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
535 |
if extid is not None: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
536 |
assert isinstance(extid, str) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
537 |
extid = b64encode(extid) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
538 |
attrs = {'type': entity.id, 'eid': entity.eid, 'extid': extid, |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
539 |
'source': source.uri, 'mtime': datetime.now()} |
0 | 540 |
session.system_sql(self.sqlgen.insert('entities', attrs), attrs) |
541 |
||
542 |
def delete_info(self, session, eid, etype, uri, extid): |
|
543 |
"""delete system information on deletion of an entity by transfering |
|
544 |
record from the entities table to the deleted_entities table |
|
545 |
""" |
|
546 |
attrs = {'eid': eid} |
|
547 |
session.system_sql(self.sqlgen.delete('entities', attrs), attrs) |
|
548 |
if self.has_deleted_entitites_table: |
|
1952
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
549 |
if extid is not None: |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
550 |
assert isinstance(extid, str), type(extid) |
8e19c813750d
fix extid handling: ensure encoded string is given, and store them as base64 (see note in native.py).
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1792
diff
changeset
|
551 |
extid = b64encode(extid) |
0 | 552 |
attrs = {'type': etype, 'eid': eid, 'extid': extid, |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
553 |
'source': uri, 'dtime': datetime.now()} |
0 | 554 |
session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs) |
1792 | 555 |
|
0 | 556 |
def fti_unindex_entity(self, session, eid): |
557 |
"""remove text content for entity with the given eid from the full text |
|
558 |
index |
|
559 |
""" |
|
560 |
try: |
|
561 |
self.indexer.cursor_unindex_object(eid, session.pool['system']) |
|
562 |
except: |
|
563 |
if self.indexer is not None: |
|
564 |
self.exception('error while unindexing %s', eid) |
|
1792 | 565 |
|
0 | 566 |
def fti_index_entity(self, session, entity): |
567 |
"""add text content of a created/modified entity to the full text index |
|
568 |
""" |
|
569 |
self.info('reindexing %r', entity.eid) |
|
570 |
try: |
|
571 |
self.indexer.cursor_reindex_object(entity.eid, entity, |
|
572 |
session.pool['system']) |
|
573 |
except: |
|
574 |
if self.indexer is not None: |
|
575 |
self.exception('error while reindexing %s', entity) |
|
576 |
# update entities.mtime |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
577 |
attrs = {'eid': entity.eid, 'mtime': datetime.now()} |
0 | 578 |
session.system_sql(self.sqlgen.update('entities', attrs, ['eid']), attrs) |
1792 | 579 |
|
0 | 580 |
def modified_entities(self, session, etypes, mtime): |
581 |
"""return a 2-uple: |
|
582 |
* list of (etype, eid) of entities of the given types which have been |
|
583 |
modified since the given timestamp (actually entities whose full text |
|
584 |
index content has changed) |
|
585 |
* list of (etype, eid) of entities of the given types which have been |
|
586 |
deleted since the given timestamp |
|
587 |
""" |
|
588 |
modsql = _modified_sql('entities', etypes) |
|
589 |
cursor = session.system_sql(modsql, {'time': mtime}) |
|
590 |
modentities = cursor.fetchall() |
|
591 |
delsql = _modified_sql('deleted_entities', etypes) |
|
592 |
cursor = session.system_sql(delsql, {'time': mtime}) |
|
593 |
delentities = cursor.fetchall() |
|
594 |
return modentities, delentities |
|
595 |
||
596 |
||
597 |
def sql_schema(driver): |
|
598 |
helper = get_adv_func_helper(driver) |
|
599 |
schema = """ |
|
600 |
/* Create the repository's system database */ |
|
601 |
||
602 |
%s |
|
603 |
||
604 |
CREATE TABLE entities ( |
|
605 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
606 |
type VARCHAR(64) NOT NULL, |
|
607 |
source VARCHAR(64) NOT NULL, |
|
608 |
mtime TIMESTAMP NOT NULL, |
|
609 |
extid VARCHAR(256) |
|
610 |
); |
|
611 |
CREATE INDEX entities_type_idx ON entities(type); |
|
612 |
CREATE INDEX entities_mtime_idx ON entities(mtime); |
|
613 |
CREATE INDEX entities_extid_idx ON entities(extid); |
|
614 |
||
615 |
CREATE TABLE deleted_entities ( |
|
616 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
617 |
type VARCHAR(64) NOT NULL, |
|
618 |
source VARCHAR(64) NOT NULL, |
|
619 |
dtime TIMESTAMP NOT NULL, |
|
620 |
extid VARCHAR(256) |
|
621 |
); |
|
622 |
CREATE INDEX deleted_entities_type_idx ON deleted_entities(type); |
|
623 |
CREATE INDEX deleted_entities_dtime_idx ON deleted_entities(dtime); |
|
624 |
CREATE INDEX deleted_entities_extid_idx ON deleted_entities(extid); |
|
625 |
""" % helper.sql_create_sequence('entities_id_seq') |
|
626 |
return schema |
|
627 |
||
628 |
||
629 |
def sql_drop_schema(driver): |
|
630 |
helper = get_adv_func_helper(driver) |
|
631 |
return """ |
|
632 |
%s |
|
633 |
DROP TABLE entities; |
|
634 |
DROP TABLE deleted_entities; |
|
635 |
""" % helper.sql_drop_sequence('entities_id_seq') |
|
636 |
||
637 |
||
638 |
def grant_schema(user, set_owner=True): |
|
639 |
result = '' |
|
640 |
if set_owner: |
|
641 |
result = 'ALTER TABLE entities OWNER TO %s;\n' % user |
|
642 |
result += 'ALTER TABLE deleted_entities OWNER TO %s;\n' % user |
|
643 |
result += 'ALTER TABLE entities_id_seq OWNER TO %s;\n' % user |
|
644 |
result += 'GRANT ALL ON entities TO %s;\n' % user |
|
645 |
result += 'GRANT ALL ON deleted_entities TO %s;\n' % user |
|
646 |
result += 'GRANT ALL ON entities_id_seq TO %s;\n' % user |
|
647 |
return result |