Skip to content

Commit

Permalink
Better database esthetics
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jun 14, 2024
1 parent a57a800 commit b80f24d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 100 deletions.
111 changes: 44 additions & 67 deletions database/modifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// It is re-indexed every night.


const char * Database_Modifications::filename ()
{
return "modifications";
}
constexpr const char * filename {"modifications"};


sqlite3 * Database_Modifications::connect ()
{
return database::sqlite::connect (filename ());
return database::sqlite::connect (filename);
}


Expand All @@ -54,21 +51,19 @@ void Database_Modifications::erase ()

void Database_Modifications::create ()
{
sqlite3 * db = connect ();
std::string sql =
"CREATE TABLE IF NOT EXISTS notifications ("
" identifier integer,"
" timestamp integer,"
" username text,"
" category text,"
" bible text,"
" book integer,"
" chapter integer,"
" verse integer,"
" modification text"
");";
database::sqlite::exec (db, sql);
database::sqlite::disconnect (db);
SqliteDatabase sql (filename);
sql.set_sql ("CREATE TABLE IF NOT EXISTS notifications ("
" identifier integer,"
" timestamp integer,"
" username text,"
" category text,"
" bible text,"
" book integer,"
" chapter integer,"
" verse integer,"
" modification text"
");");
sql.execute ();
}


Expand All @@ -80,9 +75,9 @@ bool Database_Modifications::healthy ()

void Database_Modifications::vacuum ()
{
sqlite3 * db = connect ();
database::sqlite::exec (db, "VACUUM;");
database::sqlite::disconnect (db);
SqliteDatabase sql (filename);
sql.set_sql ("VACUUM;");
sql.execute ();
}


