12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include "db.h"
- #include <SQLiteCpp/VariadicBind.h>
- #include <iomanip>
- #include <iostream>
- #include <sstream>
- // configuration settings access
- #include "yaml-cpp/yaml.h"
- extern YAML::Node config;
- DBData::DBData(void)
- : db("space-data.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE) {
- init();
- stmt_getSet = std::make_unique<SQLite::Statement>(
- db, "SELECT value FROM settings WHERE username=? AND setting=?");
- stmt_setSet = std::make_unique<SQLite::Statement>(
- db, "REPLACE INTO settings(username, setting, value) VALUES(?,?,?);");
- }
- // DBData::DBData(void) : sql(std::string(DB_CONNECT_STRING)) {}
- DBData::~DBData() {}
- void DBData::init(void) {
- db.exec("CREATE TABLE IF NOT EXISTS \
- settings(username TEXT, setting TEXT, value TEXT, \
- PRIMARY KEY(username, setting));");
- db.exec("CREATE TABLE IF NOT EXISTS \
- scores ( \"username\" TEXT, \"when\" INTEGER, \
- \"date\" TEXT, \"hand\" INTEGER, \"score\" INTEGER, \
- PRIMARY KEY(\"username\", \"date\", \"hand\"));");
- }
- void DBData::setUser(std::string currentUser) { user = currentUser; }
- std::string DBData::getSetting(const std::string &setting,
- std::string ifMissing) {
- stmt_getSet->reset();
- stmt_getSet->bind(1, user);
- stmt_getSet->bind(2, setting);
- if (stmt_getSet->executeStep()) {
- std::string value = stmt_getSet->getColumn(0);
- return value;
- };
- return ifMissing;
- }
- void DBData::setSetting(const std::string &setting, const std::string &value) {
- stmt_setSet->reset();
- stmt_setSet->bind(1, user);
- stmt_setSet->bind(2, setting);
- stmt_setSet->bind(3, value);
- stmt_setSet->exec();
- }
- void DBData::save_score(time_t when, std::string date, int hand, int score) {
- SQLite::Statement stmt(db, "INSERT INTO scores( \"username\", \"when\", "
- "\"date\", \"hand\", \"score\") VALUES(?,?,?,?);");
- stmt.bind(1, user);
- stmt.bind(2, when);
- stmt.bind(3, date);
- stmt.bind(4, hand);
- stmt.bind(5, score);
- stmt.exec();
- }
- bool DBData::has_played_day(time_t day) {
- // get date from this
- // std::stringstream ss;
- // ss << std::put_time(std::localtime(&day), "%Y/%0m/%0d");
- std::string today = make_date(day);
- SQLite::Statement stmt(
- db, "SELECT COUNT(*) FROM scores WHERE \"username\"=? AND \"DATE\"=?;");
- stmt.bind(1, user);
- stmt.bind(2, today);
- int count = -1;
- if (stmt.executeStep()) {
- count = stmt.getColumn(0);
- };
- return (count > 0);
- }
- std::string make_date(time_t tt) {
- std::stringstream ss;
- if (config["date_score"]) {
- std::string custom_format = config["date_score"].as<std::string>();
- ss << std::put_time(std::localtime(&tt), custom_format.c_str());
- } else {
- ss << std::put_time(std::localtime(&tt), "%Y/%0m/%0d");
- }
- std::string date = ss.str();
- return date;
- }
|