forked from metacoin/flo-market-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.go
105 lines (87 loc) · 2.02 KB
/
sqlite.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
package main
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
var (
DBH *sql.DB
)
func createDBFile() {
//path := path.Dir("." + string(filepath.Separator) + DB_DIR)
path := "." + string(filepath.Separator) + DB_DIR
fmt.Printf("path(%T): %v", path, path)
// create database directory
if os.MkdirAll(path, 0755) != nil {
log.Fatal("couldn't create database directory ./" + DB_DIR + "/")
} else {
fmt.Println("created database directory " + path)
}
// create database file
_, err := os.Create(DB_PATH)
if err != nil {
log.Fatal("couldnt create database file " + DB_PATH)
}
}
func initDB() {
// check if database exists
fmt.Println("opening database " + DB_PATH + "?cache=shared&mode=wrc... ")
if _, err := os.Stat(DB_PATH); err != nil {
if os.IsNotExist(err) {
fmt.Println("database file doesn't exist, creating it...")
createDBFile()
}
}
var err error
DBH, err = sql.Open("sqlite3", "file:"+DB_PATH+"?cache=shared&mode=rwc")
// TODO: handle this properly
if err != nil {
fmt.Println("exit 1")
log.Fatal(err)
}
if DBH == nil {
fmt.Println("exit 2")
log.Fatal(err)
}
fmt.Println("done!")
}
func createTables() {
// TODO: move this!
fmt.Printf("creating tables and triggers if they don't exist... ")
SQLiteStatements := []string{
`CREATE TABLE if not exists markets (
uid integer not null primary key AUTOINCREMENT,
unixtime int not null,
cryptsy TEXT,
poloniex TEXT,
bittrex TEXT,
daily_volume TEXT,
weighted TEXT,
USD TEXT
)`,
}
for _, v := range SQLiteStatements {
tx, err := DBH.Begin()
if err != nil {
fmt.Println("exit 16")
log.Fatal(err)
}
stmt, err := tx.Prepare(v)
if err != nil {
fmt.Println("exit 16")
log.Fatal(err)
}
_, stmt_err := stmt.Exec()
if stmt_err != nil {
fmt.Printf("%q: %s\n", err, v)
fmt.Println("exit 17")
log.Fatal(stmt_err)
}
tx.Commit()
stmt.Close()
}
fmt.Println(" done!")
}