2018-01-28 14:55:43 +08:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2018-01-28 15:53:37 +08:00
|
|
|
"fmt"
|
2018-01-28 14:55:43 +08:00
|
|
|
"github.com/RadhiFadlillah/shiori/model"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2018-01-29 08:40:29 +08:00
|
|
|
"sort"
|
2018-01-28 14:55:43 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SQLiteDatabase struct {
|
|
|
|
sqlx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenSQLiteDatabase() (*SQLiteDatabase, error) {
|
|
|
|
// Open database and start transaction
|
|
|
|
var err error
|
|
|
|
db := sqlx.MustConnect("sqlite3", "shiori.db")
|
|
|
|
tx := db.MustBegin()
|
|
|
|
|
|
|
|
// Make sure to rollback if panic ever happened
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
panicErr, _ := r.(error)
|
2018-01-29 16:00:37 +08:00
|
|
|
fmt.Println("Database error:", panicErr)
|
2018-01-28 14:55:43 +08:00
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
db = nil
|
|
|
|
err = panicErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`CREATE TABLE IF NOT EXISTS account(
|
2018-01-28 14:55:43 +08:00
|
|
|
id INTEGER NOT NULL,
|
|
|
|
username TEXT NOT NULL,
|
|
|
|
password TEXT NOT NULL,
|
|
|
|
CONSTRAINT account_PK PRIMARY KEY(id),
|
|
|
|
CONSTRAINT account_username_UNIQUE UNIQUE(username))`)
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`CREATE TABLE IF NOT EXISTS bookmark(
|
2018-01-28 14:55:43 +08:00
|
|
|
id INTEGER NOT NULL,
|
|
|
|
url TEXT NOT NULL,
|
|
|
|
title TEXT NOT NULL,
|
|
|
|
image_url TEXT NOT NULL DEFAULT "",
|
|
|
|
excerpt TEXT NOT NULL DEFAULT "",
|
|
|
|
author TEXT NOT NULL DEFAULT "",
|
|
|
|
language TEXT NOT NULL DEFAULT "",
|
|
|
|
min_read_time INTEGER NOT NULL DEFAULT 0,
|
|
|
|
max_read_time INTEGER NOT NULL DEFAULT 0,
|
|
|
|
modified TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
CONSTRAINT bookmark_PK PRIMARY KEY(id),
|
|
|
|
CONSTRAINT bookmark_url_UNIQUE UNIQUE(url))`)
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`CREATE TABLE IF NOT EXISTS tag(
|
2018-01-28 14:55:43 +08:00
|
|
|
id INTEGER NOT NULL,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
CONSTRAINT tag_PK PRIMARY KEY(id),
|
|
|
|
CONSTRAINT tag_name_UNIQUE UNIQUE(name))`)
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`CREATE TABLE IF NOT EXISTS bookmark_tag(
|
2018-01-28 14:55:43 +08:00
|
|
|
bookmark_id INTEGER NOT NULL,
|
|
|
|
tag_id INTEGER NOT NULL,
|
|
|
|
CONSTRAINT bookmark_tag_PK PRIMARY KEY(bookmark_id, tag_id),
|
|
|
|
CONSTRAINT bookmark_id_FK FOREIGN KEY(bookmark_id) REFERENCES bookmark(id),
|
|
|
|
CONSTRAINT tag_id_FK FOREIGN KEY(tag_id) REFERENCES tag(id))`)
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`CREATE VIRTUAL TABLE IF NOT EXISTS bookmark_content USING fts4(title, content)`)
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
return &SQLiteDatabase{*db}, err
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
func (db *SQLiteDatabase) SaveBookmark(bookmark model.Bookmark) (bookmarkID int64, err error) {
|
2018-01-30 15:57:36 +08:00
|
|
|
// Check URL and title
|
2018-01-30 17:16:25 +08:00
|
|
|
if bookmark.URL == "" {
|
|
|
|
return -1, fmt.Errorf("URL must not empty")
|
2018-01-30 15:57:36 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
if bookmark.Title == "" {
|
|
|
|
return -1, fmt.Errorf("Title must not empty")
|
2018-01-30 15:57:36 +08:00
|
|
|
}
|
|
|
|
|
2018-01-28 14:55:43 +08:00
|
|
|
// Prepare transaction
|
|
|
|
tx, err := db.Beginx()
|
|
|
|
if err != nil {
|
2018-01-30 17:16:25 +08:00
|
|
|
return -1, err
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to rollback if panic ever happened
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
panicErr, _ := r.(error)
|
|
|
|
tx.Rollback()
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmarkID = -1
|
2018-01-28 14:55:43 +08:00
|
|
|
err = panicErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Save article to database
|
2018-01-29 16:00:37 +08:00
|
|
|
res := tx.MustExec(`INSERT INTO bookmark (
|
2018-01-28 14:55:43 +08:00
|
|
|
url, title, image_url, excerpt, author,
|
|
|
|
language, min_read_time, max_read_time)
|
|
|
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?)`,
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmark.URL,
|
|
|
|
bookmark.Title,
|
|
|
|
bookmark.ImageURL,
|
|
|
|
bookmark.Excerpt,
|
|
|
|
bookmark.Author,
|
|
|
|
bookmark.Language,
|
|
|
|
bookmark.MinReadTime,
|
|
|
|
bookmark.MaxReadTime)
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
// Get last inserted ID
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmarkID, err = res.LastInsertId()
|
2018-01-28 14:55:43 +08:00
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
// Save bookmark content
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec(`INSERT INTO bookmark_content
|
2018-01-28 14:55:43 +08:00
|
|
|
(docid, title, content) VALUES (?, ?, ?)`,
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmarkID, bookmark.Title, bookmark.Content)
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
// Save tags
|
|
|
|
stmtGetTag, err := tx.Preparex(`SELECT id FROM tag WHERE name = ?`)
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
stmtInsertTag, err := tx.Preparex(`INSERT INTO tag (name) VALUES (?)`)
|
|
|
|
checkError(err)
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
stmtInsertBookmarkTag, err := tx.Preparex(`INSERT OR IGNORE INTO bookmark_tag (tag_id, bookmark_id) VALUES (?, ?)`)
|
2018-01-28 14:55:43 +08:00
|
|
|
checkError(err)
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
for _, tag := range bookmark.Tags {
|
|
|
|
tagName := strings.ToLower(tag.Name)
|
|
|
|
tagName = strings.TrimSpace(tagName)
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
tagID := int64(-1)
|
2018-01-30 17:16:25 +08:00
|
|
|
err = stmtGetTag.Get(&tagID, tagName)
|
2018-01-29 16:00:37 +08:00
|
|
|
checkError(err)
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
if tagID == -1 {
|
2018-01-30 17:16:25 +08:00
|
|
|
res := stmtInsertTag.MustExec(tagName)
|
2018-01-28 14:55:43 +08:00
|
|
|
tagID, err = res.LastInsertId()
|
|
|
|
checkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stmtInsertBookmarkTag.Exec(tagID, bookmarkID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
err = tx.Commit()
|
|
|
|
checkError(err)
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
return bookmarkID, err
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SQLiteDatabase) GetBookmarks(indices ...string) ([]model.Bookmark, error) {
|
2018-01-29 16:00:37 +08:00
|
|
|
// Convert list of index to int
|
|
|
|
listIndex := []int{}
|
|
|
|
errInvalidIndex := fmt.Errorf("Index is not valid")
|
2018-01-28 15:53:37 +08:00
|
|
|
|
2018-01-28 14:55:43 +08:00
|
|
|
for _, strIndex := range indices {
|
|
|
|
if strings.Contains(strIndex, "-") {
|
|
|
|
parts := strings.Split(strIndex, "-")
|
2018-01-28 15:53:37 +08:00
|
|
|
if len(parts) != 2 {
|
2018-01-29 16:00:37 +08:00
|
|
|
return nil, errInvalidIndex
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
minIndex, errMin := strconv.Atoi(parts[0])
|
|
|
|
maxIndex, errMax := strconv.Atoi(parts[1])
|
2018-01-29 16:00:37 +08:00
|
|
|
if errMin != nil || errMax != nil || minIndex < 1 || minIndex > maxIndex {
|
|
|
|
return nil, errInvalidIndex
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
for i := minIndex; i <= maxIndex; i++ {
|
|
|
|
listIndex = append(listIndex, i)
|
|
|
|
}
|
2018-01-28 14:55:43 +08:00
|
|
|
} else {
|
|
|
|
index, err := strconv.Atoi(strIndex)
|
2018-01-29 16:00:37 +08:00
|
|
|
if err != nil || index < 1 {
|
|
|
|
return nil, errInvalidIndex
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 16:00:37 +08:00
|
|
|
listIndex = append(listIndex, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare where clause
|
|
|
|
args := []interface{}{}
|
|
|
|
whereClause := " WHERE 1"
|
|
|
|
|
|
|
|
if len(listIndex) > 0 {
|
|
|
|
whereClause = " WHERE id IN ("
|
|
|
|
for _, idx := range listIndex {
|
|
|
|
args = append(args, idx)
|
2018-01-29 21:45:27 +08:00
|
|
|
whereClause += "?,"
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
2018-01-29 16:00:37 +08:00
|
|
|
|
|
|
|
whereClause = whereClause[:len(whereClause)-1]
|
|
|
|
whereClause += ")"
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch bookmarks
|
2018-01-29 16:00:37 +08:00
|
|
|
query := `SELECT id,
|
|
|
|
url, title, image_url, excerpt, author,
|
|
|
|
language, min_read_time, max_read_time, modified
|
|
|
|
FROM bookmark` + whereClause
|
|
|
|
|
2018-01-28 14:55:43 +08:00
|
|
|
bookmarks := []model.Bookmark{}
|
|
|
|
err := db.Select(&bookmarks, query, args...)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tags for each bookmarks
|
|
|
|
stmtGetTags, err := db.Preparex(`SELECT t.id, t.name
|
|
|
|
FROM bookmark_tag bt LEFT JOIN tag t ON bt.tag_id = t.id
|
|
|
|
WHERE bt.bookmark_id = ? ORDER BY t.name`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmtGetTags.Close()
|
|
|
|
|
|
|
|
for i := range bookmarks {
|
|
|
|
tags := []model.Tag{}
|
|
|
|
err = stmtGetTags.Select(&tags, bookmarks[i].ID)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks[i].Tags = tags
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookmarks, nil
|
|
|
|
}
|
2018-01-29 08:40:29 +08:00
|
|
|
|
|
|
|
func (db *SQLiteDatabase) DeleteBookmarks(indices ...string) (oldIndices, newIndices []int, err error) {
|
|
|
|
// Convert list of index to int
|
|
|
|
listIndex := []int{}
|
|
|
|
errInvalidIndex := fmt.Errorf("Index is not valid")
|
|
|
|
|
|
|
|
for _, strIndex := range indices {
|
|
|
|
if strings.Contains(strIndex, "-") {
|
|
|
|
parts := strings.Split(strIndex, "-")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, nil, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := minIndex; i <= maxIndex; i++ {
|
|
|
|
listIndex = append(listIndex, i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index, err := strconv.Atoi(strIndex)
|
|
|
|
if err != nil || index < 1 {
|
|
|
|
return nil, nil, errInvalidIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
listIndex = append(listIndex, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the index
|
|
|
|
sort.Ints(listIndex)
|
|
|
|
|
|
|
|
// Create args and where clause
|
|
|
|
args := []interface{}{}
|
|
|
|
whereClause := " WHERE 1"
|
|
|
|
|
|
|
|
if len(listIndex) > 0 {
|
|
|
|
whereClause = " WHERE id IN ("
|
|
|
|
for _, idx := range listIndex {
|
|
|
|
args = append(args, idx)
|
2018-01-29 21:45:27 +08:00
|
|
|
whereClause += "?,"
|
2018-01-29 08:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
whereClause = whereClause[:len(whereClause)-1]
|
|
|
|
whereClause += ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin transaction
|
|
|
|
tx, err := db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errInvalidIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure to rollback if panic ever happened
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
panicErr, _ := r.(error)
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
oldIndices = nil
|
|
|
|
newIndices = nil
|
|
|
|
err = panicErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Delete bookmarks
|
|
|
|
whereTagClause := strings.Replace(whereClause, "id", "bookmark_id", 1)
|
|
|
|
whereContentClause := strings.Replace(whereClause, "id", "docid", 1)
|
2018-01-30 14:31:10 +08:00
|
|
|
|
|
|
|
tx.MustExec("DELETE FROM bookmark "+whereClause, args...)
|
|
|
|
tx.MustExec("DELETE FROM bookmark_tag "+whereTagClause, args...)
|
2018-01-29 16:00:37 +08:00
|
|
|
tx.MustExec("DELETE FROM bookmark_content "+whereContentClause, args...)
|
2018-01-29 08:40:29 +08:00
|
|
|
|
2018-01-30 14:31:10 +08:00
|
|
|
// Prepare statement for updating index
|
|
|
|
stmtGetMaxID, err := tx.Preparex(`SELECT IFNULL(MAX(id), 0) FROM bookmark`)
|
2018-01-29 16:00:37 +08:00
|
|
|
checkError(err)
|
2018-01-29 08:40:29 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-01-30 14:31:10 +08:00
|
|
|
// 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)
|
2018-01-29 08:40:29 +08:00
|
|
|
|
2018-01-30 14:31:10 +08:00
|
|
|
newIndices = append(newIndices, removedIndex)
|
|
|
|
oldIndices = append(oldIndices, oldIndex)
|
2018-01-29 08:40:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
err = tx.Commit()
|
|
|
|
checkError(err)
|
|
|
|
|
|
|
|
return oldIndices, newIndices, err
|
|
|
|
}
|
2018-01-29 21:45:27 +08:00
|
|
|
|
|
|
|
func (db *SQLiteDatabase) SearchBookmarks(keyword string, tags ...string) ([]model.Bookmark, error) {
|
|
|
|
// Create initial variable
|
|
|
|
keyword = strings.TrimSpace(keyword)
|
|
|
|
whereClause := "WHERE 1"
|
|
|
|
args := []interface{}{}
|
|
|
|
|
|
|
|
// Create where clause for keyword
|
|
|
|
if keyword != "" {
|
|
|
|
whereClause += ` AND id IN (
|
|
|
|
SELECT docid id FROM bookmark_content
|
|
|
|
WHERE bookmark_content MATCH ?)`
|
|
|
|
args = append(args, keyword)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create where clause for tags
|
|
|
|
if len(tags) > 0 {
|
|
|
|
whereTagClause := ` AND id IN (
|
|
|
|
SELECT DISTINCT bookmark_id FROM bookmark_tag
|
|
|
|
WHERE tag_id IN (SELECT id FROM tag WHERE name IN (`
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
args = append(args, tag)
|
|
|
|
whereTagClause += "?,"
|
|
|
|
}
|
|
|
|
|
|
|
|
whereTagClause = whereTagClause[:len(whereTagClause)-1]
|
|
|
|
whereTagClause += ")))"
|
|
|
|
|
|
|
|
whereClause += whereTagClause
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search bookmarks
|
|
|
|
query := `SELECT id,
|
|
|
|
url, title, image_url, excerpt, author,
|
|
|
|
language, min_read_time, max_read_time, modified
|
|
|
|
FROM bookmark ` + whereClause
|
|
|
|
|
|
|
|
bookmarks := []model.Bookmark{}
|
|
|
|
err := db.Select(&bookmarks, query, args...)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tags for each bookmarks
|
|
|
|
stmtGetTags, err := db.Preparex(`SELECT t.id, t.name
|
|
|
|
FROM bookmark_tag bt LEFT JOIN tag t ON bt.tag_id = t.id
|
|
|
|
WHERE bt.bookmark_id = ? ORDER BY t.name`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmtGetTags.Close()
|
|
|
|
|
|
|
|
for i := range bookmarks {
|
|
|
|
tags := []model.Tag{}
|
|
|
|
err = stmtGetTags.Select(&tags, bookmarks[i].ID)
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarks[i].Tags = tags
|
|
|
|
}
|
|
|
|
|
|
|
|
return bookmarks, nil
|
|
|
|
}
|