From eaecb1b38b76dd261a7238b4ddff33b38aa7a20c Mon Sep 17 00:00:00 2001 From: leafee98 Date: Tue, 14 Feb 2023 18:56:03 +0800 Subject: [PATCH] defer close rows, change paste not found behavior --- internel/storage/sqlite/sqlite.go | 5 +++++ internel/storage/sqlite_test.go | 5 ++--- internel/storage/storage.go | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internel/storage/sqlite/sqlite.go b/internel/storage/sqlite/sqlite.go index 4dd8ace..3803ca8 100644 --- a/internel/storage/sqlite/sqlite.go +++ b/internel/storage/sqlite/sqlite.go @@ -95,13 +95,18 @@ func (s *StorageSqlite) Get(id string) (*storage.Paste, error) { if err != nil { return nil, err } + rows, err := stmt.Query(id) + if err != nil { + return nil, err + } var p storage.Paste if !rows.Next() { return nil, nil } + defer rows.Close() var plain = 0 diff --git a/internel/storage/sqlite_test.go b/internel/storage/sqlite_test.go index 14d9585..0e21c54 100644 --- a/internel/storage/sqlite_test.go +++ b/internel/storage/sqlite_test.go @@ -62,7 +62,7 @@ func testGetSave(s *sqlite.StorageSqlite, t * testing.T) { t.Fatalf("Purged %v rows, expect 1 row", affected) } - var p_a, _ *storage.Paste + var p_a, p_b, _ *storage.Paste if p_a, err = s.Get(id_a); err != nil { t.Fatalf("Fatal on Get a: %v", err) @@ -71,8 +71,7 @@ func testGetSave(s *sqlite.StorageSqlite, t * testing.T) { t.Fatal("Content not match") } } - if _, err = s.Get(id_b); err == nil { + if p_b, err = s.Get(id_b); err == nil && p_b != nil { t.Fatalf("Fatal on Get b, it shoud have been purged") - } } diff --git a/internel/storage/storage.go b/internel/storage/storage.go index 95ac370..75d6c02 100644 --- a/internel/storage/storage.go +++ b/internel/storage/storage.go @@ -1,7 +1,7 @@ package storage type Storage interface { - Save(Paste) (string, error) + Save(*Paste) (string, error) Get(string) (*Paste, error) Purge() (int64, error) }