njord-rs / njord

A versatile, feature-rich Rust ORM ⛵
https://njord.rs
BSD 3-Clause "New" or "Revised" License
409 stars 21 forks source link

Add Subqueries to Delete #145

Closed chaseWillden closed 1 month ago

chaseWillden commented 1 month ago

This does add a little complexity. I'm not sure the other way to implement this gracefully though.

Delete without a subquery, using just the literals:

match conn {
    Ok(ref c) => {
        let condition = Condition::Eq(
            "address".to_string(),
            Value::Literal("Some Random Address 1".to_string()),
        );

        let result = sqlite::delete(c)
            .from(User::default())
            .where_clause(condition)
            .order_by(order)
            .limit(20)
            .offset(0)
            .build();
        println!("{:?}", result);
        assert!(result.is_ok());
    }
    Err(e) => {
        panic!("Failed to DELETE: {:?}", e);
    }
}

Delete with subquery:

match conn {
    Ok(ref c) => {
        let sub_query =
            SelectQueryBuilder::new(c, vec![Column::<User>::Text("username".to_string())])
                .where_clause(Condition::Eq(
                    "id".to_string(),
                    Value::Literal(1.to_string()),
                ))
                .limit(1);

        let condition =
            Condition::Eq("address".to_string(), Value::Subquery(Box::new(sub_query)));

        let result = sqlite::delete(c)
            .from(User::default())
            .where_clause(condition)
            .build();
        println!("{:?}", result);
        assert!(result.is_ok());
    }
    Err(e) => {
        panic!("Failed to DELETE: {:?}", e);
    }
}
vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
njord ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 3:48am
mjovanc commented 1 month ago

LGTM. Thanks Chase!