-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.go
104 lines (89 loc) · 1.96 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
package hitrack2gpx
import (
"bytes"
"database/sql"
"fmt"
"log"
_ "github.com/mattn/go-sqlite3"
)
type scannerFn func(dest ...interface{}) error
type callbackFn func(scannerFn)
type DB struct {
db *sql.DB
}
func GetDb(conf *HT2G) *DB {
db, err := sql.Open("sqlite3", conf.DbPath)
if err != nil {
log.Fatal(err)
}
return &DB{
db: db,
}
}
func (db *DB) Close() {
db.db.Close()
}
func (db *DB) GetTracks(fileIndex int) string {
// var acc []string
files := db.getFiles(fileIndex)
if fileIndex == 0 {
fmt.Println("\nSelect an ID from the list above and pass it as second argument\n")
return ""
}
for _, id := range files {
if id == fileIndex {
return db.getTrack(id)
}
// acc = append(acc, db.getTrack(id))
}
// return acc
log.Fatal("Cannot find the selected ID")
return ""
}
func (db *DB) getFiles(selectedFileIndex int) []int {
query := `SELECT file_index, file_path FROM apk_file_info WHERE file_path LIKE '%HiTrack%';`
var acc []int
callback := func(scanFn scannerFn) {
var fileIndex int
var filePath string
err := scanFn(&fileIndex, &filePath)
if err != nil {
log.Fatal(err)
}
if selectedFileIndex == 0 {
fmt.Printf("File %s (ID %d)\n", filePath, fileIndex)
}
acc = append(acc, fileIndex)
}
db.makeQuery(query, callback)
return acc
}
func (db *DB) getTrack(id int) string {
query := fmt.Sprintf(`SELECT file_data FROM apk_file_data WHERE file_index=%d ORDER BY data_index;`, id)
// lines to be joined. If doesn't end with ; is interrupted?
var b bytes.Buffer
callback := func(scanFn scannerFn) {
var fileData string
err := scanFn(&fileData)
if err != nil {
log.Fatal(err)
}
b.WriteString(fileData)
}
db.makeQuery(query, callback)
return b.String()
}
func (db *DB) makeQuery(query string, callback callbackFn) {
rows, err := db.db.Query(query)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
callback(rows.Scan)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}