sqlconn/include/sqlConnection.h

177 lines
5.1 KiB
C
Raw Normal View History

#include <exception>
2024-11-22 17:14:20 +08:00
#define DEBUG
#ifdef DEBUG
#include <iostream>
#endif
2024-10-14 18:08:50 +08:00
#include <optional>
#include <ranges>
#include <algorithm>
#include <string_view>
#include <expected>
2024-10-23 18:45:36 +08:00
#include <format>
#include <map>
2024-10-14 18:08:50 +08:00
#include "mysql.h"
#include "util.h"
2024-11-22 17:14:20 +08:00
template<typename T>
void log(T msg){
#ifdef DEBUG
std::cout<<msg<<std::endl;
#endif
}
2024-10-14 18:08:50 +08:00
using util::_support_string;
2024-11-22 17:14:20 +08:00
[[maybe_unused]]
2024-10-23 18:45:36 +08:00
struct RowItem{
std::vector<std::string_view> row;
};
//Not Implemented;
2024-11-22 17:14:20 +08:00
[[maybe_unused]]
2024-10-23 18:45:36 +08:00
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;
}
};
2024-10-14 18:08:50 +08:00
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){
2024-10-23 18:45:36 +08:00
return std::unexpected(get_error_msg());
2024-10-14 18:08:50 +08:00
}
return true;
}
void Close() noexcept {
mysql_close(&mysql_client);
}
2024-10-23 18:45:36 +08:00
std::vector<std::string_view> get_fields(std::string_view table_name){
std::vector<std::string_view> really_result;
std::string command;
2024-11-22 17:14:20 +08:00
std::format_to(std::back_inserter(command), "SHOW COLUMNS FROM {};", table_name);
2024-10-23 18:45:36 +08:00
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);
2024-11-22 17:14:20 +08:00
while(auto row = mysql_fetch_row(mysql_result)){
really_result.emplace_back(row[0]);
2024-10-23 18:45:36 +08:00
}
mysql_free_result(mysql_result);
}
return really_result;
}
2024-10-24 14:14:12 +08:00
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;
2024-10-23 18:45:36 +08:00
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);
2024-10-24 14:14:12 +08:00
std::vector<std::string_view> temp;
2024-10-23 18:45:36 +08:00
while(auto row = mysql_fetch_row(mysql_result)){
2024-10-24 14:14:12 +08:00
temp.clear();
2024-10-23 18:45:36 +08:00
for(int i=0; i<field_length; i++){
2024-10-24 14:14:12 +08:00
if(row[i] != nullptr){
temp.emplace_back(row[i]);
}else{
temp.emplace_back("");
}
2024-10-23 18:45:36 +08:00
}
2024-10-24 14:14:12 +08:00
really_result.emplace_back(temp);
2024-10-23 18:45:36 +08:00
}
mysql_free_result(mysql_result);
}
2024-10-24 14:14:12 +08:00
return really_result;
2024-10-14 18:08:50 +08:00
}
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;
2024-10-14 18:08:50 +08:00
}
2024-11-22 17:14:20 +08:00
std::vector<std::string_view> get_tables(){
2024-10-23 18:45:36 +08:00
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;
2024-10-14 18:08:50 +08:00
};
};