diff options
-rw-r--r-- | main.go | 33 |
1 files changed, 20 insertions, 13 deletions
@@ -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 } } |