-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
44 lines (35 loc) · 785 Bytes
/
database.go
File metadata and controls
44 lines (35 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
var schemaUp = `CREATE TABLE schedule
(id INTEGER PRIMARY KEY AUTOINCREMENT,
hour INTEGER NOT NULL,
days STRING NOT NULL,
target_temp INTEGER NOT NULL);`
var schemaDown = "DROP TABLE schedule;"
type Database struct {
connection *sqlx.DB
}
func (d *Database) Connection() *sqlx.DB {
if d.connection != nil {
return d.connection
}
dbName := fmt.Sprintf("%s-%s.db", DB_NAME, ENV)
db, err := sqlx.Connect("sqlite3", dbName)
if err != nil {
panic(err)
}
return db
}
func (d *Database) Create() error {
_, err := d.Connection().Exec(schemaUp)
return err
}
func (d *Database) Destroy() error {
_, err := d.Connection().Exec(schemaDown)
d.Connection().Close()
return err
}