mirror of
https://github.com/alliedmodders/amxmodx.git
synced 2024-12-27 07:15:37 +03:00
96 lines
1.9 KiB
C++
96 lines
1.9 KiB
C++
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "MysqlQuery.h"
|
|
#include "MysqlDatabase.h"
|
|
#include "MysqlResultSet.h"
|
|
|
|
#if defined WIN32
|
|
#define snprintf _snprintf
|
|
#endif
|
|
|
|
using namespace SourceMod;
|
|
|
|
MysqlQuery::MysqlQuery(const char *querystring, MysqlDatabase *db) :
|
|
m_pDatabase(db)
|
|
{
|
|
m_QueryLen = strlen(querystring);
|
|
m_QueryString = new char[m_QueryLen + 1];
|
|
m_LastRes = NULL;
|
|
strcpy(m_QueryString, querystring);
|
|
}
|
|
|
|
MysqlQuery::~MysqlQuery()
|
|
{
|
|
if (m_LastRes)
|
|
{
|
|
m_LastRes->FreeHandle();
|
|
}
|
|
|
|
delete [] m_QueryString;
|
|
}
|
|
|
|
void MysqlQuery::FreeHandle()
|
|
{
|
|
delete this;
|
|
}
|
|
|
|
bool MysqlQuery::Execute(QueryInfo *info, char *error, size_t maxlength)
|
|
{
|
|
bool res = ExecuteR(info, error, maxlength);
|
|
|
|
if (m_LastRes)
|
|
m_LastRes->FreeHandle();
|
|
|
|
m_LastRes = (MysqlResultSet *)info->rs;
|
|
|
|
return res;
|
|
}
|
|
|
|
bool MysqlQuery::ExecuteR(QueryInfo *info, char *error, size_t maxlength)
|
|
{
|
|
int err;
|
|
|
|
if ( (err=mysql_real_query(m_pDatabase->m_pMysql, m_QueryString, (unsigned long)m_QueryLen)) )
|
|
{
|
|
info->errorcode = mysql_errno(m_pDatabase->m_pMysql);
|
|
info->success = false;
|
|
info->affected_rows = 0;
|
|
info->rs = NULL;
|
|
if (error && maxlength)
|
|
{
|
|
snprintf(error, maxlength, "%s", mysql_error(m_pDatabase->m_pMysql));
|
|
}
|
|
} else {
|
|
MYSQL_RES *res = mysql_store_result(m_pDatabase->m_pMysql);
|
|
if (!res)
|
|
{
|
|
if (mysql_field_count(m_pDatabase->m_pMysql) > 0)
|
|
{
|
|
//error !111!!11
|
|
info->errorcode = mysql_errno(m_pDatabase->m_pMysql);
|
|
info->success = false;
|
|
info->affected_rows = 0;
|
|
info->rs = NULL;
|
|
} else {
|
|
info->errorcode = 0;
|
|
info->success = true;
|
|
info->affected_rows = mysql_affected_rows(m_pDatabase->m_pMysql);
|
|
info->rs = NULL;
|
|
}
|
|
} else {
|
|
info->errorcode = 0;
|
|
info->success = true;
|
|
info->affected_rows = mysql_affected_rows(m_pDatabase->m_pMysql);
|
|
MysqlResultSet *rs = new MysqlResultSet(res);
|
|
info->rs = rs;
|
|
}
|
|
}
|
|
|
|
if (info->success && error && maxlength)
|
|
{
|
|
*error = '\0';
|
|
}
|
|
|
|
return info->success;
|
|
}
|