From 3b29bad6c72a820e922555e5ea554a7d44df0d9a Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Sun, 28 Jan 2018 14:53:37 +0700 Subject: [PATCH] Clean up query for getting bookmark --- database/sqlite.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/database/sqlite.go b/database/sqlite.go index 68e617d9..5178e446 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -2,6 +2,7 @@ package database import ( "database/sql" + "fmt" "github.com/RadhiFadlillah/go-readability" "github.com/RadhiFadlillah/shiori/model" "github.com/jmoiron/sqlx" @@ -187,37 +188,38 @@ func (db *SQLiteDatabase) GetBookmarks(indices ...string) ([]model.Bookmark, err query := `SELECT id, url, title, image_url, excerpt, author, language, min_read_time, max_read_time, modified - FROM bookmark ` + FROM bookmark` args := []interface{}{} + if len(indices) == 0 { + query += " WHERE 1" + } else { + query += " WHERE 0" + } + // Add where clause for _, strIndex := range indices { - clause := "WHERE" - if strings.Contains(query, "WHERE") { - clause = "OR" - } - if strings.Contains(strIndex, "-") { parts := strings.Split(strIndex, "-") - if len(parts) > 2 { - continue + if len(parts) != 2 { + return nil, fmt.Errorf("Index is not valid") } minIndex, errMin := strconv.Atoi(parts[0]) maxIndex, errMax := strconv.Atoi(parts[1]) if errMin != nil || errMax != nil { - continue + return nil, fmt.Errorf("Index is not valid") } - query += clause + ` (id BETWEEN ? AND ?) ` + query += ` OR (id BETWEEN ? AND ?)` args = append(args, minIndex, maxIndex) } else { index, err := strconv.Atoi(strIndex) if err != nil { - continue + return nil, fmt.Errorf("Index is not valid") } - query += clause + ` id = ? ` + query += ` OR id = ?` args = append(args, index) } }