Update MySQLConn
This commit is contained in:
parent
d13e4042bd
commit
7598ed496d
|
@ -1,5 +1,11 @@
|
|||
#include "MySQLWrapper.h"
|
||||
#include "MySQLInclude.h"
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
/**********************
|
||||
MySQL Result Class.
|
||||
**********************/
|
||||
|
||||
struct MySQLResult::impl
|
||||
{
|
||||
|
@ -48,9 +54,15 @@ void MySQLResult::stepRow(const std::function<void(char** RowPtr, unsigned long*
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**********************
|
||||
MySQL Connection Class.
|
||||
**********************/
|
||||
|
||||
struct MySQLConn::impl
|
||||
{
|
||||
MYSQL m;
|
||||
string lasterr;
|
||||
};
|
||||
|
||||
MySQLConn::MySQLConn() : pimpl(new impl)
|
||||
|
@ -65,7 +77,15 @@ MySQLConn::~MySQLConn()
|
|||
|
||||
int MySQLConn::connect(const char* host, const char* user, const char* passwd, const char* db, unsigned int port)
|
||||
{
|
||||
return (mysql_real_connect(&(pimpl->m),host,user,passwd,db,port,NULL,0)==NULL)?0:-1;
|
||||
if(mysql_real_connect(&(pimpl->m),host,user,passwd,db,port,NULL,0)==NULL)
|
||||
{
|
||||
pimpl->lasterr=mysql_error(&(pimpl->m));
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int MySQLConn::exec(const std::string& SQLCommand, const std::function<void(MySQLResult&)>& func)
|
||||
|
@ -73,8 +93,10 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::function<void(MySQ
|
|||
if(mysql_real_query(&(pimpl->m),SQLCommand.c_str(),SQLCommand.size())!=0)
|
||||
{
|
||||
/// Failed to Query
|
||||
pimpl->lasterr=mysql_error(&(pimpl->m));
|
||||
return -1;
|
||||
}
|
||||
|
||||
MYSQL_RES* pres=mysql_store_result(&(pimpl->m));
|
||||
if(pres==nullptr)
|
||||
{
|
||||
|
@ -87,6 +109,7 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::function<void(MySQ
|
|||
else
|
||||
{
|
||||
/// Failed to store result.
|
||||
pimpl->lasterr=mysql_error(&(pimpl->m));
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +117,7 @@ int MySQLConn::exec(const std::string& SQLCommand, const std::function<void(MySQ
|
|||
MySQLResult res;
|
||||
res.pimpl->res=pres;
|
||||
|
||||
func(res);
|
||||
if(func) func(res);
|
||||
|
||||
/// MySQL_RES will be released normally by MySQLResult::~MySQLResult()
|
||||
return 0;
|
||||
|
@ -109,3 +132,8 @@ uint64_t MySQLConn::getAffectedRows()
|
|||
{
|
||||
return mysql_affected_rows(&(pimpl->m));
|
||||
}
|
||||
|
||||
const std::string& MySQLConn::getError()
|
||||
{
|
||||
return pimpl->lasterr;
|
||||
}
|
||||
|
|
|
@ -32,13 +32,15 @@ public:
|
|||
~MySQLConn();
|
||||
|
||||
/// 数据库连接
|
||||
/// 返回值: 0:连接正常, -1:连接错误
|
||||
int connect(const char* host,const char* user,const char* passwd,const char* db,unsigned int port);
|
||||
|
||||
/// 执行SQL语句
|
||||
int exec(const std::string& SQLCommand,const std::function<void(MySQLResult&)>& res);
|
||||
/// 返回值: 1: 语句执行正常,无返回结果(func未执行) 0:语句执行正常,返回结果集交付func执行. -1:语句执行错误.(mysql_real_query失败) -2: 语句执行正确,获取结果出现错误
|
||||
int exec(const std::string& SQLCommand,const std::function<void(MySQLResult&)>& func);
|
||||
|
||||
/// 获取错误信息
|
||||
const char* getError();
|
||||
const std::string& getError();
|
||||
|
||||
/// 当结果为空时用于判断数据库运行正常与否
|
||||
unsigned int getFieldCount();
|
||||
|
|
Loading…
Reference in New Issue
Block a user