-
Notifications
You must be signed in to change notification settings - Fork 0
/
qdb.go
123 lines (106 loc) · 2.81 KB
/
qdb.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package qdb
import (
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"strconv"
//"sync"
//"sync/atomic"
"github.com/VukoDrakkeinen/Quasar/datadir"
"github.com/VukoDrakkeinen/Quasar/datadir/qlog"
"github.com/VukoDrakkeinen/Quasar/qutils/qerr"
_ "github.com/mattn/go-sqlite3"
)
const dbFile = "quasar.db"
var (
thumbsDir string
db *sql.DB
//stmtTypeCounter uint32
futureStatements []futureStmt
)
func init() {
thumbsDir = filepath.Join(datadir.Path(), "thumbnails")
os.Mkdir(thumbsDir, os.ModeDir|0755)
}
type futureStmt struct {
assignToVar **sql.Stmt
source string
}
func PrepareStmt(assignToVar **sql.Stmt, source string) {
if db == nil {
futureStatements = append(futureStatements, futureStmt{assignToVar, source})
} else {
panic("It is too late to prepare!") //todo: better message
}
}
//type StmtId uint32
//
//func NewStmtId() StmtId {
// return StmtId(atomic.AddUint32(&stmtTypeCounter, 1))
//}
type QDB struct {
*sql.DB
}
func (this *QDB) MustPrepare(query string) *sql.Stmt {
ret, err := this.Prepare(query)
if err != nil {
panic(`DB: MustPrepare(` + strconv.Quote(query) + `): ` + qerr.NewEmbeddedLocated(err).Error())
}
return ret
}
func DB() *QDB { //todo: de-singleton
if db == nil {
var err error
/*ci_crt := func(identity int64) (corrected int64) {
return identity &^ 0xFF
}
ci_ver := func(identity int64) (version int64) {
return int64(byte(identity))
}
sql.Register("sqlite3_quasar", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
if err := conn.RegisterFunc("ci_crt", ci_crt, true); err != nil {
return err
}
if err := conn.RegisterFunc("ci_ver", ci_ver, true); err != nil {
return err
}
return nil
},
})//*/
db, err = sql.Open("sqlite3", filepath.Join(datadir.Path(), dbFile)) //todo: sql.Open() swallows errors from driver.Open(); is it intentional? report it?
if err != nil {
qlog.Log(qlog.Error, "Opening database failed.", err)
return nil
}
db.Exec(`PRAGMA foreign_keys = ON;`) //enable foreign keys
qdb := &QDB{db}
for _, fStmt := range futureStatements {
*fStmt.assignToVar = qdb.MustPrepare(fStmt.source)
}
}
return &QDB{db}
}
func SaveThumbnail(filename string, b []byte) {
ioutil.WriteFile(filepath.Join(thumbsDir, filename), b, 0644)
}
func GetThumbnailPath(filename string) string {
if filename == "" {
return ""
}
return filepath.Join(thumbsDir, filename)
}
func ThumbnailExists(filename string) bool {
if fullpath := GetThumbnailPath(filename); fullpath != "" {
_, err := os.Lstat(fullpath)
return !os.IsNotExist(err)
}
return false
}
type InsertionStmtExecutor interface {
ExecuteInsertionStmt(stmt *sql.Stmt, additionalArgs ...interface{}) error
}
type QueryStmtExecutor interface {
ExecuteQueryStmt(stmt *sql.Stmt, additionalArgs ...interface{}) error
}