-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.go
72 lines (61 loc) · 1.47 KB
/
postgres.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
package main
import (
"context"
"fmt"
"sync"
"github.com/jackc/pgx/v5/pgxpool"
)
type PgDataSource struct {
connstr string
poolOnce sync.Once
err error
pool *pgxpool.Pool
}
func NewPgDataSource(connstr string) *PgDataSource {
return &PgDataSource{
connstr: connstr,
}
}
func (p *PgDataSource) GetDataSet(ctx context.Context, query string, params ...any) (DataSet, error) {
p.poolOnce.Do(func() {
conf, err := pgxpool.ParseConfig(p.connstr)
if err != nil {
p.err = fmt.Errorf("unable to parse connection string: %w", err)
return
}
pool, err := pgxpool.NewWithConfig(context.Background(), conf)
if err != nil {
p.err = fmt.Errorf("unable to connect to database: %w", err)
return
}
p.pool = pool
})
if p.err != nil {
return nil, p.err
}
conn, err := p.pool.Acquire(ctx)
if err != nil {
p.err = fmt.Errorf("unable to connect to database: %w", err)
return nil, fmt.Errorf("execute query: %w", err)
}
defer conn.Release()
rows, err := conn.Query(ctx, query, params...)
if err != nil {
return nil, fmt.Errorf("execute query: %w", err)
}
data := make(map[string][]any)
fds := rows.FieldDescriptions()
for rows.Next() {
vals, err := rows.Values()
if err != nil {
return nil, fmt.Errorf("read row values: %w", err)
}
for i, fd := range fds {
data[fd.Name] = append(data[fd.Name], vals[i])
}
}
if rows.Err() != nil {
return nil, fmt.Errorf("collect rows: %w", rows.Err())
}
return NewStaticDataSet(data), nil
}