mirror of https://github.com/usememos/memos.git
fix(store): properly handle sql.ErrNoRows in SQLite GetReaction
- Use direct QueryRowContext with proper sql.ErrNoRows handling - Return nil when reaction is not found (sql.ErrNoRows) - Return error for other database errors - More performant than List approach (single row fetch vs list iteration)
This commit is contained in:
parent
64ef80a21c
commit
f5d56b5db7
|
|
@ -2,6 +2,8 @@ package sqlite
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/usememos/memos/store"
|
||||
|
|
@ -88,15 +90,43 @@ func (d *DB) ListReactions(ctx context.Context, find *store.FindReaction) ([]*st
|
|||
}
|
||||
|
||||
func (d *DB) GetReaction(ctx context.Context, find *store.FindReaction) (*store.Reaction, error) {
|
||||
list, err := d.ListReactions(ctx, find)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
where, args := []string{"1 = 1"}, []any{}
|
||||
|
||||
if find.ID != nil {
|
||||
where, args = append(where, "id = ?"), append(args, *find.ID)
|
||||
}
|
||||
if len(list) == 0 {
|
||||
return nil, nil
|
||||
if find.CreatorID != nil {
|
||||
where, args = append(where, "creator_id = ?"), append(args, *find.CreatorID)
|
||||
}
|
||||
if find.ContentID != nil {
|
||||
where, args = append(where, "content_id = ?"), append(args, *find.ContentID)
|
||||
}
|
||||
|
||||
reaction := &store.Reaction{}
|
||||
if err := d.db.QueryRowContext(ctx, `
|
||||
SELECT
|
||||
id,
|
||||
created_ts,
|
||||
creator_id,
|
||||
content_id,
|
||||
reaction_type
|
||||
FROM reaction
|
||||
WHERE `+strings.Join(where, " AND ")+`
|
||||
LIMIT 1`,
|
||||
args...,
|
||||
).Scan(
|
||||
&reaction.ID,
|
||||
&reaction.CreatedTs,
|
||||
&reaction.CreatorID,
|
||||
&reaction.ContentID,
|
||||
&reaction.ReactionType,
|
||||
); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reaction := list[0]
|
||||
return reaction, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue