-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
58 lines (44 loc) · 1.8 KB
/
common.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package pgsq
import (
"context"
"fmt"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5/pgconn"
)
// Queryable interface containing operations necessary to query database.
// Both Pool and Tx implement it.
type Queryable interface {
// Exec executes the builder query.
Exec(ctx context.Context, query sqlizer) (pgconn.CommandTag, error)
// ExecRaw executes the raw query.
ExecRaw(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
// Get queries a single row. Returns pgx.ErrNoRows, if there are no rows satisfying the builder query.
Get(ctx context.Context, dst any, sqlizer sqlizer) error
// GetRaw queries a single row. Returns pgx.ErrNoRows, if there are no rows satisfying the raw query.
GetRaw(ctx context.Context, dst any, sql string, args ...any) error
// Select queries multiple rows. Returns nil, if there are no rows satisfying the builder query.
Select(ctx context.Context, dst any, query sqlizer) error
// SelectRaw queries multiple rows. Returns nil, if there are no rows satisfying the raw query.
SelectRaw(ctx context.Context, dst any, sql string, args ...any) error
}
func execFn(ctx context.Context, q execer, query sqlizer) (pgconn.CommandTag, error) {
sql, args, err := query.ToSql()
if err != nil {
return pgconn.CommandTag{}, fmt.Errorf("to sql: %w", err)
}
return q.Exec(ctx, sql, args...)
}
func selectFn(ctx context.Context, q pgxscan.Querier, dst any, query sqlizer) error {
sql, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("to sql: %w", err)
}
return pgxscan.Select(ctx, q, dst, sql, args...)
}
func getFn(ctx context.Context, q pgxscan.Querier, dst any, query sqlizer) error {
sql, args, err := query.ToSql()
if err != nil {
return fmt.Errorf("to sql: %w", err)
}
return pgxscan.Get(ctx, q, dst, sql, args...)
}