-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3.go
62 lines (45 loc) · 859 Bytes
/
sqlite3.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"log"
)
func getDbTransaction(db *sql.DB) *sql.Tx {
tx,err := db.Begin()
if err!=nil {
log.Fatal(err)
}
return tx
}
func prepareQuery(tx *sql.Tx, query string) *sql.Stmt {
q, err := tx.Prepare(query)
if err!=nil {
log.Fatal(err)
}
return q
}
func getDbConnection() *sql.DB {
db, err := sql.Open("sqlite3", getDatabaseFileLocation())
if err!=nil {
log.Fatal(err)
}
return db
}
func GetQuery(query string) (*sql.Stmt, func()) {
db := getDbConnection()
tx := getDbTransaction(db)
q := prepareQuery(tx, query)
f := func() {
db.Close()
tx.Commit()
q.Close()
}
return q, f
}
func getDatabaseFileLocation() string {
c, e := ReadConfig()
if e!= nil {
log.Fatal(e)
}
return c.GetDatabaseFileLocation()
}