author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 26 Feb 2010 16:51:41 +0100 | |
branch | stable |
changeset 4734 | 4ae30c9ca11b |
parent 4692 | 11a040e2601c |
child 4806 | 4f12f59b1a13 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4198
diff
changeset
|
10 |
:copyright: 2001-2010 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 |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
21 |
from logilab.common.configuration import Method |
0 | 22 |
from logilab.common.adbh import get_adv_func_helper |
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
23 |
from logilab.common.shellutils import getlogin |
0 | 24 |
|
25 |
from indexer import get_indexer |
|
26 |
||
27 |
from cubicweb import UnknownEid, AuthenticationError, Binary, server |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
28 |
from cubicweb.cwconfig import CubicWebNoAppConfiguration |
0 | 29 |
from cubicweb.server.utils import crypt_password |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
30 |
from cubicweb.server.sqlutils import SQL_PREFIX, SQLAdapterMixIn |
0 | 31 |
from cubicweb.server.rqlannotation import set_qdata |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
32 |
from cubicweb.server.sources import AbstractSource, dbg_st_search, dbg_results |
0 | 33 |
from cubicweb.server.sources.rql2sql import SQLGenerator |
34 |
||
35 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
36 |
ATTR_MAP = {} |
0 | 37 |
NONSYSTEM_ETYPES = set() |
38 |
NONSYSTEM_RELATIONS = set() |
|
39 |
||
40 |
class LogCursor(object): |
|
41 |
def __init__(self, cursor): |
|
42 |
self.cu = cursor |
|
1792 | 43 |
|
0 | 44 |
def execute(self, query, args=None): |
45 |
"""Execute a query. |
|
46 |
it's a function just so that it shows up in profiling |
|
47 |
""" |
|
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
48 |
if server.DEBUG & server.DBG_SQL: |
0 | 49 |
print 'exec', query, args |
50 |
try: |
|
51 |
self.cu.execute(str(query), args) |
|
52 |
except Exception, ex: |
|
53 |
print "sql: %r\n args: %s\ndbms message: %r" % ( |
|
54 |
query, args, ex.args[0]) |
|
55 |
raise |
|
1792 | 56 |
|
0 | 57 |
def fetchall(self): |
58 |
return self.cu.fetchall() |
|
1792 | 59 |
|
0 | 60 |
def fetchone(self): |
61 |
return self.cu.fetchone() |
|
1792 | 62 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
63 |
|
0 | 64 |
def make_schema(selected, solution, table, typemap): |
65 |
"""return a sql schema to store RQL query result""" |
|
66 |
sql = [] |
|
67 |
varmap = {} |
|
68 |
for i, term in enumerate(selected): |
|
69 |
name = 'C%s' % i |
|
70 |
key = term.as_string() |
|
71 |
varmap[key] = '%s.%s' % (table, name) |
|
72 |
ttype = term.get_type(solution) |
|
73 |
try: |
|
74 |
sql.append('%s %s' % (name, typemap[ttype])) |
|
75 |
except KeyError: |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3497
diff
changeset
|
76 |
# assert not schema(ttype).final |
0 | 77 |
sql.append('%s %s' % (name, typemap['Int'])) |
78 |
return ','.join(sql), varmap |
|
79 |
||
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
80 |
|
0 | 81 |
def _modified_sql(table, etypes): |
82 |
# XXX protect against sql injection |
|
83 |
if len(etypes) > 1: |
|
84 |
restr = 'type IN (%s)' % ','.join("'%s'" % etype for etype in etypes) |
|
85 |
else: |
|
86 |
restr = "type='%s'" % etypes[0] |
|
87 |
if table == 'entities': |
|
88 |
attr = 'mtime' |
|
89 |
else: |
|
90 |
attr = 'dtime' |
|
91 |
return 'SELECT type, eid FROM %s WHERE %s AND %s > %%(time)s' % ( |
|
92 |
table, restr, attr) |
|
93 |
||
94 |
||
95 |
class NativeSQLSource(SQLAdapterMixIn, AbstractSource): |
|
96 |
"""adapter for source using the native cubicweb schema (see below) |
|
97 |
""" |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
98 |
sqlgen_class = SQLGenerator |
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 |
}), |
|
2566
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
112 |
('db-port', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
113 |
{'type' : 'string', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
114 |
'default': '', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
115 |
'help': 'database port', |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
116 |
'group': 'native-source', 'inputlevel': 1, |
714a8743d423
missing db-port option to source's option definitions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
117 |
}), |
0 | 118 |
('db-name', |
119 |
{'type' : 'string', |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
120 |
'default': Method('default_instance_id'), |
0 | 121 |
'help': 'database name', |
122 |
'group': 'native-source', 'inputlevel': 0, |
|
123 |
}), |
|
124 |
('db-user', |
|
125 |
{'type' : 'string', |
|
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
126 |
'default': CubicWebNoAppConfiguration.mode == 'user' and getlogin() or 'cubicweb', |
0 | 127 |
'help': 'database user', |
128 |
'group': 'native-source', 'inputlevel': 0, |
|
129 |
}), |
|
130 |
('db-password', |
|
131 |
{'type' : 'password', |
|
132 |
'default': '', |
|
133 |
'help': 'database password', |
|
134 |
'group': 'native-source', 'inputlevel': 0, |
|
135 |
}), |
|
136 |
('db-encoding', |
|
137 |
{'type' : 'string', |
|
138 |
'default': 'utf8', |
|
139 |
'help': 'database encoding', |
|
140 |
'group': 'native-source', 'inputlevel': 1, |
|
141 |
}), |
|
142 |
) |
|
1792 | 143 |
|
0 | 144 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
145 |
SQLAdapterMixIn.__init__(self, source_config) |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
146 |
self.authentifiers = [LoginPasswordAuthentifier(self)] |
0 | 147 |
AbstractSource.__init__(self, repo, appschema, source_config, |
148 |
*args, **kwargs) |
|
149 |
# sql generator |
|
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
150 |
self._rql_sqlgen = self.sqlgen_class(appschema, self.dbhelper, |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
151 |
self.encoding, ATTR_MAP.copy()) |
0 | 152 |
# full text index helper |
153 |
self.indexer = get_indexer(self.dbdriver, self.encoding) |
|
154 |
# advanced functionality helper |
|
155 |
self.dbhelper.fti_uid_attr = self.indexer.uid_attr |
|
156 |
self.dbhelper.fti_table = self.indexer.table |
|
157 |
self.dbhelper.fti_restriction_sql = self.indexer.restriction_sql |
|
158 |
self.dbhelper.fti_need_distinct_query = self.indexer.need_distinct |
|
159 |
# sql queries cache |
|
160 |
self._cache = Cache(repo.config['rql-cache-size']) |
|
161 |
self._temp_table_data = {} |
|
162 |
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
|
163 |
# 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
|
164 |
# 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
|
165 |
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
|
166 |
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
|
167 |
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
|
168 |
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
|
169 |
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
|
170 |
def pool_reset(cnx): |
2620
de68f84b8f54
R [sql source] cnx._cnx checked in cnx.close, don't do it here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2619
diff
changeset
|
171 |
cnx.close() |
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
|
172 |
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
|
173 |
|
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 |
@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
|
175 |
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
|
176 |
# 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
|
177 |
# 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
|
178 |
# 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
|
179 |
return SQLAdapterMixIn.get_connection(self) |
0 | 180 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
181 |
def add_authentifier(self, authentifier): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
182 |
self.authentifiers.append(authentifier) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
183 |
authentifier.source = self |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
184 |
authentifier.set_schema(self.schema) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
185 |
|
0 | 186 |
def reset_caches(self): |
187 |
"""method called during test to reset potential source caches""" |
|
188 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
|
1792 | 189 |
|
0 | 190 |
def clear_eid_cache(self, eid, etype): |
191 |
"""clear potential caches for the given eid""" |
|
2610
2933cc6bf9ad
[F native source] fix attempts to clear cache key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
192 |
self._cache.pop('Any X WHERE X eid %s, X is %s' % (eid, etype), None) |
0 | 193 |
self._cache.pop('Any X WHERE X eid %s' % eid, None) |
2610
2933cc6bf9ad
[F native source] fix attempts to clear cache key
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
194 |
self._cache.pop('Any %s' % eid, None) |
1792 | 195 |
|
0 | 196 |
def sqlexec(self, session, sql, args=None): |
197 |
"""execute the query and return its result""" |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
198 |
return self.process_result(self.doexec(session, sql, args)) |
1792 | 199 |
|
0 | 200 |
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
|
201 |
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
|
202 |
pool.pool_set() |
0 | 203 |
# check full text index availibility |
204 |
if not self.indexer.has_fti_table(pool['system']): |
|
205 |
self.error('no text index table') |
|
206 |
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
|
207 |
pool.pool_reset() |
0 | 208 |
self.repo._free_pool(pool) |
209 |
||
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
210 |
def backup(self, backupfile): |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
211 |
"""method called to create a backup of the source's data""" |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
212 |
self.close_pool_connections() |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
213 |
try: |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
214 |
self.backup_to_file(backupfile) |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
215 |
finally: |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
216 |
self.open_pool_connections() |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
217 |
|
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4185
diff
changeset
|
218 |
def restore(self, backupfile, confirm, drop): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
219 |
"""method called to restore a backup of source's data""" |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
220 |
if self.repo.config.open_connections_pools: |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
221 |
self.close_pool_connections() |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
222 |
try: |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4185
diff
changeset
|
223 |
self.restore_from_file(backupfile, confirm, drop=drop) |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2638
diff
changeset
|
224 |
finally: |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
225 |
if self.repo.config.open_connections_pools: |
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
226 |
self.open_pool_connections() |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
227 |
|
0 | 228 |
def init(self): |
1792 | 229 |
self.init_creating() |
230 |
||
2354
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
231 |
def map_attribute(self, etype, attr, cb): |
9b4bac626977
ability to map attributes to something else than usual cw mapping on sql generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2310
diff
changeset
|
232 |
self._rql_sqlgen.attr_map['%s.%s' % (etype, attr)] = cb |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
233 |
|
4512
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
234 |
def unmap_attribute(self, etype, attr): |
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
235 |
self._rql_sqlgen.attr_map.pop('%s.%s' % (etype, attr), None) |
e7ac20bf3629
unset_attribute_storage, for testing purpose at least
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
236 |
|
0 | 237 |
# ISource interface ####################################################### |
238 |
||
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
239 |
def compile_rql(self, rql, sols): |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
240 |
rqlst = self.repo.vreg.rqlhelper.parse(rql) |
0 | 241 |
rqlst.restricted_vars = () |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
242 |
rqlst.children[0].solutions = sols |
0 | 243 |
self.repo.querier.sqlgen_annotate(rqlst) |
438 | 244 |
set_qdata(self.schema.rschema, rqlst, ()) |
0 | 245 |
return rqlst |
1792 | 246 |
|
0 | 247 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2354
diff
changeset
|
248 |
"""set the instance'schema""" |
0 | 249 |
self._cache = Cache(self.repo.config['rql-cache-size']) |
250 |
self.cache_hit, self.cache_miss, self.no_cache = 0, 0, 0 |
|
251 |
self.schema = schema |
|
252 |
try: |
|
253 |
self._rql_sqlgen.schema = schema |
|
254 |
except AttributeError: |
|
255 |
pass # __init__ |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
256 |
for authentifier in self.authentifiers: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
257 |
authentifier.set_schema(self.schema) |
1792 | 258 |
|
0 | 259 |
def support_entity(self, etype, write=False): |
260 |
"""return true if the given entity's type is handled by this adapter |
|
261 |
if write is true, return true only if it's a RW support |
|
262 |
""" |
|
263 |
return not etype in NONSYSTEM_ETYPES |
|
1792 | 264 |
|
0 | 265 |
def support_relation(self, rtype, write=False): |
266 |
"""return true if the given relation's type is handled by this adapter |
|
267 |
if write is true, return true only if it's a RW support |
|
268 |
""" |
|
269 |
if write: |
|
270 |
return not rtype in NONSYSTEM_RELATIONS |
|
271 |
# due to current multi-sources implementation, the system source |
|
1792 | 272 |
# can't claim not supporting a relation |
0 | 273 |
return True #not rtype == 'content_for' |
274 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
275 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
276 |
return True |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
277 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
278 |
def authenticate(self, session, login, **kwargs): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
279 |
"""return CWUser eid for the given login and other authentication |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
280 |
information found in kwargs, else raise `AuthenticationError` |
0 | 281 |
""" |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
282 |
for authentifier in self.authentifiers: |
0 | 283 |
try: |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
284 |
return authentifier.authenticate(session, login, **kwargs) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
285 |
except AuthenticationError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
286 |
continue |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
287 |
raise AuthenticationError() |
1792 | 288 |
|
289 |
def syntax_tree_search(self, session, union, args=None, cachekey=None, |
|
0 | 290 |
varmap=None): |
291 |
"""return result from this source for a rql query (actually from |
|
292 |
a rql syntax tree and a solution dictionary mapping each used |
|
293 |
variable to a possible type). If cachekey is given, the query |
|
294 |
necessary to fetch the results (but not the results themselves) |
|
295 |
may be cached using this key. |
|
296 |
""" |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
297 |
assert dbg_st_search(self.uri, union, varmap, args, cachekey) |
0 | 298 |
# remember number of actually selected term (sql generation may append some) |
299 |
if cachekey is None: |
|
300 |
self.no_cache += 1 |
|
301 |
# generate sql query if we are able to do so (not supported types...) |
|
302 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
303 |
else: |
|
304 |
# sql may be cached |
|
305 |
try: |
|
306 |
sql, query_args = self._cache[cachekey] |
|
307 |
self.cache_hit += 1 |
|
308 |
except KeyError: |
|
309 |
self.cache_miss += 1 |
|
310 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
|
311 |
self._cache[cachekey] = sql, query_args |
|
312 |
args = self.merge_args(args, query_args) |
|
313 |
assert isinstance(sql, basestring), repr(sql) |
|
314 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
315 |
cursor = self.doexec(session, sql, args) |
0 | 316 |
except (self.dbapi_module.OperationalError, |
317 |
self.dbapi_module.InterfaceError): |
|
318 |
# FIXME: better detection of deconnection pb |
|
319 |
self.info("request failed '%s' ... retry with a new cursor", sql) |
|
320 |
session.pool.reconnect(self) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
321 |
cursor = self.doexec(session, sql, args) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
322 |
results = self.process_result(cursor) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
323 |
assert dbg_results(results) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
324 |
return results |
1792 | 325 |
|
0 | 326 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
327 |
"""similar as .syntax_tree_search, but inserts data in the |
|
328 |
temporary table (on-the-fly if possible, eg for the system |
|
329 |
source whose the given cursor come from). If not possible, |
|
330 |
inserts all data by calling .executemany(). |
|
331 |
""" |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
332 |
assert dbg_st_search( |
2638 | 333 |
self.uri, union, varmap, args, |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
334 |
prefix='ON THE FLY temp data insertion into %s from' % table) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
335 |
# generate sql queries if we are able to do so |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
336 |
sql, query_args = self._rql_sqlgen.generate(union, args, varmap) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
337 |
query = 'INSERT INTO %s %s' % (table, sql.encode(self.encoding)) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
338 |
self.doexec(session, query, self.merge_args(args, query_args)) |
1792 | 339 |
|
2627
d710278e0c1c
manual_insert is a public method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2625
diff
changeset
|
340 |
def manual_insert(self, results, table, session): |
0 | 341 |
"""insert given result into a temporary table on the system source""" |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
342 |
if server.DEBUG & server.DBG_RQL: |
3958
505025eb0d37
fix name error while debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3835
diff
changeset
|
343 |
print ' manual insertion of', results, 'into', table |
0 | 344 |
if not results: |
345 |
return |
|
346 |
query_args = ['%%(%s)s' % i for i in xrange(len(results[0]))] |
|
347 |
query = 'INSERT INTO %s VALUES(%s)' % (table, ','.join(query_args)) |
|
348 |
kwargs_list = [] |
|
349 |
for row in results: |
|
350 |
kwargs = {} |
|
351 |
row = tuple(row) |
|
352 |
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
|
353 |
if isinstance(cell, Binary): |
0 | 354 |
cell = self.binary(cell.getvalue()) |
355 |
kwargs[str(index)] = cell |
|
356 |
kwargs_list.append(kwargs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
357 |
self.doexecmany(session, query, kwargs_list) |
0 | 358 |
|
359 |
def clean_temp_data(self, session, temptables): |
|
360 |
"""remove temporary data, usually associated to temporary tables""" |
|
361 |
if temptables: |
|
362 |
for table in temptables: |
|
363 |
try: |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
364 |
self.doexec(session,'DROP TABLE %s' % table) |
0 | 365 |
except: |
366 |
pass |
|
367 |
try: |
|
368 |
del self._temp_table_data[table] |
|
369 |
except KeyError: |
|
370 |
continue |
|
1792 | 371 |
|
0 | 372 |
def add_entity(self, session, entity): |
373 |
"""add a new entity to the source""" |
|
374 |
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
|
375 |
sql = self.sqlgen.insert(SQL_PREFIX + str(entity.e_schema), attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
376 |
self.doexec(session, sql, attrs) |
1792 | 377 |
|
0 | 378 |
def update_entity(self, session, entity): |
379 |
"""replace an entity in the source""" |
|
380 |
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
|
381 |
sql = self.sqlgen.update(SQL_PREFIX + str(entity.e_schema), attrs, [SQL_PREFIX + 'eid']) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
382 |
self.doexec(session, sql, attrs) |
0 | 383 |
|
384 |
def delete_entity(self, session, etype, eid): |
|
385 |
"""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
|
386 |
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
|
387 |
sql = self.sqlgen.delete(SQL_PREFIX + etype, attrs) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
388 |
self.doexec(session, sql, attrs) |
0 | 389 |
|
390 |
def add_relation(self, session, subject, rtype, object): |
|
391 |
"""add a relation to the source""" |
|
392 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
393 |
sql = self.sqlgen.insert('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
394 |
self.doexec(session, sql, attrs) |
1792 | 395 |
|
0 | 396 |
def delete_relation(self, session, subject, rtype, object): |
397 |
"""delete a relation from the source""" |
|
398 |
rschema = self.schema.rschema(rtype) |
|
399 |
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
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
SQL_PREFIX) |
0 | 404 |
attrs = {'eid' : subject} |
405 |
else: |
|
406 |
attrs = {'eid_from': subject, 'eid_to': object} |
|
407 |
sql = self.sqlgen.delete('%s_relation' % rtype, attrs) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
408 |
self.doexec(session, sql, attrs) |
0 | 409 |
|
2618
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
410 |
def doexec(self, session, query, args=None, rollback=True): |
0 | 411 |
"""Execute a query. |
412 |
it's a function just so that it shows up in profiling |
|
413 |
""" |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
414 |
cursor = session.pool[self.uri] |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
415 |
if server.DEBUG & server.DBG_SQL: |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
416 |
cnx = session.pool.connection(self.uri) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
417 |
# getattr to get the actual connection if cnx is a ConnectionWrapper |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
418 |
# instance |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2620
diff
changeset
|
419 |
print 'exec', query, args, getattr(cnx, '_cnx', cnx) |
0 | 420 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
421 |
# str(query) to avoid error if it's an unicode string |
0 | 422 |
cursor.execute(str(query), args) |
423 |
except Exception, ex: |
|
4682
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
424 |
if self.repo.config.mode != 'test': |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
425 |
# during test we get those message when trying to alter sqlite |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
426 |
# db schema |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
427 |
self.critical("sql: %r\n args: %s\ndbms message: %r", |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
428 |
query, args, ex.args[0]) |
2618
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
429 |
if rollback: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
430 |
try: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
431 |
session.pool.connection(self.uri).rollback() |
4692
11a040e2601c
[test] also hide this message during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4682
diff
changeset
|
432 |
if self.repo.config.mode != 'test': |
11a040e2601c
[test] also hide this message during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4682
diff
changeset
|
433 |
self.critical('transaction has been rollbacked') |
2618
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
434 |
except: |
ff9b0d5bd884
[F repo sqlite schema changes] don't rollback on potentially expected schema changes failure
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2611
diff
changeset
|
435 |
pass |
0 | 436 |
raise |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
437 |
return cursor |
1792 | 438 |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
439 |
def doexecmany(self, session, query, args): |
0 | 440 |
"""Execute a query. |
441 |
it's a function just so that it shows up in profiling |
|
442 |
""" |
|
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
443 |
if server.DEBUG & server.DBG_SQL: |
0 | 444 |
print 'execmany', query, 'with', len(args), 'arguments' |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
445 |
cursor = session.pool[self.uri] |
0 | 446 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
447 |
# str(query) to avoid error if it's an unicode string |
0 | 448 |
cursor.executemany(str(query), args) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
449 |
except Exception, ex: |
4682
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
450 |
if self.repo.config.mode != 'test': |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
451 |
# during test we get those message when trying to alter sqlite |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
452 |
# db schema |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
453 |
self.critical("sql many: %r\n args: %s\ndbms message: %r", |
4994901b7379
don't issue critical message when trying to alter sqlite db during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4517
diff
changeset
|
454 |
query, args, ex.args[0]) |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
455 |
try: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
456 |
session.pool.connection(self.uri).rollback() |
4692
11a040e2601c
[test] also hide this message during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4682
diff
changeset
|
457 |
if self.repo.config.mode != 'test': |
11a040e2601c
[test] also hide this message during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4682
diff
changeset
|
458 |
self.critical('transaction has been rollbacked') |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
459 |
except: |
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
460 |
pass |
0 | 461 |
raise |
1792 | 462 |
|
0 | 463 |
# short cut to method requiring advanced db helper usage ################## |
1792 | 464 |
|
0 | 465 |
def create_index(self, session, table, column, unique=False): |
466 |
cursor = LogCursor(session.pool[self.uri]) |
|
467 |
self.dbhelper.create_index(cursor, table, column, unique) |
|
1792 | 468 |
|
0 | 469 |
def drop_index(self, session, table, column, unique=False): |
470 |
cursor = LogCursor(session.pool[self.uri]) |
|
471 |
self.dbhelper.drop_index(cursor, table, column, unique) |
|
472 |
||
473 |
# system source interface ################################################# |
|
474 |
||
475 |
def eid_type_source(self, session, eid): |
|
476 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
477 |
sql = 'SELECT type, source, extid FROM entities WHERE eid=%s' % eid |
|
478 |
try: |
|
479 |
res = session.system_sql(sql).fetchone() |
|
480 |
except: |
|
1079
452cb76fe07a
backport typo fix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
973
diff
changeset
|
481 |
assert session.pool, 'session has no pool set' |
0 | 482 |
raise UnknownEid(eid) |
483 |
if res is None: |
|
484 |
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
|
485 |
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
|
486 |
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
|
487 |
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
|
488 |
res[-1] = b64decode(res[-1]) |
0 | 489 |
return res |
490 |
||
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
|
491 |
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
|
492 |
"""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
|
493 |
assert isinstance(extid, str) |
0 | 494 |
cursor = session.system_sql('SELECT eid FROM entities WHERE ' |
495 |
'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
|
496 |
{'x': b64encode(extid), 's': source.uri}) |
0 | 497 |
# XXX testing rowcount cause strange bug with sqlite, results are there |
498 |
# but rowcount is 0 |
|
1792 | 499 |
#if cursor.rowcount > 0: |
0 | 500 |
try: |
501 |
result = cursor.fetchone() |
|
502 |
if result: |
|
1954 | 503 |
return result[0] |
0 | 504 |
except: |
505 |
pass |
|
506 |
return None |
|
1792 | 507 |
|
0 | 508 |
def temp_table_def(self, selected, sol, table): |
509 |
return make_schema(selected, sol, table, self.dbhelper.TYPE_MAPPING) |
|
510 |
||
511 |
def create_temp_table(self, session, table, schema): |
|
512 |
# we don't want on commit drop, this may cause problem when |
|
513 |
# running with an ldap source, and table will be deleted manually any way |
|
514 |
# on commit |
|
515 |
sql = self.dbhelper.sql_temporary_table(table, schema, False) |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2072
diff
changeset
|
516 |
self.doexec(session, sql) |
1792 | 517 |
|
0 | 518 |
def create_eid(self, session): |
519 |
self._eid_creation_lock.acquire() |
|
520 |
try: |
|
521 |
for sql in self.dbhelper.sqls_increment_sequence('entities_id_seq'): |
|
2310
80fcdfbb8eed
oops, needs the cursor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
522 |
cursor = self.doexec(session, sql) |
0 | 523 |
return cursor.fetchone()[0] |
524 |
finally: |
|
525 |
self._eid_creation_lock.release() |
|
526 |
||
527 |
def add_info(self, session, entity, source, extid=None): |
|
528 |
"""add type and source info for an eid into the system table""" |
|
529 |
# 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
|
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) |
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) |
3399
2b84f4adb6f8
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
533 |
attrs = {'type': entity.__regid__, 'eid': entity.eid, 'extid': extid, |
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
|
534 |
'source': source.uri, 'mtime': datetime.now()} |
0 | 535 |
session.system_sql(self.sqlgen.insert('entities', attrs), attrs) |
536 |
||
537 |
def delete_info(self, session, eid, etype, uri, extid): |
|
538 |
"""delete system information on deletion of an entity by transfering |
|
539 |
record from the entities table to the deleted_entities table |
|
540 |
""" |
|
541 |
attrs = {'eid': eid} |
|
542 |
session.system_sql(self.sqlgen.delete('entities', attrs), attrs) |
|
2955
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
543 |
if extid is not None: |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
544 |
assert isinstance(extid, str), type(extid) |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
545 |
extid = b64encode(extid) |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
546 |
attrs = {'type': etype, 'eid': eid, 'extid': extid, |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
547 |
'source': uri, 'dtime': datetime.now()} |
6bb5025c9fc7
remove some pretty old deprecation code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
548 |
session.system_sql(self.sqlgen.insert('deleted_entities', attrs), attrs) |
1792 | 549 |
|
0 | 550 |
def fti_unindex_entity(self, session, eid): |
551 |
"""remove text content for entity with the given eid from the full text |
|
552 |
index |
|
553 |
""" |
|
554 |
try: |
|
555 |
self.indexer.cursor_unindex_object(eid, session.pool['system']) |
|
3497
63b9541dd36f
[fti] don't catch BaseException subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
556 |
except Exception: # let KeyboardInterrupt / SystemExit propagate |
0 | 557 |
if self.indexer is not None: |
558 |
self.exception('error while unindexing %s', eid) |
|
1792 | 559 |
|
0 | 560 |
def fti_index_entity(self, session, entity): |
561 |
"""add text content of a created/modified entity to the full text index |
|
562 |
""" |
|
4515
e8835c68f581
turn down some info logs into debug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
563 |
self.debug('reindexing %r', entity.eid) |
0 | 564 |
try: |
565 |
self.indexer.cursor_reindex_object(entity.eid, entity, |
|
566 |
session.pool['system']) |
|
3497
63b9541dd36f
[fti] don't catch BaseException subclasses
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3240
diff
changeset
|
567 |
except Exception: # let KeyboardInterrupt / SystemExit propagate |
0 | 568 |
if self.indexer is not None: |
569 |
self.exception('error while reindexing %s', entity) |
|
570 |
# update entities.mtime |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
973
diff
changeset
|
571 |
attrs = {'eid': entity.eid, 'mtime': datetime.now()} |
0 | 572 |
session.system_sql(self.sqlgen.update('entities', attrs, ['eid']), attrs) |
1792 | 573 |
|
0 | 574 |
def modified_entities(self, session, etypes, mtime): |
575 |
"""return a 2-uple: |
|
576 |
* list of (etype, eid) of entities of the given types which have been |
|
577 |
modified since the given timestamp (actually entities whose full text |
|
578 |
index content has changed) |
|
579 |
* list of (etype, eid) of entities of the given types which have been |
|
580 |
deleted since the given timestamp |
|
581 |
""" |
|
582 |
modsql = _modified_sql('entities', etypes) |
|
583 |
cursor = session.system_sql(modsql, {'time': mtime}) |
|
584 |
modentities = cursor.fetchall() |
|
585 |
delsql = _modified_sql('deleted_entities', etypes) |
|
586 |
cursor = session.system_sql(delsql, {'time': mtime}) |
|
587 |
delentities = cursor.fetchall() |
|
588 |
return modentities, delentities |
|
589 |
||
590 |
||
591 |
def sql_schema(driver): |
|
592 |
helper = get_adv_func_helper(driver) |
|
4198
8d644eb862b9
TYPE_MAPPING expected to be a mapping from yams type to db type, and timestamp is the Datetime yams type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4195
diff
changeset
|
593 |
tstamp_col_type = helper.TYPE_MAPPING['Datetime'] |
0 | 594 |
schema = """ |
595 |
/* Create the repository's system database */ |
|
596 |
||
597 |
%s |
|
598 |
||
599 |
CREATE TABLE entities ( |
|
600 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
601 |
type VARCHAR(64) NOT NULL, |
|
602 |
source VARCHAR(64) NOT NULL, |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
603 |
mtime %s NOT NULL, |
0 | 604 |
extid VARCHAR(256) |
605 |
); |
|
606 |
CREATE INDEX entities_type_idx ON entities(type); |
|
607 |
CREATE INDEX entities_mtime_idx ON entities(mtime); |
|
608 |
CREATE INDEX entities_extid_idx ON entities(extid); |
|
609 |
||
610 |
CREATE TABLE deleted_entities ( |
|
611 |
eid INTEGER PRIMARY KEY NOT NULL, |
|
612 |
type VARCHAR(64) NOT NULL, |
|
613 |
source VARCHAR(64) NOT NULL, |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
614 |
dtime %s NOT NULL, |
0 | 615 |
extid VARCHAR(256) |
616 |
); |
|
617 |
CREATE INDEX deleted_entities_type_idx ON deleted_entities(type); |
|
618 |
CREATE INDEX deleted_entities_dtime_idx ON deleted_entities(dtime); |
|
619 |
CREATE INDEX deleted_entities_extid_idx ON deleted_entities(extid); |
|
4113
986fc01be83c
TIMESTAMP column type has a special meaning for SQLServer,
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3958
diff
changeset
|
620 |
""" % (helper.sql_create_sequence('entities_id_seq'), tstamp_col_type, tstamp_col_type) |
0 | 621 |
return schema |
622 |
||
623 |
||
624 |
def sql_drop_schema(driver): |
|
625 |
helper = get_adv_func_helper(driver) |
|
626 |
return """ |
|
627 |
%s |
|
628 |
DROP TABLE entities; |
|
629 |
DROP TABLE deleted_entities; |
|
630 |
""" % helper.sql_drop_sequence('entities_id_seq') |
|
631 |
||
632 |
||
633 |
def grant_schema(user, set_owner=True): |
|
634 |
result = '' |
|
635 |
if set_owner: |
|
636 |
result = 'ALTER TABLE entities OWNER TO %s;\n' % user |
|
637 |
result += 'ALTER TABLE deleted_entities OWNER TO %s;\n' % user |
|
638 |
result += 'ALTER TABLE entities_id_seq OWNER TO %s;\n' % user |
|
639 |
result += 'GRANT ALL ON entities TO %s;\n' % user |
|
640 |
result += 'GRANT ALL ON deleted_entities TO %s;\n' % user |
|
641 |
result += 'GRANT ALL ON entities_id_seq TO %s;\n' % user |
|
642 |
return result |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
643 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
644 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
645 |
class BaseAuthentifier(object): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
646 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
647 |
def __init__(self, source=None): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
648 |
self.source = source |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
649 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
650 |
def set_schema(self, schema): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
651 |
"""set the instance'schema""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
652 |
pass |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
653 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
654 |
class LoginPasswordAuthentifier(BaseAuthentifier): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
655 |
passwd_rql = "Any P WHERE X is CWUser, X login %(login)s, X upassword P" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
656 |
auth_rql = "Any X WHERE X is CWUser, X login %(login)s, X upassword %(pwd)s" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
657 |
_sols = ({'X': 'CWUser', 'P': 'Password'},) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
658 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
659 |
def set_schema(self, schema): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
660 |
"""set the instance'schema""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
661 |
if 'CWUser' in schema: # probably an empty schema if not true... |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
662 |
# rql syntax trees used to authenticate users |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
663 |
self._passwd_rqlst = self.source.compile_rql(self.passwd_rql, self._sols) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
664 |
self._auth_rqlst = self.source.compile_rql(self.auth_rql, self._sols) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
665 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
666 |
def authenticate(self, session, login, password=None, **kwargs): |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
667 |
"""return CWUser eid for the given login/password if this account is |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
668 |
defined in this source, else raise `AuthenticationError` |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
669 |
|
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
670 |
two queries are needed since passwords are stored crypted, so we have |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
671 |
to fetch the salt first |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
672 |
""" |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
673 |
args = {'login': login, 'pwd' : password} |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
674 |
if password is not None: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
675 |
rset = self.source.syntax_tree_search(session, self._passwd_rqlst, args) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
676 |
try: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
677 |
pwd = rset[0][0] |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
678 |
except IndexError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
679 |
raise AuthenticationError('bad login') |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
680 |
# passwords are stored using the Bytes type, so we get a StringIO |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
681 |
if pwd is not None: |
4204 | 682 |
args['pwd'] = Binary(crypt_password(password, pwd.getvalue()[:2])) |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
683 |
# get eid from login and (crypted) password |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
684 |
rset = self.source.syntax_tree_search(session, self._auth_rqlst, args) |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
685 |
try: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
686 |
return rset[0][0] |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
687 |
except IndexError: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
688 |
raise AuthenticationError('bad password') |