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