From 46c88d5db5d44a2648e22016bd4cddf795524363 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 5 Mar 2018 11:02:36 +0700 Subject: [PATCH] Stop changing index after delete --- cmd/delete.go | 8 +----- cmd/serve.go | 2 +- database/database.go | 2 +- database/sqlite.go | 63 +++++--------------------------------------- 4 files changed, 9 insertions(+), 66 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 00b98db..2aae6c6 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -32,17 +32,11 @@ var ( } // Delete bookmarks from database - oldIndices, newIndices, err := DB.DeleteBookmarks(args...) + err := DB.DeleteBookmarks(args...) if err != nil { cError.Println(err) os.Exit(1) } - - fmt.Println("Bookmarks has been deleted") - for i, oldIndex := range oldIndices { - newIndex := newIndices[i] - fmt.Printf("Index %d moved to %d\n", oldIndex, newIndex) - } }, } ) diff --git a/cmd/serve.go b/cmd/serve.go index e321dbb..b779170 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -266,7 +266,7 @@ func apiDeleteBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Pa checkError(err) // Delete bookmarks - _, _, err = DB.DeleteBookmarks(request...) + err = DB.DeleteBookmarks(request...) checkError(err) fmt.Fprint(w, request) diff --git a/database/database.go b/database/database.go index 4722f22..e876a87 100644 --- a/database/database.go +++ b/database/database.go @@ -15,7 +15,7 @@ type Database interface { GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) // DeleteBookmarks removes all record with matching indices from database. - DeleteBookmarks(indices ...string) ([]int, []int, error) + DeleteBookmarks(indices ...string) error // SearchBookmarks search bookmarks by the keyword or tags. SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error) diff --git a/database/sqlite.go b/database/sqlite.go index 5b71a44..94f99cf 100644 --- a/database/sqlite.go +++ b/database/sqlite.go @@ -3,7 +3,6 @@ package database import ( "database/sql" "fmt" - "sort" "strconv" "strings" "time" @@ -263,7 +262,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m } // DeleteBookmarks removes all record with matching indices from database. -func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) { +func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (err error) { // Convert list of index to int listIndex := []int{} errInvalidIndex := fmt.Errorf("Index is not valid") @@ -272,13 +271,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd if strings.Contains(strIndex, "-") { parts := strings.Split(strIndex, "-") if len(parts) != 2 { - return nil, nil, errInvalidIndex + return errInvalidIndex } minIndex, errMin := strconv.Atoi(parts[0]) maxIndex, errMax := strconv.Atoi(parts[1]) if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex { - return nil, nil, errInvalidIndex + return errInvalidIndex } for i := minIndex; i <= maxIndex; i++ { @@ -287,16 +286,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd } else { index, err := strconv.Atoi(strIndex) if err != nil || index < 1 { - return nil, nil, errInvalidIndex + return errInvalidIndex } listIndex = append(listIndex, index) } } - // Sort the index - sort.Ints(listIndex) - // Create args and where clause args := []interface{}{} whereClause := " WHERE 1" @@ -315,7 +311,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd // Begin transaction tx, err := db.Beginx() if err != nil { - return nil, nil, errInvalidIndex + return errInvalidIndex } // Make sure to rollback if panic ever happened @@ -324,8 +320,6 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd panicErr, _ := r.(error) tx.Rollback() - oldIndices = nil - newIndices = nil err = panicErr } }() @@ -338,56 +332,11 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd tx.MustExec("DELETE FROM bookmark_tag "+whereTagClause, args...) tx.MustExec("DELETE FROM bookmark_content "+whereContentClause, args...) - // Prepare statement for updating index - stmtGetMaxID, err := tx.Preparex(`SELECT IFNULL(MAX(id), 0) FROM bookmark`) - checkError(err) - - stmtUpdateBookmark, err := tx.Preparex(`UPDATE bookmark SET id = ? WHERE id = ?`) - checkError(err) - - stmtUpdateBookmarkTag, err := tx.Preparex(`UPDATE bookmark_tag SET bookmark_id = ? WHERE bookmark_id = ?`) - checkError(err) - - stmtUpdateBookmarkContent, err := tx.Preparex(`UPDATE bookmark_content SET docid = ? WHERE docid = ?`) - checkError(err) - - // Get list of removed indices - maxIndex := 0 - err = stmtGetMaxID.Get(&maxIndex) - checkError(err) - - removedIndices := []int{} - err = tx.Select(&removedIndices, - `WITH cnt(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM cnt LIMIT ?) - SELECT x FROM cnt WHERE x NOT IN (SELECT id FROM bookmark)`, - maxIndex) - checkError(err) - - // Fill removed indices - newIndices = []int{} - oldIndices = []int{} - for _, removedIndex := range removedIndices { - oldIndex := 0 - err = stmtGetMaxID.Get(&oldIndex) - checkError(err) - - if oldIndex <= removedIndex { - break - } - - stmtUpdateBookmark.MustExec(removedIndex, oldIndex) - stmtUpdateBookmarkTag.MustExec(removedIndex, oldIndex) - stmtUpdateBookmarkContent.MustExec(removedIndex, oldIndex) - - newIndices = append(newIndices, removedIndex) - oldIndices = append(oldIndices, oldIndex) - } - // Commit transaction err = tx.Commit() checkError(err) - return oldIndices, newIndices, err + return err } // SearchBookmarks search bookmarks by the keyword or tags.