db.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. class DBData {
  22. SQLite::Database db;
  23. void create_tables(void);
  24. std::string user;
  25. int locked_retries;
  26. /*
  27. I thought some of my performance problems were from the prepared statement
  28. calls. It was not the case, there's a weird delay when I save/hit the
  29. database. This didn't fix it, but they are still present.
  30. Prepared statements, prepared ahead of time, are a good thing.
  31. std::unique_ptr<SQLite::Statement> stmt_getSet;
  32. std::unique_ptr<SQLite::Statement> stmt_setSet;
  33. */
  34. void retry_wait(void);
  35. public:
  36. DBData();
  37. virtual ~DBData();
  38. void setUser(std::string currentUser) { user = currentUser; };
  39. void clearUser(void) { user.clear(); };
  40. std::string getSetting(const std::string &setting, std::string ifMissing);
  41. void setSetting(const std::string &setting, const std::string &value);
  42. void saveScore(time_t when, time_t date, int hand, int won, int score);
  43. std::vector<scores_details> getScoresOnDay(time_t date);
  44. std::map<time_t, std::vector<scores_data>> getScores(void);
  45. std::map<time_t, int> getPlayed(void);
  46. void expireScores(void);
  47. int handsPlayedOnDay(time_t day);
  48. std::map<time_t, int> whenPlayed(void);
  49. };
  50. void normalizeDate(std::chrono::_V2::system_clock::time_point &date);
  51. void normalizeDate(time_t &tt, int hour = 2);
  52. std::string convertDateToDateScoreFormat(time_t tt);
  53. #endif