-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdkeeper.go
478 lines (397 loc) · 13.8 KB
/
bdkeeper.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
Package bdkeeper provides functionalities to interact with a SQLite database
and perform operations like user management, data manipulation, and synchronization.
Usage:
import "github.com/example.com/bdkeeper"
keeper := bdkeeper.NewKeeper()
// Example: Check if a user exists
exists, err := keeper.UserExists(context.Background(), "username")
if err != nil {
log.Fatal(err)
}
fmt.Println("User exists:", exists)
// Example: Add a new user
err := keeper.AddUser(context.Background(), "username", "hashedPassword")
if err != nil {
log.Fatal(err)
}
// Other functionalities are used similarly.
Data Structures:
Keeper - Represents a database keeper with methods to interact with the database.
SyncQueue - Represents a synchronization queue entry with pending operations.
Example Usage:
keeper := bdkeeper.NewKeeper()
// Check if a user exists
exists, err := keeper.UserExists(context.Background(), "username")
if err != nil {
log.Fatal(err)
}
fmt.Println("User exists:", exists)
// Add a new user
err := keeper.AddUser(context.Background(), "username", "hashedPassword")
if err != nil {
log.Fatal(err)
}
// Other functionalities are used similarly.
Thread Safety:
The methods provided by Keeper are not thread-safe. It is expected that
the user of the package ensures proper synchronization when used concurrently.
*/
package bdkeeper
import (
"context"
"database/sql"
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"strings"
_ "github.com/mattn/go-sqlite3" // SQLite driver
"github.com/wurt83ow/gophkeeper-client/pkg/models"
"golang.org/x/crypto/bcrypt"
)
// embeddedMigrations contains SQL migration files.
//
//go:embed migrations/*.sql
var embeddedMigrations embed.FS
// Keeper represents a database keeper with methods to interact with the database.
type Keeper struct {
db *sql.DB
}
// NewKeeper creates a new instance of Keeper and initializes the database.
func NewKeeper(db *sql.DB) *Keeper {
if db == nil {
var err error
db, err = sql.Open("sqlite3", "./data.db")
if err != nil {
panic(err)
}
// Create the migrations table if it doesn't exist
_, err = db.Exec("CREATE TABLE IF NOT EXISTS migrations (name TEXT PRIMARY KEY)")
if err != nil {
panic(err)
}
// Apply migrations
files, err := fs.ReadDir(embeddedMigrations, "migrations")
if err != nil {
panic(err)
}
for _, file := range files {
if !file.IsDir() {
// Check if the migration has already been applied
var name string
err = db.QueryRow("SELECT name FROM migrations WHERE name = ?", file.Name()).Scan(&name)
if err == sql.ErrNoRows {
// The migration has not been applied yet
f, err := embeddedMigrations.Open("migrations/" + file.Name())
if err != nil {
panic(err)
}
defer f.Close()
bytes, err := io.ReadAll(f)
if err != nil {
panic(err)
}
upAndDown := strings.Split(string(bytes), "-- +goose Down")
upStatements := strings.Split(upAndDown[0], ";")
// Run the "up" statements.
for _, stmt := range upStatements {
if _, err := db.Exec(stmt); err != nil {
log.Fatalf("Failed to execute migration %s: %v", file.Name(), err)
}
}
// Record the migration as having been applied
_, err = db.Exec("INSERT INTO migrations (name) VALUES (?)", file.Name())
if err != nil {
panic(err)
}
} else if err != nil {
// An error occurred checking if the migration has been applied
panic(err)
}
}
}
}
k := &Keeper{
db: db,
}
return k
}
// UserExists checks if a user exists in the database.
func (k *Keeper) UserExists(ctx context.Context, username string) (bool, error) {
// Query to check the existence of a user in the database
query := `SELECT COUNT(*) FROM Users WHERE username = ?;`
// Execute the query
row := k.db.QueryRowContext(ctx, query, username)
// Get the result
var count int
err := row.Scan(&count)
if err != nil {
return false, err
}
// If the count is greater than 0, the user exists
return count > 0, nil
}
// AddUser adds a new user to the database.
func (k *Keeper) AddUser(ctx context.Context, username string, hashedPassword string) error {
// Query to add a new user to the database
query := `INSERT INTO Users (username, password) VALUES (?, ?);`
// Execute the query
_, err := k.db.ExecContext(ctx, query, username, hashedPassword)
return err
}
// IsEmpty checks if the database is empty.
func (k *Keeper) IsEmpty(ctx context.Context) (bool, error) {
// Query to get the count of entries in all tables
query := `SELECT count(*) FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%';`
// Execute the query
row := k.db.QueryRowContext(ctx, query)
// Get the result
var count int
err := row.Scan(&count)
if err != nil {
return false, err
}
// If the count is 0, the database is empty
return count == 0, nil
}
// CreateSyncEntry creates a synchronization queue entry in the database.
func (k *Keeper) CreateSyncEntry(ctx context.Context, operation string, table string, user_id int, entry_id string, data map[string]string) error {
// Convert data to JSON
dataJson, err := json.Marshal(data)
if err != nil {
return err
}
// Set initial status as "Pending"
status := "Pending"
// Add entry to SyncQueue table
_, err = k.db.ExecContext(ctx, "INSERT INTO SyncQueue (operation, table_name, user_id, entry_id, data, status) VALUES (?, ?, ?, ?, ?, ?)",
operation, table, user_id, entry_id, dataJson, status)
return err
}
// GetPassword retrieves the hashed password of a user from the database.
func (k *Keeper) GetPassword(ctx context.Context, username string) (string, error) {
// Query to get the hashed password of a user from the database
query := `SELECT password FROM Users WHERE username = ?;`
// Execute the query
row := k.db.QueryRowContext(ctx, query, username)
// Get the result
var password string
err := row.Scan(&password)
if err != nil {
return "", err
}
// Return the hashed password
return password, nil
}
// CompareHashAndPassword compares a hashed password with a plaintext password.
func (k *Keeper) CompareHashAndPassword(hashedPassword, password string) bool {
// Compare the hashed password with the hash of the entered password
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
return err == nil
}
// GetUserID retrieves the user ID of a user from the database.
func (k *Keeper) GetUserID(ctx context.Context, username string) (int, error) {
// Query to get the user ID from the database
query := `SELECT id FROM Users WHERE username = ?;`
// Execute the query
row := k.db.QueryRowContext(ctx, query, username)
// Get the result
var id int
err := row.Scan(&id)
if err != nil {
return 0, err
}
// Return the user ID
return id, nil
}
// AddData adds data to the specified database table.
// Keys and values from the provided data map will be inserted into the specified table, along with the given user_id and entry_id.
func (k *Keeper) AddData(ctx context.Context, table string, user_id int, entry_id string, data map[string]string) error {
// Create lists of keys and values considering user_id and entry_id
keys := make([]string, 0, len(data)+2) // +2 for user_id and entry_id
values := make([]interface{}, 0, len(data)+2) // +2 for user_id and entry_id
// Add user_id and entry_id to the beginning of keys and values lists
keys = append(keys, "user_id", "id")
values = append(values, user_id, entry_id)
// Add other keys and values from the provided data map
for key, value := range data {
if key == "deleted" {
continue
}
keys = append(keys, key)
values = append(values, value)
}
// Prepare the SQL query and execute it
stmt, err := k.db.Prepare(fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)", table, strings.Join(keys, ","), strings.Repeat("?,", len(keys)-1)+"?"))
if err != nil {
return err
}
_, err = stmt.ExecContext(ctx, values...)
return err
}
// UpdateData updates data in the specified database table.
// Values from the provided data map will be used to update the record with the specified user_id and entry_id in the specified table.
func (k *Keeper) UpdateData(ctx context.Context, table string, user_id int, entry_id string, data map[string]string) error {
// Create lists of keys and values to update data
keys := make([]string, 0, len(data))
values := make([]interface{}, 0, len(data))
for key, value := range data {
keys = append(keys, key+" = ?")
values = append(values, value)
}
// Append user_id and entry_id to the end of the lists
values = append(values, user_id, entry_id)
// Prepare the SQL query and execute it
stmt, err := k.db.Prepare(fmt.Sprintf("UPDATE %s SET %s WHERE user_id = ? AND id = ?", table, strings.Join(keys, ",")))
if err != nil {
return err
}
_, err = stmt.ExecContext(ctx, values...)
return err
}
// DeleteData deletes a record from the specified table in the database.
// It checks for the existence of the specified user_id and entry_id, and then deletes the record if found.
func (k *Keeper) DeleteData(ctx context.Context, table string, user_id int, entry_id string) error {
// Check user_id and table
if user_id == 0 || table == "" {
return errors.New("user_id and table must be specified")
}
// Check entry_id
if entry_id == "" {
return errors.New("id must be specified")
}
// Prepare the query to check the existence of the record
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE user_id = ? AND id = ?", table)
args := []interface{}{user_id, entry_id}
// Execute the query
row := k.db.QueryRowContext(ctx, query, args...)
var count int
err := row.Scan(&count)
if err != nil {
return err
}
// Check the number of records found
if count > 1 {
return errors.New("More than one record found")
} else if count == 0 {
return errors.New("No records found")
}
// Delete the record
query = strings.Replace(query, "SELECT COUNT(*)", "DELETE", 1)
_, err = k.db.ExecContext(ctx, query, args...)
return err
}
// GetData retrieves data for a specific entry from the specified table in the database.
// It returns a map containing column names as keys and corresponding values for the entry.
func (k *Keeper) GetData(ctx context.Context, table string, user_id int, entry_id string) (map[string]string, error) {
// Get all columns of the table
columns, err := k.db.QueryContext(ctx, fmt.Sprintf("PRAGMA table_info(%s)", table))
if err != nil {
return nil, fmt.Errorf("failed to get columns: %w", err)
}
var cols []string
for columns.Next() {
var col struct {
Cid int
Name string
Type string
NotNull bool
Dflt_value *string
Pk int
}
err := columns.Scan(&col.Cid, &col.Name, &col.Type, &col.NotNull, &col.Dflt_value, &col.Pk)
if err != nil {
return nil, fmt.Errorf("failed to scan column: %w", err)
}
// Exclude unnecessary columns
if col.Name != "id" && col.Name != "deleted" && col.Name != "user_id" && col.Name != "updated_at" {
cols = append(cols, col.Name)
}
}
// Query the specific entry
row := k.db.QueryRowContext(ctx, fmt.Sprintf("SELECT %s FROM %s WHERE id = ?", strings.Join(cols, ","), table), entry_id)
values := make([]interface{}, len(cols))
for i := range values {
var value string
values[i] = &value
}
err = row.Scan(values...)
if err != nil {
return nil, fmt.Errorf("failed to scan row: %w", err)
}
data := make(map[string]string)
for i, column := range cols {
data[column] = *(values[i].(*string))
}
return data, nil
}
// GetAllData retrieves all data from the specified table in the database for the given user_id.
// It returns a slice of maps, with each map containing column names as keys and corresponding values for each row.
func (k *Keeper) GetAllData(ctx context.Context, table string, user_id int, columns ...string) ([]map[string]string, error) {
query := fmt.Sprintf("SELECT %s FROM %s WHERE user_id = ?", strings.Join(columns, ","), table)
rows, err := k.db.QueryContext(ctx, query, user_id)
if err != nil {
return nil, fmt.Errorf("failed to execute query: %w", err)
}
defer rows.Close()
values := make([]interface{}, len(columns))
for i := range values {
values[i] = new(sql.RawBytes)
}
var data []map[string]string
for rows.Next() {
err := rows.Scan(values...)
if err != nil {
return nil, fmt.Errorf("failed to scan row: %w", err)
}
row := make(map[string]string)
for i, column := range columns {
row[column] = string(*values[i].(*sql.RawBytes))
}
data = append(data, row)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("rows encountered an error: %w", err)
}
return data, nil
}
// ClearData deletes all records associated with the specified user_id from the specified table in the database.
func (k *Keeper) ClearData(ctx context.Context, table string, userID int) error {
stmt, err := k.db.Prepare(fmt.Sprintf("DELETE FROM %s WHERE user_id = ?", table))
if err != nil {
return err
}
_, err = stmt.ExecContext(ctx, userID)
return err
}
// GetSyncEntriesByStatus returns all entries from the sync table with the given status.
func (k *Keeper) GetSyncEntriesByStatus(ctx context.Context, status string) ([]models.SyncQueue, error) {
var entries []models.SyncQueue
query := "SELECT * FROM SyncQueue WHERE status = ?"
rows, err := k.db.QueryContext(ctx, query, status)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var entry models.SyncQueue
err = rows.Scan(&entry.ID, &entry.TableName, &entry.UserID, &entry.EntryID, &entry.Operation, &entry.Data, &entry.Status)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
if err := rows.Err(); err != nil {
return nil, err
}
return entries, nil
}
// UpdateSyncEntryStatus updates the status of an entry in the sync table.
func (k *Keeper) UpdateSyncEntryStatus(ctx context.Context, id int, status string) error {
_, err := k.db.ExecContext(ctx, "UPDATE SyncQueue SET status = ? WHERE id = ?", status, id)
return err
}