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",
with_active_status = "active = :active",
with_complex_condition = "score > :min_score$i32 AND created_at > :since$String"
)]
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 Documentation Generation");
println!("This example demonstrates the improved documentation for builder methods");
let pool = SqlitePool::connect(":memory:").await?;
create_users_table(&pool).await?;
let alice = User {
id: 0, email: "alice@company.com".to_string(),
score: 95,
active: true,
created_at: "2023-01-01".to_string(),
name: "Alice".to_string(),
};
User::insert(&alice, &pool).await?;
println!("\n🔧 Testing Builder Methods with Documentation:");
println!("\n1. Field-based methods:");
let _builder = User::builder_select()
.email("alice@company.com")? .score_gte(&80)? .active(&true)? .order_by_score_desc()?;
println!(" ✅ Field methods: .email(), .score_gte(), .active(), .order_by_score_desc()");
println!("\n2. Custom condition methods:");
let _builder = User::builder_select()
.with_email_domain("%@company.com")? .with_score_range(60, 90)? .with_active_status(true)? .with_complex_condition(75, "2022-01-01")?;
println!(" ✅ Custom methods: .with_email_domain(), .with_score_range(), .with_active_status(), .with_complex_condition()");
let users = User::builder_select()
.with_email_domain("%@company.com")?
.with_active_status(true)?
.find_all(&pool)
.await?;
println!("\n📊 Query Results:");
println!(" Found {} users matching conditions", users.len());
for user in users {
println!(" - {}: {} (score: {}, active: {})", user.name, user.email, user.score, user.active);
}
println!("\n📝 Documentation Features:");
println!("✅ Custom condition methods show actual SQL expressions");
println!("✅ Parameter documentation includes parameter names and types");
println!("✅ Field-based methods have descriptive documentation");
println!("✅ Builder pattern methods are properly documented");
println!("✅ ORDER BY methods include direction information");
println!("\n🎯 Documentation Examples:");
println!("- with_email_domain(): Custom WHERE condition: `email LIKE :domain$String`");
println!("- with_score_range(): Custom WHERE condition: `score BETWEEN :min$i32 AND :max$i32`");
println!("- with_complex_condition(): Custom WHERE condition: `score > :min_score$i32 AND created_at > :since$String`");
println!("\n💡 To see full documentation, use:");
println!(" cargo doc --open");
println!(" Then navigate to the User struct and its impl blocks");
Ok(())
}