pub struct SqlInterfaceRefill {
pub conn: PooledConn,
}Expand description
Contains the configuration and the implementation of the SQL interface for Refill.
Fields§
§conn: PooledConnConnection to the database
Implementations§
Source§impl SqlInterfaceRefill
impl SqlInterfaceRefill
Sourcepub fn new(
conn: PooledConn,
max_rows_refill: u64,
) -> Result<SqlInterfaceRefill, SqlInterfaceError>
pub fn new( conn: PooledConn, max_rows_refill: u64, ) -> Result<SqlInterfaceRefill, SqlInterfaceError>
Creates a new SqlInterfaceRefill instance.
This constructor initializes the refill management SQL interface with an established
database connection. It performs a pre-flight check to ensure the number of rows
in the refill table does not exceed the configured limit.
§Arguments
conn- An active, pooled database connection.max_rows_refill- The maximum allowed number of rows in therefilltable. If0, the check is skipped.
§Returns
A Result containing a new SqlInterfaceRefill instance on success.
§Errors
This function will return an error if:
- The initial database query to get the table row count fails (
DatabaseCheckRefillFailure). - The retrieved row count is a negative number, indicating a database issue (
DatabaseRefillTableNegativeValue). - The number of rows in the
refilltable exceeds the configuredmax_rows_refilllimit (DatabaseRefillTableContainsTooManyRows).
Sourcefn get_last_refill_timestamp(
&mut self,
) -> Result<NaiveDateTime, SqlInterfaceError>
fn get_last_refill_timestamp( &mut self, ) -> Result<NaiveDateTime, SqlInterfaceError>
Fetches the timestamp of the most recent water refill event from the database.
This private helper function queries the database for the latest recorded refill event.
§Returns
A Result containing the NaiveDateTime of the last refill event on success.
§Errors
This function will return an Err variant of SqlInterfaceError, most likely
TimestampRequestFailure, if:
- The underlying database query fails due to a connection issue or invalid SQL.
- The
refilltable is empty, and thus no timestamp can be returned. - The query unexpectedly returns more than one row.
Trait Implementations§
Source§impl DatabaseInterfaceRefillTrait for SqlInterfaceRefill
impl DatabaseInterfaceRefillTrait for SqlInterfaceRefill
Source§fn get_duration_since_last_refill(&mut self) -> Result<u64, SqlInterfaceError>
fn get_duration_since_last_refill(&mut self) -> Result<u64, SqlInterfaceError>
Calculates the time elapsed in seconds since the most recent water refill event.
This function determines the duration by comparing the current database timestamp with the timestamp of the last recorded refill event.
§Returns
A Result containing the number of seconds (u64) that have passed since the last refill event.
§Errors
This function will return an error if:
- It fails to retrieve either the current timestamp or the last refill timestamp from the database. The original error from the database call will be propagated.
- The calculated duration is negative (
NegativeTimeDeltaSeconds), which suggests a data anomaly where the “last refill” timestamp is in the future.
Source§fn get_positive_integer_value_from_database(
&mut self,
sql_query_string: &str,
) -> Result<u64, SqlInterfaceError>
fn get_positive_integer_value_from_database( &mut self, sql_query_string: &str, ) -> Result<u64, SqlInterfaceError>
Executes an SQL query to retrieve a single integer, validating it as non-negative.
This helper function is used for database queries that are expected to return a single, non-negative integer.
§Arguments
sql_query_string- The SQL query to be executed.
§Returns
A Result containing the retrieved integer value as a u64 on success.
§Errors
This function will return an error if:
- The underlying database call fails (e.g., query execution error, no result, or multiple results). The original error is propagated.
- The database query returns a negative integer (
NegativeInteger), which is not expected.
Source§fn check_empty(&mut self) -> Result<u64, SqlInterfaceError>
fn check_empty(&mut self) -> Result<u64, SqlInterfaceError>
Checks whether the refill events table in the database is empty.
This function executes an SQL query to determine if any entries exist in the refill log.
§Returns
A Result which is:
Ok(0)if the refill table contains no entries.Ok(1)if the refill table contains at least one entry.
§Errors
This function will return an Err if the database query fails or if the query
returns an unexpected value (e.g., a negative number).
Source§fn get_refill_count_of_last_hour(&mut self) -> Result<u64, SqlInterfaceError>
fn get_refill_count_of_last_hour(&mut self) -> Result<u64, SqlInterfaceError>
Retrieves the total count of water refill events that occurred within the last hour.
This function queries the database to count all refill events whose timestamps fall within the most recent 60-minute period.
§Returns
A Result containing the number of refill events (u64) recorded in the last hour.
§Errors
Returns SqlInterfaceError::RefillPastDataRetrievalFailed if the underlying
database call fails. This can be caused by a connection issue, an invalid query,
or if the query returns a negative number where a positive count is expected.
Source§fn get_refill_count_of_last_24h(&mut self) -> Result<u64, SqlInterfaceError>
fn get_refill_count_of_last_24h(&mut self) -> Result<u64, SqlInterfaceError>
Retrieves the total count of water refill events that occurred within the last 24 hours.
This function queries the database to count all refill events whose timestamps fall within the most recent 24-hour period.
§Returns
A Result containing the number of refill events (u64) recorded in the last 24 hours.
§Errors
Returns SqlInterfaceError::RefillPastDataRetrievalFailed if the underlying
database call fails. This can be caused by a connection issue, an invalid query,
or if the query returns a negative number where a positive count is expected.
Source§fn get_refill_volume_of_last_24h(&mut self) -> Result<f64, SqlInterfaceError>
fn get_refill_volume_of_last_24h(&mut self) -> Result<f64, SqlInterfaceError>
Retrieves the total volume of water (in liters) refilled within the last 24 hours.
This function queries the database to sum the volume of all refill events that occurred within the most recent 24-hour period.
§Returns
A Result containing the total volume (f64) of water refilled in the last 24 hours.
§Errors
Returns SqlInterfaceError::RefillPastDataRetrievalFailed if the underlying
database call fails. This can be caused by a connection issue, an invalid query,
or if the query does not return a single float value.
Source§fn get_refill_volume_of_last_hour(&mut self) -> Result<f64, SqlInterfaceError>
fn get_refill_volume_of_last_hour(&mut self) -> Result<f64, SqlInterfaceError>
Retrieves the total volume of water (in liters) refilled within the last hour.
This function queries the database to sum the volume of all refill events that occurred within the most recent 60-minute period.
§Returns
A Result containing the total volume (f64) of water refilled in the last hour.
§Errors
Returns SqlInterfaceError::RefillPastDataRetrievalFailed if the underlying
database call fails. This can be caused by a connection issue, an invalid query,
or if the query does not return a single float value.
Source§fn insert_refill_event(
&mut self,
timestamp: NaiveDateTime,
duration: f64,
volume: f64,
error_code: i32,
) -> Result<(), SqlInterfaceError>
fn insert_refill_event( &mut self, timestamp: NaiveDateTime, duration: f64, volume: f64, error_code: i32, ) -> Result<(), SqlInterfaceError>
Inserts a new water refill event record into the database.
This function logs the details of a completed water refill operation, including its timestamp, duration, volume, and any associated error code.
§Arguments
timestamp- TheNaiveDateTimewhen the refill event occurred.duration- The duration (in seconds) of the refill event.volume- The volume (in liters) of water dispensed during the refill.error_code- A numerical code associated with the event (0 for success).
§Returns
An empty Result (Ok(())) if the refill event record was successfully inserted.
§Errors
Returns SqlInterfaceError::InsertRefillEventFailure if the INSERT query fails.
This can be caused by a lost connection, constraint violations, or incorrect
data types. The original mysql::Error is included as the source.