Skip to content

Scalar Deduction (No Struct Wrapper) #25

@Nareshix

Description

@Nareshix

If a user writes a query that only selects one single column, why force them to access it through a struct?
For instance LIMIT 1, OFFSET 1, where =? Etc

If the AST detects that select.projection.len() == 1, you don't need to generate a struct GetAge { age: i64 }. You can just map it directly to the primitive!

Combined with the Option and Aggregate deductions above:

#[sqlitex]
struct AppDb {
    // 1 col, Aggregate -> Returns exactly 1 row, 1 primitive
    get_count: sql!("SELECT COUNT(*) FROM users"),

    // 1 col, PRIMARY KEY -> Returns 0 or 1 row, 1 primitive
    get_age: sql!("SELECT age FROM users WHERE id = ?"),
}

fn main() {
    // Macro natively returns Result<i64, SqlReadError> !
    let total_users: i64 = db.get_count().unwrap();

    // Macro natively returns Result<Option<i64>, SqlReadError> !
    let alice_age: Option<i64> = db.get_age(1).unwrap();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions