#[macro_export]
macro_rules! impl_builder_model {
($struct_name:ident, $table_builder:expr) => {
impl $crate::IgnisModel for $struct_name {
fn table_name() -> &'static str {
stringify!($struct_name).to_lowercase().as_str()
}
fn primary_key() -> &'static str {
"id" }
fn schema() -> Option<&'static str> {
None }
fn get_primary_key_value(&self) -> $crate::anyhow::Result<$crate::BindValue> {
Err($crate::anyhow::anyhow!(
"get_primary_key_value must be implemented for {}",
stringify!($struct_name)
))
}
}
};
($struct_name:ident, $table_builder:expr, $pk_field:expr) => {
impl $crate::IgnisModel for $struct_name {
fn table_name() -> &'static str {
stringify!($struct_name).to_lowercase().as_str()
}
fn primary_key() -> &'static str {
$pk_field
}
fn schema() -> Option<&'static str> {
None
}
fn get_primary_key_value(&self) -> $crate::anyhow::Result<$crate::BindValue> {
Err($crate::anyhow::anyhow!(
"get_primary_key_value must be implemented for {}",
stringify!($struct_name)
))
}
}
};
}
#[macro_export]
macro_rules! impl_builder_insertable {
($struct_name:ident) => {
impl $crate::Insertable for $struct_name {
fn table_name() -> &'static str {
<Self as $crate::IgnisModel>::table_name()
}
fn primary_key() -> &'static str {
<Self as $crate::IgnisModel>::primary_key()
}
fn schema() -> Option<&'static str> {
<Self as $crate::IgnisModel>::schema()
}
}
};
}
#[macro_export]
macro_rules! impl_builder_updatable {
($struct_name:ident) => {
impl $crate::Updatable for $struct_name {
fn table_name() -> &'static str {
<Self as $crate::IgnisModel>::table_name()
}
fn primary_key() -> &'static str {
<Self as $crate::IgnisModel>::primary_key()
}
fn schema() -> Option<&'static str> {
<Self as $crate::IgnisModel>::schema()
}
}
};
}
#[macro_export]
macro_rules! impl_builder_deletable {
($struct_name:ident) => {
impl $crate::Deletable for $struct_name {
fn table_name() -> &'static str {
<Self as $crate::IgnisModel>::table_name()
}
fn primary_key() -> &'static str {
<Self as $crate::IgnisModel>::primary_key()
}
fn schema() -> Option<&'static str> {
<Self as $crate::IgnisModel>::schema()
}
}
};
}
#[macro_export]
macro_rules! impl_builder_crud {
($struct_name:ident) => {
$crate::impl_builder_model!($struct_name, table(stringify!($struct_name).to_lowercase()));
$crate::impl_builder_insertable!($struct_name);
$crate::impl_builder_updatable!($struct_name);
$crate::impl_builder_deletable!($struct_name);
};
($struct_name:ident, $table_name:expr) => {
$crate::impl_builder_model!($struct_name, table($table_name));
$crate::impl_builder_insertable!($struct_name);
$crate::impl_builder_updatable!($struct_name);
$crate::impl_builder_deletable!($struct_name);
};
($struct_name:ident, $table_name:expr, $pk_field:expr) => {
$crate::impl_builder_model!($struct_name, table($table_name), $pk_field);
$crate::impl_builder_insertable!($struct_name);
$crate::impl_builder_updatable!($struct_name);
$crate::impl_builder_deletable!($struct_name);
};
}
pub mod type_mapping {
use crate::schema::ColumnType;
pub fn rust_to_pg_type<T>() -> ColumnType {
ColumnType::Text
}
pub fn pg_to_rust_type(column_type: &ColumnType) -> &'static str {
match column_type {
ColumnType::Int2 => "i16",
ColumnType::Int4 => "i32",
ColumnType::Int8 => "i64",
ColumnType::Text => "String",
ColumnType::Varchar(_) => "String",
ColumnType::Bool => "bool",
ColumnType::Uuid => "uuid::Uuid",
ColumnType::Timestamptz => "chrono::DateTime<chrono::Utc>",
ColumnType::Date => "chrono::NaiveDate",
ColumnType::Time => "chrono::NaiveTime",
ColumnType::Json => "serde_json::Value",
ColumnType::Jsonb => "serde_json::Value",
ColumnType::Numeric(_, _) => "rust_decimal::Decimal",
_ => "String", }
}
}
pub mod safe_sql {
use crate::common::validate_identifier;
pub fn safe_select(table: &str, columns: &[&str], where_clause: &str) -> String {
let table_safe = validate_identifier(table).unwrap_or_else(|| table.to_string());
let columns_safe: Vec<String> = columns
.iter()
.map(|col| validate_identifier(col).unwrap_or_else(|| col.to_string()))
.collect();
format!(
"SELECT {} FROM {} WHERE {}",
columns_safe.join(", "),
table_safe,
where_clause
)
}
pub fn safe_insert(table: &str, columns: &[&str]) -> String {
let table_safe = validate_identifier(table).unwrap_or_else(|| table.to_string());
let columns_safe: Vec<String> = columns
.iter()
.map(|col| validate_identifier(col).unwrap_or_else(|| col.to_string()))
.collect();
let placeholders: Vec<String> = (0..columns.len()).map(|_| "?".to_string()).collect();
format!(
"INSERT INTO {} ({}) VALUES ({})",
table_safe,
columns_safe.join(", "),
placeholders.join(", ")
)
}
pub fn safe_update(table: &str, columns: &[&str], where_clause: &str) -> String {
let table_safe = validate_identifier(table).unwrap_or_else(|| table.to_string());
let columns_safe: Vec<String> = columns
.iter()
.map(|col| {
format!(
"{} = ?",
validate_identifier(col).unwrap_or_else(|| col.to_string())
)
})
.collect();
format!(
"UPDATE {} SET {} WHERE {}",
table_safe,
columns_safe.join(", "),
where_clause
)
}
pub fn safe_delete(table: &str, where_clause: &str) -> String {
let table_safe = validate_identifier(table).unwrap_or_else(|| table.to_string());
format!("DELETE FROM {} WHERE {}", table_safe, where_clause)
}
}