use sqlx_template::{SqliteTemplate, sqlite_query};
use sqlx::{FromRow, SqlitePool};
#[derive(SqliteTemplate, FromRow, Debug, Clone)]
#[table("users")]
#[tp_select_builder(
with_name_and_score = "name = :name$String AND score > :min_score$i32",
with_complex_condition = "score * score > :threshold$i32 AND active = :active$bool",
with_like_pattern = "name LIKE :pattern$String"
)]
pub struct User {
#[auto]
pub id: i32,
pub name: String,
pub score: i32,
pub active: bool,
}
#[sqlite_query(
r#"
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
score INTEGER NOT NULL,
active BOOLEAN NOT NULL
)
"#
)]
async fn create_users_table() {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing complex SQL expressions");
let pool = SqlitePool::connect(":memory:").await?;
create_users_table(&pool).await?;
let test_users = vec![
User {
id: 0, name: "Alice".to_string(),
score: 15,
active: true,
},
User {
id: 0, name: "Bob".to_string(),
score: 8,
active: false,
},
User {
id: 0, name: "Charlie".to_string(),
score: 12,
active: true,
},
];
for user in test_users {
User::insert(&user, &pool).await?;
}
println!("Testing with_name_and_score:");
let users = User::builder_select()
.with_name_and_score("Alice", 10)?
.find_all(&pool)
.await?;
println!("Found {} users with name Alice and score > 10", users.len());
println!("Testing with_complex_condition:");
let users = User::builder_select()
.with_complex_condition(100, true)?
.find_all(&pool)
.await?;
println!("Found {} users with score^2 > 100 and active = true", users.len());
println!("Testing with_like_pattern:");
let users = User::builder_select()
.with_like_pattern("A%")?
.find_all(&pool)
.await?;
println!("Found {} users with name starting with 'A'", users.len());
Ok(())
}