Function ccntool_core::connectdb
source · pub fn connectdb(
un: Option<String>,
pw: Option<String>,
burl: Option<String>
) -> Result<Pool<MySql>, Error>
Expand description
Establishes a connection to a MySQL database using provided credentials and base URL, either passed along the function or via dotenvy.
Arguments
un
- An optionalString
representing the username to use for the database connection. If not provided, the function will attempt to retrieve the username from theDCIMUSER
environment variable.pw
- An optionalString
representing the password to use for the database connection. If not provided, the function will attempt to retrieve the password from theDCIMPASSWORD
environment variable.burl
- An optionalString
representing the base URL of the database server. If not provided, the function will attempt to retrieve the base URL from theDCIMHOST
environment variable.
Returns
Returns a Result
containing a Pool<MySql>
if the connection was
successful, or an sqlx::Error
if an error occurred.
Examples
use ccntool_core::*;
#[tokio::main]
async fn main() {
// Username, Password and Hostname are passed via dotenvy
let conn = match connectdb(None, None, None) {
Ok(pool) => pool,
Err(error) => {
let error_message = format!("Connection timeout: {error}");
self.error = error_message;
eprintln!("Error: {}", error);
return;
}
};
..
}