package Database
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
type FilesystemHashRow struct {
|
|
Path string
|
|
Hash string
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
func FindOrCreateFileHash(rows []FilesystemHashRow) error {
|
|
var (
|
|
stmtString string
|
|
stmt *sql.Stmt
|
|
values []interface{} = []interface{}{}
|
|
e error
|
|
)
|
|
|
|
stmtString = "INSERT OR REPLACE INTO filesystem_hash (id, path, hash, updated_at) VALUES "
|
|
|
|
for _, row := range rows {
|
|
stmtString += `(
|
|
(SELECT id FROM filesystem_hash WHERE path = ?),
|
|
?,
|
|
?,
|
|
?
|
|
),`
|
|
values = append(values, row.Path, row.Path, row.Hash, row.UpdatedAt.Unix())
|
|
}
|
|
|
|
stmtString = stmtString[0 : len(stmtString)-1]
|
|
|
|
stmt, e = DB.Prepare(stmtString)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
|
|
_, e = stmt.Exec(values...)
|
|
|
|
return e
|
|
}
|
|
|
|
func FindModifiedFiles(hashes []string) (map[int]string, error) {
|
|
var (
|
|
stmtString string
|
|
stmt *sql.Stmt
|
|
values []interface{} = []interface{}{}
|
|
result *sql.Rows
|
|
dirtyFiles map[int]string = make(map[int]string)
|
|
counter int = 0
|
|
e error
|
|
)
|
|
|
|
stmtString = "SELECT id, path FROM filesystem_hash WHERE hash NOT IN ("
|
|
|
|
for _, row := range hashes {
|
|
stmtString += "?,"
|
|
values = append(values, row)
|
|
}
|
|
|
|
stmtString = stmtString[0:len(stmtString)-1] + ")"
|
|
|
|
stmt, e = DB.Prepare(stmtString)
|
|
if e != nil {
|
|
return dirtyFiles, e
|
|
}
|
|
|
|
result, e = stmt.Query(values...)
|
|
if e != nil {
|
|
return dirtyFiles, e
|
|
}
|
|
|
|
defer result.Close()
|
|
for result.Next() {
|
|
var id string
|
|
var path string
|
|
e = result.Scan(&id, &path)
|
|
if e != nil {
|
|
return dirtyFiles, e
|
|
}
|
|
dirtyFiles[counter] = path
|
|
counter++
|
|
}
|
|
|
|
e = result.Err()
|
|
return dirtyFiles, e
|
|
}
|
|
|
|
func GetMostRecentTimestamp() (time.Time, error) {
|
|
var (
|
|
stmt *sql.Stmt
|
|
result *sql.Row
|
|
lastUpdatedAt int64
|
|
e error
|
|
)
|
|
|
|
stmt, e = DB.Prepare(`
|
|
SELECT updated_at FROM filesystem_hash
|
|
ORDER BY updated_at DESC
|
|
LIMIT 1;
|
|
`)
|
|
if e != nil {
|
|
return time.Now(), e
|
|
}
|
|
|
|
result = stmt.QueryRow()
|
|
|
|
result.Scan(&lastUpdatedAt)
|
|
|
|
return time.Unix(lastUpdatedAt, 0), nil
|
|
}
|