2014-08-04 10:49:44 +00: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
|
|
|
|
|
|
|
|
//
|
|
|
|
// SQLite Module
|
|
|
|
//
|
|
|
|
|
2014-08-08 10:44:37 +00:00
|
|
|
#include "amxxmodule.h"
|
2006-06-03 19:52:21 +00:00
|
|
|
#include "SqliteHeaders.h"
|
|
|
|
#include "SqliteDriver.h"
|
|
|
|
#include "SqliteDatabase.h"
|
|
|
|
|
|
|
|
#if defined WIN32
|
2007-10-02 23:38:31 +00:00
|
|
|
#define WINDOWS_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2006-06-03 19:52:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace SourceMod;
|
|
|
|
|
|
|
|
bool SqliteDriver::IsCompatDriver(const char *namestr)
|
|
|
|
{
|
|
|
|
return (strncasecmp(namestr, "sqlite", 5) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *SqliteDriver::NameString()
|
|
|
|
{
|
|
|
|
return "sqlite";
|
|
|
|
}
|
|
|
|
|
2007-10-02 23:38:31 +00:00
|
|
|
int busy_handler(void *unused1, int unused2)
|
|
|
|
{
|
2013-02-13 07:14:37 +00:00
|
|
|
#if defined __linux__ || defined __APPLE__
|
2007-10-02 23:38:31 +00:00
|
|
|
usleep(100000);
|
|
|
|
#else
|
|
|
|
Sleep(100);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2007-10-26 00:10:09 +00:00
|
|
|
IDatabase *SqliteDriver::Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
return Connect(info, errcode, error, maxlength);
|
|
|
|
}
|
2007-10-02 23:38:31 +00:00
|
|
|
|
2006-06-03 19:52:21 +00:00
|
|
|
IDatabase *SqliteDriver::Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength)
|
|
|
|
{
|
|
|
|
sqlite3 *pSql;
|
|
|
|
int err = sqlite3_open(info->database, &pSql);
|
|
|
|
if (err != SQLITE_OK)
|
|
|
|
{
|
|
|
|
if (errcode)
|
|
|
|
{
|
|
|
|
*errcode = sqlite3_errcode(pSql);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
{
|
2014-08-08 10:44:37 +00:00
|
|
|
UTIL_Format(error, maxlength, "%s", sqlite3_errmsg(pSql));
|
2006-06-03 19:52:21 +00:00
|
|
|
}
|
|
|
|
sqlite3_close(pSql);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2007-10-02 23:38:31 +00:00
|
|
|
sqlite3_busy_handler(pSql, busy_handler, NULL);
|
2006-06-03 19:52:21 +00:00
|
|
|
SqliteDatabase *pDb = new SqliteDatabase(pSql, this);
|
|
|
|
return static_cast<IDatabase *>(pDb);
|
|
|
|
}
|
|
|
|
}
|
2007-10-02 23:38:31 +00:00
|
|
|
|
2009-01-08 20:02:09 +00:00
|
|
|
int SqliteDriver::QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize)
|
|
|
|
{
|
|
|
|
char *res = sqlite3_snprintf(static_cast<int>(maxlen), buffer, "%q", str);
|
|
|
|
|
|
|
|
if (res != NULL && newsize != NULL)
|
|
|
|
{
|
|
|
|
*newsize = strlen(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|