/
accounts_database_sql.go
193 lines (138 loc) · 4.52 KB
/
accounts_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
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_ACCOUNTS_TABLE_NAME string = "accounts"
type SQLAccountsDatabase struct {
AccountsDatabase
database *sql.DB
}
func init() {
ctx := context.Background()
RegisterAccountsDatabase(ctx, "sql", NewSQLAccountsDatabase)
}
func NewSQLAccountsDatabase(ctx context.Context, uri string) (AccountsDatabase, 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 set up SQLite, %w", err)
}
}
db := &SQLAccountsDatabase{
database: conn,
}
return db, nil
}
func (db *SQLAccountsDatabase) GetAccounts(ctx context.Context, acct GetAccountsCallbackFunc) error {
return ErrNotImplemented
}
func (db *SQLAccountsDatabase) GetAccountIdsForDateRange(ctx context.Context, start int64, end int64, cb GetAccountIdsCallbackFunc) 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 account %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_ACCOUNTS_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 *SQLAccountsDatabase) AddAccount(ctx context.Context, a *Account) error {
q := fmt.Sprintf("INSERT INTO %s (id, account_type, name, display_name, blurb, url, public_key_uri, private_key_uri, created, lastmodified) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", SQL_ACCOUNTS_TABLE_NAME)
_, err := db.database.ExecContext(ctx, q, a.Id, a.AccountType, a.Name, a.DisplayName, a.Blurb, a.URL, a.PublicKeyURI, a.PrivateKeyURI, a.Created, a.LastModified)
if err != nil {
return fmt.Errorf("Failed to add account, %w", err)
}
return nil
}
func (db *SQLAccountsDatabase) GetAccountWithId(ctx context.Context, id int64) (*Account, error) {
where := "id = ?"
return db.getAccount(ctx, where, id)
}
func (db *SQLAccountsDatabase) GetAccountWithName(ctx context.Context, name string) (*Account, error) {
where := "name = ?"
return db.getAccount(ctx, where, name)
}
func (db *SQLAccountsDatabase) getAccount(ctx context.Context, where string, args ...interface{}) (*Account, error) {
var id int64
var account_type uint32
var name string
var display_name string
var blurb string
var url string
var public_key_uri string
var private_key_uri string
var created int64
var lastmod int64
q := fmt.Sprintf("SELECT id, account_type, name, display_name, blurb, url, public_key_uri, private_key_uri, created, lastmodified FROM %s WHERE %s", SQL_ACCOUNTS_TABLE_NAME, where)
row := db.database.QueryRowContext(ctx, q, args...)
err := row.Scan(&id, &account_type, &name, &display_name, &blurb, &url, &public_key_uri, &private_key_uri, &created, &lastmod)
switch {
case err == sql.ErrNoRows:
return nil, ErrNotFound
case err != nil:
return nil, err
default:
//
}
a := &Account{
Id: id,
AccountType: AccountType(account_type),
Name: name,
DisplayName: display_name,
Blurb: blurb,
URL: url,
PublicKeyURI: public_key_uri,
PrivateKeyURI: private_key_uri,
Created: created,
LastModified: lastmod,
}
return a, nil
}
func (db *SQLAccountsDatabase) UpdateAccount(ctx context.Context, acct *Account) error {
return ErrNotImplemented
}
func (db *SQLAccountsDatabase) RemoveAccount(ctx context.Context, acct *Account) error {
return ErrNotImplemented
}
func (db *SQLAccountsDatabase) Close(ctx context.Context) error {
return db.database.Close()
}