db.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. Unless, they lock the entire database.
  32. std::unique_ptr<SQLite::Statement> stmt_getSet;
  33. std::unique_ptr<SQLite::Statement> stmt_setSet;
  34. */
  35. void retry_wait(void);
  36. public:
  37. DBData();
  38. virtual ~DBData();
  39. void setUser(std::string currentUser) { user = currentUser; };
  40. void clearUser(void) { user.clear(); };
  41. std::string getSetting(const std::string &setting, std::string ifMissing);
  42. void setSetting(const std::string &setting, const std::string &value);
  43. void saveScore(time_t when, time_t date, int hand, int won, int score);
  44. std::vector<scores_details> getScoresOnDay(time_t date);
  45. std::map<time_t, std::vector<scores_data>> getScores(void);
  46. std::map<time_t, int> getPlayed(void);
  47. void expireScores(void);
  48. int handsPlayedOnDay(time_t day);
  49. std::map<time_t, int> whenPlayed(void);
  50. };
  51. void normalizeDate(std::chrono::_V2::system_clock::time_point &date);
  52. void normalizeDate(time_t &tt, int hour = 2);
  53. std::string convertDateToDateScoreFormat(time_t tt);
  54. #endif