2006-04-23 01:10:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "MysqlDriver.h"
|
|
|
|
#include "MysqlDatabase.h"
|
|
|
|
|
|
|
|
#if defined WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#define strncasecmp strnicmp
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace SourceMod;
|
|
|
|
|
|
|
|
bool MysqlDriver::IsCompatDriver(const char *namestring)
|
|
|
|
{
|
|
|
|
return (strncasecmp(namestring, "mysql", 5) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *MysqlDriver::NameString()
|
|
|
|
{
|
|
|
|
return "mysql";
|
|
|
|
}
|
|
|
|
|
|
|
|
IDatabase *MysqlDriver::Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength)
|
2006-10-01 19:46:56 +00:00
|
|
|
{
|
|
|
|
return _Connect(info, errcode, error, maxlength, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDatabase *MysqlDriver::Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
return _Connect(info, errcode, error, maxlength, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDatabase *MysqlDriver::_Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength, bool do_timeout)
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
MYSQL *mysql = mysql_init(NULL);
|
|
|
|
|
|
|
|
if (!mysql)
|
|
|
|
{
|
|
|
|
if (errcode)
|
|
|
|
*errcode = -1;
|
|
|
|
if (error && maxlength)
|
|
|
|
{
|
|
|
|
snprintf(error, maxlength, "Initialization failed");
|
|
|
|
}
|
2013-02-13 07:14:37 +00:00
|
|
|
return NULL;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
2006-10-01 19:46:56 +00:00
|
|
|
if (do_timeout && info->max_timeout)
|
|
|
|
{
|
|
|
|
mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&(info->max_timeout));
|
|
|
|
}
|
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
if (mysql_real_connect(mysql,
|
|
|
|
info->host,
|
|
|
|
info->user,
|
|
|
|
info->pass,
|
|
|
|
info->database,
|
|
|
|
info->port,
|
|
|
|
NULL,
|
2007-10-22 21:31:02 +00:00
|
|
|
CLIENT_MULTI_STATEMENTS) == NULL)
|
2006-04-23 01:10:06 +00:00
|
|
|
{
|
|
|
|
if (errcode)
|
2006-10-01 19:46:56 +00:00
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
*errcode = mysql_errno(mysql);
|
2006-10-01 19:46:56 +00:00
|
|
|
}
|
2006-04-23 01:10:06 +00:00
|
|
|
if (error && maxlength)
|
2006-10-01 19:46:56 +00:00
|
|
|
{
|
2006-04-23 01:10:06 +00:00
|
|
|
snprintf(error, maxlength, "%s", mysql_error(mysql));
|
2006-10-01 19:46:56 +00:00
|
|
|
}
|
2013-02-13 07:14:37 +00:00
|
|
|
return NULL;
|
2006-04-23 01:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MysqlDatabase *pMysql = new MysqlDatabase(mysql, this);
|
|
|
|
|
2014-04-18 20:04:32 +00:00
|
|
|
if (info->charset && *info->charset)
|
|
|
|
{
|
|
|
|
pMysql->SetCharacterSet(info->charset);
|
|
|
|
}
|
|
|
|
|
2006-04-23 01:10:06 +00:00
|
|
|
return static_cast<IDatabase *>(pMysql);
|
|
|
|
}
|
2009-01-08 20:02:09 +00:00
|
|
|
|
|
|
|
int MysqlDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize)
|
|
|
|
{
|
|
|
|
unsigned long size = static_cast<unsigned long>(strlen(str));
|
|
|
|
unsigned long needed = size*2 + 1;
|
|
|
|
|
|
|
|
if (maxlen < needed)
|
|
|
|
{
|
|
|
|
return (int)needed;
|
|
|
|
}
|
|
|
|
|
|
|
|
needed = mysql_escape_string(buffer, str, size);
|
|
|
|
if (newsize)
|
|
|
|
{
|
|
|
|
*newsize = static_cast<size_t>(needed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|