-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqllite.go
60 lines (53 loc) · 1.2 KB
/
sqllite.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
package store
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/winjeg/hamster/log"
"github.com/winjeg/hamster/util"
"sync"
)
const (
createTableSql = `
CREATE TABLE sites(
id INTEGER PRIMARY KEY AUTOINCREMENT,
author CHAR(50) NOT NULL,
mapping CHAR(128) NOT NULL,
created INTEGER NOT NULL,
updated INTEGER NOT NULL
)`
createIdxMappingSql = `CREATE UNIQUE INDEX idx_mapping on sites (mapping)`
/*
// may need later
createAuthorIndexSql = `CREATE INDEX idx_sites_author ON sites (author);`
createDirIndexSql = `CREATE INDEX idx_sites_dir ON sites (dir);`
createMappingIndexSql = `CREATE INDEX idx_sites_mapping ON sites (mapping);`
*/
)
var (
once sync.Once
lite *sql.DB
)
func GetDb() *sql.DB {
if lite != nil {
return lite
}
fileNameWithPath := util.GetCurrentPath("hamster.db")
once.Do(func() {
db, err := sql.Open("sqlite3", fileNameWithPath)
lite = db
if db == nil {
return
}
// create table
stmt, err := db.Prepare(createTableSql)
if err != nil {
log.LogErr(err)
return
}
if _, err := stmt.Exec(); err == nil {
_, i2err := db.Exec(createIdxMappingSql)
log.LogErr(i2err)
}
})
return lite
}