use xmake to build project
This commit is contained in:
160
include/sqlConnection.h
Normal file
160
include/sqlConnection.h
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <optional>
|
||||
#include <ranges>
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <expected>
|
||||
#include <format>
|
||||
#include <map>
|
||||
|
||||
#include "mysql.h"
|
||||
|
||||
#include "util.h"
|
||||
|
||||
using util::_support_string;
|
||||
|
||||
struct RowItem{
|
||||
std::vector<std::string_view> row;
|
||||
};
|
||||
|
||||
//Not Implemented;
|
||||
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 %s;", 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);
|
||||
auto row = mysql_fetch_row(mysql_result);
|
||||
for(int i=0; i<field_length; i++){
|
||||
really_result.emplace_back(row[i]);
|
||||
}
|
||||
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){
|
||||
//Not Implemented;
|
||||
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;
|
||||
}
|
||||
|
||||
bool Execute(std::string_view command){
|
||||
//Not Implemented;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string_view> GetTables(){
|
||||
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;
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user