summaryrefslogtreecommitdiff
path: root/model/database.go
blob: ed9a98580537de2879e82892a7d8b91dbc5b1de4 (plain)
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
package model

import (
	"database/sql"
	"log"
	"path/filepath"

	_ "github.com/mattn/go-sqlite3"
)

var db *sql.DB

func InitDatabase() {
	var err error

	fn := filepath.Join(storage, "ceres.sqlite3")

	db, err = sql.Open("sqlite3", fn)
	if err != nil {
		log.Fatal(err)
	}

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}
}

func CloseDatabase() {
	var err error = db.Close()
	if err != nil {
		log.Printf("Failed to close database: %v\n", err)
	} else {
		log.Println("Closed database")
	}
}