Struct SqlInterfaceHeatingStats

Source
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: PooledConn

Connection 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

Source

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 the heatingstats table.
  • 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 heatingstats table contains entries with NULL values (DatabaseHeatingStatsTableContainsNull).
  • The number of rows in the heatingstats table exceeds max_rows_heating_stats (DatabaseHeatingStatsTableContainsTooManyRows).
Source

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).
Source

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 heatingstats table for the date specified in heating_stats_entry, a new row containing all provided statistical data is inserted.
  • There is a PRIMARY KEY constraint for the Date column in the heatingstats table.
  • If a record already exists for heating_stats_entry.date, the existing row’s Energy, AmbientTempAverage, AmbientTempCounter, WaterTempAverage, WaterTempCounter, and HeatingControlRuntime columns are updated with the new values from heating_stats_entry.

**Important Note on Query Construction: ** The query is constructed in two parts:

  1. The INSERT clause uses parameterized queries (e.g., :date, :energy), which is the recommended and secure way to prevent SQL injection.
  2. The ON DUPLICATE KEY UPDATE clause, however, is built by directly embedding string representations of the data (e.g., Energy=#Energy).
§Arguments
  • heating_stats_entry - A reference to the HeatingStatsEntry struct containing all the statistical data (date, energy, temperatures, runtime) to be inserted or updated. The date field 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.

Source

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.

Trait Implementations§

Source§

impl Pingable for SqlInterfaceHeatingStats

Source§

fn ping(&mut self)

Pings the database to check connection health and prevent timeouts.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T