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