ahvn.utils.db package¶
- ahvn.utils.db.resolve_db_config(database=None, provider=None, pool=None, **kwargs)[源代码]¶
Compile a database configuration dictionary based on the following order of priority: 1. kwargs 2. provider 3. global configuration When a parameter is specified in multiple places, the one with the highest priority is used. When missing, the provider falls back to the default provider.
- 参数:
- 返回:
1. The resolved database configuration dictionary with 'url', 'pool', and hyperparameters. Connection parameters (dialect, driver, username, password, host, port, database) are used to build the URL and then removed from the final config. 2. The connection parameters dictionary.
- 返回类型:
- ahvn.utils.db.create_database_engine(config, conn_args, autocreate=True)[源代码]¶
Create a SQLAlchemy engine from the resolved database configuration.
Uses appropriate connection pooling strategy based on dialect: - SQLite: StaticPool (file) or SingletonThreadPool (:memory:) - DuckDB: NullPool (thread-safe, no pooling needed) - PostgreSQL/MySQL/MSSQL: QueuePool with configurable settings
Pool settings are read from conn_args['pool'] (set by resolve_db_config from provider config).
- 参数:
- 返回:
A SQLAlchemy engine instance.
- 返回类型:
Engine
- 抛出:
ImportError -- If SQLAlchemy is not installed.
ValueError -- If required configuration is missing.
- ahvn.utils.db.create_database(config, engine_kwargs=None)[源代码]¶
Create the database if it does not already exist.
This helper supports SQLite (directory creation), PostgreSQL, and MySQL database creation.
- 参数:
- 返回类型:
备注
- The function is best-effort and will log on failure; callers may choose to
ignore failures by catching exceptions.
- ahvn.utils.db.split_sqls(queries, dialect='sqlite')[源代码]¶
Split a string containing multiple SQL queries into a list.
- ahvn.utils.db.transpile_sql(query, src_dialect='sqlite', tgt_dialect='sqlite')[源代码]¶
Transpile a SQL query from one dialect to another.
- 参数:
- 返回:
The transpiled query.
- 返回类型:
- 抛出:
ImportError -- If SQLGlot is not installed.
ValueError -- If transpilation fails.
- ahvn.utils.db.load_builtin_sql(query_name, dialect='sqlite', **kwargs)[源代码]¶
Load SQL query from file and return the query for the current dialect.
警告
This function uses string formatting (.format(**kwargs)) to inject parameters into the SQL query. This is vulnerable to SQL injection if kwargs contains untrusted user input. Only use this function with trusted input or for internal queries where parameters are controlled. For user-supplied values, prefer using parameterized queries supported by your database driver.
- 参数:
- 返回:
SQL query for the current dialect. None if the query is not found.
- 返回类型:
- 抛出:
FileNotFoundError -- If SQL file is not found.
- class ahvn.utils.db.SQLProcessor(target_dialect)[源代码]¶
基类:
objectHandles SQL transpilation and parameter normalization across different database dialects.
This class centralizes all SQL processing logic including: - SQL dialect transpilation via SQLGlot - Parameter format normalization (convert all to :param format) - Cross-database parameter binding support
- 参数:
target_dialect (str)
- __init__(target_dialect)[源代码]¶
Initialize SQL processor for a target database dialect.
- 参数:
target_dialect (
str) -- Target database dialect (sqlite, postgres, mysql, duckdb, etc.)
Submodules¶
- ahvn.utils.db.base module
SQLResponseSQLErrorResponseDatabaseErrorHandlerDatabaseDatabase.__init__()Database.clone()Database.connect()Database.close_conn()Database.connectedDatabase.connDatabase.in_transaction()Database.commit()Database.rollback()Database.__enter__()Database.__exit__()Database.orm_execute()Database.execute()Database.db_tabs()Database.db_views()Database.tab_cols()Database.tab_pks()Database.tab_fks()Database.row_count()Database.col_type()Database.col_distincts()Database.col_enums()Database.col_freqs()Database.col_freqk()Database.col_nonnulls()Database.clear_tab()Database.drop_tab()Database.drop_view()Database.drop()Database.init()Database.clear()Database.close()
table_display()
- ahvn.utils.db.compiler module
- ahvn.utils.db.db_utils module
- ahvn.utils.db.types module