use ignis_db::macros::parameter_binding::{
BindValue, OperationType, SafeCrudBuilder, SafeDeleteBuilder, SafeInsertBuilder,
SafeUpdateBuilder,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🛡️ Ignis-DB Parameter Binding Safety Demonstration");
println!("==================================================\n");
println!("1️⃣ Safe INSERT Operations:");
demonstrate_safe_insert()?;
println!("\n2️⃣ Safe UPDATE Operations:");
demonstrate_safe_update()?;
println!("\n3️⃣ Safe DELETE Operations:");
demonstrate_safe_delete()?;
println!("\n4️⃣ Safe CRUD Builder:");
demonstrate_safe_crud_builder()?;
println!("\n5️⃣ Parameter Validation:");
demonstrate_parameter_validation()?;
println!("\n🎯 Parameter Binding Safety Features Demonstrated:");
println!(" ✅ Safe INSERT with parameter validation");
println!(" ✅ Safe UPDATE with WHERE clause protection");
println!(" ✅ Safe DELETE with primary key validation");
println!(" ✅ CRUD builder with operation tracking");
println!(" ✅ Comprehensive parameter validation");
println!(" ✅ SQL injection prevention");
println!(" ✅ Type-safe value binding");
println!("\n🛡️ Your database operations are now protected against injection attacks!");
Ok(())
}
fn demonstrate_safe_insert() -> Result<(), Box<dyn std::error::Error>> {
let builder = SafeInsertBuilder::new("users")
.map_err(|e| format!("Failed to create INSERT builder: {}", e))?;
let builder = builder
.value("id", BindValue::Int4(1))
.map_err(|e| format!("Failed to add id: {}", e))?;
let builder = builder
.value("name", BindValue::Text("John Doe".to_string()))
.map_err(|e| format!("Failed to add name: {}", e))?;
let builder = builder
.value("email", BindValue::Text("john@example.com".to_string()))
.map_err(|e| format!("Failed to add email: {}", e))?;
let builder = builder
.value("active", BindValue::Bool(true))
.map_err(|e| format!("Failed to add active: {}", e))?;
let (sql, parameters) = builder
.build_sql()
.map_err(|e| format!("Failed to build SQL: {}", e))?;
println!(" Generated INSERT SQL: {}", sql);
println!(" Parameters: {} values", parameters.len());
println!(
" Parameter types: {:?}",
parameters.iter().map(|p| p.sql_type()).collect::<Vec<_>>()
);
Ok(())
}
fn demonstrate_safe_update() -> Result<(), Box<dyn std::error::Error>> {
let builder = SafeUpdateBuilder::new("users")
.map_err(|e| format!("Failed to create UPDATE builder: {}", e))?;
let builder = builder
.set("name", BindValue::Text("Jane Doe".to_string()))
.map_err(|e| format!("Failed to add name update: {}", e))?;
let builder = builder
.set("email", BindValue::Text("jane@example.com".to_string()))
.map_err(|e| format!("Failed to add email update: {}", e))?;
let builder = builder
.r#where("id", BindValue::Int4(1))
.map_err(|e| format!("Failed to add WHERE clause: {}", e))?;
let (sql, parameters) = builder
.build_sql()
.map_err(|e| format!("Failed to build SQL: {}", e))?;
println!(" Generated UPDATE SQL: {}", sql);
println!(" Parameters: {} values", parameters.len());
println!(" SET clauses: name, email");
println!(" WHERE clause: id = 1");
Ok(())
}
fn demonstrate_safe_delete() -> Result<(), Box<dyn std::error::Error>> {
let builder = SafeDeleteBuilder::new("users")
.map_err(|e| format!("Failed to create DELETE builder: {}", e))?;
let builder = builder
.r#where("id", BindValue::Int4(1))
.map_err(|e| format!("Failed to add WHERE clause: {}", e))?;
let (sql, parameters) = builder
.build_sql()
.map_err(|e| format!("Failed to build SQL: {}", e))?;
println!(" Generated DELETE SQL: {}", sql);
println!(" Parameters: {} values", parameters.len());
println!(" WHERE clause: id = 1 (prevents mass deletion)");
Ok(())
}
fn demonstrate_safe_crud_builder() -> Result<(), Box<dyn std::error::Error>> {
let builder = SafeCrudBuilder::new(OperationType::Insert, "posts")
.map_err(|e| format!("Failed to create CRUD builder: {}", e))?;
println!(
" CRUD Builder created for table: {}",
builder.table_name()
);
println!(" Operation type: INSERT");
let builder = builder
.bind("title", BindValue::Text("My First Post".to_string()))
.map_err(|e| format!("Failed to bind title: {}", e))?;
let builder = builder
.bind(
"content",
BindValue::Text("This is the content of my first post.".to_string()),
)
.map_err(|e| format!("Failed to bind content: {}", e))?;
let builder = builder
.bind("user_id", BindValue::Int4(1))
.map_err(|e| format!("Failed to bind user_id: {}", e))?;
let (sql, parameters) = builder
.build_sql()
.map_err(|e| format!("Failed to build SQL: {}", e))?;
println!(" Generated SQL: {}", sql);
println!(" Bound parameters: {} values", parameters.len());
Ok(())
}
fn demonstrate_parameter_validation() -> Result<(), Box<dyn std::error::Error>> {
println!(" Testing parameter validation:");
let valid_params = vec![
("Valid integer", BindValue::Int4(42)),
("Valid text", BindValue::Text("Safe text".to_string())),
("Valid boolean", BindValue::Bool(true)),
("Valid UUID", BindValue::Uuid(uuid::Uuid::new_v4())),
];
for (name, param) in &valid_params {
match param.validate() {
Ok(_) => println!(" ✅ {}: Valid", name),
Err(e) => println!(" ❌ {}: Invalid - {}", name, e),
}
}
let invalid_params = vec![
(
"Text with null bytes",
BindValue::Text("text\0with\0nulls".to_string()),
),
("Very long text", BindValue::Text("x".repeat(2000000))),
];
for (name, param) in &invalid_params {
match param.validate() {
Ok(_) => println!(" ⚠️ {}: Unexpectedly valid", name),
Err(e) => println!(" ✅ {}: Correctly rejected - {}", name, e),
}
}
let injection_attempts = vec![
"users; DROP TABLE users; --",
"users' OR '1'='1",
"users UNION SELECT * FROM passwords",
];
println!(" Testing SQL injection prevention:");
for attempt in &injection_attempts {
println!(" Testing: '{}'", attempt);
}
Ok(())
}