Dev: add column plain to indicat if paste is plain

This commit is contained in:
leafee98 2023-02-11 14:19:01 +08:00
parent 949ec07e8b
commit c526fa7862
3 changed files with 23 additions and 5 deletions

View file

@ -39,11 +39,15 @@ func initDatabase(db *sql.DB) error {
create table if not exists paste ( create table if not exists paste (
id text unique, id text unique,
content blob, content blob,
plain integer,
expire integer expire integer
); );
create index if not exists paste_id create index if not exists paste_id
on paste ( id ); on paste ( id );
create index if not exists paste_expire
on paste ( expire );
` `
if _, err := db.Exec(sqlStmt); err != nil { 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) { 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 { if err != nil {
return "", err return "", err
} }
defer stmt.Close() defer stmt.Close()
var id string var id string
var plain = 0
if p.Plain {
plain = 1
}
for true { for true {
id = utils.GenerateId(s.idLength) 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 { if err == nil {
break break
} else if errors.Is(err, sqlite3.ErrConstraint) { } 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) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -91,13 +100,19 @@ func (s *StorageSqlite) Get(id string) (*storage.Paste, error) {
var p storage.Paste var p storage.Paste
if !rows.Next() { 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 return nil, err
} }
if plain != 0 {
p.Plain = true
}
return &p, nil return &p, nil
} }

View file

@ -35,10 +35,12 @@ func TestSqlite(t * testing.T) {
func testGetSave(s *sqlite.StorageSqlite, t * testing.T) { func testGetSave(s *sqlite.StorageSqlite, t * testing.T) {
var a = storage.Paste { var a = storage.Paste {
Content: []byte("abc"), Content: []byte("abc"),
Plain: true,
Expire: time.Now().UnixMilli() + 15 * 1000, Expire: time.Now().UnixMilli() + 15 * 1000,
} }
var b = storage.Paste { var b = storage.Paste {
Content: []byte("def"), Content: []byte("def"),
Plain: true,
Expire: time.Now().UnixMilli() - 15 * 1000, Expire: time.Now().UnixMilli() - 15 * 1000,
} }

View file

@ -7,6 +7,7 @@ type Storage interface {
} }
type Paste struct { type Paste struct {
Plain bool
Content []byte Content []byte
Expire int64 Expire int64
} }