use super::get_psql_client;
use super::ArrErr;
use crate::postgres::{PsqlField, PsqlFieldSend};
use crate::resources::base::Resource;
use std::collections::HashMap;
use std::vec;
use tokio_postgres::Row;
use uuid::Uuid;
pub async fn get_by_id<V>(id: &Uuid) -> Result<Row, ArrErr>
where
V: Resource + super::simple_resource::PsqlType,
{
psql_debug!("(get_by_id) Start: {:?}", id);
let definition = V::get_definition();
let id_col = V::try_get_id_field()?;
let client = get_psql_client().await?;
let query = format!(
r#"SELECT * FROM "{}" WHERE "{}" = $1"#,
definition.psql_table, id_col
);
let stmt = client.prepare_cached(&query).await?;
psql_info!(
"(get_by_id) Fetching row data for table [{}]. uuid: {}",
definition.psql_table,
id
);
psql_debug!("(get_by_id) [{}].", &query);
match client.query_one(&stmt, &[&id]).await {
Ok(row) => Ok(row),
Err(e) => Err(e.into()),
}
}
pub async fn get_for_ids<V>(ids: &HashMap<String, Uuid>) -> Result<Row, ArrErr>
where
V: Resource,
{
psql_debug!("(get_for_ids) Start: {:?}", ids);
let definition = V::get_definition();
let mut params: Vec<Box<PsqlFieldSend>> = vec![];
let mut query = format!(r#"SELECT * FROM "{}""#, definition.psql_table);
let mut search_operator = "WHERE";
let mut next_param_index: i32 = 1;
for (field, value) in ids.clone() {
if V::has_id_col(&field) {
query.push_str(&format!(
r#" {} "{}" = ${}"#,
search_operator, field, next_param_index
));
params.push(Box::new(value));
search_operator = "AND";
next_param_index += 1;
}
}
let client = get_psql_client().await?;
let stmt = client.prepare_cached(&query).await?;
psql_info!(
"(get_for_ids) Fetching row data for table [{}]. uuids: {:?}",
definition.psql_table,
ids
);
psql_debug!("(get_for_ids) [{}].", &query);
psql_debug!("(get_for_ids) [{:?}].", ¶ms);
let mut ref_params: Vec<&PsqlField> = vec![];
for field in params.iter() {
ref_params.push(field.as_ref());
}
match client.query_one(&stmt, &ref_params[..]).await {
Ok(row) => Ok(row),
Err(e) => Err(e.into()),
}
}