diff options
Diffstat (limited to 'database.cpp')
-rw-r--r-- | database.cpp | 217 |
1 files changed, 0 insertions, 217 deletions
diff --git a/database.cpp b/database.cpp deleted file mode 100644 index 19ba0e0..0000000 --- a/database.cpp +++ /dev/null | |||
@@ -1,217 +0,0 @@ | |||
1 | #include "database.h" | ||
2 | #include <sqlite3.h> | ||
3 | #include <stdexcept> | ||
4 | #include <sstream> | ||
5 | #include <iomanip> | ||
6 | |||
7 | database::database(std::string path) | ||
8 | { | ||
9 | if (sqlite3_open_v2( | ||
10 | path.c_str(), | ||
11 | &ppdb_, | ||
12 | SQLITE_OPEN_READONLY, | ||
13 | NULL) != SQLITE_OK) | ||
14 | { | ||
15 | // We still have to free the resources allocated. In the event that | ||
16 | // allocation failed, ppdb will be null and sqlite3_close_v2 will just | ||
17 | // ignore it. | ||
18 | std::string errmsg(sqlite3_errmsg(ppdb_)); | ||
19 | sqlite3_close_v2(ppdb_); | ||
20 | |||
21 | throw std::logic_error(errmsg); | ||
22 | } | ||
23 | } | ||
24 | |||
25 | database::database(database&& other) : database() | ||
26 | { | ||
27 | swap(*this, other); | ||
28 | } | ||
29 | |||
30 | database& database::operator=(database&& other) | ||
31 | { | ||
32 | swap(*this, other); | ||
33 | |||
34 | return *this; | ||
35 | } | ||
36 | |||
37 | void swap(database& first, database& second) | ||
38 | { | ||
39 | std::swap(first.ppdb_, second.ppdb_); | ||
40 | } | ||
41 | |||
42 | database::~database() | ||
43 | { | ||
44 | sqlite3_close_v2(ppdb_); | ||
45 | } | ||
46 | |||
47 | achievement database::getRandomAchievement() const | ||
48 | { | ||
49 | 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"; | ||
50 | |||
51 | sqlite3_stmt* ppstmt; | ||
52 | if (sqlite3_prepare_v2( | ||
53 | ppdb_, | ||
54 | queryString.c_str(), | ||
55 | queryString.length(), | ||
56 | &ppstmt, | ||
57 | NULL) != SQLITE_OK) | ||
58 | { | ||
59 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
60 | sqlite3_finalize(ppstmt); | ||
61 | |||
62 | throw std::logic_error(errorMsg); | ||
63 | } | ||
64 | |||
65 | if (sqlite3_step(ppstmt) != SQLITE_ROW) | ||
66 | { | ||
67 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
68 | sqlite3_finalize(ppstmt); | ||
69 | |||
70 | throw std::logic_error(errorMsg); | ||
71 | } | ||
72 | |||
73 | achievement result; | ||
74 | |||
75 | result.achievementId = sqlite3_column_int(ppstmt, 0); | ||
76 | result.gameId = sqlite3_column_int(ppstmt, 1); | ||
77 | result.title = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 2)); | ||
78 | result.color = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 3)); | ||
79 | |||
80 | sqlite3_finalize(ppstmt); | ||
81 | |||
82 | return result; | ||
83 | } | ||
84 | |||
85 | bool database::doesGameHaveImages(int gameId) const | ||
86 | { | ||
87 | std::string queryString = "SELECT COUNT(*) FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1"; | ||
88 | |||
89 | sqlite3_stmt* ppstmt; | ||
90 | if (sqlite3_prepare_v2( | ||
91 | ppdb_, | ||
92 | queryString.c_str(), | ||
93 | queryString.length(), | ||
94 | &ppstmt, | ||
95 | NULL) != SQLITE_OK) | ||
96 | { | ||
97 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
98 | sqlite3_finalize(ppstmt); | ||
99 | |||
100 | throw std::logic_error(errorMsg); | ||
101 | } | ||
102 | |||
103 | if (sqlite3_bind_int(ppstmt, 1, gameId) != SQLITE_OK) | ||
104 | { | ||
105 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
106 | sqlite3_finalize(ppstmt); | ||
107 | |||
108 | throw std::logic_error(errorMsg); | ||
109 | } | ||
110 | |||
111 | if (sqlite3_step(ppstmt) != SQLITE_ROW) | ||
112 | { | ||
113 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
114 | sqlite3_finalize(ppstmt); | ||
115 | |||
116 | throw std::logic_error(errorMsg); | ||
117 | } | ||
118 | |||
119 | int result = sqlite3_column_int(ppstmt, 0); | ||
120 | |||
121 | sqlite3_finalize(ppstmt); | ||
122 | |||
123 | return (result > 0); | ||
124 | } | ||
125 | |||
126 | std::string database::getRandomImageForGame(int gameId) const | ||
127 | { | ||
128 | std::string queryString = "SELECT filename FROM images WHERE game_id = ? ORDER BY RANDOM() LIMIT 1"; | ||
129 | |||
130 | sqlite3_stmt* ppstmt; | ||
131 | if (sqlite3_prepare_v2( | ||
132 | ppdb_, | ||
133 | queryString.c_str(), | ||
134 | queryString.length(), | ||
135 | &ppstmt, | ||
136 | NULL) != SQLITE_OK) | ||
137 | { | ||
138 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
139 | sqlite3_finalize(ppstmt); | ||
140 | |||
141 | throw std::logic_error(errorMsg); | ||
142 | } | ||
143 | |||
144 | if (sqlite3_bind_int(ppstmt, 1, gameId) != SQLITE_OK) | ||
145 | { | ||
146 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
147 | sqlite3_finalize(ppstmt); | ||
148 | |||
149 | throw std::logic_error(errorMsg); | ||
150 | } | ||
151 | |||
152 | if (sqlite3_step(ppstmt) != SQLITE_ROW) | ||
153 | { | ||
154 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
155 | sqlite3_finalize(ppstmt); | ||
156 | |||
157 | throw std::logic_error(errorMsg); | ||
158 | } | ||
159 | |||
160 | std::string result = reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 0)); | ||
161 | |||
162 | sqlite3_finalize(ppstmt); | ||
163 | |||
164 | return result; | ||
165 | } | ||
166 | |||
167 | did database::getRandomDidForAchievement(int achievementId) const | ||
168 | { | ||
169 | std::string queryString = "SELECT profile_id, DATETIME(achieved_at) FROM dids WHERE achievement_id = ? ORDER BY RANDOM() LIMIT 1"; | ||
170 | |||
171 | sqlite3_stmt* ppstmt; | ||
172 | if (sqlite3_prepare_v2( | ||
173 | ppdb_, | ||
174 | queryString.c_str(), | ||
175 | queryString.length(), | ||
176 | &ppstmt, | ||
177 | NULL) != SQLITE_OK) | ||
178 | { | ||
179 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
180 | sqlite3_finalize(ppstmt); | ||
181 | |||
182 | throw std::logic_error(errorMsg); | ||
183 | } | ||
184 | |||
185 | if (sqlite3_bind_int(ppstmt, 1, achievementId) != SQLITE_OK) | ||
186 | { | ||
187 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
188 | sqlite3_finalize(ppstmt); | ||
189 | |||
190 | throw std::logic_error(errorMsg); | ||
191 | } | ||
192 | |||
193 | if (sqlite3_step(ppstmt) != SQLITE_ROW) | ||
194 | { | ||
195 | std::string errorMsg = sqlite3_errmsg(ppdb_); | ||
196 | sqlite3_finalize(ppstmt); | ||
197 | |||
198 | throw std::logic_error(errorMsg); | ||
199 | } | ||
200 | |||
201 | did result; | ||
202 | result.profileId = sqlite3_column_int(ppstmt, 0); | ||
203 | |||
204 | std::tm achievedAt = {}; | ||
205 | |||
206 | std::stringstream dateParser; | ||
207 | dateParser << reinterpret_cast<const char*>(sqlite3_column_text(ppstmt, 1)); | ||
208 | dateParser >> std::get_time(&achievedAt, "%Y-%m-%d %H:%M:%S"); | ||
209 | |||
210 | std::stringstream dateFormatter; | ||
211 | dateFormatter << std::put_time(&achievedAt, "%m/%d/%Y"); | ||
212 | result.date = dateFormatter.str(); | ||
213 | |||
214 | sqlite3_finalize(ppstmt); | ||
215 | |||
216 | return result; | ||
217 | } | ||