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