Stop changing index after delete

This commit is contained in:
Radhi Fadlillah 2018-03-05 11:02:36 +07:00
parent 24bdec2c05
commit 46c88d5db5
4 changed files with 9 additions and 66 deletions

View file

@ -32,17 +32,11 @@ var (
} }
// Delete bookmarks from database // Delete bookmarks from database
oldIndices, newIndices, err := DB.DeleteBookmarks(args...) err := DB.DeleteBookmarks(args...)
if err != nil { if err != nil {
cError.Println(err) cError.Println(err)
os.Exit(1) 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)
}
}, },
} }
) )

View file

@ -266,7 +266,7 @@ func apiDeleteBookmarks(w http.ResponseWriter, r *http.Request, ps httprouter.Pa
checkError(err) checkError(err)
// Delete bookmarks // Delete bookmarks
_, _, err = DB.DeleteBookmarks(request...) err = DB.DeleteBookmarks(request...)
checkError(err) checkError(err)
fmt.Fprint(w, request) fmt.Fprint(w, request)

View file

@ -15,7 +15,7 @@ type Database interface {
GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error) GetBookmarks(withContent bool, indices ...string) ([]model.Bookmark, error)
// DeleteBookmarks removes all record with matching indices from database. // 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 search bookmarks by the keyword or tags.
SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error) SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error)

View file

@ -3,7 +3,6 @@ package database
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -263,7 +262,7 @@ func (db *SQLiteDatabase) GetBookmarks(withContent bool, indices ...string) ([]m
} }
// DeleteBookmarks removes all record with matching indices from database. // 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 // Convert list of index to int
listIndex := []int{} listIndex := []int{}
errInvalidIndex := fmt.Errorf("Index is not valid") errInvalidIndex := fmt.Errorf("Index is not valid")
@ -272,13 +271,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd
if strings.Contains(strIndex, "-") { if strings.Contains(strIndex, "-") {
parts := strings.Split(strIndex, "-") parts := strings.Split(strIndex, "-")
if len(parts) != 2 { if len(parts) != 2 {
return nil, nil, errInvalidIndex return errInvalidIndex
} }
minIndex, errMin := strconv.Atoi(parts[0]) minIndex, errMin := strconv.Atoi(parts[0])
maxIndex, errMax := strconv.Atoi(parts[1]) maxIndex, errMax := strconv.Atoi(parts[1])
if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex { if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex {
return nil, nil, errInvalidIndex return errInvalidIndex
} }
for i := minIndex; i <= maxIndex; i++ { for i := minIndex; i <= maxIndex; i++ {
@ -287,16 +286,13 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd
} else { } else {
index, err := strconv.Atoi(strIndex) index, err := strconv.Atoi(strIndex)
if err != nil || index < 1 { if err != nil || index < 1 {
return nil, nil, errInvalidIndex return errInvalidIndex
} }
listIndex = append(listIndex, index) listIndex = append(listIndex, index)
} }
} }
// Sort the index
sort.Ints(listIndex)
// Create args and where clause // Create args and where clause
args := []interface{}{} args := []interface{}{}
whereClause := " WHERE 1" whereClause := " WHERE 1"
@ -315,7 +311,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd
// Begin transaction // Begin transaction
tx, err := db.Beginx() tx, err := db.Beginx()
if err != nil { if err != nil {
return nil, nil, errInvalidIndex return errInvalidIndex
} }
// Make sure to rollback if panic ever happened // Make sure to rollback if panic ever happened
@ -324,8 +320,6 @@ func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newInd
panicErr, _ := r.(error) panicErr, _ := r.(error)
tx.Rollback() tx.Rollback()
oldIndices = nil
newIndices = nil
err = panicErr 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_tag "+whereTagClause, args...)
tx.MustExec("DELETE FROM bookmark_content "+whereContentClause, 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 // Commit transaction
err = tx.Commit() err = tx.Commit()
checkError(err) checkError(err)
return oldIndices, newIndices, err return err
} }
// SearchBookmarks search bookmarks by the keyword or tags. // SearchBookmarks search bookmarks by the keyword or tags.