forked from grafana/xk6-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgxpool.go
109 lines (90 loc) · 2.67 KB
/
pgxpool.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
package sql
import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"go.k6.io/k6/js/modules"
)
// init is called by the Go runtime at application startup.
func init() {
modules.Register("k6/x/pgxpool", New())
}
type (
// RootModule is the global module instance that will create module
// instances for each Pool.
RootModule struct{}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
// vu provides methods for accessing internal k6 objects for a Pool
vu modules.VU
// pool is the exported type
pool *Pool
}
// KeyValue is a simple key-value pair.
KeyValue map[string]interface{}
)
// Ensure the interfaces are implemented correctly.
var (
_ modules.Instance = &ModuleInstance{}
_ modules.Module = &RootModule{}
)
// New returns a pointer to a new RootModule instance.
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the modules.Module interface returning a new instance for each Pool.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
pool: &Pool{vu: vu},
}
}
// Pool is the type for our custom API.
type Pool struct {
vu modules.VU // provides methods for accessing internal k6 objects
}
// Open establishes a connection to the specified database type using the provided connection string.
func (p *Pool) Open(connString string, minConns int, maxConns int) (*pgxpool.Pool, error) {
config, err := pgxpool.ParseConfig(connString)
if err != nil {
return nil, err
}
config.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
config.MinConns = int32(minConns)
config.MaxConns = int32(maxConns)
pool, err := pgxpool.NewWithConfig(p.vu.Context(), config)
if err != nil {
return nil, err
}
return pool, nil
}
func (p *Pool) Exec(pool *pgxpool.Pool, sql string, args ...interface{}) (pgconn.CommandTag, error) {
return pool.Exec(p.vu.Context(), sql, args...)
}
func (p *Pool) Query(pool *pgxpool.Pool, sql string, args ...interface{}) ([]KeyValue, error) {
rows, err := pool.Query(p.vu.Context(), sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var results []KeyValue
columns := rows.FieldDescriptions()
for rows.Next() {
values, err := rows.Values()
if err != nil {
return nil, err
}
result := make(KeyValue)
for i, column := range columns {
result[column.Name] = values[i]
}
results = append(results, result)
}
return results, nil
}
// Exports implements the modules.Instance interface and returns the exported types for the JS module.
func (m *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Default: m.pool,
}
}