-
Notifications
You must be signed in to change notification settings - Fork 0
/
likes_database_sql.go
256 lines (174 loc) · 5.22 KB
/
likes_database_sql.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package activitypub
import (
"context"
"database/sql"
"fmt"
"net/url"
pg_sql "github.com/aaronland/go-pagination-sql"
"github.com/aaronland/go-pagination/countable"
"github.com/sfomuseum/go-activitypub/sqlite"
)
const SQL_LIKES_TABLE_NAME string = "likes"
type SQLLikesDatabase struct {
LikesDatabase
database *sql.DB
}
func init() {
ctx := context.Background()
RegisterLikesDatabase(ctx, "sql", NewSQLLikesDatabase)
}
func NewSQLLikesDatabase(ctx context.Context, uri string) (LikesDatabase, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}
engine := u.Host
q := u.Query()
dsn := q.Get("dsn")
conn, err := sql.Open(engine, dsn)
if err != nil {
return nil, fmt.Errorf("Failed to open database connection, %w", err)
}
if engine == "sqlite3" {
err := sqlite.SetupConnection(ctx, conn)
if err != nil {
return nil, fmt.Errorf("Failed to configure SQLite, %w", err)
}
}
db := &SQLLikesDatabase{
database: conn,
}
return db, nil
}
func (db *SQLLikesDatabase) GetLikeIdsForDateRange(ctx context.Context, start int64, end int64, cb GetLikeIdsCallbackFunc) error {
pg_callback := func(pg_rsp pg_sql.PaginatedResponse) error {
rows := pg_rsp.Rows()
for rows.Next() {
var id int64
err := rows.Scan(&id)
if err != nil {
return fmt.Errorf("Failed to query database, %w", err)
}
err = cb(ctx, id)
if err != nil {
return fmt.Errorf("Failed to execute following callback for like %d, %w", id, err)
}
return nil
}
err := rows.Close()
if err != nil {
return fmt.Errorf("Failed to iterate through database rows, %w", err)
}
return nil
}
pg_opts, err := countable.NewCountableOptions()
if err != nil {
return fmt.Errorf("Failed to create pagination options, %w", err)
}
q := fmt.Sprintf("SELECT id FROM %s WHERE created >= ? AND created <= ?", SQL_LIKES_TABLE_NAME)
err = pg_sql.QueryPaginatedAll(db.database, pg_opts, pg_callback, q, start, end)
if err != nil {
return fmt.Errorf("Failed to execute paginated query, %w", err)
}
return nil
}
func (db *SQLLikesDatabase) GetLikeWithId(ctx context.Context, id int64) (*Like, error) {
where := "id = ?"
return db.getLike(ctx, where, id)
}
func (db *SQLLikesDatabase) GetLikeWithPostIdAndActor(ctx context.Context, post_id int64, actor string) (*Like, error) {
where := "post_id = ? AND actor = ?"
return db.getLike(ctx, where, post_id, actor)
}
func (db *SQLLikesDatabase) GetLikesForPostIdAndActor(ctx context.Context, post_id int64, actor string, cb GetLikesCallbackFunc) error {
pg_callback := func(pg_rsp pg_sql.PaginatedResponse) error {
rows := pg_rsp.Rows()
for rows.Next() {
var id int64
var account_id int64
var post_id int64
var actor string
var created int64
err := rows.Scan(&id, &account_id, &post_id, &actor, &created)
switch {
case err == sql.ErrNoRows:
return nil
case err != nil:
return err
default:
b := &Like{
Id: id,
AccountId: account_id,
PostId: post_id,
Actor: actor,
Created: created,
}
err = cb(ctx, b)
if err != nil {
return fmt.Errorf("Failed to execute callback for '%b', %w", b.Id, err)
}
}
return nil
}
err := rows.Close()
if err != nil {
return fmt.Errorf("Failed to iterate through database rows, %w", err)
}
return nil
}
pg_opts, err := countable.NewCountableOptions()
if err != nil {
return fmt.Errorf("Failed to create pagination options, %w", err)
}
q := fmt.Sprintf("SELECT id, account_id, post_id, actor, created FROM %s WHERE post_id = ? AND actor = ?", SQL_LIKES_TABLE_NAME)
err = pg_sql.QueryPaginatedAll(db.database, pg_opts, pg_callback, q, post_id, actor)
if err != nil {
return fmt.Errorf("Failed to execute paginated query, %w", err)
}
return nil
}
func (db *SQLLikesDatabase) AddLike(ctx context.Context, b *Like) error {
q := fmt.Sprintf("INSERT INTO %s (id, account_id, psot_id, actor, created) VALUES (?, ?, ?, ?, ?)", SQL_LIKES_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, b.Id, b.AccountId, b.PostId, b.Actor, b.Created)
if err != nil {
return fmt.Errorf("Failed to add like, %w", err)
}
return nil
}
func (db *SQLLikesDatabase) RemoveLike(ctx context.Context, b *Like) error {
q := fmt.Sprintf("DELETE FROM %s WHERE id= ?", SQL_LIKES_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, b.Id)
if err != nil {
return fmt.Errorf("Failed to remove like, %w", err)
}
return nil
}
func (db *SQLLikesDatabase) Close(ctx context.Context) error {
return db.database.Close()
}
func (db *SQLLikesDatabase) getLike(ctx context.Context, where string, args ...interface{}) (*Like, error) {
var id int64
var account_id int64
var post_id int64
var actor string
var created int64
q := fmt.Sprintf("SELECT id, account_id, post_id, actor, created FROM %s WHERE %s", SQL_LIKES_TABLE_NAME, where)
row := db.database.QueryRowContext(ctx, q, args...)
err := row.Scan(&id, &account_id, &post_id, &actor, &created)
switch {
case err == sql.ErrNoRows:
return nil, ErrNotFound
case err != nil:
return nil, err
default:
//
}
b := &Like{
Id: id,
AccountId: account_id,
PostId: post_id,
Actor: actor,
Created: created,
}
return b, nil
}