kurtbuilds / ormlite

An ORM in Rust for developers that love SQL.
https://crates.io/crates/ormlite
MIT License
216 stars 11 forks source link

[feature request] support count query & exists query #35

Closed holmofy closed 10 months ago

holmofy commented 10 months ago

The current select query only supports querying all columns, which is very monotonous.

#[derive(Model, Debug)]
pub struct Person {
    pub id: i32,
    pub name: String,
    pub age: i32,
}

async fn query_builder_example() {
    let people = Person::select()
        .where_("age > ?")
        .bind(50i32)
        .fetch_all(&mut conn)
        .await?;
    println!("All people over 50: {:?}", people); 
}

It would be perfect if this library could provide count and exists APIs. count query and exists query are also very common query scenarios, it will be very convenient to build where clause through SelectQueryBuilder.

async fn query_builder_example() {
    let count = Person::count()
        .where_("age > ?")
        .bind(50i32)
        .fetch_all(&mut conn)
        .await?;

    let exists = Person::exists()
        .where_("age > ?")
        .bind(50i32)
        .fetch_all(&mut conn)
        .await?;
}
kurtbuilds commented 10 months ago

Why wouldn't you use raw queries for those?

You're not trying to ORM map an object when you do count(*) - you just need to map it to a number.

For exists, I think fetch_optional does the behavior you're describing. Or are you referring to SQL keyword EXISTS somehow?

--

You've opened a number of issues that are effectively Q&A tickets. I encourage you to think through how to solve problems yourself. If you've already done that, your write ups do not indicate that. If you need additional help, there are general Rust discords, and a sqlx one. I'm happy to answer questions time permitting, but I'll close future issues without comment if, for bug reports, they don't have repro steps, or for feature requests, aren't clearly well thought out, with alternatives considered, prior art discussion, trade off discussion, etc.

holmofy commented 10 months ago

I'm new to rust and just started learning rust in the last few weeks. I found ormlite, an ORM library based on sqlx. I think ormlite is the best library I've found this week. Therefore, I hope it can provide more and better APIs so that more people can use and improve this library. I'm very sorry if these issues I raised have caused trouble to you. I will not open any issues in the future.