cabinet/db/db.go

42 lines
686 B
Go
Raw Normal View History

2019-10-30 15:31:07 +00:00
package db
import "github.com/jmoiron/sqlx"
import _ "github.com/mattn/go-sqlite3"
2019-10-30 15:31:07 +00:00
type Database struct {
*sqlx.DB
}
func New(path string) (*Database, error) {
d, err := sqlx.Open("sqlite3", path)
if err != nil {
return nil, err
}
return &Database{d}, nil
}
func (d *Database) MakeDB() error {
2019-10-30 15:37:45 +00:00
q := `create table if not exists tags (
2019-10-30 15:31:07 +00:00
id integer primary key,
2019-10-30 15:37:45 +00:00
tag text
2019-10-30 15:31:07 +00:00
)`
if _, err := d.Exec(q); err != nil {
return err
}
q = `select count(*) from tags`
var count int
if err := d.Get(&count, q); err != nil {
return err
}
if count == 0 {
2019-10-30 15:37:45 +00:00
return d.populate()
2019-10-30 15:31:07 +00:00
}
return nil
}
2019-10-30 15:37:45 +00:00
func (d *Database) populate() error {
2019-10-30 15:31:07 +00:00
tx := d.MustBegin()
return tx.Commit()
}