diff --git a/CMakeLists.txt b/CMakeLists.txt index 58f5efd..8132a8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,8 +65,9 @@ add_subdirectory(3rdparty/lua) add_subdirectory(3rdparty/sqlite3) if(MSVC) add_subdirectory(3rdparty/libzip) -endif() add_subdirectory(3rdparty/leveldb) +endif() + diff --git a/build.sh b/build.sh index 74688c4..699a2aa 100644 --- a/build.sh +++ b/build.sh @@ -8,6 +8,8 @@ sudo apt install -y libssl-dev sudo apt install -y xorg libx11-dev libgl1-mesa-dev sudo apt install -y openssl sudo apt install -y libboost-dev +sudo apt install -y libmysqlcppconn-dev +sudo apt install -y libleveldb-dev # 初始化变量 src_dir=$(pwd) diff --git a/include/db/mssql.h b/include/db/mssql.h index 5aaf9f9..6bf1dc2 100644 --- a/include/db/mssql.h +++ b/include/db/mssql.h @@ -19,7 +19,7 @@ namespace ylib::mssql class prepare_statement; struct mssql_conn_info { - mssql_conn_info(){ + mssql_conn_info() { } std::string odbcname; std::string username; @@ -68,19 +68,19 @@ namespace ylib::mssql std::map m_field_name_index; }; - class prepare_statement:public ylib::error_base{ + class prepare_statement :public ylib::error_base { public: prepare_statement(); ~prepare_statement(); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); + void set_boolean(uint32 index, bool value); + void set_datetime(uint32 index, const std::string& value); + void set_double(uint32 index, double value); + void set_int32(uint32 index, int32 value); + void set_uint32(uint32 index, uint32 value); + void set_int64(uint32 index, int64 value); + void set_uint64(uint32 index, uint64 value); void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); + void set_string(uint32 index, const std::string& value); void clear(); uint64 update(); ylib::mssql::result* query(); @@ -90,12 +90,12 @@ namespace ylib::mssql ylib::mssql::result* m_result = nullptr; void* m_handle = nullptr; }; - class conn :public ylib::example,public ylib::error_base + class conn :public ylib::example, public ylib::error_base { public: conn(); ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mssql::mssql_conn_info&info) override; + virtual EXAMPLE_START_RESULT start(const ylib::mssql::mssql_conn_info& info) override; virtual void close() override; @@ -110,14 +110,14 @@ namespace ylib::mssql void rollback(); friend class ylib::mssql::pool; private: - void *m_handle = nullptr; + void* m_handle = nullptr; ylib::mssql::mssql_conn_info m_info; class prepare_statement* m_ppst = nullptr; int m_sw = 0; }; - class pool: public ylib::pool + class pool : public ylib::pool { public: pool(); diff --git a/include/db/mysql.h b/include/db/mysql.h index 1518691..96c3d68 100644 --- a/include/db/mysql.h +++ b/include/db/mysql.h @@ -17,14 +17,14 @@ namespace ylib::mysql class result; class conn; class prepare_statement; - struct field{ + struct field { uint32 index = 0; std::string name; std::string type_name; }; struct mysql_conn_info { - mysql_conn_info(){ + mysql_conn_info() { port = 0; } std::string ipaddress; @@ -81,21 +81,21 @@ namespace ylib::mysql std::vector m_fields; }; - class prepare_statement:public ylib::error_base{ + class prepare_statement :public ylib::error_base { public: prepare_statement(); ~prepare_statement(); - void set_bigint(uint32 index,const std::string& value); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); + void set_bigint(uint32 index, const std::string& value); + void set_boolean(uint32 index, bool value); + void set_datetime(uint32 index, const std::string& value); + void set_double(uint32 index, double value); + void set_int32(uint32 index, int32 value); + void set_uint32(uint32 index, uint32 value); + void set_int64(uint32 index, int64 value); + void set_uint64(uint32 index, uint64 value); void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); - void set_blob(uint32 index,const ylib::buffer& value); + void set_string(uint32 index, const std::string& value); + void set_blob(uint32 index, const ylib::buffer& value); void clear(); uint64 update(); ylib::mysql::result* query(); @@ -105,12 +105,12 @@ namespace ylib::mysql ylib::mysql::result* m_result = nullptr; void* m_handle = nullptr; }; - class conn :public ylib::example,public ylib::error_base + class conn :public ylib::example, public ylib::error_base { public: conn(); ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mysql::mysql_conn_info&info) override; + virtual EXAMPLE_START_RESULT start(const ylib::mysql::mysql_conn_info& info) override; virtual void close() override; @@ -130,7 +130,7 @@ namespace ylib::mysql friend class ylib::mysql::pool; private: - void *m_handle = nullptr; + void* m_handle = nullptr; ylib::mysql::mysql_conn_info m_info; class prepare_statement* m_ppst = nullptr; // 事务状态 0=未开启 1=已开启 2=执行完毕 @@ -138,7 +138,7 @@ namespace ylib::mysql }; - class pool: public ylib::pool + class pool : public ylib::pool { public: pool() diff --git a/include/db/sqler.h b/include/db/sqler.h index e026ac7..274532c 100644 --- a/include/db/sqler.h +++ b/include/db/sqler.h @@ -21,7 +21,7 @@ namespace ylib }; struct order_by { std::string field; - sort sort; + ylib::sort sort; }; struct keyvalue { std::string name; @@ -31,7 +31,7 @@ namespace ylib /// /// MYSQL SELECT 查询器 /// - class select:public ylib::error_base { + class select :public ylib::error_base { public: select(mysql::conn* conn); ~select(); @@ -51,11 +51,11 @@ namespace ylib /// /// 条件[A=B] /// - select& where(const std::string& name,const std::string& expression,const std::any& value); + select& where(const std::string& name, const std::string& expression, const std::any& value); /// /// 条件[模糊查询] /// - select& where_like(const std::string& name,const std::string& value); + select& where_like(const std::string& name, const std::string& value); /// /// 条件[自定义] /// @@ -63,7 +63,7 @@ namespace ylib /// /// LIMIT[页] /// - select& page(uint32 page,uint32 count); + select& page(uint32 page, uint32 count); /// /// LIMIT /// @@ -96,7 +96,7 @@ namespace ylib /// /// /// - void make_sql(std::string& field_name,std::string& where,std::string& orderby,std::string& limit,std::vector& insert_values); + void make_sql(std::string& field_name, std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); private: mysql::conn* m_conn = nullptr; std::vector m_wheres; @@ -106,18 +106,6 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Lselect :public ylib::select { - public: - Lselect(void* conn); - ~Lselect(); - void where_i32(const std::string& name, const std::string& expression,int32 value); - void where_i64(const std::string& name, const std::string& expression,int64 value); - void where_dob(const std::string& name, const std::string& expression,double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif /// /// MYSQL UPDATE /// @@ -189,26 +177,6 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Lupdate :public ylib::update { - public: - Lupdate(mysql::conn* conn); - ~Lupdate(); - - void set_i32(const std::string& name, int32 value); - void set_i64(const std::string& name, int64 value); - void set_dob(const std::string& name, double value); - void set_str(const std::string& name, const std::string& value); - - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - - void where_expression(const std::string& expression); - }; - -#endif /// /// MYSQL INSERT /// @@ -236,17 +204,6 @@ namespace ylib std::string m_table_name; std::vector m_sets; }; -#if 0 - class Linsert :public ylib::insert { - public: - Linsert(mysql::conn* conn); - ~Linsert(); - void i32(const std::string& name, int32 value); - void i64(const std::string& name, int64 value); - void dob(const std::string& name, double value); - void str(const std::string& name, const std::string& value); - }; -#endif /// /// MYSQL DELETE /// @@ -304,16 +261,4 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Ldelete :public ylib::delete_ { - public: - Ldelete(mysql::conn* conn); - ~Ldelete(); - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif -} \ No newline at end of file +} diff --git a/src/db/mssql.h b/src/db/mssql.h deleted file mode 100644 index ca7c4ad..0000000 --- a/src/db/mssql.h +++ /dev/null @@ -1,127 +0,0 @@ -#pragma once -#ifdef _WIN32 -#include -#include -#include -#ifndef _WIN32 -#include -#endif -#include "ybase/define.h" -#include "ybase/error.h" -#include "yutil/json.h" -#include "yutil/pool.hpp" - -namespace ylib::mssql -{ - class pool; - class result; - class conn; - class prepare_statement; - struct mssql_conn_info - { - mssql_conn_info(){ - } - std::string odbcname; - std::string username; - std::string password; - }; - class result :public ylib::error_base - { - public: - result(); - ~result(); - // 列数量 - uint32 field_count(); - // 行数量 - size_t row_count(); - // 取文本值 - std::string get(uint32 field); - // 下一行 - bool next(); - int32 get_int32(uint32 index); - int32 get_int32(const std::string& name); - - uint32 get_uint32(uint32 index); - uint32 get_uint32(const std::string& name); - - int64 get_int64(uint32 index); - int64 get_int64(const std::string& name); - - uint64 get_uint64(uint32 index); - uint64 get_uint64(const std::string& name); - - std::string get_string(uint32 index); - std::string get_string(const std::string& name); - - bool get_boolean(uint32 index); - bool get_boolean(const std::string& name); - - double get_double(uint32 index); - double get_double(const std::string& name); - - friend class ylib::mssql::prepare_statement; - private: - void* m_handle = nullptr; - uint32 m_field_count = 0; - uint32 m_row_count = 0; - - std::map m_field_name_index; - }; - - class prepare_statement:public ylib::error_base{ - public: - prepare_statement(); - ~prepare_statement(); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); - void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); - void clear(); - uint64 update(); - ylib::mssql::result* query(); - - friend class ylib::mssql::conn; - private: - ylib::mssql::result* m_result = nullptr; - void* m_handle = nullptr; - }; - class conn :public ylib::example,public ylib::error_base - { - public: - conn(); - ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mssql::mssql_conn_info&info) override; - virtual void close() override; - - - virtual void recover() override; - virtual void task_out() override; - - - void clear(); - ylib::mssql::prepare_statement* setsql(const std::string& sql); - void begin(); - void commit(); - void rollback(); - friend class ylib::mssql::pool; - private: - void *m_handle = nullptr; - ylib::mssql::mssql_conn_info m_info; - class prepare_statement* m_ppst = nullptr; - int m_sw = 0; - }; - - - class pool: public ylib::pool - { - public: - pool(); - ~pool(); - }; -} -#endif \ No newline at end of file diff --git a/src/db/mysql.h b/src/db/mysql.h deleted file mode 100644 index 331d82e..0000000 --- a/src/db/mysql.h +++ /dev/null @@ -1,161 +0,0 @@ -#pragma once - -#include -#include -#include -#ifndef _WIN32 -#include -#endif -#include "ybase/define.h" -#include "ybase/error.h" -#include "yutil/json.h" -#include "yutil/pool.hpp" - -namespace ylib::mysql -{ - class pool; - class result; - class conn; - class prepare_statement; - struct field{ - uint32 index = 0; - std::string name; - std::string type_name; - }; - struct mysql_conn_info - { - mysql_conn_info(){ - port = 0; - } - std::string ipaddress; - std::string username; - std::string password; - std::string database; - std::string charset; - uint32 port; - }; - class result :public ylib::error_base - { - public: - result(); - ~result(); - // 列名 - std::string field_name(uint32 index); - ylib::mysql::field field(uint32 index); - - // 列数量 - uint32 field_count(); - // 行数量 - size_t row_count(); - // 下一行 - bool next(); - int32 get_int32(uint32 index); - int32 get_int32(const std::string& name); - - uint32 get_uint32(uint32 index); - uint32 get_uint32(const std::string& name); - - int64 get_int64(uint32 index); - int64 get_int64(const std::string& name); - - uint64 get_uint64(uint32 index); - uint64 get_uint64(const std::string& name); - - std::string get_string(uint32 index); - std::string get_string(const std::string& name); - - bool get_boolean(uint32 index); - bool get_boolean(const std::string& name); - - double get_double(uint32 index); - double get_double(const std::string& name); - - ylib::json to_json(); - - friend class ylib::mysql::prepare_statement; - private: - void* m_handle = nullptr; - uint32 m_field_count = 0; - uint32 m_row_count = 0; - }; - - class prepare_statement:public ylib::error_base{ - public: - prepare_statement(); - ~prepare_statement(); - void set_bigint(uint32 index,const std::string& value); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); - void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); - void set_blob(uint32 index,const ylib::buffer& value); - void clear(); - uint64 update(); - ylib::mysql::result* query(); - - friend class ylib::mysql::conn; - private: - ylib::mysql::result* m_result = nullptr; - void* m_handle = nullptr; - }; - class conn :public ylib::example,public ylib::error_base - { - public: - conn(); - ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mysql::mysql_conn_info&info) override; - virtual void close() override; - - - virtual void recover() override; - virtual void task_out() override; - - - void clear(); - ylib::mysql::prepare_statement* setsql(const std::string& sql); - uint64 insert_id(); - - void begin(bool autocommit = false); - void commit(); - void rollback(); - - void setDatabase(const std::string& name); - - friend class ylib::mysql::pool; - private: - void *m_handle = nullptr; - ylib::mysql::mysql_conn_info m_info; - class prepare_statement* m_ppst = nullptr; - // 事务状态 0=未开启 1=已开启 2=执行完毕 - int m_sw = 0; - }; - - - class pool: public ylib::pool - { - public: - pool() - { - - } - ~pool() - { - - } - }; - - - - /// - /// 协程调用 - /// 执行期间会让出协程,并等待再次调度 - /// - /// - /// - std::tuple co_query(ylib::mysql::prepare_statement* ppst); -} diff --git a/src/db/sqler.h b/src/db/sqler.h deleted file mode 100644 index 59e17be..0000000 --- a/src/db/sqler.h +++ /dev/null @@ -1,317 +0,0 @@ -#pragma once -#include "ybase/error.h" -#include "mysql.h" -#include -namespace ylib -{ - struct where { - std::string name; - std::string expression; - std::any value; - // 0=普通 1=Like 2=自定义 - int type = 0; - }; - struct limit { - int64 start = -1; - int64 count = -1; - }; - enum sort { - ASC, - DESC - }; - struct order_by { - std::string field; - sort sort; - }; - struct keyvalue { - std::string name; - std::any value; - int extra = 0; - }; - /// - /// MYSQL SELECT 查询器 - /// - class select:public ylib::error_base { - public: - select(mysql::conn* conn); - ~select(); - /// - /// 表名 - /// - /// - /// - select& table(const std::string& table_name); - /// - /// 查询字段 - /// - /// - /// - /// - select& field(const std::vector& field); - /// - /// 条件[A=B] - /// - select& where(const std::string& name,const std::string& expression,const std::any& value); - /// - /// 条件[模糊查询] - /// - select& where_like(const std::string& name,const std::string& value); - /// - /// 条件[自定义] - /// - select& where(const std::string& expression); - /// - /// LIMIT[页] - /// - select& page(uint32 page,uint32 count); - /// - /// LIMIT - /// - select& limit(uint32 start, uint32 count); - /// - /// 排序 - /// - select& orderby(const std::string& field, sort sort = DESC); - /// - /// 查询数量 - /// - /// - uint64 count(); - /// - /// 查询 - /// - ylib::mysql::result* query(); - /// - /// 查询转换为layui需求模板 - /// - /// - ylib::json query_layui(); - private: - /// - /// 生成SQL片段 - /// - /// - /// - /// - /// - void make_sql(std::string& field_name,std::string& where,std::string& orderby,std::string& limit,std::vector& insert_values); - private: - mysql::conn* m_conn = nullptr; - std::vector m_wheres; - std::string m_table_name; - std::vector m_fields; - ylib::limit m_limit; - order_by m_orderby; - - }; -#if 0 - class Lselect :public ylib::select { - public: - Lselect(void* conn); - ~Lselect(); - void where_i32(const std::string& name, const std::string& expression,int32 value); - void where_i64(const std::string& name, const std::string& expression,int64 value); - void where_dob(const std::string& name, const std::string& expression,double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif - /// - /// MYSQL UPDATE - /// - class update :public ylib::error_base { - struct set { - std::string name; - std::any value; - std::string expression; - // 0=普通 1=表达式 - int type = 0; - }; - public: - update(mysql::conn* conn); - ~update(); - /// - /// 表名 - /// - /// - /// - update& table(const std::string& table_name); - /// - /// 更新 - /// - update& set(const std::string& name, const std::any& value); - update& set(const std::string& expression); - /// - /// 条件[A=B] - /// - update& where(const std::string& name, const std::string& expression, const std::any& value); - /// - /// 条件[模糊查询] - /// - update& where_like(const std::string& name, const std::string& value); - /// - /// 条件[自定义] - /// - update& where(const std::string& expression); - /// - /// LIMIT[页] - /// - update& page(uint32 page, uint32 count); - /// - /// LIMIT - /// - update& limit(uint32 start, uint32 count); - /// - /// 排序 - /// - update& orderby(const std::string& field, sort sort = DESC); - /// - /// 查询 - /// - uint64 exec(); - private: - /// - /// 生成SQL片段 - /// - /// - /// - /// - /// - void make_sql(std::string& set, std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); - private: - mysql::conn* m_conn = nullptr; - std::vector m_wheres; - std::string m_table_name; - std::vector m_sets; - ylib::limit m_limit; - order_by m_orderby; - - }; -#if 0 - class Lupdate :public ylib::update { - public: - Lupdate(mysql::conn* conn); - ~Lupdate(); - - void set_i32(const std::string& name, int32 value); - void set_i64(const std::string& name, int64 value); - void set_dob(const std::string& name, double value); - void set_str(const std::string& name, const std::string& value); - - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - - void where_expression(const std::string& expression); - }; - -#endif - /// - /// MYSQL INSERT - /// - class insert :public ylib::error_base { - public: - insert(mysql::conn* conn); - ~insert(); - /// - /// 表名 - /// - /// - /// - insert& table(const std::string& table_name); - /// - /// 更新 - /// - insert& set(const std::string& name, const std::any& value); - insert& set_not_pret(const std::string& name, const std::string& value); - /// - /// 查询 - /// - uint64 exec(); - private: - mysql::conn* m_conn = nullptr; - std::string m_table_name; - std::vector m_sets; - }; -#if 0 - class Linsert :public ylib::insert { - public: - Linsert(mysql::conn* conn); - ~Linsert(); - void i32(const std::string& name, int32 value); - void i64(const std::string& name, int64 value); - void dob(const std::string& name, double value); - void str(const std::string& name, const std::string& value); - }; -#endif - /// - /// MYSQL DELETE - /// - class delete_ :public ylib::error_base { - public: - delete_(mysql::conn* conn); - ~delete_(); - /// - /// 表名 - /// - /// - /// - delete_& table(const std::string& table_name); - /// - /// 条件[A=B] - /// - delete_& where(const std::string& name, const std::string& expression, const std::any& value); - /// - /// 条件[模糊查询] - /// - delete_& where_like(const std::string& name, const std::string& value); - /// - /// 条件[自定义] - /// - delete_& where(const std::string& expression); - /// - /// LIMIT[页] - /// - delete_& page(uint32 page, uint32 count); - /// - /// LIMIT - /// - delete_& limit(uint32 start, uint32 count); - /// - /// 排序 - /// - delete_& orderby(const std::string& field, sort sort = DESC); - /// - /// 查询 - /// - uint64 exec(); - private: - /// - /// 生成SQL片段 - /// - /// - /// - /// - void make_sql(std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); - private: - mysql::conn* m_conn = nullptr; - std::vector m_wheres; - std::string m_table_name; - ylib::limit m_limit; - order_by m_orderby; - - }; -#if 0 - class Ldelete :public ylib::delete_ { - public: - Ldelete(mysql::conn* conn); - ~Ldelete(); - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif -} \ No newline at end of file