mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-06 05:53:25 +01:00
refactor: move leaf utilities under internal/ (#650)
Phase 1: the packages with no project dependencies move first, so every later phase builds on finished leaves; the goldens pin the behavior.
This commit is contained in:
1 parent
fb86bd368e
commit
9937dbf9fb
54 files changed
+319
-335
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package sqlitex
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// CountRows runs a scalar count query (e.g. SELECT COUNT(*) FROM ...) and
|
||||
// returns the integer result. Unlike eachRow (which swallows per-row scan
|
||||
// errors), CountRows uses QueryRow for fail-fast behavior on scan failures.
|
||||
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
|
||||
}
|
||||
|
||||
// QueryRows is a generic helper (Go 1.18+) that wraps eachRow and collects
|
||||
// results into a typed slice. Each extract method only needs to provide the
|
||||
// scan function that converts one database row into a typed value.
|
||||
//
|
||||
// Rows that fail to scan are skipped (logged at debug level by eachRow).
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user