forked from raystack/siren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (143 loc) · 4.11 KB
/
client.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package pgc
import (
"context"
"database/sql"
"fmt"
"github.com/goto/salt/db"
"github.com/goto/salt/log"
"github.com/goto/siren/pkg/errors"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"go.nhat.io/otelsql"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
var (
transactionContextKey = struct{}{}
ErrDuplicateKey = errors.New("duplicate key")
ErrCheckViolation = errors.New("check constraint violation")
ErrForeignKeyViolation = errors.New("foreign key violation")
)
type Client struct {
db *db.Client
logger log.Logger
}
// NewClient wraps salt/db client
func NewClient(logger log.Logger, cfg db.Config) (*Client, error) {
driverName, err := otelsql.Register(
cfg.Driver,
otelsql.TraceQueryWithoutArgs(),
otelsql.TraceRowsClose(),
otelsql.TraceRowsAffected(),
otelsql.WithSystem(semconv.DBSystemPostgreSQL),
)
if err != nil {
return nil, fmt.Errorf("new pgq processor: %w", err)
}
// backup origin driver name that is going to be overrided
originDriverName := cfg.Driver
cfg.Driver = driverName
dbClient, err := db.New(cfg)
if err != nil {
return nil, fmt.Errorf("error creating postgres client: %w", err)
}
sqlDB := dbClient.DB.DB
// need to use NewDb if we want to use NamedContext with otelsql
// ref: https://github.com/nhatthm/otelsql?tab=readme-ov-file#jmoironsqlx
wrappedSQLxDB := sqlx.NewDb(sqlDB, originDriverName)
if err := otelsql.RecordStats(
sqlDB,
otelsql.WithSystem(semconv.DBSystemPostgreSQL),
otelsql.WithInstanceName(dbClient.Host()),
); err != nil {
return nil, err
}
dbClient.DB = wrappedSQLxDB
return &Client{
db: dbClient,
logger: logger,
}, nil
}
// Close closes the database connection
func (c *Client) Close() error {
if c.db != nil {
return c.db.Close()
}
return nil
}
func CheckError(err error) error {
var pqErr *pq.Error
if errors.As(err, &pqErr) {
switch pqErr.Code.Name() {
case "unique_violation":
return fmt.Errorf("%w [%s]", ErrDuplicateKey, pqErr.Detail)
case "check_violation":
return fmt.Errorf("%w [%s]", ErrCheckViolation, pqErr.Detail)
case "foreign_key_violation":
return fmt.Errorf("%w [%s]", ErrForeignKeyViolation, pqErr.Detail)
}
}
return err
}
func (c *Client) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row {
return c.GetDB(ctx).QueryRowxContext(ctx, query, args...)
}
func (c *Client) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) {
return c.GetDB(ctx).QueryxContext(ctx, query, args...)
}
func (c *Client) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
return c.GetDB(ctx).QueryRowxContext(ctx, query, args...).StructScan(dest)
}
func (c *Client) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
return c.GetDB(ctx).ExecContext(ctx, query, args...)
}
func (c *Client) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) {
return sqlx.NamedExecContext(ctx, c.GetDB(ctx), query, arg)
}
func (c *Client) WithTransaction(ctx context.Context, opts *sql.TxOptions) context.Context {
var (
tx *sqlx.Tx
err error
)
if tx = extractTransaction(ctx); tx != nil {
return ctx
}
tx, err = c.db.BeginTxx(ctx, opts)
if err != nil {
return ctx
}
return context.WithValue(ctx, transactionContextKey, tx)
}
func (c *Client) Rollback(ctx context.Context) error {
if tx := extractTransaction(ctx); tx != nil {
if err := tx.Rollback(); err != nil {
return err
}
return nil
}
return errors.New("no transaction")
}
func (c *Client) Commit(ctx context.Context) error {
if tx := extractTransaction(ctx); tx != nil {
if err := tx.Commit(); err != nil {
return err
}
return nil
}
return errors.New("no transaction")
}
func (c *Client) GetDB(ctx context.Context) sqlx.ExtContext {
if tx := extractTransaction(ctx); tx != nil {
return tx
}
return c.db
}
func (c *Client) GetSaltDB(ctx context.Context) *db.Client {
return c.db
}
func extractTransaction(ctx context.Context) *sqlx.Tx {
if tx, ok := ctx.Value(transactionContextKey).(*sqlx.Tx); !ok {
return nil
} else {
return tx
}
}