-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathguildleavetime.go
More file actions
71 lines (58 loc) · 1.55 KB
/
Copy pathguildleavetime.go
File metadata and controls
71 lines (58 loc) · 1.55 KB
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
package database
import (
"context"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4/pgxpool"
"time"
)
type GuildLeaveTime struct {
*pgxpool.Pool
}
func newGuildLeaveTime(db *pgxpool.Pool) *GuildLeaveTime {
return &GuildLeaveTime{
db,
}
}
func (GuildLeaveTime) Schema() string {
return `
CREATE TABLE IF NOT EXISTS guild_leave_time(
"guild_id" int8 NOT NULL UNIQUE,
"leave_time" timestamptz NOT NULL,
PRIMARY KEY("guild_id")
);`
}
func (c *GuildLeaveTime) GetBefore(ctx context.Context, before time.Duration) (ids []uint64, e error) {
query := `
SELECT "guild_id"
FROM guild_leave_time
WHERE "leave_time" < NOW() - $1::interval;
`
rows, err := c.Query(ctx, query, before)
if err != nil {
return nil, err
}
for rows.Next() {
var id uint64
if err = rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return
}
func (c *GuildLeaveTime) Set(ctx context.Context, guildId uint64) (err error) {
_, err = c.Exec(ctx, `INSERT INTO guild_leave_time("guild_id", "leave_time") VALUES($1, NOW()) ON CONFLICT("guild_id") DO UPDATE SET "leave_time" = NOW();`, guildId)
return
}
func (c *GuildLeaveTime) Delete(ctx context.Context, guildId uint64) (err error) {
_, err = c.Exec(ctx, `DELETE FROM guild_leave_time WHERE "guild_id" = $1;`, guildId)
return
}
func (c *GuildLeaveTime) DeleteAll(ctx context.Context, guildIds []uint64) (err error) {
array := &pgtype.Int8Array{}
if err = array.Set(guildIds); err != nil {
return
}
_, err = c.Exec(ctx, `DELETE FROM guild_leave_time WHERE "guild_id" = ANY($1);`, array)
return
}