pub struct SqlInterfaceHeatingStats {
pub conn: PooledConn,
pub sql_interface_heating_midnight_calculator: Box<dyn SqlInterfaceMidnightCalculatorTrait + Sync + Send>,
}Expand description
Contains the configuration and the implementation of the SQL interface for heating.
Fields§
§conn: PooledConnConnection to the database
sql_interface_heating_midnight_calculator: Box<dyn SqlInterfaceMidnightCalculatorTrait + Sync + Send>Struct with implementation of trait for calculation of the time interval until midnight in seconds
Implementations§
Source§impl SqlInterfaceHeatingStats
impl SqlInterfaceHeatingStats
Sourcepub fn new(
conn: PooledConn,
max_rows_heating_stats: u64,
sql_interface_heating_midnight_calculator: Box<dyn SqlInterfaceMidnightCalculatorTrait + Sync + Send>,
) -> Result<SqlInterfaceHeatingStats, SqlInterfaceError>
pub fn new( conn: PooledConn, max_rows_heating_stats: u64, sql_interface_heating_midnight_calculator: Box<dyn SqlInterfaceMidnightCalculatorTrait + Sync + Send>, ) -> Result<SqlInterfaceHeatingStats, SqlInterfaceError>
Creates a new SqlInterfaceHeatingStats instance.
This constructor initializes the SQL interface for heating statistics. It performs
several pre-flight checks to ensure data integrity, such as verifying that the
heatingstats table contains no NULL values and that its row count is within
the configured limit.
§Arguments
conn- An active, pooled database connection.max_rows_heating_stats- The maximum allowed number of rows in theheatingstatstable.sql_interface_heating_midnight_calculator- A boxed trait object that provides the implementation for calculating the duration until midnight.
§Returns
A Result containing a new SqlInterfaceHeatingStats instance on success.
§Errors
This function will return an error if:
- Any of the initial database queries to get table counts fail (
DatabaseCheckHeatingStatsFailure). - Any of the retrieved counts are negative, indicating a database issue (
DatabaseHeatingStatsTableNegativeValue). - The
heatingstatstable contains entries withNULLvalues (DatabaseHeatingStatsTableContainsNull). - The number of rows in the
heatingstatstable exceedsmax_rows_heating_stats(DatabaseHeatingStatsTableContainsTooManyRows).
Sourcepub fn get_single_heating_stats_from_database(
&mut self,
) -> Result<HeatingStatsEntry, SqlInterfaceError>
pub fn get_single_heating_stats_from_database( &mut self, ) -> Result<HeatingStatsEntry, SqlInterfaceError>
Retrieves a single HeatingStatsEntry for the current date from the database.
This function executes an SQL query to fetch heating statistical data that corresponds to the database’s current date. It is designed to retrieve exactly one record for “today.”
§Returns
A Result containing the HeatingStatsEntry for the current date on success.
§Errors
This function will return an error if:
- The underlying database query fails (
SingleHeatingStatsRequestFailure). This can be due to a connection issue or an invalid query. - The query returns zero rows, indicating no statistics have been recorded for
the current day (
SingleHeatingStatsRequestEmptyResponse). - The query returns more than one row, which signifies a data inconsistency
(
SingleHeatingStatsRequestNoSingleResponse). - The date string retrieved from the database cannot be parsed into a
NaiveDate(TimestampConversionFailure).
Sourcepub fn insert_heating_stats_entry(
&mut self,
heating_stats_entry: &HeatingStatsEntry,
) -> Result<(), SqlInterfaceError>
pub fn insert_heating_stats_entry( &mut self, heating_stats_entry: &HeatingStatsEntry, ) -> Result<(), SqlInterfaceError>
Inserts new heating statistical data into the database or updates an existing entry for a specific date.
This function attempts to write a HeatingStatsEntry into the heatingstats table.
It’s designed to manage daily statistics, where only one unique entry per date is expected.
The underlying SQL query utilizes an INSERT ... ON DUPLICATE KEY UPDATE clause:
- If no record exists in the
heatingstatstable for thedatespecified inheating_stats_entry, a new row containing all provided statistical data is inserted. - There is a
PRIMARY KEYconstraint for theDatecolumn in theheatingstatstable. - If a record already exists for
heating_stats_entry.date, the existing row’sEnergy,AmbientTempAverage,AmbientTempCounter,WaterTempAverage,WaterTempCounter, andHeatingControlRuntimecolumns are updated with the new values fromheating_stats_entry.
**Important Note on Query Construction: ** The query is constructed in two parts:
- The
INSERTclause uses parameterized queries (e.g.,:date,:energy), which is the recommended and secure way to prevent SQL injection. - The
ON DUPLICATE KEY UPDATEclause, however, is built by directly embedding string representations of the data (e.g.,Energy=#Energy).
§Arguments
heating_stats_entry- A reference to theHeatingStatsEntrystruct containing all the statistical data (date, energy, temperatures, runtime) to be inserted or updated. Thedatefield is crucial as it identifies the unique daily record.
§Returns
An empty Result (Ok(())) if the data was successfully inserted or updated.
§Errors
Returns SqlInterfaceError::InsertHeatingStatsEntryFailure if the database
operation fails. This can be caused by a lost connection, constraint violations,
incorrect data types, or a malformed SQL query. The original mysql::Error is
included as the source for detailed debugging.
Sourcepub fn get_duration_until_midnight(&mut self) -> Result<i64, SqlInterfaceError>
pub fn get_duration_until_midnight(&mut self) -> Result<i64, SqlInterfaceError>
Calculates the duration in seconds until the next midnight.
This function delegates the calculation to the sql_interface_heating_midnight_calculator
trait object, which was provided during construction. This allows for flexible
and testable time calculations.
§Returns
A Result containing the number of seconds until midnight on success.
§Errors
This function will return an error if the underlying calculator implementation
fails. This is typically a pass-through of the error from the calculator’s
get_duration_until_midnight method.