88 lines
2.3 KiB
C++
88 lines
2.3 KiB
C++
#include <optional>
|
|
#include <ranges>
|
|
#include <algorithm>
|
|
#include <string_view>
|
|
#include <expected>
|
|
|
|
#include "mysql.h"
|
|
|
|
#include "util.h"
|
|
|
|
using util::_support_string;
|
|
|
|
class SqlConnection{
|
|
private:
|
|
MYSQL mysql_client;
|
|
auto get_error_msg(){
|
|
return mysql_error(&mysql_client);
|
|
}
|
|
|
|
public:
|
|
char* ip;
|
|
char* port;
|
|
char* username;
|
|
char* password;
|
|
char* database_name;
|
|
|
|
SqlConnection(){
|
|
Init();
|
|
}
|
|
~SqlConnection() noexcept{
|
|
Close();
|
|
}
|
|
|
|
SqlConnection(const SqlConnection& other) = delete;
|
|
SqlConnection& operator=(const SqlConnection& other) = delete;
|
|
|
|
SqlConnection(SqlConnection&& other){
|
|
other.Close();
|
|
this->Connect(other.ip, other.port, other.username, other.password, other.database_name);
|
|
}
|
|
|
|
SqlConnection& operator=(SqlConnection&& other){
|
|
if(this != &other){
|
|
auto temp = SqlConnection{};
|
|
temp.Connect(other.ip, other.port, other.username, other.password, other.database_name);
|
|
other.Close();
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
void Init() noexcept {
|
|
mysql_init(&mysql_client);
|
|
}
|
|
|
|
std::expected<bool, std::string_view> Connect(std::string_view _ip, std::string_view _port = "3306", std::string_view _username = "root", std::string_view _password = "", std::string_view _database_name = "mysql"){
|
|
this->ip = util::to_char(_ip);
|
|
this->port = util::to_char(_port);
|
|
this->username = util::to_char(_username);
|
|
this->password = util::to_char(_password);
|
|
this->database_name = util::to_char(_database_name);
|
|
if(mysql_real_connect(&mysql_client, ip, username, password, database_name, std::stoi(port), nullptr, 0) == nullptr){
|
|
auto error_msg = get_error_msg();
|
|
return std::unexpected(error_msg);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Close() noexcept {
|
|
mysql_close(&mysql_client);
|
|
}
|
|
|
|
std::optional<std::vector<std::string_view>> Query(std::string_view command){
|
|
//Not Implemented;
|
|
return std::nullopt;
|
|
}
|
|
|
|
bool Execute(std::string_view command){
|
|
//Not Implemented;
|
|
return false;
|
|
}
|
|
|
|
std::vector<std::string_view> GetTables(){
|
|
//Not Implemented;
|
|
return std::vector<std::string_view>{};
|
|
};
|
|
|
|
};
|