-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (62 loc) · 1.52 KB
/
main.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
package main
import (
"database/sql"
"flag"
"fmt"
"os"
"vroomberg/util"
_ "github.com/mattn/go-sqlite3"
)
var DefaultInitFile string = ""
var DefaultQuery string = ""
var DBFilepath string = "./statements.db"
func main() {
var initFile string
var query string
flag.StringVar(&initFile, "init", DefaultInitFile, "Initialize DB (./statements.db) with JSON filepath")
flag.StringVar(&query, "query", DefaultQuery, "Supported query")
flag.Parse()
db, err := sql.Open("sqlite3", DBFilepath)
if err != nil {
fmt.Printf("Failed to open or create db on disk: %v", err)
os.Exit(1)
}
if initFile != DefaultInitFile {
util.CleanDB(DBFilepath)
errInit := initializeDB(db, initFile)
if errInit != nil {
fmt.Printf("Failed to init with file: %v", initFile)
os.Exit(2)
}
}
defer db.Close()
// If no query provided, exit
if query == DefaultQuery {
os.Exit(0)
}
output, err := util.GenerateSQLFromInput(db, query)
if err != nil {
fmt.Printf("Failed to query DB: %v", err)
os.Exit(3)
}
fmt.Println(output)
}
// Initialize DB by reading JSON, running INSERTS
func initializeDB(db *sql.DB, initFile string) error {
fmt.Println("Structuring data...")
lineItems, err := util.ReadLineItemsFromDisk(initFile)
if err != nil {
return err
}
// TODO Roadmap Item #2: Bulk INSERT for initializing DB
fmt.Printf("Initializing db...")
util.CreateTable(db)
for i, lineItem := range lineItems {
util.InsertLineItem(db, lineItem)
if i%10000 == 0 {
fmt.Printf(".")
}
}
fmt.Println("initialized db")
return nil
}