henwen/store.go

174 lines
3.4 KiB
Go
Raw Normal View History

2020-03-24 14:59:27 +00:00
package henwen
import (
"context"
2020-03-31 14:47:40 +00:00
"errors"
"fmt"
"log"
2020-03-24 14:59:27 +00:00
"os"
"time"
2020-03-31 14:47:40 +00:00
"crawshaw.io/sqlite"
2020-03-24 14:59:27 +00:00
"crawshaw.io/sqlite/sqlitex"
)
type GenString func() (string, error)
type Store struct {
pool *sqlitex.Pool
genString GenString
}
const InMemory = ":memory:"
func NewStore(filename string, genString GenString) (*Store, error) {
var needCreate bool
if filename == InMemory {
filename = "file::memory:?mode=memory"
needCreate = true
} else if _, err := os.Stat(filename); os.IsNotExist(err) {
needCreate = true
}
// If the file exists, then assume it was created properly.
pool, err := sqlitex.Open(filename, 0, 10)
if err != nil {
return nil, err
}
store := &Store{
pool: pool,
genString: genString,
}
if needCreate {
2020-03-31 14:47:40 +00:00
log.Println("creating schema")
2020-03-24 14:59:27 +00:00
err = store.createSchema()
if err != nil {
defer pool.Close()
return nil, err
}
}
return store, nil
}
func (s *Store) Close() error {
return s.pool.Close()
}
const schema = `
CREATE TABLE event (
id INTEGER PRIMARY KEY,
alpha_id TEXT NOT NULL,
2020-03-31 14:47:40 +00:00
admin_code TEXT NOT NULL,
2020-03-24 14:59:27 +00:00
name TEXT NOT NULL,
earliest_date DATE NOT NULL,
latest_date DATE NOT NULL,
duration INTEGER NOT NULL,
UNIQUE (alpha_id)
);
CREATE TABLE response (
id INTEGER PRIMARY KEY,
alpha_id TEXT NOT NULL,
event_id INTEGER NOT NULL,
UNIQUE (alpha_id),
FOREIGN KEY (event_id) REFERENCES event(id)
);
CREATE TABLE response_time (
response_id INTEGER PRIMARY KEY,
time INTEGER NOT NULL,
CHECK (0 <= time < 24)
);`
func (s *Store) createSchema() error {
conn := s.pool.Get(context.Background())
defer s.pool.Put(conn)
return sqlitex.ExecScript(conn, schema)
}
type CreateEventCommand struct {
Name string
EarliestDate, LatestDate time.Time
Duration int
}
type CreateEventResult struct {
2020-03-31 14:47:40 +00:00
AlphaID, AdminCode string
2020-03-24 14:59:27 +00:00
}
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
alphaID, err := s.genString()
if err != nil {
return
}
2020-03-31 14:47:40 +00:00
adminCode, err := s.genString()
2020-03-24 14:59:27 +00:00
if err != nil {
return
}
const dateFmt = "2006-01-02"
const query = `
2020-03-31 14:47:40 +00:00
INSERT INTO event(alpha_id, admin_code, name, earliest_date, latest_date, duration)
2020-03-24 14:59:27 +00:00
VALUES (?, ?, ?, ?, ?, ?);`
err = sqlitex.Exec(conn, query, nil,
alphaID,
2020-03-31 14:47:40 +00:00
adminCode,
2020-03-24 14:59:27 +00:00
cmd.Name,
cmd.EarliestDate.Format(dateFmt),
cmd.LatestDate.Format(dateFmt),
cmd.Duration,
)
if err != nil {
return
}
2020-03-31 14:47:40 +00:00
result.AdminCode = adminCode
2020-03-24 14:59:27 +00:00
result.AlphaID = alphaID
return
}
2020-03-31 14:47:40 +00:00
type GetEventQuery struct {
AlphaID, AdminCode string
}
type GetEventResult struct {
Name string
EarliestDate, LatestDate time.Time
Duration int
}
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) error {
conn := s.pool.Get(ctx)
defer s.pool.Put(conn)
const dbQuery = `
SELECT name, earliest_date, latest_date, duration
FROM event
WHERE alpha_id = ? AND admin_code = ?;`
var found bool
err := sqlitex.Exec(conn, dbQuery,
func(stmt *sqlite.Stmt) error {
found = true
fmt.Printf("Name: %s\n", stmt.ColumnText(0))
fmt.Printf("Earliest date: %s\n", stmt.ColumnText(1))
fmt.Printf("Latest date: %s\n", stmt.ColumnText(2))
fmt.Printf("Duration: %d\n", stmt.ColumnInt(3))
return nil
}, query.AlphaID, query.AdminCode)
if err != nil {
return err
}
if !found {
return errors.New("not found")
}
return nil
}