Fix cursored query for emoji

This commit is contained in:
Andrea Maria Piana 2020-07-28 07:41:50 +02:00
parent 9ddd963591
commit d067b56fc2
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 8 additions and 7 deletions

View File

@ -494,17 +494,17 @@ func (db sqlitePersistence) MessageByChatID(chatID string, currCursor string, li
func (db sqlitePersistence) EmojiReactionsByChatID(chatID string, currCursor string, limit int) ([]*EmojiReaction, error) { func (db sqlitePersistence) EmojiReactionsByChatID(chatID string, currCursor string, limit int) ([]*EmojiReaction, error) {
cursorWhere := "" cursorWhere := ""
if currCursor != "" { if currCursor != "" {
cursorWhere = "AND substr('0000000000000000000000000000000000000000000000000000000000000000' || m.clock_value, -64, 64) <= ?" cursorWhere = "AND substr('0000000000000000000000000000000000000000000000000000000000000000' || m.clock_value, -64, 64) || m.id <= ?"
} }
args := []interface{}{chatID} args := []interface{}{chatID}
if currCursor != "" { if currCursor != "" {
args = append(args, currCursor) args = append(args, currCursor)
} }
args = append(args, limit)
// Build a new column `cursor` at the query time by having a fixed-sized clock value at the beginning // Build a new column `cursor` at the query time by having a fixed-sized clock value at the beginning
// concatenated with message ID. Results are sorted using this new column. // concatenated with message ID. Results are sorted using this new column.
// This new column values can also be returned as a cursor for subsequent requests. // This new column values can also be returned as a cursor for subsequent requests.
rows, err := db.db.Query( query := fmt.Sprintf(`
fmt.Sprintf(`
SELECT SELECT
e.clock_value, e.clock_value,
e.source, e.source,
@ -520,8 +520,11 @@ func (db sqlitePersistence) EmojiReactionsByChatID(chatID string, currCursor str
(SELECT id FROM user_messages m WHERE NOT(m.hide) AND m.local_chat_id = ? %s (SELECT id FROM user_messages m WHERE NOT(m.hide) AND m.local_chat_id = ? %s
ORDER BY substr('0000000000000000000000000000000000000000000000000000000000000000' || m.clock_value, -64, 64) || m.id DESC LIMIT ?) ORDER BY substr('0000000000000000000000000000000000000000000000000000000000000000' || m.clock_value, -64, 64) || m.id DESC LIMIT ?)
LIMIT 100 LIMIT 100
`, cursorWhere), `, cursorWhere)
append(args, limit)...,
rows, err := db.db.Query(
query,
args...,
) )
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -2,7 +2,6 @@ package protocol
import ( import (
"database/sql" "database/sql"
"fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"sort" "sort"
@ -453,7 +452,6 @@ func TestPersistenceEmojiReactions(t *testing.T) {
// Try with a cursor // Try with a cursor
_, cursor, err := p.MessageByChatID(chatID, "", 1) _, cursor, err := p.MessageByChatID(chatID, "", 1)
require.NoError(t, err) require.NoError(t, err)
fmt.Println("CURSOR", cursor)
reactions, err = p.EmojiReactionsByChatID(chatID, cursor, 2) reactions, err = p.EmojiReactionsByChatID(chatID, cursor, 2)
require.NoError(t, err) require.NoError(t, err)