cubicweb/server/sqlutils.py
changeset 12809 9b809497978f
parent 12808 6cbb1e2a6e49
child 12810 4a98b93ac5ac
equal deleted inserted replaced
12808:6cbb1e2a6e49 12809:9b809497978f
   278         try:
   278         try:
   279             self.dbdriver = source_config['db-driver'].lower()
   279             self.dbdriver = source_config['db-driver'].lower()
   280             dbname = source_config['db-name']
   280             dbname = source_config['db-name']
   281         except KeyError:
   281         except KeyError:
   282             raise ConfigurationError('missing some expected entries in sources file')
   282             raise ConfigurationError('missing some expected entries in sources file')
       
   283 
   283         dbhost = source_config.get('db-host')
   284         dbhost = source_config.get('db-host')
   284         port = source_config.get('db-port')
   285         port = source_config.get('db-port')
   285         dbport = port and int(port) or None
   286         dbport = port and int(port) or None
   286         dbuser = source_config.get('db-user')
   287         dbuser = source_config.get('db-user')
   287         dbpassword = source_config.get('db-password')
   288         dbpassword = source_config.get('db-password')
   288         dbencoding = source_config.get('db-encoding', 'UTF-8')
   289         dbencoding = source_config.get('db-encoding', 'UTF-8')
   289         dbextraargs = source_config.get('db-extra-arguments')
   290         dbextraargs = source_config.get('db-extra-arguments')
   290         dbnamespace = source_config.get('db-namespace')
   291         dbnamespace = source_config.get('db-namespace')
       
   292 
   291         self.dbhelper = db.get_db_helper(self.dbdriver)
   293         self.dbhelper = db.get_db_helper(self.dbdriver)
   292         self.dbhelper.record_connection_info(dbname, dbhost, dbport, dbuser,
   294         self.dbhelper.record_connection_info(dbname, dbhost, dbport, dbuser,
   293                                              dbpassword, dbextraargs,
   295                                              dbpassword, dbextraargs,
   294                                              dbencoding, dbnamespace)
   296                                              dbencoding, dbnamespace)
   295         self.sqlgen = SQLGenerator()
   297         self.sqlgen = SQLGenerator()
       
   298 
   296         # copy back some commonly accessed attributes
   299         # copy back some commonly accessed attributes
   297         dbapi_module = self.dbhelper.dbapi_module
   300         dbapi_module = self.dbhelper.dbapi_module
   298         self.OperationalError = dbapi_module.OperationalError
   301         self.OperationalError = dbapi_module.OperationalError
   299         self.InterfaceError = dbapi_module.InterfaceError
   302         self.InterfaceError = dbapi_module.InterfaceError
   300         self.DbapiError = dbapi_module.Error
   303         self.DbapiError = dbapi_module.Error
   308         else:
   311         else:
   309             self.cnx_wrap = ConnectionWrapper
   312             self.cnx_wrap = ConnectionWrapper
   310 
   313 
   311         if not repairing:
   314         if not repairing:
   312             statement_timeout = int(source_config.get('db-statement-timeout', 0))
   315             statement_timeout = int(source_config.get('db-statement-timeout', 0))
       
   316 
   313             if statement_timeout > 0:
   317             if statement_timeout > 0:
   314                 def set_postgres_timeout(cnx):
   318                 def set_postgres_timeout(cnx):
   315                     cnx.cursor().execute('SET statement_timeout to %d' % statement_timeout)
   319                     cnx.cursor().execute('SET statement_timeout to %d' % statement_timeout)
   316                     cnx.commit()
   320                     cnx.commit()
       
   321 
   317                 postgres_hooks = SQL_CONNECT_HOOKS['postgres']
   322                 postgres_hooks = SQL_CONNECT_HOOKS['postgres']
   318                 postgres_hooks.append(set_postgres_timeout)
   323                 postgres_hooks.append(set_postgres_timeout)
   319 
   324 
   320     def wrapped_connection(self):
   325     def wrapped_connection(self):
   321         """open and return a connection to the database, wrapped into a class
   326         """open and return a connection to the database, wrapped into a class