defer close rows, change paste not found behavior

This commit is contained in:
leafee98 2023-02-14 18:56:03 +08:00
parent c526fa7862
commit eaecb1b38b
3 changed files with 8 additions and 4 deletions

View file

@ -95,13 +95,18 @@ func (s *StorageSqlite) Get(id string) (*storage.Paste, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
rows, err := stmt.Query(id) rows, err := stmt.Query(id)
if err != nil {
return nil, err
}
var p storage.Paste var p storage.Paste
if !rows.Next() { if !rows.Next() {
return nil, nil return nil, nil
} }
defer rows.Close()
var plain = 0 var plain = 0

View file

@ -62,7 +62,7 @@ func testGetSave(s *sqlite.StorageSqlite, t * testing.T) {
t.Fatalf("Purged %v rows, expect 1 row", affected) 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 { if p_a, err = s.Get(id_a); err != nil {
t.Fatalf("Fatal on Get a: %v", err) 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") 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") t.Fatalf("Fatal on Get b, it shoud have been purged")
} }
} }

View file

@ -1,7 +1,7 @@
package storage package storage
type Storage interface { type Storage interface {
Save(Paste) (string, error) Save(*Paste) (string, error)
Get(string) (*Paste, error) Get(string) (*Paste, error)
Purge() (int64, error) Purge() (int64, error)
} }