Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): handle usage of special characters in searches #721

Merged
merged 4 commits into from Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 36 additions & 12 deletions internal/database/database_test.go
Expand Up @@ -14,18 +14,19 @@ type testDatabaseFactory func(ctx context.Context) (DB, error)
func testDatabase(t *testing.T, dbFactory testDatabaseFactory) {
tests := map[string]databaseTestCase{
// Bookmarks
"testBookmarkAutoIncrement": testBookmarkAutoIncrement,
"testCreateBookmark": testCreateBookmark,
"testCreateBookmarkWithContent": testCreateBookmarkWithContent,
"testCreateBookmarkTwice": testCreateBookmarkTwice,
"testCreateBookmarkWithTag": testCreateBookmarkWithTag,
"testCreateTwoDifferentBookmarks": testCreateTwoDifferentBookmarks,
"testUpdateBookmark": testUpdateBookmark,
"testUpdateBookmarkWithContent": testUpdateBookmarkWithContent,
"testGetBookmark": testGetBookmark,
"testGetBookmarkNotExistant": testGetBookmarkNotExistant,
"testGetBookmarks": testGetBookmarks,
"testGetBookmarksCount": testGetBookmarksCount,
"testBookmarkAutoIncrement": testBookmarkAutoIncrement,
"testCreateBookmark": testCreateBookmark,
"testCreateBookmarkWithContent": testCreateBookmarkWithContent,
"testCreateBookmarkTwice": testCreateBookmarkTwice,
"testCreateBookmarkWithTag": testCreateBookmarkWithTag,
"testCreateTwoDifferentBookmarks": testCreateTwoDifferentBookmarks,
"testUpdateBookmark": testUpdateBookmark,
"testUpdateBookmarkWithContent": testUpdateBookmarkWithContent,
"testGetBookmark": testGetBookmark,
"testGetBookmarkNotExistant": testGetBookmarkNotExistant,
"testGetBookmarks": testGetBookmarks,
"testGetBookmarksWithSQLCharacters": testGetBookmarksWithSQLCharacters,
"testGetBookmarksCount": testGetBookmarksCount,
// Tags
"testCreateTag": testCreateTag,
"testCreateTags": testCreateTags,
Expand Down Expand Up @@ -261,6 +262,29 @@ func testGetBookmarks(t *testing.T, db DB) {
assert.Equal(t, savedBookmark.ID, results[0].ID, "bookmark should be the one saved")
}

func testGetBookmarksWithSQLCharacters(t *testing.T, db DB) {
ctx := context.TODO()

// _ := 0
book := model.Bookmark{
URL: "https://github.com/go-shiori/shiori",
Title: "shiori",
}
_, err := db.SaveBookmarks(ctx, true, book)
assert.NoError(t, err, "Save bookmarks must not fail")

characters := []string{";", "%", "_", "\\", "\""}

for _, char := range characters {
t.Run(char, func(t *testing.T) {
_, err := db.GetBookmarks(ctx, GetBookmarksOptions{
Keyword: char,
})
assert.NoError(t, err, "Get bookmarks should not fail")
})
}
}

func testGetBookmarksCount(t *testing.T, db DB) {
ctx := context.TODO()

Expand Down
9 changes: 4 additions & 5 deletions internal/database/pg.go
Expand Up @@ -237,13 +237,12 @@ func (db *PGDatabase) GetBookmarks(ctx context.Context, opts GetBookmarksOptions
// Add where clause for search keyword
if opts.Keyword != "" {
query += ` AND (
url LIKE :lkw OR
title LIKE :kw OR
excerpt LIKE :kw OR
content LIKE :kw
url LIKE '%' || :kw || '%' OR
title LIKE '%' || :kw || '%' OR
excerpt LIKE '%' || :kw || '%' OR
content LIKE '%' || :kw || '%'
)`

arg["lkw"] = "%" + opts.Keyword + "%"
arg["kw"] = opts.Keyword
}

Expand Down
17 changes: 10 additions & 7 deletions internal/database/sqlite.go
Expand Up @@ -270,19 +270,22 @@ func (db *SQLiteDatabase) GetBookmarks(ctx context.Context, opts GetBookmarksOpt

// Add where clause for search keyword
if opts.Keyword != "" {
query += ` AND (b.url LIKE ? OR b.excerpt LIKE ? OR b.id IN (
query += ` AND (b.url LIKE '%' || ? || '%' OR b.excerpt LIKE '%' || ? || '%' OR b.id IN (
SELECT docid id
FROM bookmark_content
WHERE title MATCH ? OR content MATCH ?))`

args = append(args,
"%"+opts.Keyword+"%",
"%"+opts.Keyword+"%")

// Replace dash with spaces since FTS5 uses `-name` as column identifier
opts.Keyword = strings.Replace(opts.Keyword, "-", " ", -1)
args = append(args, opts.Keyword, opts.Keyword)

// Replace dash with spaces since FTS5 uses `-name` as column identifier and double quote
// since FTS5 uses double quote as string identifier
// Reference: https://sqlite.org/fts5.html#fts5_strings
ftsKeyword := strings.Replace(opts.Keyword, "-", " ", -1)
fmartingr marked this conversation as resolved.
Show resolved Hide resolved

// Properly set double quotes for string literals in sqlite's fts
ftsKeyword = strings.Replace(ftsKeyword, "\"", "\"\"", -1)

args = append(args, "\""+ftsKeyword+"\"", "\""+ftsKeyword+"\"")
}

// Add where clause for tags.
Expand Down