-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
163 lines (146 loc) · 2.84 KB
/
db.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bufio"
"database/sql"
"fmt"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
)
func removeDB(path string) {
log.Printf("Removing database at %s", path)
os.Remove(path)
}
func RemoveDBIfAllowed(path string, skipConfirmation bool) {
fmt.Print("Do you want to overwrite it? ([y]/n): ")
if skipConfirmation {
fmt.Println("y")
removeDB(path)
return
}
reader := bufio.NewReader(os.Stdin)
option, _ := reader.ReadString('\n')
switch option {
case "\n":
removeDB(path)
case "y\n", "Y\n":
removeDB(path)
case "n\n", "N\n":
log.Fatalln("Aborting...")
default:
fmt.Println(option)
log.Println("Invalid option")
RemoveDBIfAllowed(path, skipConfirmation)
}
}
func ConnectDB(path string, skipConfirmation bool) (*sql.DB, error) {
var db *sql.DB
_, err := os.Stat(path)
if os.IsNotExist(err) {
log.Printf("Creating new database at %s", path)
} else if err != nil {
log.Fatalf("Error when checking file: %v", err)
} else {
log.Printf("WARNING: Database %s is already present", path)
RemoveDBIfAllowed(path, skipConfirmation)
}
db, err = sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(`CREATE TABLE FileNodes (
Id INTEGER PRIMARY KEY,
ParentId INTEGER,
Path TEXT,
IsDir BOOLEAN,
LinkTarget TEXT,
Size INTEGER,
Count INTEGER,
SymlinkCount INTEGER,
SFileCount INTEGER,
SFileSize INTEGER,
MFileCount INTEGER,
MFileSize INTEGER,
LFileCount INTEGER,
LFileSize INTEGER,
XLFileCount INTEGER,
XLFileSize INTEGER,
XXLFileCount INTEGER,
XXLFileSize INTEGER
)`)
if err != nil {
log.Fatal(err)
}
return db, nil
}
func CloseDB(db *sql.DB) {
defer db.Close()
}
func BatchInsertData(data []*FSNodeStat, db *sql.DB) {
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare(`INSERT INTO
FileNodes(
Id,
ParentId,
Path,
IsDir,
LinkTarget,
Size,
Count,
SymlinkCount,
SFileCount,
SFileSize,
MFileCount,
MFileSize,
LFileCount,
LFileSize,
XLFileCount,
XLFileSize,
XXLFileCount,
XXLFileSize
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
tx.Rollback()
log.Fatal(err)
}
defer stmt.Close()
for _, datum := range data {
_, err := stmt.Exec(
datum.Id,
datum.ParentId,
datum.Path,
datum.IsDir,
datum.LinkTarget,
datum.Size,
datum.Count,
datum.SymlinkCount,
datum.SFileCount,
datum.SFileSize,
datum.MFileCount,
datum.MFileSize,
datum.LFileCount,
datum.LFileSize,
datum.XLFileCount,
datum.XLFileSize,
datum.XXLFileCount,
datum.XXLFileSize,
)
if err != nil {
tx.Rollback() // roll back if failed
log.Fatal(err)
}
}
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}
func CreateIndex(db *sql.DB) {
_, err := db.Exec(`CREATE INDEX Index_ParentId ON FileNodes(ParentId)`)
if err != nil {
log.Fatal(err)
}
}