-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
73 lines (61 loc) · 1.45 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
package db
import (
"database/sql"
"errors"
"fmt"
"io/fs"
"log"
"os"
_ "github.com/mattn/go-sqlite3"
"github.com/wutzi15/storago/files"
"github.com/wutzi15/storago/interactive"
)
func OpenSqlite() *sql.DB {
filename := "storago.sqlite"
newFile := false
if _, err := os.Stat(filename); errors.Is(err, fs.ErrNotExist) {
newFile = true
}
db, err := sql.Open("sqlite3", filename)
if err != nil {
log.Panic(err)
}
if !newFile {
return db
}
sqlStmt := "CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL, size INTERGER DEFAULT 0, scantime INTERGER DEFAULT 0, isDir INTEGER DEFAULT 0)"
_, err = db.Exec(sqlStmt)
if err != nil {
log.Panic(err)
}
return db
}
func InsertFilesIntoDB(file *files.File, scantime int64, db *sql.DB) {
sqlStmt := fmt.Sprintf(`INSERT INTO files (name, size, scantime, isDir) VALUES (?,?,%d,?)`, scantime)
tx, err := db.Begin()
if err != nil {
log.Panic(err)
}
stmt, err := tx.Prepare(sqlStmt)
if err != nil {
log.Panic(err)
}
defer stmt.Close()
recurseThroughFiles(file, stmt)
tx.Commit()
}
func recurseThroughFiles(file *files.File, stmt *sql.Stmt) {
isDir := 0
if file.IsDir {
isDir = 1
}
name := interactive.GetParentName(file)
// fmt.Printf("Inserting: %s, %d, %d\n", name, file.Size, isDir)
_, err := stmt.Exec(name, file.Size, isDir)
if err != nil {
log.Panic(err)
}
for _, subfile := range file.Files {
recurseThroughFiles(subfile, stmt)
}
}