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