split limit determination into separate function

This commit is contained in:
filifa 2024-03-19 20:31:32 -05:00
parent 139bfff209
commit 1892e77e4f
1 changed files with 15 additions and 4 deletions

19
main.go
View File

@ -34,17 +34,23 @@ func formula(p float64) int64 {
return int64(yearsAgo * 31556926) return int64(yearsAgo * 31556926)
} }
func (db *TimelineDB) closestEvents(t int64, n int, after bool, before bool) ([]EventsRow, error) { func limits(t int64, after bool, before bool) (int64, int64, error) {
ulimit := int64(math.MaxInt64)
llimit := int64(math.MinInt64) llimit := int64(math.MinInt64)
ulimit := int64(math.MaxInt64)
var err error
if after && before { if after && before {
return nil, errors.New("after and before can't both be true") err = errors.New("after and before can't both be true")
} else if after { } else if after {
llimit = t llimit = t
} else if before { } else if before {
ulimit = t ulimit = t
} }
return llimit, ulimit, err
}
func (db *TimelineDB) closestEvents(t int64, n int, llimit int64, ulimit int64) ([]EventsRow, error) {
query := ` query := `
select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown
from events from events
@ -130,7 +136,12 @@ func main() {
} }
defer db.Close() defer db.Close()
events, err := db.closestEvents(t, *nevents, *after, *before) llimit, ulimit, err := limits(t, *after, *before)
if err != nil {
log.Fatal(err)
}
events, err := db.closestEvents(t, *nevents, llimit, ulimit)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }