Struct SqlInterfaceRefill

Source
pub struct SqlInterfaceRefill {
    pub conn: PooledConn,
}
Expand description

Contains the configuration and the implementation of the SQL interface for Refill.

Fields§

§conn: PooledConn

Connection to the database

Implementations§

Source§

impl SqlInterfaceRefill

Source

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 the refill table. If 0, 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 refill table exceeds the configured max_rows_refill limit (DatabaseRefillTableContainsTooManyRows).
Source

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 refill table is empty, and thus no timestamp can be returned.
  • The query unexpectedly returns more than one row.

Trait Implementations§

Source§

impl DatabaseInterfaceRefillTrait for SqlInterfaceRefill

Source§

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>

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>

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>

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>

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>

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>

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>

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 - The NaiveDateTime when 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.

Source§

impl Debug for SqlInterfaceRefill

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Pingable for SqlInterfaceRefill

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