From 1b5e1f436572c2503cc1620e4e2a33ef207df542 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 12 Aug 2020 01:07:34 +0100 Subject: [PATCH] Improve sql for items table Now if item_id or unique_name change, that counts as a record being deleted and a new one inserted. If neither of that changed but any other field did, the old field gets updated with the new value. --- .gitignore | 1 + src/oro/originsdb.cpp | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 0cf0bd1..16acd6f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ tags compile_commands.json links.txt +*.bin diff --git a/src/oro/originsdb.cpp b/src/oro/originsdb.cpp index 8826e8e..1d7f08b 100644 --- a/src/oro/originsdb.cpp +++ b/src/oro/originsdb.cpp @@ -176,10 +176,10 @@ void OriginsDB::update (const Items& items) { db.exec(string_query_create_items("items")); db.exec("CREATE UNIQUE INDEX IF NOT EXISTS items_item_id_idx ON items(item_id, removal_date)"); //see https://stackoverflow.com/questions/22699409/sqlite-null-and-unique - db.exec("CREATE UNIQUE INDEX IF NOT EXISTS items_item_id_constraint ON items(item_id, ifnull(removal_date, 0))"); + db.exec("CREATE UNIQUE INDEX IF NOT EXISTS unique_active_item_constraint ON items(item_id, unique_name, ifnull(removal_date, 0))"); SQLite::Statement query(db, - "INSERT OR IGNORE INTO items_staging " + "INSERT INTO items_staging " "(item_id, unique_name, name, type, subtype, npc_price, slots) " "VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7)" ); @@ -200,13 +200,18 @@ void OriginsDB::update (const Items& items) { db.exec( "UPDATE items SET removal_date = CURRENT_TIMESTAMP WHERE " "removal_date IS NULL AND item_id IN (" - "SELECT item_id FROM items WHERE removal_date IS NULL EXCEPT " - "SELECT item_id FROM items_staging" + "SELECT item_id FROM (" + "SELECT item_id, unique_name FROM items WHERE removal_date IS NULL EXCEPT " + "SELECT item_id, unique_name FROM items_staging" + ")" ")" ); db.exec( - "INSERT OR IGNORE INTO items (item_id, unique_name, name, type, subtype, npc_price, slots) " - "SELECT item_id, unique_name, name, type, subtype, npc_price, slots FROM items_staging" + "INSERT INTO items (item_id, unique_name, name, type, subtype, npc_price, slots) " + "SELECT item_id, unique_name, name, type, subtype, npc_price, slots FROM items_staging WHERE true " + "ON CONFLICT(item_id, unique_name, ifnull(removal_date, 0)) DO UPDATE SET " + "name=excluded.name, type=excluded.type, subtype=excluded.subtype, " + "npc_price=excluded.npc_price, slots=excluded.slots" ); db.exec("DROP TABLE items_staging");