2020-12-01 15:28:59 +01:00
|
|
|
#include "mysql_database.h"
|
|
|
|
|
2020-12-01 18:29:41 +01:00
|
|
|
#include <cstdio>
|
|
|
|
|
2020-12-01 15:28:59 +01:00
|
|
|
#include "core/database_manager.h"
|
|
|
|
|
2020-12-01 21:48:13 +01:00
|
|
|
|
2020-12-01 18:29:41 +01:00
|
|
|
void MysqlDatabase::connect(const std::string &connection_str) {
|
|
|
|
mysql = mysql_init(mysql);
|
|
|
|
mysql_options(mysql, MYSQL_OPT_NONBLOCK, 0);
|
|
|
|
|
|
|
|
std::string host = "127.0.0.1";
|
|
|
|
std::string user = "";
|
|
|
|
std::string password = "";
|
|
|
|
std::string dbname = "testappdb";
|
|
|
|
int port = 3306;
|
|
|
|
|
|
|
|
mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0);
|
|
|
|
|
|
|
|
if (mysql) {
|
|
|
|
printf("mysql connected\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlDatabase::query(const std::string &query) {
|
|
|
|
if (!mysql)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%s\n", query.c_str());
|
|
|
|
int error = mysql_real_query(mysql, query.c_str(), query.length());
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
const char *merr = mysql_error(mysql);
|
|
|
|
|
|
|
|
printf("MySQL error: %s\n", merr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("----------------\n");
|
|
|
|
|
|
|
|
MYSQL_RES *result = mysql_use_result(mysql);
|
|
|
|
//MYSQL_RES *result = mysql_store_result(mysql);
|
|
|
|
|
|
|
|
MYSQL_ROW row;
|
|
|
|
while (row = mysql_fetch_row(result)) {
|
|
|
|
printf("%s\n", row[0]);
|
|
|
|
//printf("%s\n", row[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("----------------\n");
|
|
|
|
|
|
|
|
mysql_free_result(result);
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:48:13 +01:00
|
|
|
/*
|
|
|
|
std::shared_ptr<QueryBuilder> MysqlDatabase::get_builder() {
|
|
|
|
//return std::make_shared<MysqlQueryBuilder>();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
*/
|
2020-12-01 18:29:41 +01:00
|
|
|
MysqlDatabase::MysqlDatabase() :
|
|
|
|
Database() {
|
|
|
|
|
|
|
|
//mysql = new MYSQL();
|
|
|
|
mysql = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MysqlDatabase::~MysqlDatabase() {
|
|
|
|
mysql_close(mysql);
|
|
|
|
|
|
|
|
delete mysql;
|
|
|
|
}
|
|
|
|
|
2020-12-01 15:28:59 +01:00
|
|
|
Database *MysqlDatabase::_creation_func() {
|
|
|
|
return new MysqlDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlDatabase::_register() {
|
|
|
|
DatabaseManager::_register_db_creation_func("mysql", MysqlDatabase::_creation_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MysqlDatabase::_unregister() {
|
|
|
|
DatabaseManager::_unregister_db_creation_func("mysql");
|
|
|
|
}
|