diff options
author | Jordan <me@jordan.im> | 2021-12-16 20:50:21 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2021-12-16 20:50:21 -0700 |
commit | 27584d82ad625428c0c3f0e9edb48d1f51647ec6 (patch) | |
tree | 966194c749cda96cca5d27b91b13c0debaed403e | |
parent | bef78585658692e65d823594e8d6fc2bc18c1fd3 (diff) | |
download | keep-27584d82ad625428c0c3f0e9edb48d1f51647ec6.tar.gz keep-27584d82ad625428c0c3f0e9edb48d1f51647ec6.zip |
db: use subqueries during insertion
-rw-r--r-- | db.go | 38 |
1 files changed, 11 insertions, 27 deletions
@@ -81,7 +81,7 @@ func addArchived(db *sql.DB, m *Message, status_code int) { defer tx.Rollback() // Insert new entries in users, guilds, channels tables for new values, - // ignoring those already present + // ignoring if already present _, err = tx.Exec(` INSERT OR IGNORE INTO users(user_id) VALUES(?); INSERT OR IGNORE INTO guilds(guild_id) VALUES(?); @@ -91,33 +91,17 @@ func addArchived(db *sql.DB, m *Message, status_code int) { log.Fatal(err) } - // Store IDs of previously-inserted (or already-existent) rows - var user_string_id int - var guild_string_id int - var channel_string_id int - - // Query users/guilds/channels tables for aforementioned rows - err = tx.QueryRow("SELECT id FROM users WHERE user_id = ?;", - m.Author).Scan(&user_string_id) - if err != nil { - log.Fatal(err) - } - err = tx.QueryRow("SELECT id FROM guilds WHERE guild_id = ?;", - m.Guild).Scan(&guild_string_id) - if err != nil { - log.Fatal(err) - } - err = tx.QueryRow("SELECT id FROM channels WHERE channel_id = ?;", - m.Channel).Scan(&channel_string_id) - if err != nil { - log.Fatal(err) - } - - // Insert entry in URLs table using IDs populated by previous selections - _, err = tx.Exec(`INSERT OR IGNORE INTO + // Insert entry in URLs table using foreign key reference IDs + _, err = tx.Exec(` + INSERT OR IGNORE INTO urls(url, user_string_id, guild_string_id, channel_string_id, status_code) - VALUES(?, ?, ?, ?, ?);`, - m.URL, user_string_id, guild_string_id, channel_string_id, status_code) + VALUES( + ?, + (SELECT id FROM users WHERE user_id = ?), + (SELECT id FROM guilds WHERE guild_id = ?), + (SELECT id FROM channels WHERE channel_id = ?), + ? + );`, m.URL, m.Author, m.Guild, m.Channel, status_code) if err != nil { log.Fatal(err) } |