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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <database/navigation.h>
#include <filter/string.h>
#include <filter/date.h>
#include <database/sqlite.h>
#include <webserver/request.h>
#include <filter/passage.h>
// Database resilience: It is re-created every night
constexpr const auto navigation {"navigation"};
void Database_Navigation::create ()
{
SqliteDatabase sql (navigation);
sql.set_sql ("CREATE TABLE IF NOT EXISTS navigation ("
" timestamp integer,"
" username text,"
" book integer,"
" chapter integer,"
" verse integer,"
" active boolean"
");");
sql.execute ();
}
void Database_Navigation::trim ()
{
// Delete items older than, say, several weeks.
int time = filter::date::seconds_since_epoch ();
time -= (3600 * 24 * 14);
SqliteDatabase sql (navigation);
sql.add ("DELETE FROM navigation WHERE timestamp <=");
sql.add (time);
sql.add (";");
sql.execute ();
}
void Database_Navigation::record (int time, std::string user, int book, int chapter, int verse)
{
SqliteDatabase sql (navigation);
// Clear any 'active' flags.
sql.add ("UPDATE navigation SET active = 0 WHERE username =");
sql.add (user);
sql.add (";");
sql.execute();
// Remove entries recorded less than several seconds ago.
int recent = time - 5;
sql.clear();
sql.add ("DELETE FROM navigation WHERE timestamp >=");
sql.add (recent);
sql.add ("AND username =");
sql.add (user);
sql.add (";");
sql.execute();
// Record entry.
sql.clear();
sql.add ("INSERT INTO navigation VALUES (");
sql.add (time);
sql.add (",");
sql.add (user);
sql.add (",");
sql.add (book);
sql.add (",");
sql.add (chapter);
sql.add (",");
sql.add (verse);
sql.add (", 1);");
sql.execute();
}
bool Database_Navigation::previous_exists (const std::string& user)
{
return (get_previous_id (user) != 0);
}
bool Database_Navigation::next_exists (const std::string& user)
{
return (get_next_id (user) != 0);
}
Passage Database_Navigation::get_previous (const std::string& user)
{
int id = get_previous_id (user);
if (id == 0) return Passage ();
// Update the 'active' flag.
SqliteDatabase sql1 (navigation);
sql1.add ("UPDATE navigation SET active = 0 WHERE username =");
sql1.add (user);
sql1.add (";");
SqliteDatabase sql2 (navigation);
sql2.add ("UPDATE navigation SET active = 1 WHERE rowid =");
sql2.add (id);
sql2.add (";");
// Read the passage.
SqliteDatabase sql3 (navigation);
sql3.add ("SELECT book, chapter, verse FROM navigation WHERE rowid =");
sql3.add (id);
sql3.add (";");
// Run all of the SQL at once, to minimize the database connection time.
sql1.execute ();
sql1.disconnect();
sql2.execute ();
sql2.disconnect();
std::map <std::string, std::vector <std::string> > result = sql3.query ();
sql3.disconnect();
const std::vector <std::string> books = result ["book"];
const std::vector <std::string> chapters = result ["chapter"];
const std::vector <std::string> verses = result ["verse"];
if (!books.empty()) {
Passage passage;
passage.m_book = filter::strings::convert_to_int (books [0]);
passage.m_chapter = filter::strings::convert_to_int (chapters [0]);
passage.m_verse = verses [0];
return passage;
}
return Passage ();
}
Passage Database_Navigation::get_next (const std::string& user)
{
int id = get_next_id (user);
if (id == 0) return Passage ();
// Update the 'active' flag.
SqliteDatabase sql1 (navigation);
sql1.add ("UPDATE navigation SET active = 0 WHERE username =");
sql1.add (user);
sql1.add (";");
SqliteDatabase sql2 (navigation);
sql2.add ("UPDATE navigation SET active = 1 WHERE rowid =");
sql2.add (id);
sql2.add (";");
// Read the passage.
SqliteDatabase sql3 (navigation);
sql3.add ("SELECT book, chapter, verse FROM navigation WHERE rowid =");
sql3.add (id);
sql3.add (";");
// Run all of the SQL at once, to minimize the database connection time.
sql1.execute ();
sql1.disconnect();
sql2.execute ();
sql2.disconnect();
std::map <std::string, std::vector <std::string> > result = sql3.query ();
sql3.disconnect();
const std::vector <std::string> books = result ["book"];
const std::vector <std::string> chapters = result ["chapter"];
const std::vector <std::string> verses = result ["verse"];
if (!books.empty()) {
Passage passage;
passage.m_book = filter::strings::convert_to_int (books [0]);
passage.m_chapter = filter::strings::convert_to_int (chapters [0]);
passage.m_verse = verses [0];
return passage;
}
return Passage ();
}
int Database_Navigation::get_previous_id (const std::string& user)
{
// Get the database row identifier of the active entry for the user.
int id = 0;
{
SqliteDatabase sql (navigation);
sql.add ("SELECT rowid FROM navigation WHERE username =");
sql.add (user);
sql.add ("AND active = 1;");
const std::vector <std::string> ids = sql.query () ["rowid"];
for (const auto& s : ids) {
id = filter::strings::convert_to_int (s);
}
}
// If no active row identifier was found, return zero.
if (id == 0) return 0;
// Get the database row identifier of the entry just before the active entry.
SqliteDatabase sql (navigation);
sql.add ("SELECT rowid FROM navigation WHERE rowid <");
sql.add (id);
sql.add ("AND username =");
sql.add (user);
sql.add ("ORDER BY rowid DESC LIMIT 1;");
const std::vector <std::string> ids = sql.query () ["rowid"];
if (!ids.empty()) {
return filter::strings::convert_to_int (ids.at(0));
}
// Nothing found.
return 0;
}
int Database_Navigation::get_next_id (const std::string& user)
{
// Get the database row identifier of the active entry for the user.
int id = 0;
{
SqliteDatabase sql (navigation);
sql.add ("SELECT rowid FROM navigation WHERE username =");
sql.add (user);
sql.add ("AND active = 1;");
const std::vector <std::string> ids = sql.query () ["rowid"];
for (const auto& s : ids) {
id = filter::strings::convert_to_int (s);
}
}
// If no active row identifier was found, return zero.
if (id == 0) return 0;
// Get the database row identifier of the entry just after the active entry.
SqliteDatabase sql (navigation);
sql.add ("SELECT rowid FROM navigation WHERE rowid >");
sql.add (id);
sql.add ("AND username =");
sql.add (user);
sql.add ("ORDER BY rowid ASC LIMIT 1;");
const std::vector <std::string> ids = sql.query () ["rowid"];
if (!ids.empty()) {
return filter::strings::convert_to_int (ids.at(0));
}
// Nothing found.
return 0;
}
// The $user for whom to get the history.
// The $direction into which to get the history:
// * negative: Get the past history as if going back.
// * positive: Get the future history as if going forward.
std::vector <Passage> Database_Navigation::get_history (const std::string& user, int direction)
{
std::vector <Passage> passages;
int id = 0;
if (direction > 0) id = get_next_id(user);
if (direction < 0) id = get_previous_id (user);
if (id) {
// Read the passages history for this user.
SqliteDatabase sql (navigation);
sql.add ("SELECT book, chapter, verse FROM navigation WHERE rowid");
if (direction > 0) sql.add (">=");
if (direction < 0) sql.add ("<=");
sql.add (id);
sql.add ("AND username =");
sql.add (user);
// Order the results depending on getting the history forward or backward.
sql.add ("ORDER BY rowid");
if (direction > 0) sql.add ("ASC");
if (direction < 0) sql.add ("DESC");
sql.add (";");
// Run the query on the database.
std::map <std::string, std::vector <std::string> > result = sql.query ();
// Assemble the results.
const std::vector <std::string> books = result ["book"];
const std::vector <std::string> chapters = result ["chapter"];
const std::vector <std::string> verses = result ["verse"];
for (unsigned int i = 0; i < books.size(); i++) {
Passage passage;
passage.m_book = filter::strings::convert_to_int (books [i]);
passage.m_chapter = filter::strings::convert_to_int (chapters [i]);
passage.m_verse = verses [i];
passages.push_back(passage);
}
}
// Done.
return passages;
}
|