planner/plan/storage/sqlite/sync.go

103 lines
2.7 KiB
Go
Raw Normal View History

2024-10-07 11:11:18 +02:00
package sqlite
import (
"database/sql"
"fmt"
"time"
"go-mod.ewintr.nl/planner/item"
2025-01-13 09:13:48 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
2024-10-07 11:11:18 +02:00
)
2025-01-13 09:13:48 +01:00
type Sync struct {
tx *storage.Tx
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
func (s *Sync) FindAll() ([]item.Item, error) {
rows, err := s.tx.Query("SELECT id, kind, updated, deleted, date, recurrer, recur_next, body FROM items")
2024-10-07 11:11:18 +02:00
if err != nil {
return nil, fmt.Errorf("%w: failed to query items: %v", ErrSqliteFailure, err)
}
defer rows.Close()
var items []item.Item
for rows.Next() {
var i item.Item
2024-12-26 09:54:55 +01:00
var updatedStr, dateStr, recurStr, recurNextStr string
err := rows.Scan(&i.ID, &i.Kind, &updatedStr, &i.Deleted, &dateStr, &recurStr, &recurNextStr, &i.Body)
2024-10-07 11:11:18 +02:00
if err != nil {
return nil, fmt.Errorf("%w: failed to scan item: %v", ErrSqliteFailure, err)
}
i.Updated, err = time.Parse(time.RFC3339, updatedStr)
if err != nil {
2024-12-19 12:06:03 +01:00
return nil, fmt.Errorf("failed to parse updated time: %v", err)
2024-10-07 11:11:18 +02:00
}
2024-12-26 09:54:55 +01:00
i.Date = item.NewDateFromString(dateStr)
2024-12-19 12:06:03 +01:00
i.Recurrer = item.NewRecurrer(recurStr)
i.RecurNext = item.NewDateFromString(recurNextStr)
2024-10-07 11:11:18 +02:00
items = append(items, i)
}
if err = rows.Err(); err != nil {
2024-12-19 12:06:03 +01:00
return nil, fmt.Errorf("error iterating over rows: %v", err)
2024-10-07 11:11:18 +02:00
}
return items, nil
}
2025-01-13 09:13:48 +01:00
func (s *Sync) Store(i item.Item) error {
2024-10-07 11:11:18 +02:00
if i.Updated.IsZero() {
i.Updated = time.Now()
}
2024-12-19 12:06:03 +01:00
var recurStr string
if i.Recurrer != nil {
recurStr = i.Recurrer.String()
}
2024-10-07 11:11:18 +02:00
2025-01-13 09:13:48 +01:00
_, err := s.tx.Exec(
2024-12-26 09:54:55 +01:00
`INSERT OR REPLACE INTO items (id, kind, updated, deleted, date, recurrer, recur_next, body)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
2024-10-07 11:11:18 +02:00
i.ID,
i.Kind,
i.Updated.UTC().Format(time.RFC3339),
i.Deleted,
2024-12-26 09:54:55 +01:00
i.Date.String(),
2024-12-19 12:06:03 +01:00
recurStr,
i.RecurNext.String(),
2024-10-07 11:11:18 +02:00
sql.NullString{String: i.Body, Valid: i.Body != ""}, // This allows empty string but not NULL
)
if err != nil {
return fmt.Errorf("%w: failed to store item: %v", ErrSqliteFailure, err)
}
return nil
}
2025-01-13 09:13:48 +01:00
func (s *Sync) DeleteAll() error {
_, err := s.tx.Exec("DELETE FROM items")
2024-10-07 11:11:18 +02:00
if err != nil {
return fmt.Errorf("%w: failed to delete all items: %v", ErrSqliteFailure, err)
}
return nil
}
2025-01-13 09:13:48 +01:00
func (s *Sync) SetLastUpdate(ts time.Time) error {
if _, err := s.tx.Exec(`UPDATE syncupdate SET timestamp = ?`, ts.Format(time.RFC3339)); err != nil {
2025-01-05 14:37:46 +01:00
return fmt.Errorf("%w: could not store timestamp: %v", ErrSqliteFailure, err)
2024-10-07 11:11:18 +02:00
}
2025-01-05 14:37:46 +01:00
return nil
}
2024-10-07 11:11:18 +02:00
2025-01-13 09:13:48 +01:00
func (s *Sync) LastUpdate() (time.Time, error) {
2025-01-05 14:37:46 +01:00
var tsStr string
2025-01-13 09:13:48 +01:00
if err := s.tx.QueryRow("SELECT timestamp FROM syncupdate").Scan(&tsStr); err != nil {
2025-01-05 14:37:46 +01:00
return time.Time{}, fmt.Errorf("%w: failed to get last update: %v", ErrSqliteFailure, err)
2024-10-07 11:11:18 +02:00
}
2025-01-05 14:37:46 +01:00
ts, err := time.Parse(time.RFC3339, tsStr)
2024-10-07 11:11:18 +02:00
if err != nil {
2025-01-05 14:37:46 +01:00
return time.Time{}, fmt.Errorf("%w: could not convert db timstamp into time.Time: %v", ErrSqliteFailure, err)
2024-10-07 11:11:18 +02:00
}
2025-01-05 14:37:46 +01:00
return ts, nil
2024-10-07 11:11:18 +02:00
}