<!-- .github/pull_request_template.md --> ## Description <!-- Provide a clear description of the changes in this PR --> ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin.
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from .sqlalchemy.SqlAlchemyAdapter import SQLAlchemyAdapter
|
|
from functools import lru_cache
|
|
|
|
|
|
@lru_cache
|
|
def create_relational_engine(
|
|
db_path: str,
|
|
db_name: str,
|
|
db_host: str,
|
|
db_port: str,
|
|
db_username: str,
|
|
db_password: str,
|
|
db_provider: str,
|
|
):
|
|
"""
|
|
Create a relational database engine based on the specified parameters.
|
|
|
|
Parameters:
|
|
-----------
|
|
|
|
- db_path (str): The file path to the database directory, applicable for SQLite.
|
|
- db_name (str): The name of the database to be accessed or created.
|
|
- db_host (str): The hostname or IP address of the database server, required for
|
|
PostgreSQL.
|
|
- db_port (str): The port number on which the database server is listening, required
|
|
for PostgreSQL.
|
|
- db_username (str): The username for database authentication, required for
|
|
PostgreSQL.
|
|
- db_password (str): The password for database authentication, required for
|
|
PostgreSQL.
|
|
- db_provider (str): The type of database provider (e.g., 'sqlite' or 'postgres').
|
|
|
|
Returns:
|
|
--------
|
|
|
|
Returns a SQLAlchemyAdapter instance for the specified database connection.
|
|
"""
|
|
if db_provider == "sqlite":
|
|
connection_string = f"sqlite+aiosqlite:///{db_path}/{db_name}"
|
|
|
|
if db_provider == "postgres":
|
|
connection_string = (
|
|
f"postgresql+asyncpg://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}"
|
|
)
|
|
|
|
return SQLAlchemyAdapter(connection_string)
|