diff --git a/src/database.cpp b/src/database.cpp index 3d980a9..23f5fa6 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -111,6 +111,13 @@ namespace Database { */ const static std::string PREPARED_STATEMENT_UPDATE_LOGIN_VALIDATION_KEY = "updateLoginValidationKey"; const static std::string SQL_STATEMENT_UPDATE_LOGIN_VALIDATION_KEY = "update freelancers set loginvalidationkey = $1 where emailaddress = $2;"; + + /* + * Name and Statement for prepared statement to update the passwordhash of a freelancer via the freelancerEmail + */ + const static std::string PREPARED_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH = "updateFreelancerPasswordHash"; + const static std::string SQL_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH = "update freelancers set hash = $1, salt = $2 where emailaddress = $3;"; + /* * Selects freelancers, their basicInfo and if the commissions are closed/open ordered by name. * Delivers if the commission limit has been reached and if the commissions are closed based on the accepted/completed state of the freelancers requests @@ -479,6 +486,34 @@ namespace Database { return result; } + /* + * Executes the prepared statement UPDATE_FREELANCER_PASSWORD_HASH + * Takes an open pqxx::connection, the freelancer email + * returns errorLevel + * 0 = no error + * 1 = query error + * 2 = critical error + */ + int executePreparedStatement_UPDATE_FREELANCER_PASSWORD_HASH(pqxx::connection &connection, const std::string& hash, const std::string& salt, const std::string& freelancerEmail) { + try { + connection.prepare(PREPARED_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH, SQL_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH); + pqxx::work work(connection); + pqxx::result result = work.exec_prepared(PREPARED_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH, hash, salt, freelancerEmail); + work.commit(); + } + catch (pqxx::sql_error const &e) { + std::cerr + << "Database error: " << e.what() << std::endl + << "Query was: " << e.query() << std::endl; + return 1; + } + catch (std::exception const &e) { + std::cerr << e.what() << std::endl; + return 2; + } + connection.unprepare(PREPARED_STATEMENT_UPDATE_FREELANCER_PASSWORD_HASH); + return 0; + } /* * parses the result and returns a JSON * Takes a result and optionally the desired row