#pragma once #include "base/error.h" #include "mysql.h" #include #include namespace ylib { struct where { std::string name; std::string expression; std::any value; std::vector values; // IN / BETWEEN 等多值条件 // 0=普通 1=Like 2=自定义 3=IS NULL 4=IS NOT NULL // 5=NOT LIKE 6=IN 7=NOT IN 8=BETWEEN 9=NOT BETWEEN int type = 0; }; struct limit { int64 start = -1; int64 count = -1; }; 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(const ylib::conn_autofree& 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); /// /// 条件[NOT LIKE] /// select& where_not_like(const std::string& name, const std::string& value); /// /// 条件[IS NULL] /// select& where_is_null(const std::string& name); /// /// 条件[IS NOT NULL] /// select& where_is_not_null(const std::string& name); /// /// 条件[IN] /// select& where_in(const std::string& name, const std::vector& values); /// /// 条件[NOT IN] /// select& where_not_in(const std::string& name, const std::vector& values); /// /// 条件[BETWEEN] /// select& where_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[NOT BETWEEN] /// select& where_not_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[自定义],需要自行最前面拼接 AND 或 OR,如:"AND (a = b)" /// 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& expression); /// /// 分组查询 /// select& groupby(const std::string& expression); /// /// 过滤 /// select& having(const std::string& expression); /// /// 查询数量 /// /// uint64 count(); /// /// 查询 /// ylib::mysql::result* query(); void clear(); #if 0 /// /// 查询转换为layui需求模板 /// /// ylib::json query_layui(); #endif private: /// /// 生成SQL片段 /// /// /// /// /// void make_sql(std::string& field_name, std::string& where,std::string& groupby,std::string& having,std::string& orderby, std::string& limit, std::vector& insert_values); public: mysql::conn* m_conn = nullptr; private: ylib::conn_autofree m_autofree; std::vector m_wheres; std::string m_table_name; std::vector m_fields; ylib::limit m_limit; std::string m_orderby; std::string m_groupby; std::string m_having; }; /// /// 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(const ylib::conn_autofree& 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); /// /// 条件[NOT LIKE] /// update& where_not_like(const std::string& name, const std::string& value); /// /// 条件[IS NULL] /// update& where_is_null(const std::string& name); /// /// 条件[IS NOT NULL] /// update& where_is_not_null(const std::string& name); /// /// 条件[IN] /// update& where_in(const std::string& name, const std::vector& values); /// /// 条件[NOT IN] /// update& where_not_in(const std::string& name, const std::vector& values); /// /// 条件[BETWEEN] /// update& where_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[NOT BETWEEN] /// update& where_not_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[自定义],需要自行最前面拼接 AND 或 OR,如:"AND (a = b)" /// 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& expression); /// /// 查询 /// uint64 exec(); void clear(); private: /// /// 生成SQL片段 /// /// /// /// /// void make_sql(std::string& set, std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); private: public: mysql::conn* m_conn = nullptr; private: ylib::conn_autofree m_autofree; std::vector m_wheres; std::string m_table_name; std::vector m_sets; ylib::limit m_limit; std::string m_orderby; }; /// /// MYSQL INSERT /// class insert :public ylib::error_base { public: insert(mysql::conn* conn); insert(const ylib::conn_autofree& 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(); void clear(); public: mysql::conn* m_conn = nullptr; private: ylib::conn_autofree m_autofree; std::string m_table_name; std::vector m_sets; }; /// /// MYSQL DELETE /// class delete_ :public ylib::error_base { public: delete_(mysql::conn* conn); delete_(const ylib::conn_autofree& 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); /// /// 条件[NOT LIKE] /// delete_& where_not_like(const std::string& name, const std::string& value); /// /// 条件[IS NULL] /// delete_& where_is_null(const std::string& name); /// /// 条件[IS NOT NULL] /// delete_& where_is_not_null(const std::string& name); /// /// 条件[IN] /// delete_& where_in(const std::string& name, const std::vector& values); /// /// 条件[NOT IN] /// delete_& where_not_in(const std::string& name, const std::vector& values); /// /// 条件[BETWEEN] /// delete_& where_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[NOT BETWEEN] /// delete_& where_not_between(const std::string& name, const std::any& begin, const std::any& end); /// /// 条件[自定义],需要自行最前面拼接 AND 或 OR,如:"AND (a = b)" /// 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& expression); /// /// 查询 /// uint64 exec(); void clear(); private: /// /// 生成SQL片段 /// /// /// /// void make_sql(std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); public: mysql::conn* m_conn = nullptr; private: ylib::conn_autofree m_autofree; std::vector m_wheres; std::string m_table_name; ylib::limit m_limit; std::string m_orderby; }; }