-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
220 lines (175 loc) · 4.71 KB
/
db.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package blockqueue
import (
"context"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/yudhasubki/blockqueue/pkg/core"
)
type Driver interface {
Conn() *sqlx.DB
Close() error
}
type db struct {
Database Driver
}
func newDb(driver Driver) *db {
return &db{
Database: driver,
}
}
func (d *db) getTopics(ctx context.Context, filter core.FilterTopic) (core.Topics, error) {
var (
topics = make(core.Topics, 0)
query = "SELECT * FROM topics"
)
clause, arg := filter.Filter("AND")
if clause != "" {
query += " WHERE " + clause
}
query, args, err := sqlx.Named(query, arg)
if err != nil {
return topics, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return topics, err
}
query = d.Database.Conn().Rebind(query)
err = d.Database.Conn().SelectContext(ctx, &topics, query, args...)
if err != nil {
return topics, err
}
return topics, nil
}
func (d *db) getSubscribers(ctx context.Context, filter core.FilterSubscriber) (core.Subscribers, error) {
var (
subscribers = make(core.Subscribers, 0)
query = "SELECT topic_subscribers.*, t.name as topic_name FROM topic_subscribers INNER JOIN topics t ON topic_subscribers.topic_id = t.id"
)
clause, arg := filter.Filter("AND")
if clause != "" {
query += " WHERE " + clause
}
query, args, err := sqlx.Named(query, arg)
if err != nil {
return subscribers, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return subscribers, err
}
query = d.Database.Conn().Rebind(query)
err = d.Database.Conn().SelectContext(ctx, &subscribers, query, args...)
if err != nil {
return subscribers, err
}
return subscribers, nil
}
func (d *db) createTxTopic(ctx context.Context, tx *sqlx.Tx, topic core.Topic) error {
stmt, err := tx.PrepareNamedContext(ctx, "INSERT INTO topics (id, name) VALUES (:id, :name)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx, topic)
if err != nil {
return err
}
return nil
}
func (d *db) deleteTxTopic(ctx context.Context, tx *sqlx.Tx, topic core.Topic) error {
stmt, err := tx.PrepareNamedContext(ctx, "UPDATE topics SET deleted_at = :deleted_at WHERE id = :id")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx, topic)
if err != nil {
return err
}
return nil
}
func (d *db) deleteTxSubscribers(ctx context.Context, tx *sqlx.Tx, topic core.Subscriber) error {
stmt, err := tx.PrepareNamedContext(ctx, "UPDATE topic_subscribers SET deleted_at = :deleted_at WHERE name = :name AND topic_id = :topic_id")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx, topic)
if err != nil {
return err
}
return nil
}
func (d *db) createTxSubscribers(ctx context.Context, tx *sqlx.Tx, subscribers core.Subscribers) error {
_, err := tx.NamedExecContext(ctx, "INSERT INTO topic_subscribers (id, topic_id, name, option) VALUES (:id, :topic_id, :name, :option)", subscribers)
if err != nil {
return err
}
return nil
}
func (d *db) createMessages(ctx context.Context, message core.Message) error {
stmt, err := d.Database.Conn().PrepareNamedContext(ctx, "INSERT INTO topic_messages (id, topic_id, message, status) VALUES (:id, :topic_id, :message, :status)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.ExecContext(ctx, message)
if err != nil {
return err
}
return nil
}
func (d *db) updateStatusMessage(ctx context.Context, status core.MessageStatus, ids ...uuid.UUID) error {
if len(ids) == 0 {
return nil
}
query, args, err := sqlx.In("UPDATE topic_messages SET status = ? WHERE id IN (?)", status, ids)
if err != nil {
return err
}
_, err = d.Database.Conn().ExecContext(ctx, d.Database.Conn().Rebind(query), args...)
if err != nil {
return err
}
return nil
}
func (d *db) getMessages(ctx context.Context, filter core.FilterMessage) (core.Messages, error) {
var (
messages = make(core.Messages, 0)
query = "SELECT * FROM topic_messages"
)
clause, arg := filter.Filter("AND")
if clause != "" {
query += " WHERE " + clause
}
query += " " + filter.Sort() + " " + filter.Page()
query, args, err := sqlx.Named(query, arg)
if err != nil {
return messages, err
}
query, args, err = sqlx.In(query, args...)
if err != nil {
return messages, err
}
query = d.Database.Conn().Rebind(query)
err = d.Database.Conn().SelectContext(ctx, &messages, query, args...)
if err != nil {
return messages, err
}
return messages, nil
}
func (d *db) tx(ctx context.Context, fn func(ctx context.Context, tx *sqlx.Tx) error) error {
tx, err := d.Database.Conn().Beginx()
if err != nil {
return err
}
err = fn(ctx, tx)
if err != nil {
if errTx := tx.Rollback(); errTx == nil {
return err
}
return err
}
return tx.Commit()
}