Switch to squirrel
This commit is contained in:
+20
-17
@@ -7,15 +7,14 @@ import (
|
||||
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/eventstore"
|
||||
"github.com/Masterminds/squirrel"
|
||||
)
|
||||
|
||||
func (s *SqliteBackend) SaveEvent(evt nostr.Event) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
// Check if event already exists
|
||||
var existingID string
|
||||
err := s.db.QueryRow("SELECT id FROM events WHERE id = ?", evt.ID.Hex()).Scan(&existingID)
|
||||
qb := squirrel.Select("id").From("events").Where(squirrel.Eq{"id": evt.ID.Hex()})
|
||||
err := qb.RunWith(s.db).QueryRow().Scan(&existingID)
|
||||
if err == nil {
|
||||
// Event already exists
|
||||
return eventstore.ErrDupEvent
|
||||
@@ -28,18 +27,19 @@ func (s *SqliteBackend) SaveEvent(evt nostr.Event) error {
|
||||
}
|
||||
|
||||
// Insert the event
|
||||
query := `INSERT INTO events (id, created_at, kind, pubkey, content, tags, sig)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
||||
insertQb := squirrel.Insert("events").
|
||||
Columns("id", "created_at", "kind", "pubkey", "content", "tags", "sig").
|
||||
Values(
|
||||
evt.ID.Hex(),
|
||||
int64(evt.CreatedAt),
|
||||
int(evt.Kind),
|
||||
evt.PubKey.Hex(),
|
||||
evt.Content,
|
||||
string(tagsJSON),
|
||||
hex.EncodeToString(evt.Sig[:]),
|
||||
)
|
||||
|
||||
_, err = s.db.Exec(query,
|
||||
evt.ID.Hex(),
|
||||
int64(evt.CreatedAt),
|
||||
int(evt.Kind),
|
||||
evt.PubKey.Hex(),
|
||||
evt.Content,
|
||||
string(tagsJSON),
|
||||
hex.EncodeToString(evt.Sig[:]),
|
||||
)
|
||||
_, err = insertQb.RunWith(s.db).Exec()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to save event '%s': %w", evt.ID, err)
|
||||
@@ -48,8 +48,11 @@ func (s *SqliteBackend) SaveEvent(evt nostr.Event) error {
|
||||
// Insert single-letter tags into event_tags table
|
||||
for _, tag := range evt.Tags {
|
||||
if len(tag) >= 2 && len(tag[0]) == 1 {
|
||||
_, err := s.db.Exec("INSERT INTO event_tags (event_id, key, value) VALUES (?, ?, ?)",
|
||||
evt.ID.Hex(), tag[0], tag[1])
|
||||
tagQb := squirrel.Insert("event_tags").
|
||||
Columns("event_id", "key", "value").
|
||||
Values(evt.ID.Hex(), tag[0], tag[1])
|
||||
|
||||
_, err := tagQb.RunWith(s.db).Exec()
|
||||
if err != nil {
|
||||
// Log error but don't fail the entire save operation
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user