diff --git a/internel/storage/sqlite/sqlite.go b/internel/storage/sqlite/sqlite.go index 48ea265..4dd8ace 100644 --- a/internel/storage/sqlite/sqlite.go +++ b/internel/storage/sqlite/sqlite.go @@ -39,11 +39,15 @@ func initDatabase(db *sql.DB) error { create table if not exists paste ( id text unique, content blob, + plain integer, expire integer ); create index if not exists paste_id on paste ( id ); + + create index if not exists paste_expire + on paste ( expire ); ` if _, err := db.Exec(sqlStmt); err != nil { @@ -58,17 +62,22 @@ func (s *StorageSqlite) Close() error { } func (s *StorageSqlite) Save(p *storage.Paste) (string, error) { - stmt, err := s.db.Prepare(`insert into paste (id, content, expire) values (?, ?, ?);`) + stmt, err := s.db.Prepare(`insert into paste (id, content, plain, expire) values (?, ?, ?, ?);`) if err != nil { return "", err } defer stmt.Close() var id string + var plain = 0 + + if p.Plain { + plain = 1 + } for true { id = utils.GenerateId(s.idLength) - _, err = stmt.Exec(id, p.Content, p.Expire) + _, err = stmt.Exec(id, p.Content, plain, p.Expire) if err == nil { break } else if errors.Is(err, sqlite3.ErrConstraint) { @@ -82,7 +91,7 @@ func (s *StorageSqlite) Save(p *storage.Paste) (string, error) { } func (s *StorageSqlite) Get(id string) (*storage.Paste, error) { - stmt, err := s.db.Prepare(`select content, expire from paste where id = ?`) + stmt, err := s.db.Prepare(`select content, plain, expire from paste where id = ?`) if err != nil { return nil, err } @@ -91,13 +100,19 @@ func (s *StorageSqlite) Get(id string) (*storage.Paste, error) { var p storage.Paste if !rows.Next() { - return nil, errors.New("No such paste") + return nil, nil } - if err = rows.Scan(&p.Content, &p.Expire); err != nil { + var plain = 0 + + if err = rows.Scan(&p.Content, &plain, &p.Expire); err != nil { return nil, err } + if plain != 0 { + p.Plain = true + } + return &p, nil } diff --git a/internel/storage/sqlite_test.go b/internel/storage/sqlite_test.go index 9447207..14d9585 100644 --- a/internel/storage/sqlite_test.go +++ b/internel/storage/sqlite_test.go @@ -35,10 +35,12 @@ func TestSqlite(t * testing.T) { func testGetSave(s *sqlite.StorageSqlite, t * testing.T) { var a = storage.Paste { Content: []byte("abc"), + Plain: true, Expire: time.Now().UnixMilli() + 15 * 1000, } var b = storage.Paste { Content: []byte("def"), + Plain: true, Expire: time.Now().UnixMilli() - 15 * 1000, } diff --git a/internel/storage/storage.go b/internel/storage/storage.go index 40f3dc5..95ac370 100644 --- a/internel/storage/storage.go +++ b/internel/storage/storage.go @@ -7,6 +7,7 @@ type Storage interface { } type Paste struct { + Plain bool Content []byte Expire int64 }