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 optional String representing the username to use for the database connection. If not provided, the function will attempt to retrieve the username from the DCIMUSER environment variable.
  • pw - An optional String representing the password to use for the database connection. If not provided, the function will attempt to retrieve the password from the DCIMPASSWORD environment variable.
  • burl - An optional String representing the base URL of the database server. If not provided, the function will attempt to retrieve the base URL from the DCIMHOST 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;
        }
    };
    ..
}