package db

import "github.com/jmoiron/sqlx"

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 {
	q := `create table if not exists tags (
		id integer primary key,
		tag text
	)`
	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 {
		return d.populate()
	}
	return nil
}

func (d *Database) populate() error {
	tx := d.MustBegin()
	return tx.Commit()
}
