Populate the shops table

This commit is contained in:
King_DuckZ 2020-08-10 21:51:57 +01:00
parent 417030f4fe
commit 616ac36a34

View file

@ -22,7 +22,12 @@
#include "SQLiteCpp/SQLiteCpp.h"
#include "oro/items.hpp"
#include "oro/icons.hpp"
#include "oro/shops.hpp"
#include <mutex>
#include <chrono>
#define HAS_UNCAUGHT_EXCEPTIONS 1
#include "date/date.h"
namespace oro {
@ -35,6 +40,15 @@ namespace {
{
}
};
std::string to_sqlite_string (const oro::Timestamp& ts) {
using date::operator<<;
using std::chrono::seconds;
using date::floor;
using date::format;
return format("%FT%T", floor<seconds>(ts.ts));
}
} //unnamed namespace
OriginsDB::OriginsDB (std::string_view path) :
@ -118,6 +132,58 @@ void OriginsDB::update (const Icons& icons) {
}
void OriginsDB::update (const Shops& shops) {
//example:
//{
// "title":"• B\u003ePoison Bottle •",
// "owner":"Mr. Boom Bastic",
// "location":{
// "map":"prontera",
// "x":124,
// "y":170
// },
// "creation_date":"2020-06-22T13:12:31Z",
// "type":"B",
// "items":[{"item_id":678,"amount":200,"price":31000}]
//}
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 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"
", shop_id INTEGER NOT NULL"
", item_id INTEGER NOT NULL"
", amount INTEGER NOT NULL"
", price INTEGER NOT NULL"
", FOREIGN KEY(shop_id) REFERENCES shops(id)"
")"
);
SQLite::Statement ins_shop(db, "INSERT INTO shops(title, owner, creation_date, loc_map, loc_x, loc_y, type) VALUES(?, ?, ?, ?, ?, ?, ?)");
SQLite::Transaction transaction(db);
for (const auto& shop : shops.shops) {
ins_shop.bind(1, shop.title);
ins_shop.bind(2, shop.owner);
ins_shop.bind(3, to_sqlite_string(shop.creation_date));
ins_shop.bind(4, shop.location.map);
ins_shop.bind(5, shop.location.x);
ins_shop.bind(6, shop.location.y);
ins_shop.bind(7, static_cast<int>(shop.type.value));
ins_shop.exec();
ins_shop.reset();
}
transaction.commit();
}
} //namespace oro