-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.go
More file actions
36 lines (29 loc) · 2.07 KB
/
query.go
File metadata and controls
36 lines (29 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package rowmap
import (
"context"
"github.com/spearson78/fsql"
)
// QueryContext executes a query, typically a SELECT that returns entities using the provided mapper function. The args are for any placeholder parameters in the query. If zero rows are selected the returned slice will be nil.
func QueryContext[E any](ctx context.Context, db Queryable, mapper MapperFunc[E], sql string, p ...any) ([]E, error) {
rows, err := fsql.QueryContext(ctx, db, sql, p...)
return mapRows(mapper, rows, err)
}
// Query executes a query, typically a SELECT that returns entities using the provided mapper function. The args are for any placeholder parameters in the query. If zero rows are selected the returned slice will be nil.
//
// Query uses context.Background internally; to specify the context, use QueryContext.
func Query[E any](db Queryable, mapper MapperFunc[E], sql string, p ...any) ([]E, error) {
rows, err := fsql.Query(db, sql, p...)
return mapRows(mapper, rows, err)
}
// QueryRowContext executes a query that is expected oreturn at most one row. The result row is mapped to an entity using the provided mapper function. The args are for any placeholder parameters in the query. If the query selects no rows sql.ErrRows is returned. If multipe rows are returnd the frst row is mapped and returned.
func QueryRowContext[E any](ctx context.Context, db Queryable, mapper MapperFunc[E], sql string, p ...any) (E, error) {
rows, err := fsql.QueryContext(ctx, db, sql, p...)
return mapSingleRow(mapper, rows, err)
}
// QueryRow executes a query that is expected oreturn at most one row. The result row is mapped to an entity using the provided mapper function. The args are for any placeholder parameters in the query. If the query selects no rows sql.ErrRows is returned. If multipe rows are returnd the frst row is mapped and returned.
//
// QueryRow uses context.Background internally; to specify the context, use QueryRowContext.
func QueryRow[E any](db Queryable, mapper MapperFunc[E], sql string, p ...any) (E, error) {
rows, err := fsql.Query(db, sql, p...)
return mapSingleRow(mapper, rows, err)
}