forked from vgarvardt/gue
/
db.go
203 lines (164 loc) · 5.58 KB
/
db.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package libpq
import (
"context"
"database/sql"
"github.com/vgarvardt/gue/v5/adapter"
)
// aRow implements adapter.Row using github.com/lib/pq
type aRow struct {
row *sql.Row
}
// Scan implements adapter.Row.Scan() using github.com/lib/pq
func (r *aRow) Scan(dest ...any) error {
err := r.row.Scan(dest...)
if err == sql.ErrNoRows {
return adapter.ErrNoRows
}
return err
}
// aCommandTag implements adapter.CommandTag using github.com/lib/pq
type aCommandTag struct {
ct sql.Result
}
// RowsAffected implements adapter.CommandTag.RowsAffected() using github.com/lib/pq
func (ct aCommandTag) RowsAffected() int64 {
ra, err := ct.ct.RowsAffected()
if err != nil {
// TODO: log this error at least
return 0
}
return ra
}
// aRows implements adapter.Rows using github.com/lib/pq
type aRows struct {
rows *sql.Rows
}
// Next implements adapter.Rows.Next() using github.com/lib/pq
func (r *aRows) Next() bool {
return r.rows.Next()
}
// Scan implements adapter.Rows.Scan() using github.com/lib/pq
func (r *aRows) Scan(dest ...any) error {
return r.rows.Scan(dest...)
}
// Err implements adapter.Rows.Err() using github.com/lib/pq
func (r *aRows) Err() error {
return r.rows.Err()
}
// aTx implements adapter.Tx using github.com/lib/pq
type aTx struct {
tx *sql.Tx
}
// NewTx instantiates new adapter.Tx using github.com/lib/pq
func NewTx(tx *sql.Tx) adapter.Tx {
return &aTx{tx: tx}
}
// UnwrapTx tries to unwrap driver-specific transaction instance from the interface.
// Returns unwrap success as the second parameter.
func UnwrapTx(tx adapter.Tx) (*sql.Tx, bool) {
driverTx, ok := tx.(*aTx)
if !ok {
return nil, false
}
return driverTx.tx, ok
}
// Exec implements adapter.Tx.Exec() using github.com/lib/pq
func (tx *aTx) Exec(ctx context.Context, query string, args ...any) (adapter.CommandTag, error) {
ct, err := tx.tx.ExecContext(ctx, query, args...)
return aCommandTag{ct}, err
}
// QueryRow implements adapter.Tx.QueryRow() using github.com/lib/pq
func (tx *aTx) QueryRow(ctx context.Context, query string, args ...any) adapter.Row {
return &aRow{tx.tx.QueryRowContext(ctx, query, args...)}
}
// Query implements adapter.Tx.Query() using github.com/lib/pq
func (tx *aTx) Query(ctx context.Context, query string, args ...any) (adapter.Rows, error) {
rows, err := tx.tx.QueryContext(ctx, query, args...)
return &aRows{rows}, err
}
// Rollback implements adapter.Tx.Rollback() using github.com/lib/pq
func (tx *aTx) Rollback(_ context.Context) error {
err := tx.tx.Rollback()
if err == sql.ErrTxDone {
return adapter.ErrTxClosed
}
return err
}
// Commit implements adapter.Tx.Commit() using github.com/lib/pq
func (tx *aTx) Commit(_ context.Context) error {
return tx.tx.Commit()
}
type conn struct {
c *sql.Conn
}
// NewConn instantiates new adapter.Conn using github.com/lib/pq
func NewConn(c *sql.Conn) adapter.Conn {
return &conn{c}
}
// Ping implements adapter.Conn.Ping() using github.com/lib/pq
func (c *conn) Ping(ctx context.Context) error {
return c.c.PingContext(ctx)
}
// Begin implements adapter.Conn.Begin() using github.com/lib/pq
func (c *conn) Begin(ctx context.Context) (adapter.Tx, error) {
tx, err := c.c.BeginTx(ctx, nil)
return NewTx(tx), err
}
// Exec implements adapter.Conn.Exec() using github.com/lib/pq
func (c *conn) Exec(ctx context.Context, query string, args ...any) (adapter.CommandTag, error) {
r, err := c.c.ExecContext(ctx, query, args...)
return aCommandTag{r}, err
}
// QueryRow implements adapter.Conn.QueryRow() github.com/lib/pq
func (c *conn) QueryRow(ctx context.Context, query string, args ...any) adapter.Row {
return &aRow{c.c.QueryRowContext(ctx, query, args...)}
}
// Query implements adapter.Conn.Query() github.com/lib/pq
func (c *conn) Query(ctx context.Context, query string, args ...any) (adapter.Rows, error) {
rows, err := c.c.QueryContext(ctx, query, args...)
return &aRows{rows}, err
}
// Release implements adapter.Conn.Release() using github.com/lib/pq
func (c *conn) Release() error {
return c.c.Close()
}
// connPool implements adapter.ConnPool using github.com/lib/pq
type connPool struct {
pool *sql.DB
}
// NewConnPool instantiates new adapter.ConnPool using github.com/lib/pq
func NewConnPool(pool *sql.DB) adapter.ConnPool {
return &connPool{pool}
}
// Ping implements adapter.ConnPool.Ping() using github.com/lib/pq
func (c *connPool) Ping(ctx context.Context) error {
return c.pool.PingContext(ctx)
}
// Exec implements adapter.ConnPool.Exec() using github.com/lib/pq
func (c *connPool) Exec(ctx context.Context, query string, args ...any) (adapter.CommandTag, error) {
ct, err := c.pool.ExecContext(ctx, query, args...)
return aCommandTag{ct}, err
}
// QueryRow implements adapter.ConnPool.QueryRow() using github.com/lib/pq
func (c *connPool) QueryRow(ctx context.Context, query string, args ...any) adapter.Row {
return &aRow{c.pool.QueryRowContext(ctx, query, args...)}
}
// Query implements adapter.ConnPool.Query() using github.com/lib/pq
func (c *connPool) Query(ctx context.Context, query string, args ...any) (adapter.Rows, error) {
rows, err := c.pool.QueryContext(ctx, query, args...)
return &aRows{rows}, err
}
// Begin implements adapter.ConnPool.Begin() using github.com/lib/pq
func (c *connPool) Begin(ctx context.Context) (adapter.Tx, error) {
tx, err := c.pool.BeginTx(ctx, nil)
return NewTx(tx), err
}
// Acquire implements adapter.ConnPool.Acquire() using github.com/lib/pq
func (c *connPool) Acquire(ctx context.Context) (adapter.Conn, error) {
cc, err := c.pool.Conn(ctx)
return NewConn(cc), err
}
// Close implements adapter.ConnPool.Close() using github.com/lib/pq
func (c *connPool) Close() error {
return c.pool.Close()
}