|
|
- package Database
-
- import (
- "database/sql"
- "fmt"
- "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.String())
- }
-
- 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) ([]string, error) {
- var (
- stmtString string
- stmt *sql.Stmt
- values []interface{} = []interface{}{}
- result *sql.Rows
- dirtyFiles []string = []string{}
- 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 hash string
- e = result.Scan(&id, &hash)
- if e != nil {
- return dirtyFiles, e
- }
- fmt.Println(id, hash)
- dirtyFiles = append(dirtyFiles, hash)
- }
-
- e = result.Err()
- return dirtyFiles, e
- }
-
- /*
- func FindNewFiles(files []string) ([]string, error) {
- var (
- stmtString string
- stmt *sql.Stmt
- values []interface{} = []interface{}{}
- result *sql.Rows
- dirtyFiles []string = []string{}
- e error
- )
-
- stmtString = `select *
- from (
- values (4),(5),(6)
- ) as v(id)
- where not exists (select *
- from images i
- where i.id = v.id);`
-
- 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 hash string
- e = result.Scan(&id, &hash)
- if e != nil {
- return dirtyFiles, e
- }
- fmt.Println(id, hash)
- dirtyFiles = append(dirtyFiles, hash)
- }
-
- e = result.Err()
- return dirtyFiles, e
- }
- */
|