-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
64 lines (54 loc) · 1.47 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tt
import (
"fmt"
)
const (
FieldStart = "start"
FieldProject = "project"
FieldTask = "task"
FieldDay = "day"
OrderAsc Order = "ASC"
OrderDsc Order = "DESC"
)
type DB interface {
// SaveTimer will write a single timer to the database.
SaveTimer(Timer) error
// GetTimer reads a single timer from the database by appending `LIMIT 1` to
// the query. Will return ErrNotFound if no timer matching the filter exists.
GetTimer(Filter, OrderBy, *Timer) error
GetTimerById(string, *Timer) error
// GetTimers returns multiple timers from the database that match the filter.
// Will never return ErrNotFound but rather just an empty list.
GetTimers(Filter, OrderBy, *Timers) error
// UpdateTimer updates existing timers in the db based on the id. If the timer
// does not exist an ErrNotFound is returned.
UpdateTimer(Timer) error
// RemoveTimer removes the timer from the database.
RemoveTimer(string) error
SaveVacationDay(VacationDay) error
GetVacationDay(VacationFilter, *VacationDay) error
GetVacationDays(OrderBy, *[]VacationDay) error
RemoveVacationDay(string) error
}
type Order string
type OrderBy struct {
Field string
Order Order
}
func (o OrderBy) SQL() string {
if o.Field == "" {
return ""
}
return fmt.Sprintf("ORDER BY json_extract(`json`, '$.%s') %s", o.Field, o.Order)
}
var db DB
func GetDB() DB {
if db == nil {
var err error
db, err = NewSQLite(GetConfig().DBFile())
if err != nil {
panic(err.Error())
}
}
return db
}