db.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef DB_H
  2. #define DB_H
  3. #include <SQLiteCpp/SQLiteCpp.h>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. typedef struct {
  8. time_t date;
  9. std::string user;
  10. int hand;
  11. int score;
  12. int won;
  13. } scores_details;
  14. typedef struct {
  15. time_t date;
  16. std::string user;
  17. int score;
  18. int won;
  19. } scores_data;
  20. class DBData {
  21. SQLite::Database db;
  22. void create_tables(void);
  23. std::string user;
  24. /*
  25. I thought some of my performance problems were from the prepared statement
  26. calls. It was not the case, there's a weird delay when I save/hit the
  27. database. This didn't fix it, but they are still present.
  28. Prepared statements, prepared ahead of time, are a good thing.
  29. */
  30. std::unique_ptr<SQLite::Statement> stmt_getSet;
  31. std::unique_ptr<SQLite::Statement> stmt_setSet;
  32. public:
  33. DBData();
  34. virtual ~DBData();
  35. void setUser(std::string currentUser) { user = currentUser; };
  36. void clearUser(void) { user.clear(); };
  37. std::string getSetting(const std::string &setting, std::string ifMissing);
  38. void setSetting(const std::string &setting, const std::string &value);
  39. void saveScore(time_t when, time_t date, int hand, int won, int score);
  40. std::vector<scores_details> getScoresOnDay(time_t date);
  41. std::map<time_t, std::vector<scores_data>> getScores(void);
  42. void expireScores(void);
  43. int handsPlayedOnDay(time_t day);
  44. };
  45. void normalizeDate(time_t &tt, int hour = 2);
  46. std::string convertDateToDateScoreFormat(time_t tt);
  47. #endif