Expand description
Contains functions for execution of basic queries used by the other modules Provides the core SQL database interface and connection management for the application.
This module is the foundation of the application’s database layer. It defines the
main SqlInterface struct, which is responsible for establishing and managing a
connection pool to the MySQL database. It also provides a suite of low-level
helper functions for executing common query patterns, which are then used by
more specialized SqlInterface... modules throughout the application.
§Key Components
-
SqlInterfaceStruct: The central struct that holds themysql::Pool. It is instantiated once at application startup and provides aget_connection()method to lend outPooledConnobjects to other threads and modules. -
new()Constructor: Handles the critical task of initializing the database connection. It builds the connection URL, creates the pool, and performs essential pre-flight checks, such as verifying the database’swait_timeoutsetting to prevent unexpected connection drops. -
Generic Helper Functions: A collection of private and public static methods like
get_single_string_from_database,get_single_integer_from_database, andget_timestamp. These functions encapsulate common SQL query patterns (e.g., fetching a single value, fetching an optional value) and provide consistent error handling. -
ping_database(): A static method that executes a lightweight query (SELECT 1) to verify that a connection is still alive. This is the core implementation used by thePingabletrait to prevent connection timeouts.
§Design and Purpose
The primary goals of this module are efficiency, robustness, and centralization.
-
Connection Pooling: By using
mysql::Pool, the application avoids the high cost of opening and closing a new database connection for every operation. The pool manages a set of ready-to-use connections, which is essential for performance in a multithreaded environment. -
Startup Validation: The
new()constructor performs critical checks at startup, such ascheck_wait_timeoutandcheck_required_tables_existing. This “fail-fast” approach ensures that the application does not start in a misconfigured or unstable state. -
Centralized Logic: It centralizes the logic for creating connections and executing basic queries, preventing this boilerplate from being scattered across the application.
Structs§
- Single
Float 🔒 - Struct is used to retrieve a single float value from the SQL database.
- Single
Integer 🔒 - Struct is used to retrieve a single integer value from the SQL database.
- Single
String 🔒 - Struct is used to retrieve a single string value from the SQL database.
- SqlInterface
- Holds the configuration and the implementation of the SQL interface.