database stuff

This commit is contained in:
kdeng00
2019-08-18 11:57:08 -04:00
parent 5230e69832
commit d26c16aa69
11 changed files with 211 additions and 48 deletions
+69 -2
View File
@@ -1,15 +1,18 @@
#include <exception>
#include <iostream>
#include <filesystem>
#include <memory>
#include <string>
#include "models.h"
#include <mysql/mysql.h>
#include "oatpp/network/server/Server.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "appComponent.hpp"
#include "controller/loginController.hpp"
//#include "loginHandler.hpp"
#include "database/base_repository.h"
namespace fs = std::filesystem;
@@ -35,13 +38,77 @@ void run(const std::string& working_path)
server.run();
}
MYSQL* mysql_connection_setup(database_connection mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server.c_str(), mysql_details.username.c_str(), mysql_details.password.c_str(), mysql_details.database.c_str(), 0, NULL, 0)) {
printf("Conection error : %s\n", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %s\n", mysql_error(connection));
exit(1);
}
return mysql_use_result(connection);
}
void test_database()
{
database_connection mysqlD;
mysqlD.server = ""; // where the mysql database is
mysqlD.username = ""; // the root user of mysql
mysqlD.password = ""; // the password of the root user in mysql
mysqlD.database = ""; // the databse to pick
base_repository base;
auto conn = base.setup_mysql_connection(mysqlD);
// assign the results return to the MYSQL_RES pointer
const std::string query = "SELECT *, NULL FROM Song";
auto res = base.perform_mysql_query(conn, query);
auto num_of_fields = mysql_num_fields(res);
printf("MySQL Tables in mysql database:\n");
auto row_count = 1;
for (MYSQL_ROW row = NULL; (row = mysql_fetch_row(res)) != NULL;) {
std::cout << " row " << row_count << std::endl;
for (auto i = 0; i != num_of_fields; ++i) {
auto val = row[i];
if ( val == NULL) {
std::cout << "found null value " << std::endl;
continue;
}
std::cout << val << " ";
}
std::cout << std::endl;
}
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
mysql_close(conn);
}
int main(int argc, char **argv)
{
oatpp::base::Environment::init();
std::string working_path = argv[0];
//std::cout << fs::canonical(fs::path(working_path.c_str())).parent_path().string() << std::endl;
test_database();
run(working_path);
oatpp::base::Environment::destroy();