diff --git a/plan/storage/sqlite/task.go b/plan/storage/sqlite/task.go index 653482d..a323ade 100644 --- a/plan/storage/sqlite/task.go +++ b/plan/storage/sqlite/task.go @@ -64,17 +64,25 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur } func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) { - query := `SELECT id, title, date, time, duration, recurrer FROM tasks WHERE 1=1` + query := `SELECT id, title, date, time, duration, recurrer FROM tasks` args := []interface{}{} + where := []string{} if params.Recurrer { - query += ` AND recurrer IS NOT NULL AND recurrer != ''` + where = append(where, `recurrer IS NOT NULL AND recurrer != ''`) } if !params.Date.IsZero() { - query += ` AND date = ?` + where = append(where, `date = ?`) args = append(args, params.Date.String()) } + if len(where) > 0 { + query += ` WHERE ` + where[0] + for _, w := range where[1:] { + query += ` AND ` + w + } + } + rows, err := t.db.Query(query, args...) if err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)