2014-08-04 14:29:31 +04:00
|
|
|
// vim: set ts=4 sw=4 tw=99 noet:
|
|
|
|
//
|
|
|
|
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
|
|
|
|
// Copyright (C) The AMX Mod X Development Team.
|
|
|
|
//
|
|
|
|
// This software is licensed under the GNU General Public License, version 3 or higher.
|
|
|
|
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
|
|
|
|
// https://alliedmods.net/amxmodx-license
|
|
|
|
|
|
|
|
//
|
|
|
|
// MySQL Module
|
|
|
|
//
|
|
|
|
|
2014-08-08 14:44:37 +04:00
|
|
|
#include "amxxmodule.h"
|
2006-04-23 05:10:06 +04:00
|
|
|
#include "MysqlDriver.h"
|
|
|
|
#include "MysqlDatabase.h"
|
|
|
|
|
|
|
|
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 23:46:56 +04: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 05:10:06 +04:00
|
|
|
{
|
|
|
|
MYSQL *mysql = mysql_init(NULL);
|
|
|
|
|
|
|
|
if (!mysql)
|
|
|
|
{
|
|
|
|
if (errcode)
|
|
|
|
*errcode = -1;
|
|
|
|
if (error && maxlength)
|
|
|
|
{
|
2014-08-08 14:44:37 +04:00
|
|
|
UTIL_Format(error, maxlength, "Initialization failed");
|
2006-04-23 05:10:06 +04:00
|
|
|
}
|
2013-02-13 11:14:37 +04:00
|
|
|
return NULL;
|
2006-04-23 05:10:06 +04:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:50:13 +03:00
|
|
|
decltype(info->max_timeout) timeout = atoi(LOCALINFO("mysql_timeout"));
|
|
|
|
|
|
|
|
if (timeout <= 0)
|
2006-10-01 23:46:56 +04:00
|
|
|
{
|
2015-01-29 15:50:13 +03:00
|
|
|
timeout = 60;
|
2006-10-01 23:46:56 +04:00
|
|
|
}
|
|
|
|
|
2015-01-29 15:50:13 +03:00
|
|
|
if (do_timeout && info->max_timeout > 0)
|
|
|
|
{
|
|
|
|
timeout = info->max_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&timeout);
|
|
|
|
mysql_options(mysql, MYSQL_OPT_READ_TIMEOUT, (const char *)&timeout);
|
|
|
|
mysql_options(mysql, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&timeout);
|
|
|
|
|
2014-10-28 03:55:23 +03:00
|
|
|
/** Have MySQL automatically reconnect if it times out or loses connection.
|
|
|
|
* This will prevent "MySQL server has gone away" errors after a while.
|
|
|
|
*/
|
|
|
|
my_bool my_true = true;
|
|
|
|
mysql_options(mysql, MYSQL_OPT_RECONNECT, (const char *)&my_true);
|
|
|
|
|
2006-04-23 05:10:06 +04:00
|
|
|
if (mysql_real_connect(mysql,
|
|
|
|
info->host,
|
|
|
|
info->user,
|
|
|
|
info->pass,
|
|
|
|
info->database,
|
|
|
|
info->port,
|
|
|
|
NULL,
|
2007-10-23 01:31:02 +04:00
|
|
|
CLIENT_MULTI_STATEMENTS) == NULL)
|
2006-04-23 05:10:06 +04:00
|
|
|
{
|
|
|
|
if (errcode)
|
2006-10-01 23:46:56 +04:00
|
|
|
{
|
2006-04-23 05:10:06 +04:00
|
|
|
*errcode = mysql_errno(mysql);
|
2006-10-01 23:46:56 +04:00
|
|
|
}
|
2006-04-23 05:10:06 +04:00
|
|
|
if (error && maxlength)
|
2006-10-01 23:46:56 +04:00
|
|
|
{
|
2014-08-08 14:44:37 +04:00
|
|
|
UTIL_Format(error, maxlength, "%s", mysql_error(mysql));
|
2006-10-01 23:46:56 +04:00
|
|
|
}
|
2013-02-13 11:14:37 +04:00
|
|
|
return NULL;
|
2006-04-23 05:10:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MysqlDatabase *pMysql = new MysqlDatabase(mysql, this);
|
|
|
|
|
2014-04-19 00:04:32 +04:00
|
|
|
if (info->charset && *info->charset)
|
|
|
|
{
|
|
|
|
pMysql->SetCharacterSet(info->charset);
|
|
|
|
}
|
|
|
|
|
2006-04-23 05:10:06 +04:00
|
|
|
return static_cast<IDatabase *>(pMysql);
|
|
|
|
}
|
2009-01-08 23:02:09 +03: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;
|
|
|
|
}
|
|
|
|
|