Expand Down Expand Up @@ -688,7 +683,7 @@ std::vector <int> Database_Modifications::getNotificationIdentifiers (std::strin
{
std::vector <int> ids;

SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT identifier FROM notifications WHERE 1");
if (username != "") {
sql.add ("AND username =");
Expand All @@ -705,9 +700,7 @@ std::vector <int> Database_Modifications::getNotificationIdentifiers (std::strin
if (sort_on_category) sql.add ("category ASC,");
sql.add ("book ASC, chapter ASC, verse ASC, identifier ASC;");

sqlite3 * db = connect ();
std::vector <std::string> sidentifiers = database::sqlite::query (db, sql.sql) ["identifier"];
database::sqlite::disconnect (db);
const std::vector <std::string> sidentifiers = sql.query () ["identifier"];
for (auto & identifier : sidentifiers) {
ids.push_back (filter::strings::convert_to_int (identifier));
}
Expand All @@ -720,7 +713,7 @@ std::vector <int> Database_Modifications::getNotificationIdentifiers (std::strin
std::vector <int> Database_Modifications::getNotificationTeamIdentifiers (const std::string& username, const std::string& category, std::string bible)
{
std::vector <int> ids;
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT identifier FROM notifications WHERE username =");
sql.add (username);
sql.add ("AND category =");
Expand All @@ -730,10 +723,8 @@ std::vector <int> Database_Modifications::getNotificationTeamIdentifiers (const
sql.add (bible);
}
sql.add ("ORDER BY book ASC, chapter ASC, verse ASC, identifier ASC;");
sqlite3 * db = connect ();
std::vector <std::string> sidentifiers = database::sqlite::query (db, sql.sql) ["identifier"];
database::sqlite::disconnect (db);
for (auto & sid : sidentifiers) {
const std::vector <std::string> sidentifiers = sql.query () ["identifier"];
for (const auto& sid : sidentifiers) {
ids.push_back (filter::strings::convert_to_int (sid));
}
return ids;
Expand All @@ -743,18 +734,14 @@ std::vector <int> Database_Modifications::getNotificationTeamIdentifiers (const
// This gets the distinct Bibles in the user's notifications.
std::vector <std::string> Database_Modifications::getNotificationDistinctBibles (std::string username)
{
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT DISTINCT bible FROM notifications WHERE 1");
if (username != "") {
sql.add ("AND username =");
sql.add (username);
}
sql.add (";");

sqlite3 * db = connect ();
std::vector <std::string> bibles = database::sqlite::query (db, sql.sql) ["bible"];
database::sqlite::disconnect (db);

const std::vector <std::string> bibles = sql.query () ["bible"];
return bibles;
}

Expand All @@ -780,15 +767,13 @@ void Database_Modifications::deleteNotification (int identifier, sqlite3 * db)

int Database_Modifications::getNotificationTimeStamp (int id)
{
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT timestamp FROM notifications WHERE identifier =");
sql.add (id);
sql.add (";");
sqlite3 * db = connect ();
std::vector <std::string> timestamps = database::sqlite::query (db, sql.sql) ["timestamp"];
database::sqlite::disconnect (db);
const std::vector <std::string> timestamps = sql.query () ["timestamp"];
int time = filter::date::seconds_since_epoch ();
for (auto & stamp : timestamps) {
for (const auto& stamp : timestamps) {
time = filter::strings::convert_to_int (stamp);
}
return time;
Expand All @@ -797,15 +782,13 @@ int Database_Modifications::getNotificationTimeStamp (int id)

std::string Database_Modifications::getNotificationCategory (int id)
{
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT category FROM notifications WHERE identifier =");
sql.add (id);
sql.add (";");
sqlite3 * db = connect ();
std::vector <std::string> categories = database::sqlite::query (db, sql.sql) ["category"];
database::sqlite::disconnect (db);
std::string category = "";
for (auto & row : categories) {
const std::vector <std::string> categories = sql.query () ["category"];
std::string category {};
for (const auto & row : categories) {
category = row;
}
return category;
Expand All @@ -814,15 +797,13 @@ std::string Database_Modifications::getNotificationCategory (int id)

std::string Database_Modifications::getNotificationBible (int id)
{
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT bible FROM notifications WHERE identifier =");
sql.add (id);
sql.add (";");
sqlite3 * db = connect ();
std::vector <std::string> bibles = database::sqlite::query (db, sql.sql) ["bible"];
database::sqlite::disconnect (db);
std::string bible = "";
for (auto & item : bibles) {
const std::vector <std::string> bibles = sql.query () ["bible"];
std::string bible {};
for (const auto& item : bibles) {
bible = item;
}
return bible;
Expand All @@ -832,16 +813,14 @@ std::string Database_Modifications::getNotificationBible (int id)
Passage Database_Modifications::getNotificationPassage (int id)
{
Passage passage;
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("SELECT book, chapter, verse FROM notifications WHERE identifier =");
sql.add (id);
sql.add (";");
sqlite3 * db = connect ();
std::map <std::string, std::vector <std::string> > result = database::sqlite::query (db, sql.sql);
database::sqlite::disconnect (db);
std::vector <std::string> books = result ["book"];
std::vector <std::string> chapters = result ["chapter"];
std::vector <std::string> verses = result ["verse"];
std::map <std::string, std::vector <std::string> > result = sql.query ();
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.m_book = filter::strings::convert_to_int (books [i]);
passage.m_chapter = filter::strings::convert_to_int (chapters [i]);
Expand Down Expand Up @@ -1049,8 +1028,7 @@ void Database_Modifications::storeClientNotification (int id, std::string userna
sql.execute ();
}
{
sqlite3 * db = connect ();
SqliteSQL sql = SqliteSQL ();
SqliteDatabase sql (filename);
sql.add ("INSERT INTO notifications VALUES (");
sql.add (id);
sql.add (",");
Expand All @@ -1070,8 +1048,7 @@ void Database_Modifications::storeClientNotification (int id, std::string userna
sql.add (",");
sql.add (modification);
sql.add (");");
database::sqlite::exec (db, sql.sql);
database::sqlite::disconnect (db);
sql.execute ();
}
}

Expand Down Expand Up @@ -1106,7 +1083,7 @@ void Database_Modifications::deleteNotificationFile (int identifier)

std::vector <std::string> Database_Modifications::getCategories ()
{
SqliteDatabase sql (filename ());
SqliteDatabase sql (filename);
sql.add ("SELECT DISTINCT category FROM notifications ORDER BY category;");
std::vector <std::string> categories = sql.query ()["category"];
return categories;
Expand Down
1 change: 0 additions & 1 deletion database/modifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class Database_Modifications
void notificationUpdateTime (int identifier, int timestamp);
std::vector <std::string> getCategories ();
private:
const char * filename ();
sqlite3 * connect ();
std::string teamFolder ();
std::string teamFile (const std::string& bible, int book, int chapter);
Expand Down
56 changes: 25 additions & 31 deletions database/navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
constexpr const auto navigation {"navigation"};


sqlite3 * Database_Navigation::connect ()
{
return database::sqlite::connect ("navigation");
}


void Database_Navigation::create ()
{
SqliteDatabase sql (navigation);
Expand Down Expand Up @@ -120,32 +114,32 @@ Passage Database_Navigation::get_previous (const std::string& user)
if (id == 0) return Passage ();

// Update the 'active' flag.
SqliteSQL sql1 = SqliteSQL ();
SqliteDatabase sql1 (navigation);
sql1.add ("UPDATE navigation SET active = 0 WHERE username =");
sql1.add (user);
sql1.add (";");
SqliteSQL sql2 = SqliteSQL ();
SqliteDatabase sql2 (navigation);
sql2.add ("UPDATE navigation SET active = 1 WHERE rowid =");
sql2.add (id);
sql2.add (";");

// Read the passage.
std::map <std::string, std::vector <std::string> > result;
SqliteSQL sql3 = SqliteSQL ();
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.
sqlite3 * db = connect ();
database::sqlite::exec (db, sql1.sql);
database::sqlite::exec (db, sql2.sql);
result = database::sqlite::query (db, sql3.sql);
database::sqlite::disconnect (db);
sql1.execute ();
sql1.disconnect();
sql2.execute ();
sql2.disconnect();
std::map <std::string, std::vector <std::string> > result = sql3.query ();
sql3.disconnect();

std::vector <std::string> books = result ["book"];
std::vector <std::string> chapters = result ["chapter"];
std::vector <std::string> verses = result ["verse"];
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]);
Expand All @@ -163,32 +157,32 @@ Passage Database_Navigation::get_next (const std::string& user)
if (id == 0) return Passage ();

// Update the 'active' flag.
SqliteSQL sql1 = SqliteSQL ();
SqliteDatabase sql1 (navigation);
sql1.add ("UPDATE navigation SET active = 0 WHERE username =");
sql1.add (user);
sql1.add (";");
SqliteSQL sql2 = SqliteSQL ();
SqliteDatabase sql2 (navigation);
sql2.add ("UPDATE navigation SET active = 1 WHERE rowid =");
sql2.add (id);
sql2.add (";");

// Read the passage.
std::map <std::string, std::vector <std::string> > result;
SqliteSQL sql3 = SqliteSQL ();
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.
sqlite3 * db = connect ();
database::sqlite::exec (db, sql1.sql);
database::sqlite::exec (db, sql2.sql);
result = database::sqlite::query (db, sql3.sql);
database::sqlite::disconnect (db);
// 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();

std::vector <std::string> books = result ["book"];
std::vector <std::string> chapters = result ["chapter"];
std::vector <std::string> verses = result ["verse"];
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]);
Expand Down
1 change: 0 additions & 1 deletion database/navigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Database_Navigation
Passage get_next (const std::string& user);
std::vector <Passage> get_history (const std::string& user, int direction);
private:
sqlite3 * connect ();
int get_previous_id (const std::string& user);
int get_next_id (const std::string& user);
};

0 comments on commit b80f24d

Please sign in to comment.