1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
//! Psql Simple resource Traits
use super::get_psql_client;
use super::{util::*, ArrErr};
use crate::grpc::server::ValidationResult;
use crate::grpc::GrpcDataObjectType;
use crate::resources::base::simple_resource::*;
use chrono::{DateTime, Utc};
use tokio_postgres::Row;
use uuid::Uuid;
/// Generic PostgreSQL trait to provide wrappers for common `Resource` functions
#[tonic::async_trait]
pub trait PsqlType
where
Self: Resource + Clone + Sized,
{
/// Get the resource's id column name using the resource's [ResourceDefinition](crate::resources::base::ResourceDefinition)
fn try_get_id_field() -> Result<String, ArrErr> {
psql_debug!(
"(try_get_id_field) Start [{:?}].",
Self::get_definition().psql_id_cols
);
let definition = Self::get_definition();
if definition.psql_id_cols.is_empty() {
let error = format!(
"No id column configured for table {}",
definition.psql_table
);
psql_error!("(try_get_id_field) {}", error);
return Err(ArrErr::Error(error));
}
Ok(definition.psql_id_cols[0].clone())
}
/// Generic get by id function to get a row using the UUID column
async fn get_by_id(id: &Uuid) -> Result<Row, ArrErr> {
psql_debug!("(get_by_id) Start [{:?}].", id);
super::queries::get_by_id::<Self>(id).await
}
/// Generic create function based on resource definition and provided data.
///
/// The data will be validated first, returning all possible errors at once.
/// If no validation errors are found, a new row will be inserted in the database and the new UUID will be returned.
async fn create<'a, T>(data: &T) -> Result<(Option<Uuid>, ValidationResult), ArrErr>
where
T: GrpcDataObjectType,
{
psql_debug!("(create) Start [{:?}].", data);
let (psql_data, validation_result) = validate::<Self>(data)?;
if !validation_result.success {
return Ok((None, validation_result));
}
let definition = Self::get_definition();
let id_col = Self::try_get_id_field()?;
let (inserts, fields, params) = get_insert_vars(data, &psql_data, &definition, false)?;
let insert_sql = &format!(
r#"INSERT INTO "{}" ({}) VALUES ({}) RETURNING "{}""#,
definition.psql_table,
fields.join(", "),
inserts.join(", "),
id_col
);
psql_debug!("(create) [{}].", insert_sql);
psql_debug!("(create) [{:?}].", ¶ms);
psql_info!(
"(create) Inserting new entry for table [{}].",
definition.psql_table
);
let client = get_psql_client().await?;
let row = client.query_one(insert_sql, ¶ms[..]).await?;
Ok((Some(row.get(&*id_col)), validation_result))
}
}
/// Generic trait for the Arrow Resources that are stored in the CockroachDB backend.
/// TODO Rust 1.74: use `#![feature(async_fn_in_trait)]` once available: <https://blog.rust-lang.org/inside-rust/2023/05/03/stabilizing-async-fn-in-trait.html>
#[tonic::async_trait]
pub trait PsqlObjectType<T>
where
Self: Send + SimpleResource<T>,
T: GrpcDataObjectType,
{
/// get data from the database using the Object's UUID
///
/// # Errors
///
/// returns [Row] on success
async fn read(&self) -> Result<Row, ArrErr> {
psql_debug!("(read) Start [{:?}].", self.try_get_uuid());
//TODO(R4): implement shared memcache here to get object data if present
let id = self.try_get_uuid()?;
Self::get_by_id(&id).await
}
/// Update the Object's database record using provided data
///
/// returns [Option(Row)] and [ValidationResult]
///
/// # Errors
/// Returns [`ArrErr`] Validation "'GrpcField::Option'" mismatch error if the database scheme does not match the gRPC struct.
/// Returns [`ArrErr`] Validation "Conversion error, unknown field type" if the provided field type could not be matched.
/// Returns [`ArrErr`] "No id column configured for table" id_col could not be found
/// Returns [`ArrErr`] if the `id` [`String`] could not be converted to a valid [`Uuid`]
/// Returns [`ArrErr`] from [`PoolError`](deadpool::managed::PoolError) if no client connection could be returned from the connection [`Pool`](deadpool::managed::Pool)
/// Returns [`ArrErr`] Database Error if database query execution failed
async fn update<'a>(&self, data: &T) -> Result<(Option<Row>, ValidationResult), ArrErr> {
psql_debug!("(update) Start [{:?}].", data);
let (psql_data, validation_result) = validate::<Self>(data)?;
if !validation_result.success {
return Ok((None, validation_result));
}
let definition = Self::get_definition();
let id_col = Self::try_get_id_field()?;
let id = self.try_get_uuid()?;
let (mut updates, mut params) = get_update_vars(data, &psql_data, &definition)?;
if definition.has_field("updated_at") {
updates.push(r#""updated_at" = NOW()"#.to_string());
}
let update_sql = &format!(
"UPDATE {} SET {} WHERE {} = ${}",
definition.psql_table,
updates.join(", "),
id_col,
params.len() + 1
);
params.push(&id);
psql_info!(
"(update) Updating entry in table [{}]. uuid: {}",
definition.psql_table,
id
);
psql_debug!("(update) [{}].", update_sql);
psql_debug!("(update) [{:?}].", ¶ms);
let client = get_psql_client().await?;
client.execute(update_sql, ¶ms[..]).await?;
//TODO(R4): flush shared memcache for this resource when memcache is implemented
Ok((Some(self.read().await?), validation_result))
}
/// Returns `true` if the resource has a `deleted_at` field and if it's [`Some`]
///
/// Returns `false` otherwise
async fn is_archived(&self) -> bool {
let data = match self.read().await {
Ok(data) => data,
Err(_) => {
return false;
}
};
match data.try_get::<&str, Option<DateTime<Utc>>>("deleted_at") {
Ok(value) => value.is_some(),
Err(_) => false,
}
}
/// Calls [set_deleted_at_now](PsqlObjectType::set_deleted_at_now) if the Object has a `deleted_at` field
///
/// Calls [delete_row](PsqlObjectType::delete_row) otherwise
async fn delete(&self) -> Result<(), ArrErr> {
psql_debug!("(delete) Start.");
let definition = Self::get_definition();
if definition.fields.contains_key("deleted_at") {
self.set_deleted_at_now().await
} else {
self.delete_row().await
}
}
/// Updates the database record setting the `deleted_at` field to current timestamp using the Object's UUID
///
/// # Errors
///
/// Returns [`ArrErr`] "No id column configured for table" id_col could not be found
/// Returns [`ArrErr`] if the `id` [`String`] could not be converted to a valid [`Uuid`]
/// Returns [`ArrErr`] "\[deleted_at\] column is already set" if [`is_archived`](Self::is_archived) returned `true`
/// Returns [`ArrErr`] from [`PoolError`](deadpool::managed::PoolError) if no client connection could be returned from the connection [`Pool`](deadpool::managed::Pool)
/// Returns [`ArrErr`] "Failed to update \[deleted_at\] col" if database query execution returns zero updated rows
/// Returns [`ArrErr`] Database Error if database query execution failed
async fn set_deleted_at_now(&self) -> Result<(), ArrErr> {
psql_debug!("(set_deleted_at_now) Start [{:?}].", self.try_get_uuid());
let definition = Self::get_definition();
let id_col = Self::try_get_id_field()?;
let id = self.try_get_uuid()?;
if self.is_archived().await {
psql_info!(
"(set_deleted_at_now) [deleted_at] column is already set, refusing to overwrite for [{}]. uuid: {}",
definition.psql_table,
id
);
return Err(ArrErr::Error(
"(set_deleted_at_now) [deleted_at] column is already set, will not overwrite."
.to_owned(),
));
}
psql_info!(
"(set_deleted_at_now) Updating [deleted_at] field for [{}]. uuid: {}",
definition.psql_table,
id
);
let client = get_psql_client().await?;
let query = format!(
r#"UPDATE "{}" SET "deleted_at" = NOW() WHERE "{}" = $1"#,
definition.psql_table, id_col
);
let stmt = client.prepare_cached(&query).await?;
match client.execute(&stmt, &[&id]).await {
Ok(num_rows) => {
if num_rows == 1 {
//TODO(R4): flush shared memcache for this resource when memcache is implemented
Ok(())
} else {
let error = format!(
"Failed to update [deleted_at] col for [{}] with id [{}] (does not exist?).",
definition.psql_table, id
);
psql_info!("(set_deleted_at_now) {}", error);
Err(ArrErr::Error(error))
}
}
Err(e) => Err(e.into()),
}
}
/// Delete database record from the database using the Object's UUID
///
/// # Errors
///
/// Returns [`ArrErr`] "No id column configured for table" id_col could not be found
/// Returns [`ArrErr`] if the `id` [`String`] could not be converted to a valid [`Uuid`]
/// Returns [`ArrErr`] from [`PoolError`](deadpool::managed::PoolError) if no client connection could be returned from the connection [`Pool`](deadpool::managed::Pool)
/// Returns [`ArrErr`] "Failed to delete entry" if database query execution returns zero updated rows
/// Returns [`ArrErr`] Database Error if database query execution failed
async fn delete_row(&self) -> Result<(), ArrErr> {
psql_debug!("(delete_row) Start [{:?}].", self.try_get_uuid());
let definition = Self::get_definition();
let id_col = Self::try_get_id_field()?;
let id = self.try_get_uuid()?;
psql_info!(
"(delete_row) Deleting entry from table [{}]. uuid: {}",
definition.psql_table,
id
);
let client = get_psql_client().await?;
let query = format!(
r#"DELETE FROM "{}" WHERE "{}" = $1"#,
definition.psql_table, id_col
);
let stmt = client.prepare_cached(&query).await?;
match client.execute(&stmt, &[&id]).await {
Ok(num_rows) => {
if num_rows == 1 {
//TODO(R4): flush shared memcache for this resource when memcache is implemented
Ok(())
} else {
let error = format!(
"Failed to delete entry for [{}] with id [{}] (does not exist?).",
definition.psql_table, id
);
psql_info!("(delete_row) {}", error);
Err(ArrErr::Error(error))
}
}
Err(e) => Err(e.into()),
}
}
}