178 lines
5.2 KiB
C++
178 lines
5.2 KiB
C++
#include <exception>
|
|
#define DEBUG
|
|
|
|
#ifdef DEBUG
|
|
#include <iostream>
|
|
#endif
|
|
|
|
#include <optional>
|
|
#include <ranges>
|
|
#include <algorithm>
|
|
#include <string_view>
|
|
#include <expected>
|
|
#include <format>
|
|
#include <map>
|
|
|
|
#include "mysql.h"
|
|
|
|
#include "util.h"
|
|
|
|
template<typename T>
|
|
void log(T msg){
|
|
#ifdef DEBUG
|
|
std::cout<<msg<<std::endl;
|
|
#endif
|
|
}
|
|
|
|
using util::_support_string;
|
|
|
|
[[maybe_unused]]
|
|
struct RowItem{
|
|
std::vector<std::string_view> row;
|
|
};
|
|
|
|
//Not Implemented;
|
|
[[maybe_unused]]
|
|
class Result{
|
|
std::vector<std::string_view> fields;
|
|
std::map<std::string_view, std::vector<std::string_view>> datas;
|
|
|
|
public:
|
|
//get size;
|
|
size_t size() const{
|
|
size_t size = 0;
|
|
for(auto&& [key, value] : datas){
|
|
if(datas.size() > size) size = datas.size();
|
|
}
|
|
return size;
|
|
}
|
|
//return fields
|
|
const std::vector<std::string_view>& get_fields() const {
|
|
return fields;
|
|
}
|
|
|
|
};
|
|
|
|
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){
|
|
return std::unexpected(get_error_msg());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Close() noexcept {
|
|
mysql_close(&mysql_client);
|
|
}
|
|
|
|
std::vector<std::string_view> get_fields(std::string_view table_name){
|
|
std::vector<std::string_view> really_result;
|
|
std::string command;
|
|
std::format_to(std::back_inserter(command), "SHOW COLUMNS FROM {};", table_name);
|
|
auto _ = mysql_query(&mysql_client, command.c_str());
|
|
if(auto mysql_result = mysql_store_result(&mysql_client); mysql_result != nullptr){
|
|
auto field_length = mysql_num_fields(mysql_result);
|
|
while(auto row = mysql_fetch_row(mysql_result)){
|
|
really_result.emplace_back(row[0]);
|
|
}
|
|
mysql_free_result(mysql_result);
|
|
}
|
|
return really_result;
|
|
}
|
|
|
|
std::expected<std::vector<std::vector<std::string_view>>, std::string_view> Query(std::string_view command){
|
|
std::vector<std::vector<std::string_view>> really_result;
|
|
mysql_query(&mysql_client, command.data());
|
|
if(auto mysql_result = mysql_store_result(&mysql_client); mysql_result != nullptr){
|
|
auto row_length = mysql_fetch_lengths(mysql_result);
|
|
auto field_length = mysql_num_fields(mysql_result);
|
|
std::vector<std::string_view> temp;
|
|
while(auto row = mysql_fetch_row(mysql_result)){
|
|
temp.clear();
|
|
for(int i=0; i<field_length; i++){
|
|
if(row[i] != nullptr){
|
|
temp.emplace_back(row[i]);
|
|
}else{
|
|
temp.emplace_back("");
|
|
}
|
|
|
|
}
|
|
really_result.emplace_back(temp);
|
|
}
|
|
|
|
mysql_free_result(mysql_result);
|
|
return really_result;
|
|
}
|
|
|
|
return std::unexpected(get_error_msg());
|
|
}
|
|
|
|
std::expected<bool> Execute(std::string_view command){
|
|
auto res = mysql_query(&mysql_client, command.data());
|
|
if(!res)
|
|
return std::unexpected(get_error_msg());
|
|
return res;
|
|
}
|
|
|
|
std::vector<std::string_view> get_tables(){
|
|
std::vector<std::string_view> really_result;
|
|
mysql_query(&mysql_client, "SHOW TABLES;");
|
|
if(auto mysql_result = mysql_store_result(&mysql_client); mysql_result != nullptr){
|
|
auto row_length = mysql_fetch_lengths(mysql_result);
|
|
while(auto row = mysql_fetch_row(mysql_result)){
|
|
really_result.emplace_back(row[0]);
|
|
}
|
|
mysql_free_result(mysql_result);
|
|
}
|
|
|
|
return really_result;
|
|
};
|
|
|
|
};
|