2020-01-13 19:17:30 +00:00
|
|
|
package waku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sqlitePersistence struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSQLitePersistence(db *sql.DB) *sqlitePersistence {
|
|
|
|
return &sqlitePersistence{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqlitePersistence) Add(chatID string, key []byte) error {
|
|
|
|
statement := "INSERT INTO waku_keys(chat_id, key) VALUES(?, ?)"
|
|
|
|
stmt, err := s.db.Prepare(statement)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(chatID, key)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqlitePersistence) All() (map[string][]byte, error) {
|
|
|
|
keys := make(map[string][]byte)
|
|
|
|
|
|
|
|
statement := "SELECT chat_id, key FROM waku_keys"
|
|
|
|
|
|
|
|
stmt, err := s.db.Prepare(statement)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
rows, err := stmt.Query()
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer rows.Close()
|
2020-01-13 19:17:30 +00:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
chatID string
|
|
|
|
key []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
err := rows.Scan(&chatID, &key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys[chatID] = key
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys, nil
|
|
|
|
}
|