强化SQL

This commit is contained in:
xx
2024-06-11 16:40:20 +08:00
parent a8c942e652
commit 6d7bf78649
6 changed files with 73 additions and 15 deletions

View File

@@ -71,6 +71,9 @@ namespace ylib::mysql
double get_double(uint32 index);
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();
private:
@@ -95,7 +98,8 @@ namespace ylib::mysql
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 char* data,int size);
void set_blob(uint32 index, const char* data, int size);
void clear();
uint64 update();
ylib::mysql::result* query();
@@ -104,6 +108,8 @@ namespace ylib::mysql
private:
ylib::mysql::result* m_result = 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
{

View File

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

View File

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

View File

@@ -11,9 +11,9 @@ file(GLOB_RECURSE HEADER_FILES
"${PROJECT_SOURCE_DIR}/include/*.h"
)
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(${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Windows/Include)
endif()

View File

@@ -92,6 +92,7 @@ EXAMPLE_START_RESULT ylib::mysql::conn::start(const mysql_conn_info &info)
void ylib::mysql::conn::close()
{
clear();
if (CONNECTION == nullptr)
return;
try
@@ -104,8 +105,6 @@ void ylib::mysql::conn::close()
}
delete CONNECTION;
m_handle = nullptr;
}
void ylib::mysql::conn::recover()
@@ -115,7 +114,6 @@ void ylib::mysql::conn::recover()
rollback();
}
clear();
}
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)
{
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;
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
std::cout << "set " << index << " = " << value << std::endl;
#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;
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
std::cout << "set " << index << " = " << value.length()<<"(BLOB)" << std::endl;
#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()
{
CHECK_SQL_PPST;

View File

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