pub struct SqlInterfaceBalling {
pub conn: PooledConn,
}Expand description
Contains the configuration and the implementation of the SQL interface for Balling.
Fields§
§conn: PooledConnConnection to the database
Implementations§
Source§impl SqlInterfaceBalling
impl SqlInterfaceBalling
Sourcepub fn new(
conn: PooledConn,
max_rows_balling_set_values: u64,
max_rows_balling_dosing_log: u64,
) -> Result<Self, SqlInterfaceError>
pub fn new( conn: PooledConn, max_rows_balling_set_values: u64, max_rows_balling_dosing_log: u64, ) -> Result<Self, SqlInterfaceError>
Creates a new SqlInterfaceBalling instance.
This constructor initializes the Balling SQL interface with an established database connection. It performs several pre-flight checks to ensure data integrity and adherence to configured limits, such as verifying that the Balling set value table contains no NULL values and that table row counts are within their specified maximums.
§Arguments
conn- An active, pooled database connection.max_rows_balling_set_values- Maximum allowed number of rows in the Balling set value table.max_rows_balling_dosing_log- Maximum allowed number of rows in the Balling dosing log table.
§Returns
A Result containing a new SqlInterfaceBalling instance on success.
§Errors
This function will return an error if:
- Any of the initial database queries to get table counts fail (
DatabaseCheckBallingSetValsFailure). - Any of the retrieved counts are negative, indicating a database issue (
DatabaseBallingSetValTableNegativeValue). - The
ballingsetvalstable contains entries withNULLvalues (DatabaseBallingSetValTableContainsNull). - The number of rows in the
ballingsetvalstable exceedsmax_rows_balling_set_values(DatabaseBallingSetValTableContainsTooManyRows). - The number of rows in the
ballingdosinglogtable exceedsmax_rows_balling_dosing_log(DatabaseBallingDosingLogTableContainsTooManyRows).
Sourcefn get_last_balling_dosing_timestamp(
&mut self,
pump_id: i64,
) -> Result<Option<NaiveDateTime>, SqlInterfaceError>
fn get_last_balling_dosing_timestamp( &mut self, pump_id: i64, ) -> Result<Option<NaiveDateTime>, SqlInterfaceError>
Retrieves the timestamp of the last Balling dosing event for a specific pump.
This function queries the database for the most recent dosing timestamp associated
with the given pump_id.
§Arguments
pump_id- The unique identifier of the pump.
§Returns
A Result containing an Option<NaiveDateTime>:
Ok(Some(NaiveDateTime))if a last dosing timestamp is found for the pump.Ok(None)if no previous dosing events are recorded for the specified pump.
§Errors
This function will return an error if the underlying database query fails. This
could be due to a connection issue, a syntax error, or if the query unexpectedly
returns more than one row. The specific error is propagated from the underlying
get_optional_timestamp call.
Sourcepub fn get_duration_since_last_balling_dosing(
&mut self,
pump_id: i64,
) -> Result<Option<Duration>, SqlInterfaceError>
pub fn get_duration_since_last_balling_dosing( &mut self, pump_id: i64, ) -> Result<Option<Duration>, SqlInterfaceError>
Calculates the duration in seconds since the last Balling dosing event for a given pump.
This function retrieves both the current database timestamp and the last dosing
timestamp for the specified pump_id. It then computes the time difference.
§Arguments
pump_id- The unique identifier of the pump.
§Returns
A Result containing an Option<i64>:
Ok(Some(Duration::new)): The duration that has passed since the last dosing event.Ok(None): If no previous dosing event is recorded for this pump.
§Errors
This function will return an error if it fails to retrieve either the current timestamp or the last dosing timestamp from the database. This could be due to a connection loss or other database-side issues. It also will return an error if the database contains any entries that are in the future.
Sourcepub fn insert_balling_event(
&mut self,
timestamp: NaiveDateTime,
pump_id: i64,
dosing_volume: f64,
) -> Result<(), SqlInterfaceError>
pub fn insert_balling_event( &mut self, timestamp: NaiveDateTime, pump_id: i64, dosing_volume: f64, ) -> Result<(), SqlInterfaceError>
Inserts a new Balling dosing event record into the SQL database.
This function logs details of a Balling dosing operation, including the exact time, the pump used, and the volume of fluid dispensed.
§Arguments
timestamp- TheNaiveDateTimewhen the dosing event occurred.pump_id- The identifier of the pump that performed the dosing.dosing_volume- The volume (in milliliters) of fluid dispensed.
§Returns
An empty Result (Ok(())) if the event record was successfully inserted.
§Errors
Returns SqlInterfaceError::InsertBallingEventFailure if the INSERT
query fails. This can happen due to a lost connection, constraint violation
(e.g., foreign key), or incorrect data types. The original mysql::Error
is included as the source.
Sourcepub fn get_single_balling_setval_from_database(
&mut self,
pump_id: i64,
) -> Result<BallingSetVal, SqlInterfaceError>
pub fn get_single_balling_setval_from_database( &mut self, pump_id: i64, ) -> Result<BallingSetVal, SqlInterfaceError>
Retrieves a single set of Balling dosing parameters for a specified pump from the database.
This function queries the database for the Balling set values (e.g., dosing speed, volume, label)
associated with a given pump_id. It strictly expects to find exactly one matching dataset.
§Arguments
pump_id- The unique identifier of the pump for which to retrieve the set values.
§Returns
A Result containing the BallingSetVal for the specified pump on success.
§Errors
This function will return an error if:
- The database query fails for any reason (
SingleBallingSetValRequestFailure). - The query returns zero rows (
SingleBallingSetValEmptyResponse). - The query returns more than one row (
SingleBallingSetValNoSingleResponse), which indicates a data consistency issue.