#pragma once #include "base/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; }; 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& 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: 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(); /// /// 表名 /// /// /// 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& 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: 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(); /// /// 表名 /// /// /// 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: std::string m_table_name; std::vector m_sets; }; /// /// 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& 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: std::vector m_wheres; std::string m_table_name; ylib::limit m_limit; std::string m_orderby; }; }