Skip to content

Commit

Permalink
fix N+1
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Aug 12, 2024
1 parent 7b971aa commit 1e0ab0f
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/game_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,22 +753,52 @@ bool player::save(dpp::snowflake user_id, bool put_backup)

if (inv_change) {
db::query("DELETE FROM game_owned_items WHERE user_id = ?", {user_id});


std::string query{"INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES"};
db::paramlist p;
for (const stacked_item& possession : possessions) {
if (possession.name != "[none]") {
db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, possession.name, possession.flags, possession.qty});
p.emplace_back(user_id);
p.emplace_back(possession.name);
p.emplace_back(possession.flags);
p.emplace_back(possession.qty);
query += "(?,?,?,?),";
}
}
if (!possessions.empty()) {
query = query.substr(0, query.length() - 1);
db::query(query, p);
}
p = {};
query = "INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES";
for (const item& herb : herbs) {
if (herb.name != "[none]") {
db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, herb.name, herb.flags, 1});
p.emplace_back(user_id);
p.emplace_back(herb.name);
p.emplace_back(herb.flags);
p.emplace_back(1);
query += "(?,?,?,?),";
}
}
if (!herbs.empty()) {
query = query.substr(0, query.length() - 1);
db::query(query, p);
}
p = {};
query = "INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES";
for (const item& spell : spells) {
if (spell.name != "[none]") {
db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, spell.name, spell.flags, 1});
p.emplace_back(user_id);
p.emplace_back(spell.name);
p.emplace_back(spell.flags);
p.emplace_back(1);
query += "(?,?,?,?),";
}
}
if (!spells.empty()) {
query = query.substr(0, query.length() - 1);
db::query(query, p);
}

inv_change = false;
}
Expand Down

0 comments on commit 1e0ab0f

Please sign in to comment.