lib / pq

Pure Go Postgres driver for database/sql
https://pkg.go.dev/github.com/lib/pq
MIT License
8.98k stars 909 forks source link

Skip binding of missing parameter placeholders #969

Open vibridi opened 4 years ago

vibridi commented 4 years ago

This is more of a question than an actual bug report:

Code Example

func doQuery(...) ([]interface{}, error) {
    args := []interface{}{
        "foo",
        2,
        "bar",
    }
    query := `
SELECT * FROM my_table m WHERE m.count = $2 AND m.title = $3
`
    db.SelectContext(ctx, dest, query, args...)
}

Here I have a query with three arguments but only two parameter placeholders, starting from $2.

Expected Not sure if it's reasonable to expect this: intuitively, I would like simply $2 to bind to the second param 2, and $3 to bind to the third param bar.

Actual pq: could not determine data type of parameter $1 (even though there's no $1 param in the source query).

Of course it's reasonable that the lib complains about mismatches between input arguments and placeholders, but I also wonder if it would equally make sense to handle this case more gracefully; i.e. as long as the length of args is >= the highest number among placeholders, it might as well not fail.

Use Case My use case is a big query with a few UNION'ed blocks that is composed dynamically. Let's say I have three main sub-queries A,B and C, where:

I would like to hear your opinion on this.

johto commented 4 years ago

Of course it's reasonable that the lib complains about mismatches between input arguments and placeholders, but I also wonder if it would equally make sense to handle this case more gracefully; i.e. as long as the length of args is >= the highest number among placeholders, it might as well not fail.

The server doesn't accept it, so this is a non-starter:

=# prepare qwr as select $2;
ERROR:  could not determine data type of parameter $1