use xmake to build project

This commit is contained in:
jie
2024-11-22 16:31:33 +08:00
parent 100e689d1b
commit 31266b363e
8 changed files with 86 additions and 1 deletions

160
include/sqlConnection.h Normal file
View 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;
};
};

96
include/util.h Normal file
View File

@@ -0,0 +1,96 @@
#ifndef UTIL_H
#define UTIL_H
#include <type_traits>
#include <string>
#include <string_view>
#include <vector>
#include <ranges>
namespace util{
namespace ranges = std::ranges;
namespace views = std::ranges::views;
//util template
// find the most strong type,
template<typename... Types>
struct _strong_type{};
template<typename T>
struct _strong_type<T>{
using type = T;
};
template<typename Ta, typename Tb>
struct _strong_type<Ta, Tb>{
using type = decltype(true ? std::declval<Ta>() : std::declval<Tb>());
};
template<typename T, typename... args>
struct _strong_type<T, args...>{
using type =
typename _strong_type<T, typename _strong_type<args...>::type>::type;
};
template<typename... args>
using _strong_type_t = typename _strong_type<args...>::type;
//can type T convert to Args...
template<typename... Types>
struct _is_castable{};
template<typename T>
struct _is_castable<T>{
static constexpr bool value = true;
using type = T;
};
template<typename T, typename U>
struct _is_castable<T, U>{
using __true = char;
using __false = struct {char _[2];};
static consteval __true __test(U);
static consteval __false __test(...);
static constexpr bool value = sizeof(__test(std::declval<T>())) == sizeof(__true);
using type = std::conditional<value, U, void>;
};
template<typename T, typename... args>
struct _is_castable<T, args...>{
static constexpr bool value = _is_castable<T, typename _is_castable<args...>::type>::value;
using type = std::conditional<value, T, void>;
};
template<typename... args>
using _is_castable_v = _is_castable<args...>::value;
//type can convert to const char*, char* std::string, std::string_view;
template<typename T>
concept _support_string = requires{
_is_castable<T, const char*, char*, std::string, std::string_view>::value;
};
//util function
template<_support_string T>
std::vector<std::string_view> split_str(T str, T d){
auto view = views::split(str, d) | views::transform([](auto word){return std::string_view{word.begin(), word.end()};});
return std::vector<std::string_view>{view.begin(), view.end()};
};
template<_support_string T>
char* to_char(T str){
if constexpr (std::is_same_v<T, const char*> || std::is_same_v<T, char>){
return const_cast<char*>(str);
}
if constexpr (std::is_same_v<T, std::string>){
return const_cast<char*>(str.c_str());
}
if constexpr (std::is_same_v<T, std::string_view>){
return const_cast<char*>(str.data());
}
}
};
#endif