db.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "db.h"
  2. #include <SQLiteCpp/VariadicBind.h>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <sstream>
  6. // configuration settings access
  7. #include "yaml-cpp/yaml.h"
  8. extern YAML::Node config;
  9. DBData::DBData(void)
  10. : db("space-data.db", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE) {
  11. init();
  12. stmt_getSet = std::make_unique<SQLite::Statement>(
  13. db, "SELECT value FROM settings WHERE username=? AND setting=?");
  14. stmt_setSet = std::make_unique<SQLite::Statement>(
  15. db, "REPLACE INTO settings(username, setting, value) VALUES(?,?,?);");
  16. }
  17. // DBData::DBData(void) : sql(std::string(DB_CONNECT_STRING)) {}
  18. DBData::~DBData() {}
  19. void DBData::init(void) {
  20. db.exec("CREATE TABLE IF NOT EXISTS \
  21. settings(username TEXT, setting TEXT, value TEXT, \
  22. PRIMARY KEY(username, setting));");
  23. db.exec("CREATE TABLE IF NOT EXISTS \
  24. scores ( \"username\" TEXT, \"when\" INTEGER, \
  25. \"date\" TEXT, \"hand\" INTEGER, \"score\" INTEGER, \
  26. PRIMARY KEY(\"username\", \"date\", \"hand\"));");
  27. }
  28. void DBData::setUser(std::string currentUser) { user = currentUser; }
  29. std::string DBData::getSetting(const std::string &setting,
  30. std::string ifMissing) {
  31. stmt_getSet->reset();
  32. stmt_getSet->bind(1, user);
  33. stmt_getSet->bind(2, setting);
  34. if (stmt_getSet->executeStep()) {
  35. std::string value = stmt_getSet->getColumn(0);
  36. return value;
  37. };
  38. return ifMissing;
  39. }
  40. void DBData::setSetting(const std::string &setting, const std::string &value) {
  41. stmt_setSet->reset();
  42. stmt_setSet->bind(1, user);
  43. stmt_setSet->bind(2, setting);
  44. stmt_setSet->bind(3, value);
  45. stmt_setSet->exec();
  46. }
  47. void DBData::save_score(time_t when, std::string date, int hand, int score) {
  48. SQLite::Statement stmt(db, "INSERT INTO scores( \"username\", \"when\", "
  49. "\"date\", \"hand\", \"score\") VALUES(?,?,?,?);");
  50. stmt.bind(1, user);
  51. stmt.bind(2, when);
  52. stmt.bind(3, date);
  53. stmt.bind(4, hand);
  54. stmt.bind(5, score);
  55. stmt.exec();
  56. }
  57. bool DBData::has_played_day(time_t day) {
  58. // get date from this
  59. // std::stringstream ss;
  60. // ss << std::put_time(std::localtime(&day), "%Y/%0m/%0d");
  61. std::string today = make_date(day);
  62. SQLite::Statement stmt(
  63. db, "SELECT COUNT(*) FROM scores WHERE \"username\"=? AND \"DATE\"=?;");
  64. stmt.bind(1, user);
  65. stmt.bind(2, today);
  66. int count = -1;
  67. if (stmt.executeStep()) {
  68. count = stmt.getColumn(0);
  69. };
  70. return (count > 0);
  71. }
  72. std::string make_date(time_t tt) {
  73. std::stringstream ss;
  74. if (config["date_score"]) {
  75. std::string custom_format = config["date_score"].as<std::string>();
  76. ss << std::put_time(std::localtime(&tt), custom_format.c_str());
  77. } else {
  78. ss << std::put_time(std::localtime(&tt), "%Y/%0m/%0d");
  79. }
  80. std::string date = ss.str();
  81. return date;
  82. }