ahvn.utils.db.db_utils module¶
Database configuration utilities for AgentHeaven.
This module provides functions to parse and resolve database configurations similar to how LLM configurations are handled. It supports multiple database providers (SQLite, PostgreSQL, DuckDB, etc.) and generates SQLAlchemy-ready configurations with URLs and hyperparameters.
- ahvn.utils.db.db_utils.resolve_db_config(database=None, provider=None, pool=None, **kwargs)[source]¶
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.
- Parameters:
- Returns:
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.
- Return type:
- ahvn.utils.db.db_utils.create_database_engine(config, conn_args, autocreate=True)[source]¶
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).
- Parameters:
- Returns:
A SQLAlchemy engine instance.
- Return type:
Engine
- Raises:
ImportError – If SQLAlchemy is not installed.
ValueError – If required configuration is missing.
- ahvn.utils.db.db_utils.create_database(config, engine_kwargs=None)[source]¶
Create the database if it does not already exist.
This helper supports SQLite (directory creation), PostgreSQL, and MySQL database creation.
- Parameters:
- Return type:
Notes
- The function is best-effort and will log on failure; callers may choose to
ignore failures by catching exceptions.
- ahvn.utils.db.db_utils.split_sqls(queries, dialect='sqlite')[source]¶
Split a string containing multiple SQL queries into a list.
- ahvn.utils.db.db_utils.transpile_sql(query, src_dialect='sqlite', tgt_dialect='sqlite')[source]¶
Transpile a SQL query from one dialect to another.
- Parameters:
- Returns:
The transpiled query.
- Return type:
- Raises:
ImportError – If SQLGlot is not installed.
ValueError – If transpilation fails.
- ahvn.utils.db.db_utils.prettify_sql(query, dialect='sqlite', comments=True)[source]¶
Prettify a SQL query for better readability (identify + strip).
- ahvn.utils.db.db_utils.compare_sqls(sql1, sql2, db)[source]¶
Given two SQL queries, execute them on the provided database and compare their results.
- ahvn.utils.db.db_utils.load_builtin_sql(query_name, dialect='sqlite', **kwargs)[source]¶
Load SQL query from file and return the query for the current dialect.
Warning
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.
- Parameters:
- Returns:
SQL query for the current dialect. None if the query is not found.
- Return type:
- Raises:
FileNotFoundError – If SQL file is not found.
- class ahvn.utils.db.db_utils.SQLProcessor(target_dialect)[source]¶
Bases:
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
- Parameters:
target_dialect (str)
- __init__(target_dialect)[source]¶
Initialize SQL processor for a target database dialect.
- Parameters:
target_dialect (
str) – Target database dialect (sqlite, postgres, mysql, duckdb, etc.)