This commit is contained in:
jie 2024-10-14 18:08:50 +08:00
commit c587572432
4 changed files with 190 additions and 0 deletions

1
build.sh Executable file
View File

@ -0,0 +1 @@
g++ -o demo main.cc -I/usr/include/mysql -lmysqlclient -std=c++23 -g

6
main.cc Normal file
View File

@ -0,0 +1,6 @@
#include "sqlConnection.h"
int main(int argc, char** const argv){
SqlConnection conn{};
return 0;
}

87
sqlConnection.h Normal file
View File

@ -0,0 +1,87 @@
#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>{};
};
};

96
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