server/sqlutils.py
changeset 9447 0636c4960259
parent 9364 73bd5012336f
child 9466 c3a5f4507f12
equal deleted inserted replaced
9446:18a186b02970 9447:0636c4960259
    28 from logilab import database as db, common as lgc
    28 from logilab import database as db, common as lgc
    29 from logilab.common.shellutils import ProgressBar
    29 from logilab.common.shellutils import ProgressBar
    30 from logilab.common.date import todate, todatetime, utcdatetime, utctime
    30 from logilab.common.date import todate, todatetime, utcdatetime, utctime
    31 from logilab.database.sqlgen import SQLGenerator
    31 from logilab.database.sqlgen import SQLGenerator
    32 
    32 
    33 from cubicweb import Binary, ConfigurationError
    33 from cubicweb import Binary, ConfigurationError, server
    34 from cubicweb.uilib import remove_html_tags
    34 from cubicweb.uilib import remove_html_tags
    35 from cubicweb.schema import PURE_VIRTUAL_RTYPES
    35 from cubicweb.schema import PURE_VIRTUAL_RTYPES
    36 from cubicweb.server import SQL_CONNECT_HOOKS
    36 from cubicweb.server import SQL_CONNECT_HOOKS
    37 from cubicweb.server.utils import crypt_password
    37 from cubicweb.server.utils import crypt_password
    38 from rql.utils import RQL_FUNCTIONS_REGISTRY
    38 from rql.utils import RQL_FUNCTIONS_REGISTRY
   175                  for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_views(sqlcursor))]
   175                  for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_views(sqlcursor))]
   176     cmds += ['DROP TABLE %s;' % name
   176     cmds += ['DROP TABLE %s;' % name
   177              for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_tables(sqlcursor))]
   177              for name in ifilter(_SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION, dbhelper.list_tables(sqlcursor))]
   178     return '\n'.join(cmds)
   178     return '\n'.join(cmds)
   179 
   179 
       
   180 
   180 class SQLAdapterMixIn(object):
   181 class SQLAdapterMixIn(object):
   181     """Mixin for SQL data sources, getting a connection from a configuration
   182     """Mixin for SQL data sources, getting a connection from a configuration
   182     dictionary and handling connection locking
   183     dictionary and handling connection locking
   183     """
   184     """
   184 
   185 
   321 
   322 
   322 from logging import getLogger
   323 from logging import getLogger
   323 from cubicweb import set_log_methods
   324 from cubicweb import set_log_methods
   324 set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter'))
   325 set_log_methods(SQLAdapterMixIn, getLogger('cubicweb.sqladapter'))
   325 
   326 
       
   327 
       
   328 class SqliteCnxLoggingWrapper(object):
       
   329     def __init__(self, source=None):
       
   330         self.source = source
       
   331         self._cnx = None
       
   332 
       
   333     def cursor(self):
       
   334         # sqlite connections can only be used in the same thread, so
       
   335         # create a new one each time necessary. If it appears to be time
       
   336         # consuming, find another way
       
   337         if self._cnx is None:
       
   338             # direct access to SQLAdapterMixIn to get an unwrapped connection
       
   339             self._cnx = SQLAdapterMixIn.get_connection(self.source)
       
   340             if server.DEBUG & server.DBG_SQL:
       
   341                 print 'sql cnx OPEN', self._cnx
       
   342         return self._cnx.cursor()
       
   343 
       
   344     def commit(self):
       
   345         if self._cnx is not None:
       
   346             if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
       
   347                 print 'sql cnx COMMIT', self._cnx
       
   348             self._cnx.commit()
       
   349 
       
   350     def rollback(self):
       
   351         if self._cnx is not None:
       
   352             if server.DEBUG & (server.DBG_SQL | server.DBG_RQL):
       
   353                 print 'sql cnx ROLLBACK', self._cnx
       
   354             self._cnx.rollback()
       
   355 
       
   356     def close(self):
       
   357         if self._cnx is not None:
       
   358             if server.DEBUG & server.DBG_SQL:
       
   359                 print 'sql cnx CLOSE', self._cnx
       
   360             self._cnx.close()
       
   361             self._cnx = None
       
   362 
       
   363 
   326 def init_sqlite_connexion(cnx):
   364 def init_sqlite_connexion(cnx):
   327 
   365 
   328     class group_concat(object):
   366     class group_concat(object):
   329         def __init__(self):
   367         def __init__(self):
   330             self.values = set()
   368             self.values = set()