-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_query.go
78 lines (66 loc) · 2 KB
/
base_query.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package dao_wrapper
import (
"context"
"github.com/CheatXGO/scany/v2/pgxscan"
"github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
// BaseQuery wrapper for queries
type BaseQuery struct {
ctx context.Context
db *pgxpool.Pool
tx pgx.Tx
}
// Runner queries executor interface
type Runner interface {
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}
// SQLConverter query builder to sql with args converter (accept any squirrel builder interface)
type SQLConverter interface {
ToSql() (string, []interface{}, error)
}
// PgQb sets placeholder format for postgres
func (q *BaseQuery) PgQb() squirrel.StatementBuilderType {
return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
}
// Context returns context of the query
func (q *BaseQuery) Context() context.Context {
return q.ctx
}
// Runner returns runner for operations with the database
func (q *BaseQuery) Runner() Runner {
if q.tx != nil {
return q.tx
}
return q.db
}
// Get query for only one row. If no rows are found it returns a pgx.ErrNoRows error.
func (q *BaseQuery) Get(sq SQLConverter, resp interface{}) error {
sql, args, _ := sq.ToSql()
err := pgxscan.Get(q.Context(), q.Runner(), resp, sql, args...)
if err != nil {
return err
}
return nil
}
// Select query for many rows. Accept slice as destination resp. If no rows are found - it returns nil error.
func (q *BaseQuery) Select(sq SQLConverter, resp interface{}) error {
sql, args, _ := sq.ToSql()
err := pgxscan.Select(q.Context(), q.Runner(), resp, sql, args...)
if err != nil {
return err
}
return nil
}
// Exec query for no result queries (insert/update/delete without "RETURNING any" suffix)
func (q *BaseQuery) Exec(sq SQLConverter) error {
sql, args, _ := sq.ToSql()
_, err := q.Runner().Exec(q.Context(), sql, args...)
if err != nil {
return err
}
return nil
}