-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgx.go
168 lines (140 loc) · 3.47 KB
/
pgx.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
package pgx
import (
"context"
"github.com/gopsql/db"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type (
DB struct {
*pgxpool.Pool
}
Tx struct {
pgx.Tx
}
Result struct {
rowsAffected int64
}
Rows struct {
pgx.Rows
}
)
var (
_ db.DB = (*DB)(nil)
_ db.Tx = (*Tx)(nil)
_ db.Result = (*Result)(nil)
_ db.Rows = (*Rows)(nil)
)
// MustOpen is like Open but panics if connect operation fails.
func MustOpen(conn string) db.DB {
c, err := Open(conn)
if err != nil {
panic(err)
}
return c
}
// Open creates and establishes one connection to database.
func Open(conn string) (db.DB, error) {
pool, err := pgxpool.New(context.Background(), conn)
if err != nil {
return nil, err
}
return &DB{pool}, nil
}
func (d *DB) Close() error {
d.Pool.Close()
return nil
}
func (d *DB) DriverName() string {
return "postgres"
}
func (d *DB) Exec(query string, args ...interface{}) (db.Result, error) {
return d.ExecContext(context.Background(), query, args...)
}
func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (db.Result, error) {
re, err := d.Pool.Exec(ctx, query, args...)
if err != nil {
return nil, err
}
return &Result{
rowsAffected: re.RowsAffected(),
}, nil
}
func (d *DB) Query(query string, args ...interface{}) (db.Rows, error) {
return d.QueryContext(context.Background(), query, args...)
}
func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (db.Rows, error) {
rows, err := d.Pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{rows}, nil
}
func (d *DB) QueryRow(query string, args ...interface{}) db.Row {
return d.QueryRowContext(context.Background(), query, args...)
}
func (d *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) db.Row {
return d.Pool.QueryRow(ctx, query, args...)
}
func (d *DB) BeginTx(ctx context.Context, isolationLevel string, readOnly bool) (db.Tx, error) {
mode := pgx.ReadWrite
if readOnly {
mode = pgx.ReadOnly
}
tx, err := d.Pool.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.TxIsoLevel(isolationLevel),
AccessMode: mode,
})
if err != nil {
return nil, err
}
return &Tx{tx}, nil
}
func (d *DB) ErrNoRows() error {
return pgx.ErrNoRows
}
func (d *DB) ErrGetCode(err error) string {
if e, ok := err.(interface{ SQLState() string }); ok { // github.com/jackc/pgconn
return e.SQLState()
}
return "unknown"
}
func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (db.Result, error) {
re, err := t.Tx.Exec(ctx, query, args...)
if err != nil {
return nil, err
}
return &Result{
rowsAffected: re.RowsAffected(),
}, nil
}
func (t *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (db.Rows, error) {
rows, err := t.Tx.Query(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{rows}, nil
}
func (t *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) db.Row {
return t.Tx.QueryRow(ctx, query, args...)
}
func (t *Tx) Commit(ctx context.Context) error {
return t.Tx.Commit(ctx)
}
func (t *Tx) Rollback(ctx context.Context) error {
return t.Tx.Rollback(ctx)
}
func (r Result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
func (r *Rows) Columns() ([]string, error) {
columns := []string{}
for _, fd := range r.Rows.FieldDescriptions() {
columns = append(columns, string(fd.Name))
}
return columns, nil
}
func (r *Rows) Close() error {
r.Rows.Close()
return nil
}