You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.3 KiB
Go
155 lines
3.3 KiB
Go
3 years ago
|
package namespace
|
||
3 years ago
|
|
||
|
import (
|
||
3 years ago
|
"caj-larsson/bog/dataswamp/namespace"
|
||
3 years ago
|
"database/sql"
|
||
|
"errors"
|
||
|
"github.com/mattn/go-sqlite3"
|
||
|
)
|
||
|
|
||
3 years ago
|
type Repository struct {
|
||
3 years ago
|
db *sql.DB
|
||
|
}
|
||
|
|
||
3 years ago
|
func NewRepository(filename string) *Repository {
|
||
3 years ago
|
db, err := sql.Open("sqlite3", filename)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
3 years ago
|
repo := Repository{
|
||
3 years ago
|
db: db,
|
||
|
}
|
||
3 years ago
|
repo.migrate()
|
||
3 years ago
|
return &repo
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (r Repository) migrate() error {
|
||
3 years ago
|
query := `
|
||
3 years ago
|
CREATE TABLE IF NOT EXISTS namespace(
|
||
3 years ago
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
name TEXT NOT NULL UNIQUE,
|
||
|
lastseen text,
|
||
|
allowance_time bigint,
|
||
|
quota_kb bigint,
|
||
|
quota_usage_kb bigint
|
||
|
);
|
||
|
`
|
||
|
_, err := r.db.Exec(query)
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
func (r *Repository) Create(ns namespace.Namespace) (*namespace.Namespace, error) {
|
||
|
var record, err = fromEntity(ns)
|
||
3 years ago
|
if err != nil {
|
||
|
|
||
|
}
|
||
|
|
||
|
res, err := r.db.Exec(
|
||
3 years ago
|
"INSERT INTO namespace(name, lastseen, allowance_time, quota_kb, quota_usage_kb) values(?,?,?,?,?)",
|
||
3 years ago
|
record.Name, record.LastSeen, record.AllowanceSeconds, record.QuotaKB, record.QuotaUsedKB,
|
||
|
)
|
||
|
if err != nil {
|
||
|
var sqliteErr sqlite3.Error
|
||
|
if errors.As(err, &sqliteErr) {
|
||
|
if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintUnique) {
|
||
3 years ago
|
return nil, namespace.ErrDuplicate
|
||
3 years ago
|
}
|
||
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
id, err := res.LastInsertId()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
ns.ID = id
|
||
3 years ago
|
|
||
3 years ago
|
return &ns, nil
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (r Repository) All() ([]namespace.Namespace, error) {
|
||
|
rows, err := r.db.Query("SELECT * FROM namespace")
|
||
3 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
3 years ago
|
var all []namespace.Namespace
|
||
3 years ago
|
for rows.Next() {
|
||
3 years ago
|
var record NamespaceRecord
|
||
3 years ago
|
if err := rows.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
var ns, err = record.toEntity()
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
all = append(all, *ns)
|
||
3 years ago
|
}
|
||
|
return all, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (r Repository) GetByName(name string) (*namespace.Namespace, error) {
|
||
|
row := r.db.QueryRow("SELECT id, name, lastseen, allowance_time, quota_kb, quota_usage_kb FROM namespace WHERE name = ?", name)
|
||
3 years ago
|
|
||
3 years ago
|
var record NamespaceRecord
|
||
3 years ago
|
if err := row.Scan(&record.ID, &record.Name, &record.LastSeen, &record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB); err != nil {
|
||
|
if errors.Is(err, sql.ErrNoRows) {
|
||
3 years ago
|
return nil, namespace.ErrNotExists
|
||
3 years ago
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
var ns, err = record.toEntity()
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
return ns, nil
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (r Repository) Update(id int64, updated namespace.Namespace) (*namespace.Namespace, error) {
|
||
3 years ago
|
if id == 0 {
|
||
|
return nil, errors.New("invalid updated ID")
|
||
|
}
|
||
|
|
||
|
var record, err = fromEntity(updated)
|
||
|
|
||
3 years ago
|
res, err := r.db.Exec("UPDATE namespace SET name = ?, lastseen = ?, allowance_time = ?, quota_kb = ?, quota_usage_kb = ? WHERE id = ?",
|
||
3 years ago
|
record.Name, record.LastSeen, record.AllowanceSeconds, &record.QuotaKB, &record.QuotaUsedKB, id)
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
rowsAffected, err := res.RowsAffected()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if rowsAffected == 0 {
|
||
3 years ago
|
return nil, namespace.ErrUpdateFailed
|
||
3 years ago
|
}
|
||
|
|
||
|
return &updated, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (r Repository) Delete(id int64) error {
|
||
|
res, err := r.db.Exec("DELETE FROM namespace WHERE id = ?", id)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
rowsAffected, err := res.RowsAffected()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if rowsAffected == 0 {
|
||
3 years ago
|
return namespace.ErrDeleteFailed
|
||
3 years ago
|
}
|
||
|
|
||
|
return err
|
||
|
}
|