From 8779b9a3334dbbaa69b7be0aedc6feda96b54cf2 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 22 Jul 2020 01:21:05 +0100 Subject: [PATCH] Added emoji_reaction get by id db func --- protocol/message_persistence.go | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/protocol/message_persistence.go b/protocol/message_persistence.go index 55c4e18de..711e9c146 100644 --- a/protocol/message_persistence.go +++ b/protocol/message_persistence.go @@ -756,3 +756,52 @@ func (db sqlitePersistence) tableEmojiReactionsAllFields() string { func (db sqlitePersistence) tableEmojiReactionsAllFieldsCount() int { return strings.Count(db.tableEmojiReactionsAllFields(), ",") + 1 } + + +func (db sqlitePersistence) EmojiReactionByID(id string) (*EmojiReaction, error) { + tx, err := db.db.BeginTx(context.Background(), &sql.TxOptions{}) + if err != nil { + return nil, err + } + defer func() { + if err == nil { + err = tx.Commit() + return + } + // don't shadow original error + _ = tx.Rollback() + }() + + row := tx.QueryRow( + fmt.Sprintf(` + SELECT + %s + FROM + emoji_reactions + WHERE + emoji_reactions.id = ? + `, db.tableEmojiReactionsAllFields()), + id, + ) + + emojiReaction := new(EmojiReaction) + args := []interface{}{ + &emojiReaction.ID, + &emojiReaction.Clock, + &emojiReaction.From, + &emojiReaction.EmojiID, + &emojiReaction.MessageID, + &emojiReaction.ChatID, + &emojiReaction.Retracted, + } + err = row.Scan(args...) + + switch err { + case sql.ErrNoRows: + return nil, errRecordNotFound + case nil: + return emojiReaction, nil + default: + return nil, err + } +}