forked from browserutils/kooky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visittablerows.go
36 lines (32 loc) · 1002 Bytes
/
visittablerows.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
package utils
import (
"fmt"
"github.com/go-sqlite/sqlite3"
)
func VisitTableRows(db *sqlite3.DbFile, tableName string, columnNameMappings map[string]string, f func(rowID *int64, row TableRow) error) error {
columns := make(map[string]int)
if table, ok := findTable(db, tableName); ok {
for index, column := range table.Columns() {
columnName := column.Name()
if mappedColumnName, ok := columnNameMappings[columnName]; ok {
columnName = mappedColumnName
}
if _, ok := columns[columnName]; !ok {
columns[columnName] = index
}
}
} else {
return fmt.Errorf("Unable to find table named [%s] in %v", tableName, db)
}
return db.VisitTableRecords(tableName, func(rowID *int64, record sqlite3.Record) error {
return f(rowID, TableRow{columns, &record})
})
}
func findTable(db *sqlite3.DbFile, tableName string) (sqlite3.Table, bool) {
for _, table := range db.Tables() {
if table.Name() == tableName {
return table, true
}
}
return sqlite3.Table{}, false
}