aquarium_control/database/
sql_interface_config.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9//! Defines the configuration structure for the application's SQL database interface.
10//!
11//! This module contains the `SqlInterfaceConfig` struct, which is a direct representation
12//! of the `[sql_interface]` section in the application's TOML configuration file. It serves
13//! as a data container for all settings required to establish, manage, and validate the
14//! database connection.
15//!
16//! ## Key Configuration Areas
17//!
18//! The struct is organized into several logical groups of parameters:
19//!
20//! - **Connection Details**: Fields like `db_user`, `db_password`, `db_host`, `db_port`,
21//!   and `db_name` provide the essential credentials for connecting to the MySQL server.
22//!
23//! - **Startup Validation**:
24//!   - `db_tables`: A list of table names that the application expects to exist. The
25//!     `SqlInterface` will verify their presence at startup.
26//!   - `db_min_wait_timeout`: Specifies the minimum required `wait_timeout` on the
27//!     MySQL server to prevent premature connection drops. A value of `0` deactivates this check.
28//!   - `max_rows_*`: A series of fields (`max_rows_balling_set_values`, `max_rows_data`, etc.)
29//!     that define the maximum acceptable number of rows for various log and configuration
30//!     tables. This acts as a safeguard against uncontrolled table growth. A value of `0` deactivates the check for a specific table.
31//!
32//! - **Operational Parameters**:
33//!   - `db_ping_interval`: The interval, in seconds, at which the application should
34//!     ping the database to keep the connection alive and prevent server-side timeouts.
35//!
36//! ## Usage
37//!
38//! An instance of `SqlInterfaceConfig` is created by deserializing the
39//! `[sql_interface]` section of a `.toml` file. This instance is then passed to
40//! `SqlInterface::new()` to initialize the main database connection pool for the
41//! entire application.
42
43use serde_derive::Deserialize;
44
45/// This struct holds the configuration data for the SQL interface.
46/// The configuration is loaded from the .toml configuration file.
47/// This struct does not contain any implementation.
48#[derive(Deserialize, Clone)]
49pub struct SqlInterfaceConfig {
50    /// database user
51    pub db_user: String,
52
53    /// database password
54    pub db_password: String,
55
56    /// hostname of the database
57    pub db_host: String,
58
59    /// port name of the database
60    pub db_port: u16,
61
62    /// database name
63    pub db_name: String,
64
65    /// array of required tables
66    pub db_tables: Vec<String>,
67
68    /// minimum wait timout - application checks server variable before execution
69    pub db_min_wait_timeout: u64,
70
71    /// the database-ping interval in seconds
72    pub db_ping_interval: u64,
73
74    /// maximum allowed rows for balling set value table
75    pub max_rows_balling_set_values: u64,
76
77    /// maximum allowed rows for feed pattern table
78    pub max_rows_feed_pattern: u64,
79
80    /// maximum allowed rows for feed schedule table
81    pub max_rows_feed_schedule: u64,
82
83    /// maximum allowed rows for heating stats table
84    pub max_rows_heating_stats: u64,
85
86    /// maximum allowed rows for schedule table
87    pub max_rows_schedule: u64,
88
89    /// maximum allowed rows for data table
90    pub max_rows_data: u64,
91
92    /// maximum allowed rows for refill log
93    pub max_rows_refill: u64,
94
95    /// maximum allowed rows for Balling dosing log
96    pub max_rows_balling_dosing_log: u64,
97
98    /// maximum allowed rows for Feed log
99    pub max_rows_feed_log: u64,
100}