Module sql_interface_midnight

Source
Expand description

Contains functions for execution of all queries relevant for calculating the time until midnight Provides a trait and implementation for calculating the time until midnight using the database clock.

This module defines an abstraction (SqlInterfaceMidnightCalculatorTrait) and a concrete implementation (SqlInterfaceMidnightCalculator) for determining the number of seconds remaining until the next midnight. The primary purpose of this module is to decouple time-sensitive components (like daily statistics aggregators) from the specific details of the database, adhering to the Dependency Inversion Principle.

§Key Components

  • SqlInterfaceMidnightCalculatorTrait: An abstraction that defines the contract for any component capable of calculating the duration until midnight. This allows for dependency injection, making it possible to use a mock implementation for testing.

  • SqlInterfaceMidnightCalculator: The concrete implementation of the trait. It holds a database connection and executes an SQL query to get a precise time from the database server’s clock.

§Design and Purpose

By relying on the database server for the current time, the application ensures that all time-based calculations are consistent, even if the application is running on a machine with a desynchronized clock.

The use of a trait is a crucial architectural choice. It allows higher-level components, such as SqlInterfaceHeatingStats, to depend on the SqlInterfaceMidnightCalculatorTrait abstraction rather than the concrete SqlInterfaceMidnightCalculator struct. This makes the system more modular and significantly easier to test, as a mock calculator can be provided during unit tests without needing a live database.

Structs§

SqlInterfaceMidnightCalculator
Struct holds connection data to the SQL database and provides implementation for calculating the time in seconds until midnight.

Traits§

SqlInterfaceMidnightCalculatorTrait
Trait for calculation the time interval until midnight in seconds. This trait allows running the main control with a mock implementation for testing.