Browse Source

强化SQL

xx 2 năm trước cách đây
mục cha
commit
6d7bf78649

+ 7 - 1
include/db/mysql.h

@@ -71,6 +71,9 @@ namespace ylib::mysql
         double get_double(uint32 index);
         double get_double(uint32 index);
         double get_double(const std::string& name);
         double get_double(const std::string& name);
 
 
+        ylib::buffer get_blob(uint32 index);
+        ylib::buffer get_blob(const std::string& name);
+
         ylib::json to_json();
         ylib::json to_json();
 
 
     private:
     private:
@@ -95,7 +98,8 @@ namespace ylib::mysql
         void set_uint64(uint32 index, uint64 value);
         void set_uint64(uint32 index, uint64 value);
         void set_null(uint32 index);
         void set_null(uint32 index);
         void set_string(uint32 index, const std::string& value);
         void set_string(uint32 index, const std::string& value);
-        void set_blob(uint32 index, const ylib::buffer& value);
+        void set_string(uint32 index,const char* data,int size);
+        void set_blob(uint32 index, const char* data, int size);
         void clear();
         void clear();
         uint64 update();
         uint64 update();
         ylib::mysql::result* query();
         ylib::mysql::result* query();
@@ -104,6 +108,8 @@ namespace ylib::mysql
     private:
     private:
         ylib::mysql::result* m_result = nullptr;
         ylib::mysql::result* m_result = nullptr;
         void* m_handle = nullptr;
         void* m_handle = nullptr;
