about summary refs log tree commit diff stats
path: root/database.cpp
blob: 19ba0e0ab3f169927b759463301151ac7a3a2400 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "database.h"
#include <sqlite3.h>
#include <stdexcept>
#include <sstream>
#include <iomanip>

database::database(std::string path)
{
  if (sqlite3_open_v2(
    path.c_str(),
    &ppdb_,
    SQLITE_OPEN_READONLY,
    NULL) != SQLITE_OK)
  {
    // We still have to free the resources allocated. In the event that
    // allocation failed, ppdb will be null and sqlite3_close_v2 will just
    // ignore it.
    std::string errmsg(sqlite3_errmsg(ppdb_));
    sqlite3_close_v2(ppdb_);

    throw std::logic_error(errmsg);
  }
}

database::database(database&& other) : database()
{
  swap(*this, other);
}

database& database::operator=(database&& other)
{
  swap(*this, other);

  return *this;
}

void swap(database& first, database& second)
{
  std::swap(first.ppdb_, second.ppdb_);
}

database::~database()
{
  sqlite3_close_v2(ppdb_);
}

achievement database::getRandomAchievement() const
{
  std::string queryString = "SELECT achievements.achievement_id, achievements.game_id, achievements.title, games.color FROM achievements INNER JOIN games ON games.game_id = achievements.game_id ORDER BY RANDOM() LIMIT 1";

  sqlite3_stmt* ppstmt;
  if (sqlite3_prepare_v2(
    ppdb_,
    queryString.c_str(),
    queryString.length(),
    &ppstmt,
    NULL) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_step(ppstmt) != SQLITE_ROW)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  achievement result;

  result.achievementId = sqlite3_column_int(ppstmt, 0);
  result.gameId = sqlite3_column_int(ppstmt, 1);
  result.title = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2));
  result.color = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3));

  sqlite3_finalize(ppstmt);

  return result;
}

bool database::doesGameHaveImages(int gameId) const
{
  std::string queryString = "SELECT COUNT(*) FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1";

  sqlite3_stmt* ppstmt;
  if (sqlite3_prepare_v2(
    ppdb_,
    queryString.c_str(),
    queryString.length(),
    &ppstmt,
    NULL) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_bind_int(ppstmt, 1, gameId) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_step(ppstmt) != SQLITE_ROW)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  int result = sqlite3_column_int(ppstmt, 0);

  sqlite3_finalize(ppstmt);

  return (result > 0);
}

std::string database::getRandomImageForGame(int gameId) const
{
  std::string queryString = "SELECT filename FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1";

  sqlite3_stmt* ppstmt;
  if (sqlite3_prepare_v2(
    ppdb_,
    queryString.c_str(),
    queryString.length(),
    &ppstmt,
    NULL) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_bind_int(ppstmt, 1, gameId) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_step(ppstmt) != SQLITE_ROW)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  std::string result = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0));

  sqlite3_finalize(ppstmt);

  return result;
}

did database::getRandomDidForAchievement(int achievementId) const
{
  std::string queryString = "SELECT profile_id, DATETIME(achieved_at) FROM dids WHERE achievement_id = ? ORDER BY RANDOM() LIMIT 1";

  sqlite3_stmt* ppstmt;
  if (sqlite3_prepare_v2(
    ppdb_,
    queryString.c_str(),
    queryString.length(),
    &ppstmt,
    NULL) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_bind_int(ppstmt, 1, achievementId) != SQLITE_OK)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  if (sqlite3_step(ppstmt) != SQLITE_ROW)
  {
    std::string errorMsg = sqlite3_errmsg(ppdb_);
    sqlite3_finalize(ppstmt);

    throw std::logic_error(errorMsg);
  }

  did result;
  result.profileId = sqlite3_column_int(ppstmt, 0);

  std::tm achievedAt = {};

  std::stringstream dateParser;
  dateParser << reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1));
  dateParser >> std::get_time(&achievedAt, "%Y-%m-%d %H:%M:%S");

  std::stringstream dateFormatter;
  dateFormatter << std::put_time(&achievedAt, "%m/%d/%Y");
  result.date = dateFormatter.str();

  sqlite3_finalize(ppstmt);

  return result;
}