From fb541e8e7e472e72595f89f7dd7c5a8eda48fcda Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sat, 9 Dec 2023 09:33:25 +0100 Subject: Elementary traversal --- main.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 10 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index b6ef3e0..34c0806 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ import ( "os/exec" "path/filepath" "regexp" + "strconv" "strings" "time" @@ -53,11 +54,11 @@ func dbCollectStrings(query string) ([]string, error) { var result []string for rows.Next() { - var sha1 string - if err := rows.Scan(&sha1); err != nil { + var s string + if err := rows.Scan(&s); err != nil { return nil, err } - result = append(result, sha1) + result = append(result, s) } if err := rows.Err(); err != nil { return nil, err @@ -103,6 +104,74 @@ func (dm *directoryManager) IDForDirectoryPath(path string) (int64, error) { return parent.Int64, nil } +// XXX: This is preliminary. +type entry struct { + Parent int64 + Name string + Mtime int64 + Sha1 string + + Thumbw int + Thumbh int + Dhash int64 +} + +// XXX: This is preliminary. +type directory struct { + Id int64 + Name string + Parent int64 + Children []int64 + Entries []entry +} + +func dbCollectDirectory(id int64) (directory, error) { + d := directory{Id: id} + dbID := sql.NullInt64{Int64: id, Valid: id != 0} + if id != 0 { + err := db.QueryRow(`SELECT name, IFNULL(parent, 0) + FROM directory WHERE id IS ?`, dbID).Scan(&d.Name, &d.Parent) + if err != nil { + return d, err + } + } + + rows1, err := db.Query(`SELECT id FROM directory WHERE parent IS ?`, dbID) + if err != nil { + return d, err + } + defer rows1.Close() + for rows1.Next() { + var child int64 + if err := rows1.Scan(&child); err != nil { + return d, err + } + d.Children = append(d.Children, child) + } + if err := rows1.Err(); err != nil { + return d, err + } + + rows2, err := db.Query(`SELECT IFNULL(entry.parent, 0), + entry.name, entry.mtime, entry.sha1, + IFNULL(image.thumbw, 0), IFNULL(image.thumbh, 0), IFNULL(image.dhash, 0) + FROM entry JOIN image ON entry.sha1 = image.sha1 + WHERE entry.parent IS ?`, dbID) + if err != nil { + return d, err + } + defer rows2.Close() + for rows2.Next() { + var e entry + if err := rows2.Scan(&e.Parent, &e.Name, &e.Mtime, &e.Sha1, + &e.Thumbw, &e.Thumbh, &e.Dhash); err != nil { + return d, err + } + d.Entries = append(d.Entries, e) + } + return d, rows2.Err() +} + // cmdInit initializes a "gallery directory" that contains gallery.sqlite, // images, thumbs. func cmdInit(args []string) error { @@ -137,8 +206,21 @@ var page = template.Must(template.New("/").Parse(`
+