-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
170 lines (132 loc) · 3.31 KB
/
conn.go
File metadata and controls
170 lines (132 loc) · 3.31 KB
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
package pgsqlxx
import (
"errors"
"reflect"
"strings"
"github.com/jackc/pgx"
"github.com/jmoiron/sqlx/reflectx"
)
// From https://github.com/jmoiron/sqlx/blob/398dd5876282499cdfd4cb8ea0f31a672abe9495/sqlx.go#L26
var NameMapper = strings.ToLower
var origMapper = reflect.ValueOf(NameMapper)
// Rather than creating on init, this is created when necessary so that
// importers have time to customize the NameMapper.
var mpr *reflectx.Mapper
// mapper returns a valid mapper using the configured NameMapper func.
func mapper() *reflectx.Mapper {
if mpr == nil {
mpr = reflectx.NewMapperFunc("db", NameMapper)
} else if origMapper != reflect.ValueOf(NameMapper) {
// if NameMapper has changed, create a new mapper
mpr = reflectx.NewMapperFunc("db", NameMapper)
origMapper = reflect.ValueOf(NameMapper)
}
return mpr
}
func ConnectFromPool(pool *pgx.ConnPool) (*Connxx, error) {
s := pool.Stat()
if s.AvailableConnections <= 0 && s.CurrentConnections <= 0 {
return nil, errors.New("no connections active in pool")
}
c, err := pool.Acquire()
if err != nil {
return nil, err
}
if !c.IsAlive() {
pool.Release(c)
deathErr := c.CauseOfDeath()
if deathErr != nil {
return nil, deathErr
} else {
return nil, errors.New("acquired dead connection from pool")
}
}
pool.Release(c)
return &Connxx{ConnPool: pool, unsafe: false, Mapper: mapper()}, nil
}
func MustConnectFromPool(pool *pgx.ConnPool) *Connxx {
c, e := ConnectFromPool(pool)
if e != nil {
panic(e)
}
return c
}
type Connxx struct {
*pgx.ConnPool
unsafe bool
Mapper *reflectx.Mapper
}
func (c *Connxx) DriverName() string {
return "pgx"
}
func (c *Connxx) Unsafe() *Connxx {
c.unsafe = true
return c
}
func (c *Connxx) MapperFunc(mf func(string) string) {
c.Mapper = reflectx.NewMapperFunc("db", mf)
}
func (c *Connxx) Queryx(query string, args ...interface{}) (*Rows, error) {
rows, err := c.ConnPool.Query(query, args...)
if err != nil {
return nil, err
}
r := RowsFromRows(rows, c.Mapper)
r.unsafe = c.unsafe
return r, nil
}
func (c *Connxx) QueryRowx(query string, args ...interface{}) *Row {
rows, err := c.Queryx(query, args...)
r := RowFromRowsx(rows, c.Mapper)
r.unsafe = c.unsafe
r.err = err
return r
}
func (c *Connxx) Rebind(query string) string {
return Rebind(query)
}
func (c *Connxx) Beginx() (*Tx, error) {
tx, err := c.Begin()
if err != nil {
return nil, err
}
t := TxFromTx(tx, c.Mapper)
t.unsafe = c.unsafe
return t, nil
}
func (c *Connxx) Execx(sql string, args ...interface{}) (*Result, error) {
r, err := c.Exec(sql, args...)
if err != nil {
return nil, err
}
return ResultFromCommandTag(r), nil
}
func (c *Connxx) IsTx() bool {
return false
}
type Preparer interface {
Prepare(string, string) (*pgx.PreparedStatement, error)
PrepareEx(string, string, *pgx.PrepareExOptions) (*pgx.PreparedStatement, error)
}
type TxChecker interface {
IsTx() bool
}
type Queryer interface {
Query(string, ...interface{}) (*pgx.Rows, error)
QueryRow(string, ...interface{}) *pgx.Row
Queryx(string, ...interface{}) (*Rows, error)
QueryRowx(string, ...interface{}) *Row
}
type Execer interface {
Exec(string, ...interface{}) (pgx.CommandTag, error)
Execx(string, ...interface{}) (*Result, error)
}
type Binder interface {
Rebind(string) string
}
type Ext interface {
Preparer
Queryer
Execer
Binder
}