server/sources/extlite.py
changeset 2625 d6012db7b93e
parent 2620 de68f84b8f54
child 2759 23d7a75693f8
child 2767 58c519e5a31f
equal deleted inserted replaced
2624:c0b5b189190e 2625:d6012db7b93e
    11 from os.path import join, exists
    11 from os.path import join, exists
    12 
    12 
    13 from cubicweb import server
    13 from cubicweb import server
    14 from cubicweb.server.sqlutils import (SQL_PREFIX, SQLAdapterMixIn, sqlexec,
    14 from cubicweb.server.sqlutils import (SQL_PREFIX, SQLAdapterMixIn, sqlexec,
    15                                       sql_source_backup, sql_source_restore)
    15                                       sql_source_backup, sql_source_restore)
    16 from cubicweb.server.sources import AbstractSource, native
    16 from cubicweb.server.sources import native, rql2sql
    17 from cubicweb.server.sources.rql2sql import SQLGenerator
    17 from cubicweb.server.sources import AbstractSource, dbg_st_search, dbg_results
    18 
    18 
    19 class ConnectionWrapper(object):
    19 class ConnectionWrapper(object):
    20     def __init__(self, source=None):
    20     def __init__(self, source=None):
    21         self.source = source
    21         self.source = source
    22         self._cnx = None
    22         self._cnx = None
    29 
    29 
    30     def cursor(self):
    30     def cursor(self):
    31         if self._cnx is None:
    31         if self._cnx is None:
    32             self._cnx = self.source._sqlcnx
    32             self._cnx = self.source._sqlcnx
    33             if server.DEBUG & server.DBG_SQL:
    33             if server.DEBUG & server.DBG_SQL:
    34                 print 'sql cnx open', self._cnx
    34                 print 'sql cnx OPEN', self._cnx
    35         return self._cnx.cursor()
    35         return self._cnx.cursor()
    36 
    36 
    37     def commit(self):
    37     def commit(self):
    38         if self._cnx is not None:
    38         if self._cnx is not None:
    39             if server.DEBUG & server.DBG_SQL:
    39             if server.DEBUG & server.DBG_SQL:
    40                 print 'sql cnx commit', self._cnx
    40                 print 'sql cnx COMMIT', self._cnx
       
    41             self._cnx.commit()
    41 
    42 
    42     def rollback(self):
    43     def rollback(self):
    43         if self._cnx is not None:
    44         if self._cnx is not None:
    44             if server.DEBUG & server.DBG_SQL:
    45             if server.DEBUG & server.DBG_SQL:
    45                 print 'sql cnx rollback', self._cnx
    46                 print 'sql cnx ROLLBACK', self._cnx
    46             self._cnx.rollback()
    47             self._cnx.rollback()
    47 
    48 
    48     def close(self):
    49     def close(self):
    49         if self._cnx is not None:
    50         if self._cnx is not None:
       
    51             if server.DEBUG & server.DBG_SQL:
       
    52                 print 'sql cnx CLOSE', self._cnx
    50             self._cnx.close()
    53             self._cnx.close()
    51             if server.DEBUG & server.DBG_SQL:
       
    52                 print 'sql cnx close', self._cnx
       
    53             self._cnx = None
    54             self._cnx = None
    54 
    55 
    55 
    56 
    56 class SQLiteAbstractSource(AbstractSource):
    57 class SQLiteAbstractSource(AbstractSource):
    57     """an abstract class for external sources using a sqlite database helper
    58     """an abstract class for external sources using a sqlite database helper
    58     """
    59     """
    59     sqlgen_class = SQLGenerator
    60     sqlgen_class = rql2sql.SQLGenerator
    60     @classmethod
    61     @classmethod
    61     def set_nonsystem_types(cls):
    62     def set_nonsystem_types(cls):
    62         # those entities are only in this source, we don't want them in the
    63         # those entities are only in this source, we don't want them in the
    63         # system source
    64         # system source
    64         for etype in cls.support_entities:
    65         for etype in cls.support_entities:
   178         """
   179         """
   179         # reset _cnx to ensure next thread using cnx will get a new
   180         # reset _cnx to ensure next thread using cnx will get a new
   180         # connection
   181         # connection
   181         cnx.close()
   182         cnx.close()
   182 
   183 
   183     def syntax_tree_search(self, session, union,
   184     def syntax_tree_search(self, session, union, args=None, cachekey=None,
   184                            args=None, cachekey=None, varmap=None, debug=0):
   185                            varmap=None):
   185         """return result from this source for a rql query (actually from a rql
   186         """return result from this source for a rql query (actually from a rql
   186         syntax tree and a solution dictionary mapping each used variable to a
   187         syntax tree and a solution dictionary mapping each used variable to a
   187         possible type). If cachekey is given, the query necessary to fetch the
   188         possible type). If cachekey is given, the query necessary to fetch the
   188         results (but not the results themselves) may be cached using this key.
   189         results (but not the results themselves) may be cached using this key.
   189         """
   190         """
   190         if self._need_sql_create:
   191         if self._need_sql_create:
   191             return []
   192             return []
       
   193         assert dbg_st_search(self.uri, union, varmap, args, cachekey)
   192         sql, query_args = self.rqlsqlgen.generate(union, args)
   194         sql, query_args = self.rqlsqlgen.generate(union, args)
   193         if server.DEBUG & server.DBG_RQL:
       
   194             print 'RQL FOR %s SOURCE:' % (self.uri, union.as_string())
       
   195         args = self.sqladapter.merge_args(args, query_args)
   195         args = self.sqladapter.merge_args(args, query_args)
   196         res = self.sqladapter.process_result(self.doexec(session, sql, args))
   196         results = self.sqladapter.process_result(self.doexec(session, sql, args))
   197         if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
   197         assert dbg_results(results)
   198             print '------>', res
   198         return results
   199         return res
       
   200 
   199 
   201     def local_add_entity(self, session, entity):
   200     def local_add_entity(self, session, entity):
   202         """insert the entity in the local database.
   201         """insert the entity in the local database.
   203 
   202 
   204         This is not provided as add_entity implementation since usually source
   203         This is not provided as add_entity implementation since usually source