From 4ed83bdb017d7d078d7d56863f35b95940988e5b Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Tue, 12 Dec 2023 06:03:51 +0100 Subject: Rearrange tag importer code --- main.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 61f089a..ca54ac0 100644 --- a/main.go +++ b/main.go @@ -574,7 +574,22 @@ func cmdTag(args []string) error { return err } - // TODO: Prepare statements for tag/assignment updates. + // XXX: It might make sense to pre-erase all tag assignments within + // the given space for that image, the first time we see it: + // + // DELETE FROM tag_assignment + // WHERE sha1 = ? AND tag IN (SELECT id FROM tag WHERE space = ?) + // + // or even just clear the tag space completely: + // + // DELETE FROM tag_assignment + // WHERE tag IN (SELECT id FROM tag WHERE space = ?) + stmt, err := tx.Prepare(`INSERT INTO tag_assignment(sha1, tag, weight) + VALUES (?, (SELECT id FROM tag WHERE space = ? AND name = ?), ?) + ON CONFLICT DO UPDATE SET weight = ?`) + if err != nil { + return err + } scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { @@ -589,20 +604,12 @@ func cmdTag(args []string) error { return err } - if _, err := tx.Exec(`INSERT OR IGNORE INTO tag(space, name) - VALUES (?, ?);`, spaceID, tag); err != nil { + if _, err := tx.Exec( + `INSERT OR IGNORE INTO tag(space, name) VALUES (?, ?);`, + spaceID, tag); err != nil { return nil } - - var tagID int64 - if err := tx.QueryRow(`SELECT id FROM tag WHERE space = ? AND name = ?`, - spaceID, tag).Scan(&tagID); err != nil { - return err - } - - if _, err := tx.Exec(`INSERT INTO tag_assignment(sha1, tag, weight) - VALUES (?, ?, ?) ON CONFLICT DO UPDATE SET weight = ?`, - sha1, tagID, weight, weight); err != nil { + if _, err := stmt.Exec(sha1, spaceID, tag, weight, weight); err != nil { return err } } -- cgit v1.2.3-70-g09d2