Extract strings from functions because they are too verbose and drive me crazy

This commit is contained in:
King_DuckZ 2020-08-14 22:27:01 +01:00
parent d4fd25b151
commit 29f11d3f57

View file

@ -36,6 +36,80 @@
namespace oro {
namespace {
constexpr const char g_create_fame_list[] =
"CREATE TABLE IF NOT EXISTS fame_list("
"id INTEGER PRIMARY KEY NOT NULL"
", type TINYINT"
", master_id INTEGER UNIQUE"
", name TEXT"
", points INTEGER"
")";
constexpr const char g_create_slotted_cards[] =
"CREATE TABLE IF NOT EXISTS slotted_cards("
"id INTEGER PRIMARY KEY NOT NULL"
", shop_item_id INTEGER NOT NULL"
", card_id INTEGER NOT NULL"
", FOREIGN KEY(shop_item_id) REFERENCES shop_items(id)"
", FOREIGN KEY(card_id) REFERENCES items(id)"
")";
constexpr const char g_create_shop_items[] =
"CREATE TABLE IF NOT EXISTS shop_items("
"id INTEGER PRIMARY KEY NOT NULL"
", snapshot_id INTEGER NOT NULL"
", item_id INTEGER NOT NULL"
", amount INTEGER NOT NULL"
", price INTEGER NOT NULL"
", refine INTEGER NOT NULL"
", star_crumbs INTEGER NOT NULL"
", element TEXT"
", creator INTEGER"
", beloved TINYINT"
", FOREIGN KEY(snapshot_id) REFERENCES shop_snapshots(id)"
", FOREIGN KEY(item_id) REFERENCES items(id)"
")";
constexpr const char g_create_shop_snapshots[] =
"CREATE TABLE IF NOT EXISTS shop_snapshots("
"id INTEGER PRIMARY KEY NOT NULL"
", shop_id INTEGER NOT NULL"
", hash TEXT NOT NULL"
", discovery_date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP"
", last_seen_date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP"
", FOREIGN KEY(shop_id) REFERENCES shops(id)"
")";
constexpr const char g_create_shops[] =
"CREATE TABLE IF NOT EXISTS shops("
"id INTEGER PRIMARY KEY NOT NULL"
", title TEXT"
", owner TEXT"
", creation_date TEXT"
", loc_map TEXT"
", loc_x INTEGER"
", loc_y INTEGER"
", type TINYINT"
", seen_date TEXT DEFAULT CURRENT_TIMESTAMP"
")";
constexpr const char g_create_items_a[] = "CREATE TABLE IF NOT EXISTS ";
constexpr const char g_create_items_b[] =
" ("
"id INTEGER PRIMARY KEY NOT NULL"
", item_id INTEGER"
", unique_name TEXT"
", name TEXT"
", type TINYINT"
", subtype TINYINT"
", npc_price INTEGER"
", slots TINYINT"
", removal_date TEXT"
")";
constexpr const char g_create_icons_a[] = "CREATE TABLE IF NOT EXISTS ";
constexpr const char g_create_icons_b[] =
" ("
"id INTEGER PRIMARY KEY NOT NULL"
", item_id INTEGER NOT NULL"
", icon TEXT"
", FOREIGN KEY(item_id) REFERENCES items(id)"
")";
class Database : private std::unique_lock<std::mutex>, public SQLite::Database {
public:
Database(std::mutex& mtx, const std::string& path) :
@ -141,31 +215,6 @@ namespace {
return shop;
}
std::string string_query_create_items (std::string_view table) {
return std::string(
"CREATE TABLE IF NOT EXISTS ").append(table) + " ("
"id INTEGER PRIMARY KEY NOT NULL"
", item_id INTEGER"
", unique_name TEXT"
", name TEXT"
", type TINYINT"
", subtype TINYINT"
", npc_price INTEGER"
", slots TINYINT"
", removal_date TEXT"
")";
}
std::string string_query_create_icons (std::string_view table) {
return std::string(
"CREATE TABLE IF NOT EXISTS ").append(table) + " ("
"id INTEGER PRIMARY KEY NOT NULL"
", item_id INTEGER NOT NULL"
", icon TEXT"
", FOREIGN KEY(item_id) REFERENCES items(id)"
")";
}
std::string shop_hash (const oro::Shop& shop) {
using dhandy::int_to_ary;
@ -217,9 +266,9 @@ void OriginsDB::update (const Items& items) {
//}
db.exec("DROP TABLE IF EXISTS items_staging");
db.exec(string_query_create_items("items_staging"));
db.exec(std::string(g_create_items_a).append("items_staging") + g_create_items_b);
db.exec(string_query_create_items("items"));
db.exec(std::string(g_create_items_a).append("items") + g_create_items_b);
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 unique_active_item_constraint ON items(item_id, unique_name, ifnull(removal_date, 0))");
@ -272,8 +321,8 @@ void OriginsDB::update (const Icons& icons) {
//}
Database db(m_db_mutex, m_path);
db.exec(string_query_create_icons("icons_staging"));
db.exec(string_query_create_icons("icons"));
db.exec(std::string(g_create_icons_a).append("icons_staging") + g_create_icons_b);
db.exec(std::string(g_create_icons_a).append("icons") + g_create_icons_b);
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS icons_item_id_idx ON icons(item_id)");
SQLite::Statement query(db, "INSERT INTO icons_staging(item_id, icon) VALUES(?, ?)");
@ -314,51 +363,11 @@ void OriginsDB::update (const Shops& shops) {
//}
Database db(m_db_mutex, m_path);
db.exec("CREATE TABLE IF NOT EXISTS shops("
"id INTEGER PRIMARY KEY NOT NULL"
", title TEXT"
", owner TEXT"
", creation_date TEXT"
", loc_map TEXT"
", loc_x INTEGER"
", loc_y INTEGER"
", type TINYINT"
", seen_date TEXT DEFAULT CURRENT_TIMESTAMP"
")"
);
db.exec("CREATE TABLE IF NOT EXISTS shop_snapshots("
"id INTEGER PRIMARY KEY NOT NULL"
", shop_id INTEGER NOT NULL"
", hash TEXT NOT NULL"
", discovery_date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP"
", last_seen_date TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP"
", FOREIGN KEY(shop_id) REFERENCES shops(id)"
")"
);
db.exec(g_create_shops);
db.exec(g_create_shop_snapshots);
db.exec("CREATE UNIQUE INDEX IF NOT EXISTS shops_owner_idx ON shops(owner, creation_date)");
db.exec("CREATE TABLE IF NOT EXISTS shop_items("
"id INTEGER PRIMARY KEY NOT NULL"
", snapshot_id INTEGER NOT NULL"
", item_id INTEGER NOT NULL"
", amount INTEGER NOT NULL"
", price INTEGER NOT NULL"
", refine INTEGER NOT NULL"
", star_crumbs INTEGER NOT NULL"
", element TEXT"
", creator INTEGER"
", beloved TINYINT"
", FOREIGN KEY(snapshot_id) REFERENCES shop_snapshots(id)"
", FOREIGN KEY(item_id) REFERENCES items(id)"
")"
);
db.exec("CREATE TABLE IF NOT EXISTS slotted_cards("
"id INTEGER PRIMARY KEY NOT NULL"
", shop_item_id INTEGER NOT NULL"
", card_id INTEGER NOT NULL"
", FOREIGN KEY(shop_item_id) REFERENCES shop_items(id)"
", FOREIGN KEY(card_id) REFERENCES items(id)"
")"
);
db.exec(g_create_shop_items);
db.exec(g_create_slotted_cards);
SQLite::Statement ins_shop(db, "INSERT INTO shops(title, owner, creation_date, loc_map, loc_x, loc_y, type) VALUES(?, ?, ?, ?, ?, ?, ?)");
SQLite::Statement ins_sshot(db, "INSERT INTO shop_snapshots(shop_id, hash) VALUES(?, ?)");
@ -419,14 +428,7 @@ void OriginsDB::update (const Shops& shops) {
void OriginsDB::update (const Creators& creat) {
Database db(m_db_mutex, m_path);
db.exec("CREATE TABLE IF NOT EXISTS fame_list("
"id INTEGER PRIMARY KEY NOT NULL"
", type TINYINT"
", master_id INTEGER UNIQUE"
", name TEXT"
", points INTEGER"
")"
);
db.exec(g_create_fame_list);
auto insert_creators = [](SQLite::Statement& query, const std::vector<oro::Creator>& creats, int type) {
for (const auto& creator : creats) {