pye/storage/sqlite/sqlite.go

85 lines
2 KiB
Go
Raw Normal View History

2024-10-12 20:41:30 +03:00
package sqlite
2024-10-12 18:39:38 +03:00
import (
"database/sql"
"errors"
"log/slog"
"os"
2024-10-12 20:41:30 +03:00
"git.a71.su/Andrew71/pye/storage"
2024-10-12 18:39:38 +03:00
_ "github.com/mattn/go-sqlite3"
)
const create string = `
CREATE TABLE "users" (
"uuid" TEXT NOT NULL UNIQUE,
"email" TEXT NOT NULL UNIQUE,
"password" TEXT NOT NULL,
PRIMARY KEY("uuid")
);`
2024-10-13 17:33:59 +03:00
// SQLiteStorage is a Storage implementation with SQLite3
2024-10-12 20:41:30 +03:00
type SQLiteStorage struct {
db *sql.DB
2024-10-12 18:39:38 +03:00
}
2024-10-13 17:18:53 +03:00
func (s SQLiteStorage) Add(email, password string) error {
user, err := storage.New(email, password)
2024-10-12 20:41:30 +03:00
if err != nil {
return err
}
_, err = s.db.Exec("insert into users (uuid, email, password) values ($1, $2, $3)",
2024-10-12 18:39:38 +03:00
user.Uuid.String(), user.Email, user.Hash)
if err != nil {
2024-10-12 20:41:30 +03:00
slog.Error("error adding user to database", "error", err, "user", user)
2024-10-12 18:39:38 +03:00
return err
}
return nil
}
2024-10-12 20:41:30 +03:00
func (s SQLiteStorage) ById(uuid string) (storage.User, bool) {
row := s.db.QueryRow("select * from users where uuid = $1", uuid)
user := storage.User{}
2024-10-12 18:39:38 +03:00
err := row.Scan(&user.Uuid, &user.Email, &user.Hash)
return user, err == nil
}
2024-10-12 20:41:30 +03:00
func (s SQLiteStorage) ByEmail(email string) (storage.User, bool) {
row := s.db.QueryRow("select * from users where email = $1", email)
user := storage.User{}
2024-10-12 18:39:38 +03:00
err := row.Scan(&user.Uuid, &user.Email, &user.Hash)
return user, err == nil
}
2024-10-13 17:18:53 +03:00
func (s SQLiteStorage) Taken(email string) bool {
2024-10-12 20:41:30 +03:00
_, ok := s.ByEmail(email)
2024-10-12 18:39:38 +03:00
return ok
}
2024-10-12 20:41:30 +03:00
2024-10-13 17:18:53 +03:00
func MustLoad(dataFile string) SQLiteStorage {
2024-10-12 21:45:00 +03:00
if _, err := os.Stat(dataFile); errors.Is(err, os.ErrNotExist) {
2024-10-12 21:58:42 +03:00
os.Create(dataFile)
2024-10-13 16:38:13 +03:00
slog.Debug("created sqlite3 database file", "file", dataFile)
2024-10-12 20:41:30 +03:00
}
2024-10-12 21:45:00 +03:00
db, err := sql.Open("sqlite3", dataFile)
2024-10-12 20:41:30 +03:00
if err != nil {
2024-10-13 11:25:00 +03:00
slog.Error("error opening sqlite3 database", "error", err)
2024-10-12 20:41:30 +03:00
os.Exit(1)
}
2024-10-12 21:58:42 +03:00
statement, err := db.Prepare(create)
if err != nil {
if err.Error() != "table \"users\" already exists" {
2024-10-13 16:38:13 +03:00
slog.Error("error initialising sqlite3 database table", "error", err)
2024-10-12 21:58:42 +03:00
os.Exit(1)
}
} else {
statement.Exec()
2024-10-12 20:41:30 +03:00
}
2024-10-12 21:58:42 +03:00
2024-10-13 16:38:13 +03:00
slog.Debug("loaded database", "file", dataFile)
2024-10-12 20:41:30 +03:00
return SQLiteStorage{db}
}