server/sources/extlite.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 11 Aug 2009 17:04:59 +0200
changeset 2767 58c519e5a31f
parent 2625 d6012db7b93e
child 2771 8074dd88e21b
permissions -rw-r--r--
[debug] cnx commit/rollback on DBG_RQL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
    14
from cubicweb.server.sqlutils import (SQL_PREFIX, SQLAdapterMixIn, sqlexec,
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
    15
                                      sql_source_backup, sql_source_restore)
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    16
from cubicweb.server.sources import native, rql2sql
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    17
from cubicweb.server.sources import AbstractSource, dbg_st_search, dbg_results
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
class ConnectionWrapper(object):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
    def __init__(self, source=None):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
        self.source = source
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
        self._cnx = None
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
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
    24
    @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
    25
    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
    26
        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
    27
            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
    28
        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
    29
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
    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
    31
        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
    32
            self._cnx = self.source._sqlcnx
2619
535bc210f799 debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2611
diff changeset
    33
            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
    34
                print 'sql cnx OPEN', self._cnx
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
    35
        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
    36
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    def commit(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
        if self._cnx is not None:
2767
58c519e5a31f [debug] cnx commit/rollback on DBG_RQL
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2625
diff changeset
    39
            if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    40
                print 'sql cnx COMMIT', self._cnx
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    41
            self._cnx.commit()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    42
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
    def rollback(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
        if self._cnx is not None:
2767
58c519e5a31f [debug] cnx commit/rollback on DBG_RQL
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2625
diff changeset
    45
            if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    46
                print 'sql cnx ROLLBACK', self._cnx
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
            self._cnx.rollback()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    48
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
    49
    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
    50
        if self._cnx is not None:
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    51
            if server.DEBUG & server.DBG_SQL:
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    52
                print 'sql cnx CLOSE', self._cnx
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
    53
            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
    54
            self._cnx = None
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    56
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
class SQLiteAbstractSource(AbstractSource):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
    """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
    59
    """
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
    60
    sqlgen_class = rql2sql.SQLGenerator
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
    @classmethod
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
    def set_nonsystem_types(cls):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
        # 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
    64
        # system source
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
        for etype in cls.support_entities:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
            native.NONSYSTEM_ETYPES.add(etype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
        for rtype in cls.support_relations:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
            native.NONSYSTEM_RELATIONS.add(rtype)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    69
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
    options = (
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
        ('helper-db-path',
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
         {'type' : 'string',
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
          'default': None,
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
          '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
    75
repository.',
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
          'inputlevel': 2,
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
          }),
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
    )
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    79
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
    def __init__(self, repo, appschema, source_config, *args, **kwargs):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
        # the helper db is used to easy querying and will store everything but
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    82
        # actual file content
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
        dbpath = source_config.get('helper-db-path')
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
        if dbpath is None:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
            dbpath = join(repo.config.appdatahome,
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
                          '%(uri)s.sqlite' % source_config)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
        self.dbpath = dbpath
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
        self.sqladapter = SQLAdapterMixIn({'db-driver': 'sqlite',
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
                                           'db-name': dbpath})
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
        # those attributes have to be initialized before ancestor's __init__
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
        # which will call set_schema
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
        self._need_sql_create = not exists(dbpath)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
        self._need_full_import = self._need_sql_create
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
        AbstractSource.__init__(self, repo, appschema, source_config,
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
                                *args, **kwargs)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
    96
2493
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
    97
    def backup(self, confirm, backupfile=None, timestamp=None, askconfirm=False):
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
    98
        """method called to create a backup of source's data"""
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
    99
        backupfile = self.backup_file(backupfile, timestamp)
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   100
        sql_source_backup(self, self.sqladapter, confirm, backupfile,
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   101
                          askconfirm)
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   102
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   103
    def restore(self, confirm, backupfile=None, timestamp=None, drop=True,
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   104
               askconfirm=False):
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   105
        """method called to restore a backup of source's data"""
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   106
        backupfile = self.backup_file(backupfile, timestamp)
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   107
        sql_source_restore(self, self.sqladapter, confirm, backupfile, drop,
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   108
                           askconfirm)
9806571ea790 major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2306
diff changeset
   109
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   110
    @property
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   111
    def _sqlcnx(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   112
        # 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
   113
        #      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
   114
        #      consuming, find another way
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   115
        return self.sqladapter.get_connection()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
    def _is_schema_complete(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
        for etype in self.support_entities:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   119
            if not etype in self.schema:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
                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
   121
                             self.uri, etype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
                return False
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
        for rtype in self.support_relations:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
            if not rtype in self.schema:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
                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
   126
                             self.uri, rtype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
                return False
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
        return True
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
    def _create_database(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
        from yams.schema2sql import eschema2sql, rschema2sql
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
        from cubicweb.toolsutils import restrict_perms_to_user
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   133
        self.warning('initializing sqlite database for %s source' % self.uri)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
        cnx = self._sqlcnx
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
        cu = cnx.cursor()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   136
        schema = self.schema
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
        for etype in self.support_entities:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
            eschema = schema.eschema(etype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   139
            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
   140
                                     skip_relations=('data',), prefix=SQL_PREFIX)
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
            sqlexec(createsqls, cu, withpb=False)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
        for rtype in self.support_relations:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
            rschema = schema.rschema(rtype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
            if not rschema.inlined:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
                sqlexec(rschema2sql(rschema), cu, withpb=False)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
        cnx.commit()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
        cnx.close()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
        self._need_sql_create = False
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
        if self.repo.config['uid']:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
            from logilab.common.shellutils import chown
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
            # 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
   152
            self.warning('set %s as owner of the database file',
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
                         self.repo.config['uid'])
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
            chown(self.dbpath, self.repo.config['uid'])
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   155
        restrict_perms_to_user(self.dbpath, self.info)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   156
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
    def set_schema(self, schema):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
        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
   159
        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
   160
            self._create_database()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
        self.rqlsqlgen = self.sqlgen_class(schema, self.sqladapter.dbhelper)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   162
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
    def get_connection(self):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
        return ConnectionWrapper(self)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   166
    def check_connection(self, cnx):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
        """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
   168
        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
   169
        being attached to a session)
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
        always return the connection to reset eventually cached cursor
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
        """
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
        return cnx
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   174
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   175
    def pool_reset(self, cnx):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
        """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
   177
        attached session: release the connection lock if the connection wrapper
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
        has a connection set
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
        """
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
   180
        # reset _cnx to ensure next thread using cnx will get a new
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
   181
        # connection
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
   182
        cnx.close()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   183
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   184
    def syntax_tree_search(self, session, union, args=None, cachekey=None,
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   185
                           varmap=None):
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   186
        """return result from this source for a rql query (actually from a rql
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   187
        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
   188
        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
   189
        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
   190
        """
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   191
        if self._need_sql_create:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   192
            return []
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   193
        assert dbg_st_search(self.uri, union, varmap, args, cachekey)
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   194
        sql, query_args = self.rqlsqlgen.generate(union, args)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   195
        args = self.sqladapter.merge_args(args, query_args)
2625
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   196
        results = self.sqladapter.process_result(self.doexec(session, sql, args))
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   197
        assert dbg_results(results)
d6012db7b93e R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2620
diff changeset
   198
        return results
257
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
    def local_add_entity(self, session, entity):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   201
        """insert the entity in the local database.
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   202
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   203
        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
   204
        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
   205
        source implementor may use this method if necessary
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   206
        """
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   207
        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
   208
        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
   209
        self.doexec(session, sql, attrs)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   210
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   211
    def add_entity(self, session, entity):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   212
        """add a new entity to the source"""
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   213
        raise NotImplementedError()
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   214
983
cf1caf460081 allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents: 257
diff changeset
   215
    def local_update_entity(self, session, entity, attrs=None):
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   216
        """update an entity in the source
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   217
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   218
        This is not provided as update_entity implementation since usually
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   219
        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
   220
        and the source implementor may use this method if necessary
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   221
        """
983
cf1caf460081 allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents: 257
diff changeset
   222
        if attrs is None:
cf1caf460081 allow to pass attrs as argument explicitly
sylvain.thenault@logilab.fr
parents: 257
diff changeset
   223
            attrs = self.sqladapter.preprocess_entity(entity)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   224
        sql = self.sqladapter.sqlgen.update(SQL_PREFIX + str(entity.e_schema),
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   225
                                            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
   226
        self.doexec(session, sql, attrs)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   227
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   228
    def update_entity(self, session, entity):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   229
        """update an entity in the source"""
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   230
        raise NotImplementedError()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   231
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   232
    def delete_entity(self, session, etype, eid):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   233
        """delete an entity from the source
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   234
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   235
        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
   236
        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
   237
        entity is deleted.
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   238
        """
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
   239
        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
   240
        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
   241
        self.doexec(session, sql, attrs)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1409
diff changeset
   242
2062
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   243
    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
   244
        """add a relation to the source
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   245
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   246
        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
   247
        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
   248
        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
   249
        """
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   250
        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
   251
        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
   252
        self.doexec(session, sql, attrs)
2062
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   253
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   254
    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
   255
        """add a relation to the source"""
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   256
        raise NotImplementedError()
20f18837107c provide local_add_relation / add_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2053
diff changeset
   257
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   258
    def delete_relation(self, session, subject, rtype, object):
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   259
        """delete a relation from the source"""
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   260
        rschema = self.schema.rschema(rtype)
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   261
        if rschema.inlined:
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2068
diff changeset
   262
            if subject in session.transaction_data.get('pendingeids', ()):
257
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   263
                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
   264
            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
   265
            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
   266
            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
   267
            attrs = {'eid' : subject}
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   268
        else:
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   269
            attrs = {'eid_from': subject, 'eid_to': object}
4c7d3af7e94d restore multi-sources capabilities
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   270
            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
   271
        self.doexec(session, sql, attrs)
2068
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   272
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
   273
    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
   274
        """Execute a query.
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   275
        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
   276
        """
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   277
        if server.DEBUG:
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   278
            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
   279
        cursor = session.pool[self.uri]
2068
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   280
        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
   281
            # 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
   282
            cursor.execute(str(query), args)
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   283
        except Exception, ex:
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   284
            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
   285
                          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
   286
            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
   287
                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
   288
                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
   289
            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
   290
                pass
2068
3d1961426bed log sql queries in debug mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2067
diff changeset
   291
            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
   292
        return cursor