diff --git a/confirm/worker.cpp b/confirm/worker.cpp index 273ec5ee..40735dd7 100644 --- a/confirm/worker.cpp +++ b/confirm/worker.cpp @@ -86,8 +86,7 @@ void setup (Webserver_Request& webserver_request, const std::string& query, const std::string& subsequent_subject, const std::string& subsequent_body) { - Database_Confirm database_confirm {}; - const unsigned int confirmation_id = database_confirm.get_new_id (); + const unsigned int confirmation_id = database::confirm::get_new_id (); pugi::xml_document document; pugi::xml_node node = document.append_child ("p"); std::string information; @@ -103,7 +102,7 @@ void setup (Webserver_Request& webserver_request, document.print (output, "", pugi::format_raw); initial_body.append (output.str ()); email_schedule (mailto, initial_subject, initial_body); - database_confirm.store (confirmation_id, query, mailto, subsequent_subject, subsequent_body, username); + database::confirm::store (confirmation_id, query, mailto, subsequent_subject, subsequent_body, username); } @@ -112,7 +111,7 @@ void setup (Webserver_Request& webserver_request, bool handle_link (Webserver_Request& webserver_request, std::string& email) { // Get the confirmation identifier from the link that was clicked. - std::string web_id = webserver_request.query["id"]; + const std::string web_id = webserver_request.query["id"]; // If the identifier was not given, the link was not handled successfully. if (web_id.empty()) @@ -120,24 +119,23 @@ bool handle_link (Webserver_Request& webserver_request, std::string& email) // Find out from the confirmation database whether the subject line contains an active ID. // If not, bail out. - Database_Confirm database_confirm {}; - const unsigned int id = database_confirm.search_id (web_id); + const unsigned int id = database::confirm::search_id (web_id); if (id == 0) { return false; } // An active ID was found: Execute the associated database query. - const std::string query = database_confirm.get_query (id); + const std::string query = database::confirm::get_query (id); webserver_request.database_users()->execute (query); // Send confirmation mail. - const std::string mailto = database_confirm.get_mail_to (id); - const std::string subject = database_confirm.get_subject (id); - const std::string body = database_confirm.get_body (id); + const std::string mailto = database::confirm::get_mail_to (id); + const std::string subject = database::confirm::get_subject (id); + const std::string body = database::confirm::get_body (id); email_schedule (mailto, subject, body); // Delete the confirmation record. - database_confirm.erase (id); + database::confirm::erase (id); // Notify the manager(s). confirm::worker::inform_managers (mailto, body); diff --git a/database/confirm.cpp b/database/confirm.cpp index fbbdad0b..e09491fd 100644 --- a/database/confirm.cpp +++ b/database/confirm.cpp @@ -33,15 +33,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef HAVE_CLOUD -const char * Database_Confirm::filename () // Todo no need for a class here. -{ - return "confirm"; -} +constexpr const char * filename ("confirm"); + + +namespace database::confirm { -void Database_Confirm::create () +void create () { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("CREATE TABLE IF NOT EXISTS confirm (" " id integer," " query text," @@ -54,13 +54,13 @@ void Database_Confirm::create () } -void Database_Confirm::upgrade () +void upgrade () { // Get the existing columns in the database. - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("PRAGMA table_info (confirm);"); - std::vector columns = sql.query () ["name"]; - + const std::vector columns = sql.query () ["name"]; + // Add the column for the username if it's not yet there. if (!in_array (static_cast ("username"), columns)) { sql.clear (); @@ -70,16 +70,16 @@ void Database_Confirm::upgrade () } -void Database_Confirm::optimize () +void optimize () { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("VACUUM;"); sql.execute (); } -// getNewID - returns a new unique confirmation ID as an integer -unsigned int Database_Confirm::get_new_id () +// Returns a new unique confirmation ID as an integer +unsigned int get_new_id () { unsigned int id = 0; do { @@ -90,23 +90,23 @@ unsigned int Database_Confirm::get_new_id () // Returns true if the $id exists -bool Database_Confirm::id_exists (unsigned int id) +bool id_exists (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT id FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector ids = sql.query () ["id"]; + const std::vector ids = sql.query () ["id"]; return !ids.empty (); } // Store a confirmation cycle -void Database_Confirm::store (unsigned int id, std::string query, - std::string to, std::string subject, std::string body, - std::string username) +void store (unsigned int id, const std::string& query, + const std::string& to, const std::string& subject, const std::string& body, + const std::string& username) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("INSERT INTO confirm VALUES ("); sql.add (static_cast(id)); sql.add (","); @@ -128,13 +128,13 @@ void Database_Confirm::store (unsigned int id, std::string query, // Search the database for an existing ID in $subject. // If it exists, it returns the ID number, else it returns 0. -unsigned int Database_Confirm::search_id (std::string subject) +unsigned int search_id (const std::string& subject) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT id FROM confirm;"); - std::vector ids = sql.query () ["id"]; - for (std::string id : ids) { - size_t pos = subject.find (id); + const std::vector ids = sql.query () ["id"]; + for (const auto& id : ids) { + const size_t pos = subject.find (id); if (pos != std::string::npos) { return static_cast(filter::strings::convert_to_int (id)); } @@ -144,74 +144,79 @@ unsigned int Database_Confirm::search_id (std::string subject) // Returns the query for $id. -std::string Database_Confirm::get_query (unsigned int id) +std::string get_query (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT query FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector result = sql.query () ["query"]; - if (!result.empty ()) return result [0]; + const std::vector result = sql.query () ["query"]; + if (!result.empty ()) + return result.front(); return std::string(); } // Returns the To: address for $id. -std::string Database_Confirm::get_mail_to (unsigned int id) +std::string get_mail_to (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT mailto FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector result = sql.query () ["mailto"]; - if (!result.empty ()) return result [0]; + const std::vector result = sql.query () ["mailto"]; + if (!result.empty ()) + return result.front(); return std::string(); } // Returns the Subject: for $id. -std::string Database_Confirm::get_subject (unsigned int id) +std::string get_subject (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT subject FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector result = sql.query () ["subject"]; - if (!result.empty ()) return result [0]; + const std::vector result = sql.query () ["subject"]; + if (!result.empty ()) + return result.front(); return std::string(); } // Returns the email's body for $id. -std::string Database_Confirm::get_body (unsigned int id) +std::string get_body (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT body FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector result = sql.query () ["body"]; - if (!result.empty ()) return result [0]; + const std::vector result = sql.query () ["body"]; + if (!result.empty ()) + return result.front(); return std::string(); } // Returns the username for $id. -std::string Database_Confirm::get_username (unsigned int id) // Test return valid and invalid username. +std::string get_username (unsigned int id) // Test return valid and invalid username. { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("SELECT username FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); - std::vector result = sql.query () ["username"]; - if (!result.empty ()) return result [0]; + const std::vector result = sql.query () ["username"]; + if (!result.empty ()) + return result.front(); return std::string(); } // Deletes $id from the table. -void Database_Confirm::erase (unsigned int id) +void erase (unsigned int id) { - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("DELETE FROM confirm WHERE id ="); sql.add (static_cast(id)); sql.add (";"); @@ -219,11 +224,11 @@ void Database_Confirm::erase (unsigned int id) } -void Database_Confirm::trim () +void trim () { // Leave entries for no more than 30 days. const int time = filter::date::seconds_since_epoch () - 2592000; - SqliteDatabase sql (filename ()); + SqliteDatabase sql (filename); sql.add ("DELETE FROM confirm WHERE timestamp <"); sql.add (time); sql.add (";"); @@ -231,4 +236,7 @@ void Database_Confirm::trim () } +} + + #endif diff --git a/database/confirm.h b/database/confirm.h index 898c2270..e9d6275c 100644 --- a/database/confirm.h +++ b/database/confirm.h @@ -25,26 +25,28 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef HAVE_CLOUD -class Database_Confirm -{ -public: - void create (); - void upgrade (); - void optimize (); - unsigned int get_new_id (); - bool id_exists (unsigned int id); - void store (unsigned int id, std::string query, std::string to, std::string subject, std::string body, std::string username); - unsigned int search_id (std::string subject); - std::string get_query (unsigned int id); - std::string get_mail_to (unsigned int id); - std::string get_subject (unsigned int id); - std::string get_body (unsigned int id); - std::string get_username (unsigned int id); - void erase (unsigned int id); - void trim (); -private: - const char * filename (); -}; +namespace database::confirm { + + +void create (); +void upgrade (); +void optimize (); +unsigned int get_new_id (); +bool id_exists (unsigned int id); +void store (unsigned int id, const std::string& query, + const std::string& to, const std::string& subject, const std::string& body, + const std::string& username); +unsigned int search_id (const std::string& subject); +std::string get_query (unsigned int id); +std::string get_mail_to (unsigned int id); +std::string get_subject (unsigned int id); +std::string get_body (unsigned int id); +std::string get_username (unsigned int id); +void erase (unsigned int id); +void trim (); + + +} #endif diff --git a/database/maintenance.cpp b/database/maintenance.cpp index 63a19064..7152214d 100644 --- a/database/maintenance.cpp +++ b/database/maintenance.cpp @@ -76,9 +76,8 @@ void database_maintenance () #ifdef HAVE_CLOUD - Database_Confirm database_confirm; - database_confirm.trim (); - database_confirm.optimize (); + database::confirm::trim (); + database::confirm::optimize (); #endif diff --git a/setup/logic.cpp b/setup/logic.cpp index aa8365a0..8db9f298 100644 --- a/setup/logic.cpp +++ b/setup/logic.cpp @@ -229,9 +229,8 @@ void setup_initialize_data () setup_generate_locale_databases (false); #ifdef HAVE_CLOUD config_globals_setup_message = "confirmations"; - Database_Confirm database_confirm; - database_confirm.create (); - database_confirm.upgrade(); + database::confirm::create (); + database::confirm::upgrade(); #endif config_globals_setup_message = "jobs"; Database_Jobs database_jobs = Database_Jobs (); diff --git a/unittests/confirm.cpp b/unittests/confirm.cpp index e72c566e..ca59c210 100644 --- a/unittests/confirm.cpp +++ b/unittests/confirm.cpp @@ -30,44 +30,43 @@ TEST (database, confirm) #ifdef HAVE_CLOUD refresh_sandbox (false); - Database_Confirm database_confirm; - database_confirm.create (); - database_confirm.upgrade(); - database_confirm.optimize (); - database_confirm.trim (); + database::confirm::create (); + database::confirm::upgrade(); + database::confirm::optimize (); + database::confirm::trim (); // New ID generation test. - unsigned int id = database_confirm.get_new_id (); + unsigned int id = database::confirm::get_new_id (); if (id < 10'000) EXPECT_EQ (std::string("Should be greater than 10000"), std::to_string(id)); // Store data for the ID. - database_confirm.store (id, "SELECT x, y, z FROM a;", "email", "subject", "body", "username"); + database::confirm::store (id, "SELECT x, y, z FROM a;", "email", "subject", "body", "username"); // Search for this ID based on subject. - unsigned int id2 = database_confirm.search_id ("Subject line CID" + std::to_string (id) + " Re:"); + unsigned int id2 = database::confirm::search_id ("Subject line CID" + std::to_string (id) + " Re:"); EXPECT_EQ (id, id2); // Retrieve data for the ID. - std::string query = database_confirm.get_query (id); + std::string query = database::confirm::get_query (id); EXPECT_EQ ("SELECT x, y, z FROM a;", query); - std::string to = database_confirm.get_mail_to (id); + std::string to = database::confirm::get_mail_to (id); EXPECT_EQ ("email", to); - std::string subject = database_confirm.get_subject (id); + std::string subject = database::confirm::get_subject (id); EXPECT_EQ ("subject", subject); - std::string body = database_confirm.get_body (id); + std::string body = database::confirm::get_body (id); EXPECT_EQ ("body", body); - std::string username = database_confirm.get_username(id); + std::string username = database::confirm::get_username(id); EXPECT_EQ ("username", username); - username = database_confirm.get_username(id + 1); + username = database::confirm::get_username(id + 1); EXPECT_EQ (std::string(), username); // Delete this ID. - database_confirm.erase (id); - query = database_confirm.get_query (id); + database::confirm::erase (id); + query = database::confirm::get_query (id); EXPECT_EQ ("", query); #endif