Skip to content

more descriptive binding #28

@Nareshix

Description

@Nareshix

Concept

better argument names:

// generated signature becomes
fn get_users(limit: i64, offset: i64) -> ...
// instead of
fn get_users(arg_0: i64, arg_1: i64) -> ...

The real win is just making arg_0, arg_1 not be the parameter names. That's a broader problem with your current codegen anyway, not just pagination.

Right now your macro generates this regardless of what the SQL is:

// generated for: SELECT * FROM users WHERE id = ? AND name = ? LIMIT ? OFFSET ?
pub fn get_users(arg_0: i64, arg_1: &str, arg_2: i64, arg_3: i64) -> ...

The binding types are correct but the names are useless. The fix is using what you already know from the AST:

// WHERE id = ?       → you know the column is `id`     → name it `id`
// WHERE name = ?     → you know the column is `name`   → name it `name`
// LIMIT ?            → SQL keyword                      → name it `limit`
// OFFSET ?           → SQL keyword                      → name it `offset`

pub fn get_users(id: i64, name: &str, limit: i64, offset: i64) -> ...

Implementation (unconfirmed)

You already have the column info in binding_patterns.rs when you traverse the WHERE clause. You're just not carrying the name through, only the type. So the change is roughly:

// current
pub struct Type {
    pub base_type: BaseType,
    pub nullable: bool,
    pub contains_placeholder: bool,
}

// add name
pub struct BindingParam {
    pub data_type: Type,
    pub name: String,  // "id", "name", "limit", "offset" etc
}

Then in codegen instead of:

let arg_name = quote::format_ident!("arg_{}", i);

You do:

let arg_name = quote::format_ident!("{}", param.name); // "id", "name" etc

The tricky cases are:

// WHERE age > ? AND age < ?  → two params from same column
// name them `age_min` and `age_max`? or `age_0` and `age_1`?

// WHERE id = ? OR name = ?  → harder to infer intent
// probably just fall back to `arg_0`, `arg_1`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions