author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 21 Jul 2009 23:09:27 +0200 | |
branch | stable |
changeset 2413 | b1bea4cab594 |
parent 2306 | 95da5d9f0870 |
child 2493 | 9806571ea790 |
permissions | -rw-r--r-- |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
1 |
"""provide an abstract class for external sources using a sqlite database helper |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
2 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
3 |
:organization: Logilab |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1482
diff
changeset
|
4 |
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
5 |
: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:
1482
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
7 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
9 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
10 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
11 |
from os.path import join, exists |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
12 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
13 |
from cubicweb import server |
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:
1137
diff
changeset
|
14 |
from cubicweb.server.sqlutils import SQL_PREFIX, sqlexec, SQLAdapterMixIn |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
15 |
from cubicweb.server.sources import AbstractSource, native |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
16 |
from cubicweb.server.sources.rql2sql import SQLGenerator |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
17 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
18 |
class ConnectionWrapper(object): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
19 |
def __init__(self, source=None): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
20 |
self.source = source |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
21 |
self._cnx = None |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
22 |
|
2067
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
23 |
@property |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
24 |
def logged_user(self): |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
25 |
if self._cnx is None: |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
26 |
self._cnx = self.source._sqlcnx |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
27 |
return self._cnx.logged_user |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
28 |
|
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
29 |
def cursor(self): |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
30 |
if self._cnx is None: |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
31 |
self._cnx = self.source._sqlcnx |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
32 |
return self._cnx.cursor() |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
33 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
34 |
def commit(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
35 |
if self._cnx is not None: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
36 |
self._cnx.commit() |
1482 | 37 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
38 |
def rollback(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
39 |
if self._cnx is not None: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
40 |
self._cnx.rollback() |
1482 | 41 |
|
2067
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
42 |
def close(self): |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
43 |
if self._cnx is not None: |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
44 |
self._cnx.close() |
668cc93f8578
need proper logged_user and close method to be usable as native source connection wrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2062
diff
changeset
|
45 |
self._cnx = None |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
46 |
|
1482 | 47 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
48 |
class SQLiteAbstractSource(AbstractSource): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
49 |
"""an abstract class for external sources using a sqlite database helper |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
50 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
51 |
sqlgen_class = SQLGenerator |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
52 |
@classmethod |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
53 |
def set_nonsystem_types(cls): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
54 |
# those entities are only in this source, we don't want them in the |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
55 |
# system source |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
56 |
for etype in cls.support_entities: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
57 |
native.NONSYSTEM_ETYPES.add(etype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
58 |
for rtype in cls.support_relations: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
59 |
native.NONSYSTEM_RELATIONS.add(rtype) |
1482 | 60 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
61 |
options = ( |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
62 |
('helper-db-path', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
63 |
{'type' : 'string', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
64 |
'default': None, |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
65 |
'help': 'path to the sqlite database file used to do queries on the \ |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
66 |
repository.', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
67 |
'inputlevel': 2, |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
68 |
}), |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
69 |
) |
1482 | 70 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
71 |
def __init__(self, repo, appschema, source_config, *args, **kwargs): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
72 |
# the helper db is used to easy querying and will store everything but |
1482 | 73 |
# actual file content |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
74 |
dbpath = source_config.get('helper-db-path') |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
75 |
if dbpath is None: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
76 |
dbpath = join(repo.config.appdatahome, |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
77 |
'%(uri)s.sqlite' % source_config) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
78 |
self.dbpath = dbpath |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
79 |
self.sqladapter = SQLAdapterMixIn({'db-driver': 'sqlite', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
80 |
'db-name': dbpath}) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
81 |
# those attributes have to be initialized before ancestor's __init__ |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
82 |
# which will call set_schema |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
83 |
self._need_sql_create = not exists(dbpath) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
84 |
self._need_full_import = self._need_sql_create |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
85 |
AbstractSource.__init__(self, repo, appschema, source_config, |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
86 |
*args, **kwargs) |
1482 | 87 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
88 |
@property |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
89 |
def _sqlcnx(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
90 |
# XXX: sqlite connections can only be used in the same thread, so |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
91 |
# create a new one each time necessary. If it appears to be time |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
92 |
# consuming, find another way |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
93 |
return self.sqladapter.get_connection() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
94 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
95 |
def _is_schema_complete(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
96 |
for etype in self.support_entities: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
97 |
if not etype in self.schema: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
98 |
self.warning('not ready to generate %s database, %s support missing from schema', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
99 |
self.uri, etype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
100 |
return False |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
101 |
for rtype in self.support_relations: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
102 |
if not rtype in self.schema: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
103 |
self.warning('not ready to generate %s database, %s support missing from schema', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
104 |
self.uri, rtype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
105 |
return False |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
106 |
return True |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
107 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
108 |
def _create_database(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
109 |
from yams.schema2sql import eschema2sql, rschema2sql |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
110 |
from cubicweb.toolsutils import restrict_perms_to_user |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
111 |
self.warning('initializing sqlite database for %s source' % self.uri) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
112 |
cnx = self._sqlcnx |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
113 |
cu = cnx.cursor() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
114 |
schema = self.schema |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
115 |
for etype in self.support_entities: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
116 |
eschema = schema.eschema(etype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
117 |
createsqls = eschema2sql(self.sqladapter.dbhelper, eschema, |
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:
1137
diff
changeset
|
118 |
skip_relations=('data',), prefix=SQL_PREFIX) |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
119 |
sqlexec(createsqls, cu, withpb=False) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
120 |
for rtype in self.support_relations: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
121 |
rschema = schema.rschema(rtype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
122 |
if not rschema.inlined: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
123 |
sqlexec(rschema2sql(rschema), cu, withpb=False) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
124 |
cnx.commit() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
125 |
cnx.close() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
126 |
self._need_sql_create = False |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
127 |
if self.repo.config['uid']: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
128 |
from logilab.common.shellutils import chown |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
129 |
# database file must be owned by the uid of the server process |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
130 |
self.warning('set %s as owner of the database file', |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
131 |
self.repo.config['uid']) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
132 |
chown(self.dbpath, self.repo.config['uid']) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
133 |
restrict_perms_to_user(self.dbpath, self.info) |
1482 | 134 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
135 |
def set_schema(self, schema): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
136 |
super(SQLiteAbstractSource, self).set_schema(schema) |
1137
9ce0ac82f94f
ensure we have a path set before creating the database
sylvain.thenault@logilab.fr
parents:
257
diff
changeset
|
137 |
if self._need_sql_create and self._is_schema_complete() and self.dbpath: |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
138 |
self._create_database() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
139 |
self.rqlsqlgen = self.sqlgen_class(schema, self.sqladapter.dbhelper) |
1482 | 140 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
141 |
def get_connection(self): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
142 |
return ConnectionWrapper(self) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
143 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
144 |
def check_connection(self, cnx): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
145 |
"""check connection validity, return None if the connection is still valid |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
146 |
else a new connection (called when the pool using the given connection is |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
147 |
being attached to a session) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
148 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
149 |
always return the connection to reset eventually cached cursor |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
150 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
151 |
return cnx |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
152 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
153 |
def pool_reset(self, cnx): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
154 |
"""the pool using the given connection is being reseted from its current |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
155 |
attached session: release the connection lock if the connection wrapper |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
156 |
has a connection set |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
157 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
158 |
if cnx._cnx is not None: |
2053
fb156d69bfd9
change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
159 |
cnx._cnx.close() |
fb156d69bfd9
change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
160 |
# reset _cnx to ensure next thread using cnx will get a new |
fb156d69bfd9
change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
161 |
# connection |
fb156d69bfd9
change extlite connection handling: connection may not be shared among threads but it's fine to have multiple connections open in several threads (as demonstrated by unittest_extlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
162 |
cnx._cnx = None |
1482 | 163 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
164 |
def syntax_tree_search(self, session, union, |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
165 |
args=None, cachekey=None, varmap=None, debug=0): |
1482 | 166 |
"""return result from this source for a rql query (actually from a rql |
167 |
syntax tree and a solution dictionary mapping each used variable to a |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
168 |
possible type). If cachekey is given, the query necessary to fetch the |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
169 |
results (but not the results themselves) may be cached using this key. |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
170 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
171 |
if self._need_sql_create: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
172 |
return [] |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
173 |
sql, query_args = self.rqlsqlgen.generate(union, args) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
174 |
if server.DEBUG: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
175 |
print self.uri, 'SOURCE RQL', union.as_string() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
176 |
args = self.sqladapter.merge_args(args, 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:
2101
diff
changeset
|
177 |
res = self.sqladapter.process_result(self.doexec(session, sql, args)) |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
178 |
if server.DEBUG: |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
179 |
print '------>', res |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
180 |
return res |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
181 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
182 |
def local_add_entity(self, session, entity): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
183 |
"""insert the entity in the local database. |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
184 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
185 |
This is not provided as add_entity implementation since usually source |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
186 |
don't want to simply do this, so let raise NotImplementedError and the |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
187 |
source implementor may use this method if necessary |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
188 |
""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
189 |
attrs = self.sqladapter.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:
1137
diff
changeset
|
190 |
sql = self.sqladapter.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:
2101
diff
changeset
|
191 |
self.doexec(session, sql, attrs) |
1482 | 192 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
193 |
def add_entity(self, session, entity): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
194 |
"""add a new entity to the source""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
195 |
raise NotImplementedError() |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
196 |
|
983
cf1caf460081
allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents:
257
diff
changeset
|
197 |
def local_update_entity(self, session, entity, attrs=None): |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
198 |
"""update an entity in the source |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
199 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
200 |
This is not provided as update_entity implementation since usually |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
201 |
source don't want to simply do this, so let raise NotImplementedError |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
202 |
and the source implementor may use this method if necessary |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
203 |
""" |
983
cf1caf460081
allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents:
257
diff
changeset
|
204 |
if attrs is None: |
cf1caf460081
allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents:
257
diff
changeset
|
205 |
attrs = self.sqladapter.preprocess_entity(entity) |
1482 | 206 |
sql = self.sqladapter.sqlgen.update(SQL_PREFIX + str(entity.e_schema), |
207 |
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:
2101
diff
changeset
|
208 |
self.doexec(session, sql, attrs) |
1482 | 209 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
210 |
def update_entity(self, session, entity): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
211 |
"""update an entity in the source""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
212 |
raise NotImplementedError() |
1482 | 213 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
214 |
def delete_entity(self, session, etype, eid): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
215 |
"""delete an entity from the source |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
216 |
|
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
217 |
this is not deleting a file in the svn but deleting entities from the |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
218 |
source. Main usage is to delete repository content when a Repository |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
219 |
entity is deleted. |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
220 |
""" |
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:
1137
diff
changeset
|
221 |
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:
1137
diff
changeset
|
222 |
sql = self.sqladapter.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:
2101
diff
changeset
|
223 |
self.doexec(session, sql, attrs) |
1482 | 224 |
|
2062
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
225 |
def local_add_relation(self, session, subject, rtype, object): |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
226 |
"""add a relation to the source |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
227 |
|
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
228 |
This is not provided as add_relation implementation since usually |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
229 |
source don't want to simply do this, so let raise NotImplementedError |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
230 |
and the source implementor may use this method if necessary |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
231 |
""" |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
232 |
attrs = {'eid_from': subject, 'eid_to': object} |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
233 |
sql = self.sqladapter.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:
2101
diff
changeset
|
234 |
self.doexec(session, sql, attrs) |
2062
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
235 |
|
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
236 |
def add_relation(self, session, subject, rtype, object): |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
237 |
"""add a relation to the source""" |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
238 |
raise NotImplementedError() |
20f18837107c
provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2053
diff
changeset
|
239 |
|
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
240 |
def delete_relation(self, session, subject, rtype, object): |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
241 |
"""delete a relation from the source""" |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
242 |
rschema = self.schema.rschema(rtype) |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
243 |
if rschema.inlined: |
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2068
diff
changeset
|
244 |
if subject in session.transaction_data.get('pendingeids', ()): |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
245 |
return |
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:
1137
diff
changeset
|
246 |
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:
1137
diff
changeset
|
247 |
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:
1137
diff
changeset
|
248 |
sql = 'UPDATE %s SET %s=NULL WHERE %seid=%%(eid)s' % (table, column, SQL_PREFIX) |
257
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
249 |
attrs = {'eid' : subject} |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
250 |
else: |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
251 |
attrs = {'eid_from': subject, 'eid_to': object} |
4c7d3af7e94d
restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
252 |
sql = self.sqladapter.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:
2101
diff
changeset
|
253 |
self.doexec(session, sql, attrs) |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
254 |
|
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:
2101
diff
changeset
|
255 |
def doexec(self, session, query, args=None): |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
256 |
"""Execute a query. |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
257 |
it's a function just so that it shows up in profiling |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
258 |
""" |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
259 |
if server.DEBUG: |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
260 |
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:
2101
diff
changeset
|
261 |
cursor = session.pool[self.uri] |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
262 |
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:
2101
diff
changeset
|
263 |
# str(query) to avoid error if it's an unicode string |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
264 |
cursor.execute(str(query), args) |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
265 |
except Exception, ex: |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
266 |
self.critical("sql: %r\n args: %s\ndbms message: %r", |
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
267 |
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:
2101
diff
changeset
|
268 |
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:
2101
diff
changeset
|
269 |
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:
2101
diff
changeset
|
270 |
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:
2101
diff
changeset
|
271 |
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:
2101
diff
changeset
|
272 |
pass |
2068
3d1961426bed
log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2067
diff
changeset
|
273 |
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:
2101
diff
changeset
|
274 |
return cursor |