-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg.go
144 lines (100 loc) · 2.8 KB
/
pg.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
package main
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type PostgresHandles struct {
pool *pgxpool.Pool
didsTable string
domainsTable string
}
func NewPostgresHandlesProvider(config *pgxpool.Config, didsTable string, domainsTable string) (*PostgresHandles, error) {
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
return &PostgresHandles{}, err
}
pg := &PostgresHandles{pool, didsTable, domainsTable}
healthy, status := pg.IsHealthy(context.Background())
if !healthy {
return &PostgresHandles{}, errors.New(status)
}
canAccessTables, err := pg.canAccessTables(context.Background())
if err != nil {
return &PostgresHandles{}, err
}
if !canAccessTables {
return &PostgresHandles{}, errors.New("cannot access tables")
}
return pg, nil
}
func (pg *PostgresHandles) GetDecentralizedIDForHandle(ctx context.Context, handle Handle) (DecentralizedID, error) {
canProvide, err := pg.CanProvideForDomain(ctx, handle.Domain)
if err != nil {
return "", err
}
if !canProvide {
return "", &CannotGetHandelsFromDomainError{domain: handle.Domain}
}
connection, err := pg.pool.Acquire(ctx)
defer connection.Release()
if err != nil {
return "", err
}
var did DecentralizedID
query := fmt.Sprintf(
"select did from %s where LOWER(handle) = LOWER($1)",
pgx.Identifier{pg.didsTable}.Sanitize(),
)
err = connection.QueryRow(ctx, query, handle.String()).Scan(&did)
if errors.Is(err, pgx.ErrNoRows) {
return "", nil
}
if err != nil {
return "", err
}
return did, nil
}
func (pg *PostgresHandles) CanProvideForDomain(ctx context.Context, domain Domain) (bool, error) {
connection, err := pg.pool.Acquire(ctx)
defer connection.Release()
if err != nil {
return false, err
}
exists := false
query := fmt.Sprintf(
"select exists(select 1 from %s where domain = $1)",
pgx.Identifier{pg.domainsTable}.Sanitize(),
)
err = connection.QueryRow(ctx, query, domain).Scan(&exists)
return exists, err
}
func (pg *PostgresHandles) IsHealthy(ctx context.Context) (bool, string) {
connection, err := pg.pool.Acquire(ctx)
if err != nil {
return false, err.Error()
}
defer connection.Release()
err = connection.Ping(ctx)
if err != nil {
return false, err.Error()
}
return true, "Connected to database"
}
func (pg *PostgresHandles) canAccessTables(ctx context.Context) (bool, error) {
connection, err := pg.pool.Acquire(ctx)
if err != nil {
return false, err
}
defer connection.Release()
canAccessTables := false
query := fmt.Sprintf(
"select true from %s union select true from %s",
pgx.Identifier{pg.didsTable}.Sanitize(),
pgx.Identifier{pg.domainsTable}.Sanitize(),
)
err = connection.QueryRow(ctx, query).Scan(&canAccessTables)
return canAccessTables, err
}