db.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef DB_H
  2. #define DB_H
  3. #include <SQLiteCpp/SQLiteCpp.h>
  4. #include <chrono>
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. typedef struct {
  9. time_t date;
  10. std::string user;
  11. int hand;
  12. int score;
  13. int won;
  14. } scores_details;
  15. typedef struct {
  16. time_t date;
  17. std::string user;
  18. int score;
  19. int won;
  20. } scores_data;
  21. typedef struct {
  22. time_t date;
  23. std::string user;
  24. int days;
  25. int hands_won;
  26. int score;
  27. } monthly_data;
  28. class DBData {
  29. SQLite::Database db;
  30. void create_tables(void);
  31. std::string user;
  32. int locked_retries;
  33. /*
  34. I thought some of my performance problems were from the prepared statement
  35. calls. It was not the case, there's a weird delay when I save/hit the
  36. database. This didn't fix it, but they are still present.
  37. Prepared statements, prepared ahead of time, are a good thing.
  38. Unless, they lock the entire database.
  39. std::unique_ptr<SQLite::Statement> stmt_getSet;
  40. std::unique_ptr<SQLite::Statement> stmt_setSet;
  41. */
  42. void retry_wait(void);
  43. public:
  44. DBData();
  45. virtual ~DBData();
  46. void setUser(std::string currentUser) { user = currentUser; };
  47. void clearUser(void) { user.clear(); };
  48. std::string getSetting(const std::string &setting, std::string ifMissing);
  49. void setSetting(const std::string &setting, const std::string &value);
  50. void saveScore(time_t when, time_t date, int hand, int won, int score);
  51. std::vector<scores_details> getScoresOnDay(time_t date);
  52. std::vector<monthly_data> getMonthlyScores(int limit = 10);
  53. std::vector<scores_data> getScores(int limit = 10);
  54. std::map<time_t, int> getPlayed(void);
  55. bool expireScores(time_t month_first_t);
  56. int handsPlayedOnDay(time_t day);
  57. std::map<time_t, int> whenPlayed(void);
  58. };
  59. void normalizeDate(std::chrono::_V2::system_clock::time_point &date);
  60. void normalizeDate(time_t &tt, int hour = 2);
  61. void firstOfMonthDate(std::chrono::_V2::system_clock::time_point &date);
  62. std::string convertDateToDateScoreFormat(time_t tt);
  63. std::string convertDateToMonthlyFormat(time_t tt);
  64. std::string convertDateToMonthDayFormat(time_t tt);
  65. #endif