use sqlx_template::{SqliteTemplate, sqlite_query};
use sqlx::{FromRow, SqlitePool};
#[derive(SqliteTemplate, FromRow, Debug, Clone)]
#[table("users")]
#[tp_select_builder(
with_email_domain = "email LIKE :domain$String",
with_score_range = "score BETWEEN :min$i32 AND :max$i32"
)]
pub struct User {
#[auto]
pub id: i32,
pub email: String,
pub score: i32,
pub active: bool,
pub created_at: String,
pub name: String,
}
#[sqlite_query(
r#"
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
score INTEGER NOT NULL,
active BOOLEAN NOT NULL,
created_at TEXT NOT NULL,
name TEXT NOT NULL
)
"#
)]
async fn create_users_table() {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing all builders with custom conditions");
let pool = SqlitePool::connect(":memory:").await?;
create_users_table(&pool).await?;
let alice = User {
id: 0, email: "alice@company.com".to_string(),
score: 85,
active: true,
created_at: "2023-01-01".to_string(),
name: "Alice".to_string(),
};
User::insert(&alice, &pool).await?;
let bob = User {
id: 0, email: "bob@personal.com".to_string(),
score: 65,
active: false,
created_at: "2022-01-01".to_string(),
name: "Bob".to_string(),
};
User::insert(&bob, &pool).await?;
println!("\n=== SELECT Builder Tests ===");
println!("SQL: {}", User::builder_select().build_sql());
let users = User::builder_select()
.with_email_domain("%@company.com")?
.find_all(&pool)
.await?;
println!("Users with company email: {} found", users.len());
let users = User::builder_select()
.with_score_range(60, 90)?
.find_all(&pool)
.await?;
println!("Users with score 60-90: {} found", users.len());
let sql = User::builder_select().active(&true).unwrap().build_sql();
println!("\nGenerated SQL: {}", sql);
assert!(sql.contains("SELECT id, email, score, active, created_at, name FROM"));
assert!(!sql.contains("SELECT * FROM"));
println!("✅ SELECT uses column list instead of *");
Ok(())
}