+
+        std::queue<std::shared_ptr<std::istringstream>> m_blobs;
     };
     };
     class conn :public ylib::example<ylib::mysql::mysql_conn_info>, public ylib::error_base
     class conn :public ylib::example<ylib::mysql::mysql_conn_info>, public ylib::error_base
     {
     {

+ 8 - 3
include/db/sqler.h

@@ -99,8 +99,9 @@ namespace ylib
 		/// <param name="orderby"></param>
 		/// <param name="orderby"></param>
 		/// <param name="limit"></param>
 		/// <param name="limit"></param>
 		void make_sql(std::string& field_name, std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
 		void make_sql(std::string& field_name, std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
-	private:
+	public:
 		mysql::conn* m_conn = nullptr;
 		mysql::conn* m_conn = nullptr;
+	private:
 		std::vector<ylib::where> m_wheres;
 		std::vector<ylib::where> m_wheres;
 		std::string m_table_name;
 		std::string m_table_name;
 		std::vector<std::string> m_fields;
 		std::vector<std::string> m_fields;
@@ -173,7 +174,9 @@ namespace ylib
 		/// <param name="limit"></param>
 		/// <param name="limit"></param>
 		void make_sql(std::string& set, std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
 		void make_sql(std::string& set, std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
 	private:
 	private:
+	public:
 		mysql::conn* m_conn = nullptr;
 		mysql::conn* m_conn = nullptr;
+	private:
 		std::vector<ylib::where> m_wheres;
 		std::vector<ylib::where> m_wheres;
 		std::string m_table_name;
 		std::string m_table_name;
 		std::vector<struct ylib::update::set> m_sets;
 		std::vector<struct ylib::update::set> m_sets;
@@ -205,8 +208,9 @@ namespace ylib
 		uint64 exec();
 		uint64 exec();
 
 
 		void clear();
 		void clear();
-	private:
+	public:
 		mysql::conn* m_conn = nullptr;
 		mysql::conn* m_conn = nullptr;
+	private:
 		std::string m_table_name;
 		std::string m_table_name;
 		std::vector<keyvalue> m_sets;
 		std::vector<keyvalue> m_sets;
 	};
 	};
@@ -261,8 +265,9 @@ namespace ylib
 		/// <param name="orderby"></param>
 		/// <param name="orderby"></param>
 		/// <param name="limit"></param>
 		/// <param name="limit"></param>
 		void make_sql(std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
 		void make_sql(std::string& where, std::string& orderby, std::string& limit, std::vector<std::any>& insert_values);
-	private:
+	public:
 		mysql::conn* m_conn = nullptr;
 		mysql::conn* m_conn = nullptr;
+	private:
 		std::vector<ylib::where> m_wheres;
 		std::vector<ylib::where> m_wheres;
 		std::string m_table_name;
 		std::string m_table_name;
 		ylib::limit m_limit;
 		ylib::limit m_limit;

+ 1 - 2
include/util/pool.hpp

@@ -24,7 +24,6 @@ namespace ylib {
             if(example == NULL)
             if(example == NULL)
                 return;
                 return;
             ((ylib::example_core*)example)->recover();
             ((ylib::example_core*)example)->recover();
-
             std::unique_lock<std::mutex> sp(m_mutex);
             std::unique_lock<std::mutex> sp(m_mutex);
             m_pop_size--;
             m_pop_size--;
             m_queue.push(example);
             m_queue.push(example);
@@ -40,9 +39,9 @@ namespace ylib {
     };
     };
     enum EXAMPLE_START_RESULT
     enum EXAMPLE_START_RESULT
     {
     {
+        SR_SUCCESS,
         SR_TIMEOUT,
         SR_TIMEOUT,
         SR_FAILED,
         SR_FAILED,
-        SR_SUCCESS
     };
     };
     template<typename INFO>
     template<typename INFO>
     class example:public example_core
     class example:public example_core

+ 2 - 2
src/CMakeLists.txt

@@ -11,9 +11,9 @@ file(GLOB_RECURSE HEADER_FILES
     "${PROJECT_SOURCE_DIR}/include/*.h"
     "${PROJECT_SOURCE_DIR}/include/*.h"
 )
 )
 
 
-
 if(MSVC)
 if(MSVC)
-    set(MYSQL_CPPCONN_INCLUDE  CACHE PATH "mysql-connector include")
+    #set(MYSQL_CPPCONN_INCLUDE  CACHE PATH "mysql-connector include")
+    set(MYSQL_CPPCONN_INCLUDE "$ENV{USERPROFILE}/MySQL/MySQL Connector C++ 8.2.0/include/jdbc")
     include_directories(${MYSQL_CPPCONN_INCLUDE})
     include_directories(${MYSQL_CPPCONN_INCLUDE})
 	include_directories(${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Windows/Include)
 	include_directories(${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Windows/Include)
 endif()
 endif()

+ 48 - 7
src/db/mysql.cpp

@@ -92,6 +92,7 @@ EXAMPLE_START_RESULT ylib::mysql::conn::start(const mysql_conn_info &info)
 
 
 void ylib::mysql::conn::close()
 void ylib::mysql::conn::close()
 {
 {
+    clear();
     if (CONNECTION == nullptr)
     if (CONNECTION == nullptr)
         return;
         return;
     try
     try
@@ -104,8 +105,6 @@ void ylib::mysql::conn::close()
     }
     }
     delete CONNECTION;
     delete CONNECTION;
     m_handle = nullptr;
     m_handle = nullptr;
-
-    
 }
 }
 
 
 void ylib::mysql::conn::recover()
 void ylib::mysql::conn::recover()
@@ -115,7 +114,6 @@ void ylib::mysql::conn::recover()
         rollback();
         rollback();
     }
     }
     clear();
     clear();
-
 }
 }
 
 
 void ylib::mysql::conn::task_out()
 void ylib::mysql::conn::task_out()
@@ -362,20 +360,25 @@ void ylib::mysql::prepare_statement::set_null(uint32 index)
 }
 }
 
 
 void ylib::mysql::prepare_statement::set_string(uint32 index, const std::string &value)
 void ylib::mysql::prepare_statement::set_string(uint32 index, const std::string &value)
