4729698049
git-subtree-dir: paste-framework git-subtree-split: 34e8684c4bc3cebbe177509f42ab4ef5b5425a7a
44 lines
1.2 KiB
Python
Executable File
44 lines
1.2 KiB
Python
Executable File
"""
|
|
从配置文件读取数据引擎连接信息,连接数据库。
|
|
"""
|
|
|
|
from typing import Union
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.engine import Engine
|
|
from sqlalchemy.engine.mock import MockConnection
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine
|
|
|
|
from paste.core import config
|
|
|
|
ASYNC_CONNECTOR_ENGINE = None
|
|
GLOBAL_CONNECTOR_ENGINE = None
|
|
|
|
|
|
def connect_engine() -> Union[MockConnection, Engine]:
|
|
"""
|
|
全局数据连接引擎。
|
|
|
|
:return: 数据连接引擎
|
|
"""
|
|
global GLOBAL_CONNECTOR_ENGINE
|
|
if GLOBAL_CONNECTOR_ENGINE is None:
|
|
GLOBAL_CONNECTOR_ENGINE = create_engine(
|
|
config.get_config('db_engine.engine'), **config.get_config('db_engine.engine_option')
|
|
)
|
|
return GLOBAL_CONNECTOR_ENGINE
|
|
|
|
|
|
def async_connect_engine() -> AsyncEngine:
|
|
"""
|
|
异步数据连接引擎。
|
|
|
|
:return: 异步数据连接引擎
|
|
"""
|
|
global ASYNC_CONNECTOR_ENGINE
|
|
if ASYNC_CONNECTOR_ENGINE is None:
|
|
ASYNC_CONNECTOR_ENGINE = create_async_engine(
|
|
config.get_config('db_engine.async_engine'), **config.get_config('db_engine.engine_option')
|
|
)
|
|
return ASYNC_CONNECTOR_ENGINE
|