aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-12 06:03:51 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-12 06:12:08 +0100
commit4ed83bdb017d7d078d7d56863f35b95940988e5b (patch)
tree271ad4f28d260aff33515913f3a5d45272621908 /main.go
parent39e7cce65fdb49f624bc3dccd4ef279db7f7b389 (diff)
downloadgallery-4ed83bdb017d7d078d7d56863f35b95940988e5b.tar.gz
gallery-4ed83bdb017d7d078d7d56863f35b95940988e5b.tar.xz
gallery-4ed83bdb017d7d078d7d56863f35b95940988e5b.zip
Rearrange tag importer code
Diffstat (limited to 'main.go')
-rw-r--r--main.go33
1 files 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
}
}