Function ccntool_core::myquery

source ·
pub fn myquery(conn: Pool<MySql>, notes: &str) -> Result<Vec<String>, Error>
Expand description

Executes a SQL query against the dcim database and returns a vector of strings containing the results. The query looks for the first port in the fac_Ports table with a matching Notes field to the notes argument, and returns various details about the associated switch and port. If no matching row is found, an error is returned.

Arguments

  • conn - A Pool<MySql> representing a connection to the dcim database.
  • notes - A &str containing the Notes field value to match against in the fac_Ports table.

Returns

A Result containing a vector of strings, where each string represents a different detail about the switch and port. The vector contains the following fields, in order:

  • results[0] - Switch hostname (Label field from fac_Device table)
  • results[1] - Port description (Notes field from fac_Ports table)
  • results[2] - Switch port (Label field from fac_Ports table)
  • results[3] - Switch IP address (PrimaryIP field from fac_Device table)
  • results[4] - Switch device ID (DeviceID field from fac_Device table)

Errors

Returns an error of type sqlx::Error if no matching row is found in the fac_Ports table.

Examples

fn main() {
    let results = match myquery(connectdb(None, None, None), "01.1.001-1") {
        Ok(rows) => {
           self.error.clear();
           rows
        }
        Err(error) => {
           let error_message = format!("Received garbage: {error}");
           self.error = error_message;
           eprintln!("Error: {error}");
           return;
        }
    };
    ..
}