-
Notifications
You must be signed in to change notification settings - Fork 4
/
closeconfirmation.go
43 lines (36 loc) · 1.01 KB
/
closeconfirmation.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
package database
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
type CloseConfirmation struct {
*pgxpool.Pool
}
func newCloseConfirmation(db *pgxpool.Pool) *CloseConfirmation {
return &CloseConfirmation{
db,
}
}
func (c CloseConfirmation) Schema() string {
return `
CREATE TABLE IF NOT EXISTS close_confirmation(
"guild_id" int8 NOT NULL UNIQUE,
"confirm" bool NOT NULL,
PRIMARY KEY("guild_id")
);`
}
func (c *CloseConfirmation) Get(ctx context.Context, guildId uint64) (confirm bool, e error) {
if err := c.QueryRow(ctx, `SELECT "confirm" from close_confirmation WHERE "guild_id" = $1;`, guildId).Scan(&confirm); err != nil {
if err == pgx.ErrNoRows {
confirm = true
} else {
e = err
}
}
return
}
func (c *CloseConfirmation) Set(ctx context.Context, guildId uint64, confirm bool) (err error) {
_, err = c.Exec(ctx, `INSERT INTO close_confirmation("guild_id", "confirm") VALUES($1, $2) ON CONFLICT("guild_id") DO UPDATE SET "confirm" = $2;`, guildId, confirm)
return
}