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