Module sql_interface

Source
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

  • SqlInterface Struct: The central struct that holds the mysql::Pool. It is instantiated once at application startup and provides a get_connection() method to lend out PooledConn objects 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’s wait_timeout setting 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, and get_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 the Pingable trait 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 as check_wait_timeout and check_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§

SingleFloat 🔒
Struct is used to retrieve a single float value from the SQL database.
SingleInteger 🔒
Struct is used to retrieve a single integer value from the SQL database.
SingleString 🔒
Struct is used to retrieve a single string value from the SQL database.
SqlInterface
Holds the configuration and the implementation of the SQL interface.