2019-08-20 15:38:40 +00:00
|
|
|
package multiaccounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/multiaccounts/migrations"
|
|
|
|
"github.com/status-im/status-go/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Account stores public information about account.
|
|
|
|
type Account struct {
|
2019-12-05 08:00:57 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
PhotoPath string `json:"photo-path"`
|
|
|
|
KeycardPairing string `json:"keycard-pairing"`
|
|
|
|
KeyUID string `json:"key-uid"`
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitializeDB creates db file at a given path and applies migrations.
|
|
|
|
func InitializeDB(path string) (*Database, error) {
|
|
|
|
db, err := sqlite.OpenUnecryptedDB(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = migrations.Migrate(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Database{db: db}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) GetAccounts() ([]Account, error) {
|
2019-12-05 08:00:57 +00:00
|
|
|
rows, err := db.db.Query("SELECT name, loginTimestamp, photoPath, keycardPairing, keyUid from accounts ORDER BY loginTimestamp DESC")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst := []Account{}
|
|
|
|
inthelper := sql.NullInt64{}
|
|
|
|
for rows.Next() {
|
|
|
|
acc := Account{}
|
2019-12-05 08:00:57 +00:00
|
|
|
err = rows.Scan(&acc.Name, &inthelper, &acc.PhotoPath, &acc.KeycardPairing, &acc.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
acc.Timestamp = inthelper.Int64
|
|
|
|
rst = append(rst, acc)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) SaveAccount(account Account) error {
|
2019-12-05 08:00:57 +00:00
|
|
|
_, err := db.db.Exec("INSERT OR REPLACE INTO accounts (name, photoPath, keycardPairing, keyUid) VALUES (?, ?, ?, ?)", account.Name, account.PhotoPath, account.KeycardPairing, account.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) UpdateAccount(account Account) error {
|
2019-12-05 08:00:57 +00:00
|
|
|
_, err := db.db.Exec("UPDATE accounts SET name = ?, photoPath = ?, keycardPairing = ? WHERE keyUid = ?", account.Name, account.PhotoPath, account.KeycardPairing, account.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:00:57 +00:00
|
|
|
func (db *Database) UpdateAccountTimestamp(keyUID string, loginTimestamp int64) error {
|
|
|
|
_, err := db.db.Exec("UPDATE accounts SET loginTimestamp = ? WHERE keyUid = ?", loginTimestamp, keyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:00:57 +00:00
|
|
|
func (db *Database) DeleteAccount(keyUID string) error {
|
|
|
|
_, err := db.db.Exec("DELETE FROM accounts WHERE keyUid = ?", keyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|