mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
Comments that restated names or narrated the code are gone, and multi-line ones are cut to a line of why. Only comments change: each edited Go file has the same token stream as before. winapi and abe come later.
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package sqlitex
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
// CountRows fails fast on a scan error, unlike QueryRows, which skips the row.
|
|
func CountRows(dbPath string, journalOff bool, query string) (int, error) {
|
|
if _, err := os.Stat(dbPath); err != nil {
|
|
return 0, fmt.Errorf("database file: %w", err)
|
|
}
|
|
db, err := sql.Open("sqlite", dbPath)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer db.Close()
|
|
|
|
if journalOff {
|
|
if _, err := db.Exec("PRAGMA journal_mode=off"); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
var count int
|
|
if err := db.QueryRow(query).Scan(&count); err != nil {
|
|
return 0, fmt.Errorf("count rows: %w", err)
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
// Rows that fail to scan are skipped and logged at debug level.
|
|
func QueryRows[T any](dbPath string, journalOff bool, query string, scanRow func(*sql.Rows) (T, error)) ([]T, error) {
|
|
var items []T
|
|
err := eachRow(dbPath, journalOff, query, func(rows *sql.Rows) error {
|
|
item, err := scanRow(rows)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
items = append(items, item)
|
|
return nil
|
|
})
|
|
return items, err
|
|
}
|