saferoad.database module

class saferoad.database.DataBase[source]

Bases: object

Class for managing a DuckDB database connection.

This class provides methods to set up a DuckDB database connection, and run SQL queries.

init_database_path()[source]

Initialize the database path.

This method creates a directory for the database if it does not exist and generates a filename based on the current date and time.

Returns:

The path to the DuckDB database file.

Return type:

str

run(query: str)[source]

Run a SQL query on the database.

This method executes the provided SQL query on the connected database.

Parameters:

query (str) – The SQL query to execute.

Raises:

AssertionError – If the database connection is not established.

setup()[source]

Setup the DuckDB database connection. This method initializes the database path and establishes a connection to the DuckDB database. It also loads the spatial extension if available.

Raises:

AssertionError – If the database connection or path is already established.