use sqlx_template::{SqliteTemplate, sqlite_query};
use sqlx::{FromRow, SqlitePool};
#[derive(SqliteTemplate, FromRow, Debug, Clone)]
#[table("users")]
#[tp_select_builder(
with_email = "email = :email$String",
with_active = "active = :active",
with_score = "score * score > :min_score$i32"
)]
pub struct User {
#[auto]
pub id: i32,
pub email: String,
pub active: bool,
pub score: i32,
pub name: String,
}
#[sqlite_query(
r#"
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
active BOOLEAN NOT NULL,
score INTEGER NOT NULL,
name TEXT NOT NULL
)
"#
)]
async fn create_users_table() {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let pool = SqlitePool::connect(":memory:").await?;
sqlx::query(
r#"
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
active BOOLEAN NOT NULL,
score INTEGER NOT NULL,
name TEXT NOT NULL
)
"#,
)
.execute(&pool)
.await?;
sqlx::query(
"INSERT INTO users (email, active, score, name) VALUES (?, ?, ?, ?)"
)
.bind("user1@example.com")
.bind(true)
.bind(15) .bind("User 1")
.execute(&pool)
.await?;
sqlx::query(
"INSERT INTO users (email, active, score, name) VALUES (?, ?, ?, ?)"
)
.bind("user2@example.com")
.bind(false)
.bind(8) .bind("User 2")
.execute(&pool)
.await?;
sqlx::query(
"INSERT INTO users (email, active, score, name) VALUES (?, ?, ?, ?)"
)
.bind("user3@example.com")
.bind(true)
.bind(12) .bind("User 3")
.execute(&pool)
.await?;
println!("Testing with_email condition:");
let users = User::builder_select()
.with_email("user1@example.com")?
.find_all(&pool)
.await?;
println!("Found {} users with email user1@example.com", users.len());
for user in users {
println!(" - {}: {} (active: {}, score: {})", user.id, user.name, user.active, user.score);
}
println!("\nTesting with_active condition:");
let users = User::builder_select()
.with_active(true)?
.find_all(&pool)
.await?;
println!("Found {} active users", users.len());
for user in users {
println!(" - {}: {} (email: {}, score: {})", user.id, user.name, user.email, user.score);
}
println!("\nTesting with_score condition:");
let users = User::builder_select()
.with_score(100)?
.find_all(&pool)
.await?;
println!("Found {} users with score^2 > 100", users.len());
for user in users {
println!(" - {}: {} (email: {}, score: {}, score^2: {})",
user.id, user.name, user.email, user.score, user.score * user.score);
}
println!("\nTesting combined conditions:");
let users = User::builder_select()
.with_active(true)?
.with_score(100)?
.find_all(&pool)
.await?;
println!("Found {} active users with score^2 > 100", users.len());
for user in users {
println!(" - {}: {} (email: {}, score: {})", user.id, user.name, user.email, user.score);
}
Ok(())
}