pub struct SqlInterfaceFeed {
pub conn: PooledConn,
}Expand description
Contains the configuration and the implementation of the SQL interface for Feed.
Fields§
§conn: PooledConnconnection to SQL database
Implementations§
Source§impl SqlInterfaceFeed
impl SqlInterfaceFeed
Sourcepub fn new(
conn: PooledConn,
max_rows_feed_pattern: u64,
max_rows_feed_schedule: u64,
max_rows_feed_log: u64,
) -> Result<Self, SqlInterfaceError>
pub fn new( conn: PooledConn, max_rows_feed_pattern: u64, max_rows_feed_schedule: u64, max_rows_feed_log: u64, ) -> Result<Self, SqlInterfaceError>
Creates a new SqlInterfaceFeed instance.
This constructor initializes the feed management 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 tables contain no NULL values and that row counts are within their specified maximums.
§Arguments
conn- An active, pooled database connection.max_rows_feed_pattern- The maximum allowed rows in thefeedpatternstable.max_rows_feed_schedule- The maximum allowed rows in thefeedscheduletable.max_rows_feed_log- The maximum allowed rows in thefeedlogtable.
§Returns
A Result containing a new SqlInterfaceFeed instance on success.
§Errors
This function will return an error if:
- Any of the initial database queries to get table counts or check for NULLs fail (
DatabaseCheckFeedSetValsFailure). - Any of the retrieved counts are negative, indicating a database issue (
DatabaseFeedTableNegativeValue). - The
feedpatternsorfeedscheduletables contain entries withNULLvalues (DatabaseFeedSetValTableContainsNull). - The number of rows in any of the checked tables exceeds its configured maximum (
DatabaseFeedTableContainsTooManyRows). .
Sourcepub(crate) fn get_feedschedule_entries_from_database(
&mut self,
sql_query: &str,
) -> Result<Option<Vec<FeedScheduleEntry>>, SqlInterfaceError>
pub(crate) fn get_feedschedule_entries_from_database( &mut self, sql_query: &str, ) -> Result<Option<Vec<FeedScheduleEntry>>, SqlInterfaceError>
Retrieves and post-processes feed schedule entries from the database based on a given SQL query.
This private helper function executes the provided sql_query to fetch raw schedule data.
It then attempts to convert these raw SqlScheduleEntry records into fully typed
FeedScheduleEntry structs.
§Arguments
sql_query- The SQL query string used to retrieve feed schedule entries.
§Returns
A Result containing an Option<Vec<FeedScheduleEntry>>:
Ok(Some(Vec<FeedScheduleEntry>)): If one or more schedule entries are successfully retrieved and converted.Ok(None): If the query returns no rows.
§Errors
This function will return an error if:
- The underlying database query fails (
FeedScheduleEntryRequestFailure). This can be due to a connection issue or an invalid query. - The query returns results, but one of the entries cannot be processed (e.g., due to a malformed timestamp), resulting in
FeedScheduleEntryTimeStampProcessingFailure.
Sourcepub fn get_feedpattern_header_from_database(
&mut self,
profile_id: i32,
) -> Result<SqlFeedpatternHeader, SqlInterfaceError>
pub fn get_feedpattern_header_from_database( &mut self, profile_id: i32, ) -> Result<SqlFeedpatternHeader, SqlInterfaceError>
Retrieves the header (basic information) of a specific feed pattern from the database.
This function queries the database using the provided profile_id to fetch
the feed pattern’s unique ID and its associated name. It strictly expects
exactly one matching header entry.
§Arguments
profile_id- The unique identifier of the feed profile whose header is to be retrieved.
§Returns
A Result which is:
Ok(SqlFeedpatternHeader)if exactly one matching header entry is found.Err(SqlInterfaceError::SingleFeedpatternRequestFailure)if the database query fails.Err(SqlInterfaceError::SingleFeedpatternRequestNoSingleResponse)if the query returns no entry or more than one entry for the givenprofile_id.
Sourcepub fn get_feed_phase_from_database(
&mut self,
profile_id: i32,
phase_nr: i32,
) -> Result<SqlFeedPhase, SqlInterfaceError>
pub fn get_feed_phase_from_database( &mut self, profile_id: i32, phase_nr: i32, ) -> Result<SqlFeedPhase, SqlInterfaceError>
Retrieves the specific data for a single phase of a feed pattern from the database.
This function constructs a query using both the profile_id and the phase_nr
to fetch the detailed configuration for that particular feed phase. It strictly
expects to find exactly one matching phase entry.
§Arguments
profile_id- The unique identifier of the feed profile.phase_nr- The specific phase number (e.g., 1, 2, …, 10) within the feed profile.
§Returns
A Result which is:
Ok(SqlFeedPhase)if exactly one matching phase entry is found for the given profile and phase number.Err(SqlInterfaceError::SingleFeedpatternRequestFailure)if the database query fails.Err(SqlInterfaceError::SingleFeedpatternRequestNoSingleResponse)if the query returns no entry or more than one entry for the specified profile and phase.
Trait Implementations§
Source§impl DatabaseInterfaceFeedTrait for SqlInterfaceFeed
impl DatabaseInterfaceFeedTrait for SqlInterfaceFeed
Source§fn get_current_timestamp(&mut self) -> Result<NaiveDateTime, SqlInterfaceError>
fn get_current_timestamp(&mut self) -> Result<NaiveDateTime, SqlInterfaceError>
Retrieves the current timestamp from the database using the internal connection.
This function acts as a proxy, calling the general SqlInterface::get_current_timestamp
method with the SqlInterfaceFeed’s established database connection.
§Returns
A Result which is:
Ok(NaiveDateTime): The current timestamp as reported by the database.Err(SqlInterfaceError): If any error occurs during the retrieval or parsing of the timestamp from the database.
Source§fn get_past_feedschedule_entries_from_database(
&mut self,
) -> Result<Option<Vec<FeedScheduleEntry>>, SqlInterfaceError>
fn get_past_feedschedule_entries_from_database( &mut self, ) -> Result<Option<Vec<FeedScheduleEntry>>, SqlInterfaceError>
Retrieves feed schedule entries from the database that are scheduled in the past.
This function queries the database for all feed schedule entries whose timestamps
are before the current time. The raw database entries are then post-processed
into FeedScheduleEntry structs.
§Returns
A Result which is:
Ok(Some(Vec<FeedScheduleEntry>))if one or more past feed schedule entries are found.Ok(None)if no past feed schedule entries are found in the database.Err(SqlInterfaceError)if any error occurs during the database query or during the conversion of raw database data intoFeedScheduleEntrystructs.
Source§fn insert_feed_event(
&mut self,
timestamp: NaiveDateTime,
feeder_run_time: f64,
profile_name: String,
profile_id: i32,
) -> Result<(), SqlInterfaceError>
fn insert_feed_event( &mut self, timestamp: NaiveDateTime, feeder_run_time: f64, profile_name: String, profile_id: i32, ) -> Result<(), SqlInterfaceError>
Inserts a new feed event record into the database.
This function logs details about a completed feed operation, including the timestamp, how long the feeder ran, and which feed profile was used.
§Arguments
timestamp- TheNaiveDateTimewhen the feed event occurred.feeder_run_time- The duration (in seconds) the feeder motor was active.profile_name- The name of the feed profile that was executed for this event.profile_id- The unique identifier of the feed profile that was executed.
§Returns
An empty Result (Ok(())) if the feed event record was successfully inserted.
§Errors
Returns SqlInterfaceError::InsertFeedEventFailure 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.
Source§fn get_single_feedpattern_from_database(
&mut self,
profile_id: i32,
) -> Result<Feedpattern, SqlInterfaceError>
fn get_single_feedpattern_from_database( &mut self, profile_id: i32, ) -> Result<Feedpattern, SqlInterfaceError>
Retrieves a complete Feedpattern from the SQL database, including its header and all associated phases.
This function first fetches the header information for the specified profile_id.
It then retrieves data for each of the 10 expected feed phases, converting raw
database flags into booleans.
§Arguments
profile_id- The unique identifier of the feed profile to retrieve.
§Returns
A Result containing a fully constructed Feedpattern object on success.
§Errors
This function will return an error if:
- The query for the feed pattern header fails, the header is not found, or multiple headers are found.
- The query for any of the 10 feed phases fails, a phase is not found, or multiple phases are found.
- A retrieved feed phase contains an invalid (negative) duration value.
Source§fn update_feedschedule_entries_in_database(
&mut self,
feedschedule_entries: &mut Vec<FeedScheduleEntry>,
) -> Result<(), SqlInterfaceError>
fn update_feedschedule_entries_in_database( &mut self, feedschedule_entries: &mut Vec<FeedScheduleEntry>, ) -> Result<(), SqlInterfaceError>
Updates or deletes multiple feed schedule entries in the database.
This function iterates through a list of FeedScheduleEntry structs. For each entry:
- If
repeat_dailyistrue, it updates the entry’s timestamp in the database to tomorrow’s date at the same time. - If
repeat_dailyisfalse, it deletes the entry from the database.
The entire operation is transactional in nature: it will stop and return an error on the first failure.
§Arguments
feedschedule_entries- A mutable reference to a vector ofFeedScheduleEntrystructs to be processed.
§Returns
An empty Result (Ok(())) if all update and delete operations were successful.
§Errors
This function will return an error if an operation fails. This can be:
FeedScheduleUpdateFailure: If fetching tomorrow’s date fails.FeedScheduleDropFailure: If a databaseUPDATEorDELETEcommand fails. This can be due to a lost connection or other database-side issues.