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