-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes_database_sql.go
202 lines (138 loc) · 4.59 KB
/
notes_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
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_NOTES_TABLE_NAME string = "notes"
type SQLNotesDatabase struct {
NotesDatabase
database *sql.DB
}
func init() {
ctx := context.Background()
RegisterNotesDatabase(ctx, "sql", NewSQLNotesDatabase)
}
func NewSQLNotesDatabase(ctx context.Context, uri string) (NotesDatabase, 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 live hard and die fast, %w", err)
}
}
db := &SQLNotesDatabase{
database: conn,
}
return db, nil
}
func (db *SQLNotesDatabase) GetNoteIdsForDateRange(ctx context.Context, start int64, end int64, cb GetNoteIdsCallbackFunc) 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 note %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_NOTES_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 *SQLNotesDatabase) GetNoteWithId(ctx context.Context, note_id int64) (*Note, error) {
where := "id = ?"
return db.getNote(ctx, where, note_id)
}
func (db *SQLNotesDatabase) GetNoteWithUUIDAndAuthorAddress(ctx context.Context, uuid string, author_address string) (*Note, error) {
// Note the order of arguments this is to account for the
// notes_by_author_address index.
where := "author_address=? AND uuid = ?"
return db.getNote(ctx, where, author_address, uuid)
}
func (db *SQLNotesDatabase) getNote(ctx context.Context, where string, args ...interface{}) (*Note, error) {
q := fmt.Sprintf("SELECT id, uuid, author_address, body, created, lastmodified FROM %s WHERE %s", SQL_NOTES_TABLE_NAME, where)
row := db.database.QueryRowContext(ctx, q, args...)
var id int64
var uuid string
var author_address string
var body string
var created int64
var lastmod int64
err := row.Scan(&id, &uuid, &author_address, &body, &created, &lastmod)
switch {
case err == sql.ErrNoRows:
return nil, ErrNotFound
case err != nil:
return nil, fmt.Errorf("Failed to query database, %w", err)
default:
n := &Note{
Id: id,
UUID: uuid,
AuthorAddress: author_address,
Body: body,
Created: created,
LastModified: lastmod,
}
return n, nil
}
}
func (db *SQLNotesDatabase) AddNote(ctx context.Context, note *Note) error {
q := fmt.Sprintf("INSERT INTO %s (id, uuid, author_address, body, created, lastmodified) VALUES (?, ?, ?, ?, ?, ?)", SQL_NOTES_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, note.Id, note.UUID, note.AuthorAddress, note.Body, note.Created, note.LastModified)
if err != nil {
return fmt.Errorf("Failed to add note, %w", err)
}
return nil
}
func (db *SQLNotesDatabase) UpdateNote(ctx context.Context, note *Note) error {
q := fmt.Sprintf("UPDATE %s SET uuid=?, author_address=?, body=?, created=?, lastmodified=? WHERE id = ?", SQL_NOTES_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, note.UUID, note.AuthorAddress, note.Body, note.Created, note.LastModified, note.Id)
if err != nil {
return fmt.Errorf("Failed to add note, %w", err)
}
return nil
}
func (db *SQLNotesDatabase) RemoveNote(ctx context.Context, note *Note) error {
q := fmt.Sprintf("DELETE FROM %s WHERE id = ?", SQL_NOTES_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, note.Id)
if err != nil {
return fmt.Errorf("Failed to remove note, %w", err)
}
return nil
}
func (db *SQLNotesDatabase) Close(ctx context.Context) error {
return db.database.Close()
}