+{
+    set_string(index, value.c_str(), value.length());
+}
+void ylib::mysql::prepare_statement::set_string(uint32 index, const char* data, int size)
 {
 {
     CHECK_SQL_PPST;
     CHECK_SQL_PPST;
     PRINT_DEBUG_SET_NSTRING;
     PRINT_DEBUG_SET_NSTRING;
-    PREPARE_STATEMENT->setString(index,value.c_str());
+    PREPARE_STATEMENT->setString(index,sql::SQLString(data,size));
 #if DEBUG_LOG_PPST_SET == 1
 #if DEBUG_LOG_PPST_SET == 1
     std::cout << "set " << index << " = " << value << std::endl;
     std::cout << "set " << index << " = " << value << std::endl;
 #endif
 #endif
 }
 }
-void ylib::mysql::prepare_statement::set_blob(uint32 index, const ylib::buffer& value)
+void ylib::mysql::prepare_statement::set_blob(uint32 index, const char* data, int size)
 {
 {
     CHECK_SQL_PPST;
     CHECK_SQL_PPST;
     PRINT_DEBUG_SET_NSTRING;
     PRINT_DEBUG_SET_NSTRING;
-    std::istringstream binaryDataStream(value.to_string());
-    PREPARE_STATEMENT->setBlob(index, &binaryDataStream);
+    auto data_ptr = std::make_shared<std::istringstream>(std::string(data,size));
+    m_blobs.push(data_ptr);
+    PREPARE_STATEMENT->setBlob(index, data_ptr.get());
 #if DEBUG_LOG_PPST_SET == 1
 #if DEBUG_LOG_PPST_SET == 1
     std::cout << "set " << index << " = " << value.length()<<"(BLOB)" << std::endl;
     std::cout << "set " << index << " = " << value.length()<<"(BLOB)" << std::endl;
 #endif
 #endif
@@ -696,6 +699,44 @@ double ylib::mysql::result::get_double(const std::string &name)
     }
     }
 }
 }
 
 
+ylib::buffer ylib::mysql::result::get_blob(uint32 index)
+{
+    CHECK_SQL_PPST;
+    try
+    {
+        auto stream = RESULT_SET->getBlob(index);
+        ylib::buffer result;
+        char ch;
+        while (stream->get(ch)) {
+            result.append<char>(ch);
+        }
+        return result;
+    }
+    catch (const sql::SQLException& e)
+    {
+        throw ylib::exception(e.what());
+    }
+}
+
+ylib::buffer ylib::mysql::result::get_blob(const std::string& name)
+{
+    CHECK_SQL_PPST;
+    try
+    {
+        auto stream = RESULT_SET->getBlob(name);
+        ylib::buffer result;
+        char ch;
+        while (stream->get(ch)) {
+            result.append<char>(ch);
+        }
+        return result;
+    }
+    catch (const sql::SQLException& e)
+    {
+        throw ylib::exception(e.what());
+    }
+}
+
 ylib::json ylib::mysql::result::to_json()
 ylib::json ylib::mysql::result::to_json()
 {
 {
     CHECK_SQL_PPST;
     CHECK_SQL_PPST;

+ 7 - 0
src/db/sqler.cpp

@@ -28,6 +28,9 @@ bool checkAnyType(const std::any& value)
 	else if (value.type() == typeid(float)) {
 	else if (value.type() == typeid(float)) {
 		return true;
 		return true;
 	}
 	}
+	else if (value.type() == typeid(std::vector<uchar>)) {
+		return true;
+	}
 	else {
 	else {
 		return false;
 		return false;
 	}
 	}
@@ -61,6 +64,10 @@ void setInsetValue(mysql::prepare_statement*ppst, uint32 index, const std::any&
 	else if (value.type() == typeid(float)) {
 	else if (value.type() == typeid(float)) {
 		ppst->set_double(index, std::any_cast<float>(value));
 		ppst->set_double(index, std::any_cast<float>(value));
 	}
 	}
+	else if (value.type() == typeid(std::vector<uchar>)) {
+		auto byte_vct = std::any_cast<std::vector<uchar>>(value);
+		ppst->set_blob(index,(char*)byte_vct.data(),byte_vct.size());
+	}
 	else {
 	else {
 		throw ylib::exception("Unsupported types("+std::string(value.type().name()) + ")");
 		throw ylib::exception("Unsupported types("+std::string(value.type().name()) + ")");
 	}
 	}