-
Notifications
You must be signed in to change notification settings - Fork 902
Closed
Labels
Description
What do you want to change?
Hi,
I attempted to look around the issue tracker and website to find a duplicate and was not successful.
I believe that generated functions that have LIMIT
clauses can pre-allocate memory so that calls to append
are asymptotically cheaper. I cannot imagine that this is so widely desirable that it merits a change to the core library. However, I figured that the discussion might be worth having.
Of course, the example below is quite hacky; however, I am interested in whether or not this has been considered? Perhaps there is a more idiomatic way?
Example:
const getFoo = `--name: GetFoo :many
SELECT bar, baz
FROM foo as f
WHERE f.bar = ?1 AND f.baz <= ?2
LIMIT 5
`
func (q *Queries) GetFoo(ctx context.Context, arg FooParams) ([]Foo, error) {
rows, err := q.db.QueryContext(ctx, getFoo, ...) // LIMIT present here
if err != nil {
return nil, err
}
defer rows.Close()
var items []Foo
// naively:
// regex `LIMIT {0,9}*` out, parse it?
// items := make([]Foo, 0, limitN)
for rows.Next() {
var i Foo
if err := rows.Scan(&i.Bar, &i.Baz); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
What database engines need to be changed?
No response
What programming language backends need to be changed?
Go