async def connect(self): cfg = config.get('databases', self.db_name, host='127.0.0.1', port=1521, username=config.MANDATORY, password=config.MANDATORY, autocommit=True, service_name=None, sid=None) if cfg.service_name is None: if cfg.sid is None: raise AttributeError( f'neither service_name or sid in configuration (dbname="{self.db_name})"' ) dsn_tns = cx_Oracle.makedsn(cfg.host, cfg.port, cfg.sid) else: dsn_tns = cx_Oracle.makedsn(cfg.host, cfg.port, service_name=cfg.service_name) os.environ['NLS_LANG'] = 'GERMAN_GERMANY.WE8MSWIN1252' self.conn = await sync_to_async(cx_Oracle.connect)(cfg.username, cfg.password, dsn_tns) self.conn.autocommit = cfg.autocommit logger.debug('connected to database "%s"', self.db_name)
def _get_template_dir(template_dir): cfg = config.get(template_dir=config.MANDATORY) if template_dir is None: return cfg.template_dir if template_dir.startswith('/'): return template_dir return cfg.template_dir + os.sep + template_dir
async def acquire(db_name): if db_name not in _pool: _pool[db_name] = {'open': 0, 'idle': []} try: dbi = _pool[db_name]['idle'].pop() logger.debug(f'use existing idle connection for "{db_name}"') return dbi except IndexError: pass cfg = config.get('databases', db_name, max_connections=3) if _pool[db_name]['open'] >= cfg.max_connections: raise DatabasePoolExhaustedException( f"max_connections reached for {db_name}") dbi = await _new_dbi(db_name) _pool[db_name]['open'] += 1 return dbi
async def _new_dbi(db_name): dbconfig = config.get('databases', db_name, type=config.MANDATORY) module = f'haip.database.drivers.{dbconfig.type}' try: # import database driver dbdriver = importlib.import_module(module) except ModuleNotFoundError: raise DatabaseException( f"database implementation for db '{db_name}' (with type {dbconfig.type}) not found" ) try: # instantiate the database driver dbi = dbdriver.Database(db_name) except AttributeError: raise DatabaseException(f'no Database class found in "{module}"') await dbi.connect() logger.debug(f'new database connection to "{db_name}" established') return dbi
async def connect(self): cfg = config.get('databases', self.db_name, database=config.MANDATORY, host='127.0.0.1', port=1433, username=config.MANDATORY, password=config.MANDATORY, driver=config.MANDATORY, autocommit=True) try: self.conn = await sync_to_async(pyodbc.connect )(DRIVER=cfg.driver, SERVER=f'{cfg.host},{cfg.port}', DATABASE=cfg.database, UID=cfg.username, PWD=cfg.password) except pyodbc.DatabaseError as e: raise DatabaseException(str(e)) self.conn.autocommit = cfg.autocommit logger.debug("connected to database '%s'", self.db_name)
async def connect(self): cfg = config.get('databases', self.db_name, database=config.MANDATORY, host='127.0.0.1', port=3306, username=config.MANDATORY, password=config.MANDATORY, autocommit=True) try: self.conn = await sync_to_async(mysql.connector.connect )(database=cfg.database, host=cfg.host, port=cfg.port, user=cfg.username, password=cfg.password) except mysql.connector.errors.InterfaceError as e: raise DatabaseException(str(e)) self.conn.raise_on_warnings = True self.conn.autocommit = cfg.autocommit logger.debug("connected to database '%s'", self.db_name)
async def release(dbi): cfg = config.get('databases', dbi.db_name, max_idle_connections=3) if len(_pool[dbi.db_name]['idle']) < cfg.max_idle_connections: _pool[dbi.db_name]['idle'].append(dbi) else: await dbi.disconnect()
def test_get_option_notset(): config_dir = os.sep.join((base_dir, 'etc_ok')) config.load(config_dir) with pytest.raises(config.HaipConfigException): config.get('A', 'B', 'C', key3=config.MANDATORY)
def test_get_option(): config_dir = os.sep.join((base_dir, 'etc_ok')) config.load(config_dir) cfg = config.get('A', 'B', 'C', key1=config.MANDATORY) assert cfg.key1 == 'value1'