-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostgres.go
More file actions
363 lines (319 loc) · 9.45 KB
/
Copy pathpostgres.go
File metadata and controls
363 lines (319 loc) · 9.45 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package postgres
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// -----------------------------------------------------------------------------
const (
defaultPoolMaxConns = 32
)
// -----------------------------------------------------------------------------
// WithinTxCallback defines a callback called in the context of the initiated transaction.
type WithinTxCallback = func(ctx context.Context, tx Tx) error
// WithinConnCallback defines a callback called in the context of a single connection.
type WithinConnCallback = func(ctx context.Context, conn Conn) error
// CopyCallback defines a callback that is called for each record being copied to the database
type CopyCallback func(ctx context.Context, idx int) ([]interface{}, error)
// -----------------------------------------------------------------------------
// Database represents a PostgreSQL database accessor.
type Database struct {
pool *pgxpool.Pool
err struct {
mutex sync.Mutex
handler ErrorHandler
last error
}
nameHash [32]byte
}
// Options defines the database connection options.
type Options struct {
Host string `json:"host"`
Port uint16 `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Name string `json:"name"`
MaxConns int32 `json:"maxConns"`
SSLMode SSLMode
ExtendedSettings map[string]string `json:"extendedSettings"`
}
// ErrorHandler defines a custom error handler.
type ErrorHandler func(err error)
// SSLMode states if secure communication with the server is optional or mandatory.
type SSLMode int
const (
SSLModeAllow SSLMode = iota
SSLModeRequired
SSLModeDisable
)
// -----------------------------------------------------------------------------
// New creates a new postgresql database driver.
func New(ctx context.Context, opts Options) (*Database, error) {
// Validate options
if len(opts.Host) == 0 {
return nil, errors.New("invalid host")
}
if len(opts.User) == 0 {
return nil, errors.New("invalid user name")
}
if len(opts.Name) == 0 {
return nil, errors.New("invalid database name")
}
sslMode := "disable"
switch opts.SSLMode {
case SSLModeDisable:
case SSLModeAllow:
sslMode = "prefer"
case SSLModeRequired:
sslMode = "require"
default:
return nil, errors.New("invalid SSL mode")
}
// Create database object
db := Database{}
db.err.mutex = sync.Mutex{}
// Create a hash of the database name
h := sha256.New()
_, _ = h.Write([]byte(opts.Name))
copy(db.nameHash[:], h.Sum(nil))
// Create PGX pool configuration. Usage of ParseConfig is mandatory :(
sbConnString := strings.Builder{}
_, _ = sbConnString.WriteString(fmt.Sprintf(
"host='%s' port=%d user='%s' password='%s' dbname='%s' sslmode=%s",
encodeDSN(opts.Host), opts.Port, encodeDSN(opts.User), encodeDSN(opts.Password), encodeDSN(opts.Name),
sslMode,
))
if opts.ExtendedSettings != nil {
for k, v := range opts.ExtendedSettings {
_, _ = sbConnString.WriteRune(' ')
_, _ = sbConnString.WriteString(k)
_, _ = sbConnString.WriteRune('=')
_, _ = sbConnString.WriteString(encodeDSN(v))
}
}
poolConfig, err := pgxpool.ParseConfig(sbConnString.String())
if err != nil {
db.Close()
return nil, errors.New("unable to parse connection string")
}
// Override some settings
poolConfig.MaxConns = opts.MaxConns
if opts.MaxConns <= 0 {
poolConfig.MaxConns = defaultPoolMaxConns
}
poolConfig.MaxConnIdleTime = 10 * time.Minute
poolConfig.HealthCheckPeriod = time.Minute
poolConfig.MaxConnLifetime = 1 * time.Hour
poolConfig.MaxConnLifetimeJitter = time.Minute
poolConfig.ConnConfig.ConnectTimeout = 15 * time.Second
// Create the database connection pool
db.pool, err = pgxpool.NewWithConfig(ctx, poolConfig)
if err != nil {
db.Close()
return nil, errors.New("unable to initialize database connection pool")
}
// Done
return &db, nil
}
// IsPostgresURL returns true if the url schema is postgres
func IsPostgresURL(rawUrl string) bool {
return strings.HasPrefix(rawUrl, "pg://") ||
strings.HasPrefix(rawUrl, "postgres://") ||
strings.HasPrefix(rawUrl, "postgresql://")
}
// NewFromURL creates a new postgresql database driver from an URL
func NewFromURL(ctx context.Context, rawUrl string) (*Database, error) {
opts := Options{
SSLMode: SSLModeAllow,
MaxConns: defaultPoolMaxConns,
}
u, err := url.ParseRequestURI(rawUrl)
if err != nil {
return nil, errors.New("invalid url provided")
}
// Check schema
if u.Scheme != "pg" && u.Scheme != "postgres" && u.Scheme != "postgresql" {
return nil, errors.New("invalid url schema")
}
// Check host name and port
opts.Host = u.Hostname()
if len(opts.Host) == 0 {
return nil, errors.New("invalid host")
}
port := u.Port()
if len(port) == 0 {
opts.Port = 5432
} else {
val, err2 := strconv.Atoi(port)
if err2 != nil || val < 1 || val > 65535 {
return nil, errors.New("invalid port")
}
opts.Port = uint16(val)
}
// Check user and password
if u.User == nil {
return nil, errors.New("invalid user name")
}
opts.User = u.User.Username()
if len(opts.User) == 0 {
return nil, errors.New("invalid user name")
}
// Check database name
if len(u.Path) < 2 || (!strings.HasPrefix(u.Path, "/")) || strings.Index(u.Path[1:], "/") >= 0 {
return nil, errors.New("invalid database name")
}
opts.Name = u.Path[1:]
// Parse query parameters
for k, values := range u.Query() {
v := ""
if len(values) > 0 {
v = values[0]
}
if k == "sslmode" {
// Check ssl mode
switch v {
case "allow":
opts.SSLMode = SSLModeAllow
case "required":
opts.SSLMode = SSLModeRequired
case "disabled":
fallthrough
case "":
default:
return nil, errors.New("invalid SSL mode")
}
} else if k == "maxconns" {
// Check max connections count
if len(v) > 0 {
val, err2 := strconv.Atoi(v)
if err2 != nil || val < 0 {
return nil, errors.New("invalid max connections count")
}
opts.MaxConns = int32(val)
}
} else {
// Extended setting
if opts.ExtendedSettings == nil {
opts.ExtendedSettings = make(map[string]string)
}
opts.ExtendedSettings[k] = v
}
}
// Create
return New(ctx, opts)
}
// Close shutdown the connection pool
func (db *Database) Close() {
if db.pool != nil {
db.pool.Close()
db.pool = nil
}
db.SetEventHandler(nil)
}
// SetEventHandler sets a new error handler callback
func (db *Database) SetEventHandler(handler ErrorHandler) {
db.err.mutex.Lock()
defer db.err.mutex.Unlock()
db.err.handler = handler
}
// Exec executes an SQL statement on a new connection
func (db *Database) Exec(ctx context.Context, sql string, args ...interface{}) (int64, error) {
ct, err := db.pool.Exec(ctx, sql, args...)
if err != nil {
err = newError(err, "unable to execute command")
}
return ct.RowsAffected(), db.processError(err)
}
// QueryRow executes a SQL query on a new connection
//
// NOTES:
// ~~~~~
// 1. Most of the commonly used types in Postgres can be mapped to standard Golang type including
// time.Time for timestamps (except time with tz which is not supported)
// 2. When reading JSON/JSONB fields, the underlying library (PGX) tries to unmarshall it into the
// destination variable. In order to just retrieve the json string, add the `::text` suffix to
// the field in the query.
// 3. To avoid overflows on high uint64 values, store them in NUMERIC(24,0) fields.
// 4. For time-only fields, date is set to Jan 1, 2000 by PGX in time.Time variables.
func (db *Database) QueryRow(ctx context.Context, sql string, args ...interface{}) Row {
return &rowGetter{
db: db,
row: db.pool.QueryRow(ctx, sql, args...),
}
}
// QueryRows executes a SQL query on a new connection
func (db *Database) QueryRows(ctx context.Context, sql string, args ...interface{}) Rows {
rows, err := db.pool.Query(ctx, sql, args...)
if err != nil {
err = newError(err, "unable to scan row")
}
return &rowsGetter{
db: db,
ctx: ctx,
rows: rows,
err: err,
}
}
// Copy executes a SQL copy query within the transaction.
func (db *Database) Copy(ctx context.Context, tableName string, columnNames []string, callback CopyCallback) (int64, error) {
n, err := db.pool.CopyFrom(
ctx,
pgx.Identifier{tableName},
columnNames,
©WithCallback{
ctx: ctx,
callback: callback,
},
)
if err != nil {
err = newError(err, "unable to execute command")
}
// Done
return n, db.processError(err)
}
// WithinTx executes a callback function within the context of a transaction
func (db *Database) WithinTx(ctx context.Context, cb WithinTxCallback) error {
tx, err := db.pool.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted, //pgx.Serializable,
AccessMode: pgx.ReadWrite,
DeferrableMode: pgx.NotDeferrable,
})
if err == nil {
err = cb(ctx, Tx{
db: db,
tx: tx,
})
if err == nil {
err = tx.Commit(ctx)
if err != nil {
err = newError(err, "unable to commit db transaction")
}
}
if err != nil {
_ = tx.Rollback(context.Background()) // Using context.Background() on purpose
}
} else {
err = newError(err, "unable to start transaction")
}
return db.processError(err)
}
// WithinConn executes a callback function within the context of a single connection
func (db *Database) WithinConn(ctx context.Context, cb WithinConnCallback) error {
conn, err := db.pool.Acquire(ctx)
if err == nil {
err = cb(ctx, Conn{
db: db,
conn: conn,
})
conn.Release()
}
return db.processError(err)
}