Move dlls/ to modules/

This commit is contained in:
xPaw
2015-03-13 15:18:47 +02:00
parent 54c978addb
commit e09f434ed8
365 changed files with 2233 additions and 2233 deletions

View File

@ -0,0 +1,187 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_DATABASE2_H
#define _INCLUDE_SOURCEMOD_DATABASE2_H
#include <stdarg.h>
namespace SourceMod
{
class IResultRow
{
public:
virtual ~IResultRow() { };
public:
/**
* This will return NULL if the entry is NULL.
* Remember that in SQL, a field can have NULL
* entries, which are not the same as 0 or "".
*/
virtual const char *GetString(unsigned int columnId) =0;
virtual double GetDouble(unsigned int columnId) =0;
virtual float GetFloat(unsigned int columnId) =0;
virtual int GetInt(unsigned int columnId) =0;
virtual bool IsNull(unsigned int columnId) =0;
/**
* NULL can be returned. The length will be zero if so.
*/
virtual const char *GetRaw(unsigned int columnId, size_t *length) =0;
};
class IResultSet
{
public:
virtual ~IResultSet() { };
public:
//free the handle if necessary (see IQuery).
virtual void FreeHandle() =0;
public: //Basic stuff
virtual unsigned int RowCount() =0;
virtual unsigned int FieldCount() =0;
virtual const char *FieldNumToName(unsigned int num) =0;
virtual bool FieldNameToNum(const char *name, unsigned int *columnId) =0;
public: //iteration
/**
* Returns true if there are no more handles left.
*/
virtual bool IsDone() =0;
/**
* Returns the current row. If "IsDone()" is false
* this is guaranteed to return non-NULL.
* Handles to IResultRow are guaranteed to not leak
* (you don't need to free them), however,
* they should be considered volatile - don't cache
* them.
*/
virtual IResultRow *GetRow() =0;
/**
* Advances to the next row. Note that you need to
* call IsDone() after each call to NextRow().
*/
virtual void NextRow() =0;
/**
* Resets back to the first row.
*/
virtual void Rewind() =0;
/* Always returns false in Sqlite */
virtual bool NextResultSet() =0;
};
struct QueryInfo
{
IResultSet *rs;
unsigned long long affected_rows;
int errorcode;
bool success;
unsigned long long insert_id;
};
class IQuery
{
public:
virtual ~IQuery() { };
public:
//you must free the handle when done
virtual void FreeHandle() =0;
public:
/**
* Executes the query. Specify optional error string buffer.
* If "info" is NULL, no results will be stored.
* Returns false on failure.
* Calling Execute() multiple times will cause each result handle
* to be freed in succession. That means that you do not need to
* explicitly free IResultSets when using Execute(), but their
* handles are deep-invalidated on succesive calls, and
* thus Execute() is also not thread safe.
*/
virtual bool Execute(QueryInfo *info, char *error, size_t maxlength) =0;
/**
* Same as above, except result handles are not freed for you.
*/
virtual bool ExecuteR(QueryInfo *info, char *error, size_t maxlength) =0;
/**
* Returns the query string.
*/
virtual const char *GetQueryString() =0;
/**
* Same as execute, but supports insert_id
*/
virtual bool Execute2(QueryInfo *info, char *error, size_t maxlength) =0;
};
class ISQLDriver;
class IDatabase
{
public:
virtual ~IDatabase() { };
public:
/**
* Closes the database and frees the handle.
*/
virtual void FreeHandle() =0;
/**
* Returns the parent driver.
*/
virtual ISQLDriver *Driver() =0;
public:
/**
* Query preparation.
*/
virtual IQuery *PrepareQueryFmt(const char *fmt, ...) =0;
virtual IQuery *PrepareQueryFmt(const char *fmt, va_list ap) =0;
virtual IQuery *PrepareQuery(const char *query) =0;
/**
* Quotes a string properly.
* Returns 0 on success. On failure, returns
* the size of the buffer needed, or a negative number
* on internal failure.
*/
virtual int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize) =0;
/**
* @brief Sets the character set of the current connection
*
* @param characterset The characterset to switch to. e.g. "utf8".
*/
virtual bool SetCharacterSet(const char *characterset) =0;
};
struct DatabaseInfo
{
DatabaseInfo() : max_timeout(0) { };
const char *host;
const char *database;
const char *user;
const char *pass;
unsigned int port;
unsigned int max_timeout;
const char *charset;
};
class ISQLDriver
{
public:
virtual ~ISQLDriver() { };
public:
virtual IDatabase *Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength) =0;
//Supports the timeout clause
virtual IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength) =0;
virtual const char *NameString() =0;
virtual bool IsCompatDriver(const char *namestring) =0;
};
};
#endif //_INCLUDE_SOURCEMOD_DATABASE2_H

View File

@ -0,0 +1,99 @@
// 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
//
#include <stdio.h>
#include <string.h>
#include "SqliteDriver.h"
#include "SqliteDatabase.h"
#include "SqliteQuery.h"
#if defined WIN32 && !defined vsnprintf
#define vsnprintf _vsnprintf
#endif
using namespace SourceMod;
SqliteDatabase::SqliteDatabase(sqlite3 *sql, SqliteDriver *drvr) :
m_pSql(sql), m_pParent(drvr)
{
}
SqliteDatabase::~SqliteDatabase()
{
Disconnect();
}
void SqliteDatabase::Disconnect()
{
if (m_pSql)
{
sqlite3_close(m_pSql);
m_pSql = NULL;
}
}
void SqliteDatabase::FreeHandle()
{
delete this;
}
ISQLDriver *SqliteDatabase::Driver()
{
return static_cast<ISQLDriver *>(m_pParent);
}
IQuery *SqliteDatabase::PrepareQuery(const char *query)
{
SqliteQuery *pQuery = new SqliteQuery(this, query);
return static_cast<IQuery *>(pQuery);
}
IQuery *SqliteDatabase::PrepareQueryFmt(const char *fmt, va_list ap)
{
char buffer[4096];
vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
return PrepareQuery(buffer);
}
IQuery *SqliteDatabase::PrepareQueryFmt(const char *fmt, ...)
{
va_list ap;
IQuery *qry;
va_start(ap, fmt);
qry = PrepareQueryFmt(fmt, ap);
va_end(ap);
return qry;
}
int SqliteDatabase::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;
}
bool SqliteDatabase::SetCharacterSet(const char *characterset)
{
// sqlite only supports utf8 and utf16 - by the time the database is created. It's too late here.
return false;
}

View File

@ -0,0 +1,48 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_SQLITE_DATABASE_H
#define _INCLUDE_SOURCEMOD_SQLITE_DATABASE_H
#include "SqliteHeaders.h"
#include "SqliteDriver.h"
namespace SourceMod
{
class SqliteDriver;
class SqliteDatabase : public IDatabase
{
friend class SqliteQuery;
public:
SqliteDatabase(sqlite3 *sql, SqliteDriver *drvr);
~SqliteDatabase();
public:
void FreeHandle();
ISQLDriver *Driver();
public:
IQuery *PrepareQueryFmt(const char *fmt, ...);
IQuery *PrepareQueryFmt(const char *fmt, va_list ap);
IQuery *PrepareQuery(const char *query);
int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize);
bool SetCharacterSet(const char *characterset);
private:
void Disconnect();
private:
sqlite3 *m_pSql;
SqliteDriver *m_pParent;
};
};
#endif //_INCLUDE_SOURCEMOD_SQLITE_DATABASE_H

View File

@ -0,0 +1,87 @@
// 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
//
#include "amxxmodule.h"
#include "SqliteHeaders.h"
#include "SqliteDriver.h"
#include "SqliteDatabase.h"
#if defined WIN32
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace SourceMod;
bool SqliteDriver::IsCompatDriver(const char *namestr)
{
return (strncasecmp(namestr, "sqlite", 5) == 0);
}
const char *SqliteDriver::NameString()
{
return "sqlite";
}
int busy_handler(void *unused1, int unused2)
{
#if defined __linux__ || defined __APPLE__
usleep(100000);
#else
Sleep(100);
#endif
return 1;
}
IDatabase *SqliteDriver::Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength)
{
return Connect(info, errcode, error, maxlength);
}
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)
{
UTIL_Format(error, maxlength, "%s", sqlite3_errmsg(pSql));
}
sqlite3_close(pSql);
return NULL;
} else {
sqlite3_busy_handler(pSql, busy_handler, NULL);
SqliteDatabase *pDb = new SqliteDatabase(pSql, this);
return static_cast<IDatabase *>(pDb);
}
}
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;
}

View File

@ -0,0 +1,32 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_SQLITE_DRIVER_H
#define _INCLUDE_SOURCEMOD_SQLITE_DRIVER_H
#include "SqliteHeaders.h"
namespace SourceMod
{
class SqliteDriver : public ISQLDriver
{
public:
IDatabase *Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength);
IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength);
const char *NameString();
bool IsCompatDriver(const char *namestr);
int QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newsize);
};
};
#endif //_INCLUDE_SOURCEMOD_SQLITE_DRIVER_H

View File

@ -0,0 +1,30 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_SQLITE_HEADERS_H
#define _INCLUDE_SOURCEMOD_SQLITE_HEADERS_H
#if _MSC_VER >= 1400
/* disable deprecation warnings */
#if !defined _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#pragma warning (disable:4996)
#endif //_MSC_VER >= 1400
#include <ISQLDriver.h>
#include "sqlite3.h"
#endif //_INCLUDE_SOURCEMOD_SQLITE_HEADERS_H

View File

@ -0,0 +1,124 @@
// 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
//
#include "amxxmodule.h"
#include "SqliteQuery.h"
#include "SqliteDatabase.h"
#include "SqliteResultSet.h"
using namespace SourceMod;
SqliteQuery::SqliteQuery(SqliteDatabase *db, const char *query) :
m_pDatabase(db), m_LastRes(NULL)
{
m_QueryString = new char[strlen(query)+1];
strcpy(m_QueryString, query);
}
SqliteQuery::~SqliteQuery()
{
if (m_LastRes)
{
m_LastRes->FreeHandle();
m_LastRes = NULL;
}
delete [] m_QueryString;
}
void SqliteQuery::FreeHandle()
{
delete this;
}
bool SqliteQuery::Execute(QueryInfo *info, char *error, size_t maxlength)
{
bool res = ExecuteR(info, error, maxlength);
if (m_LastRes)
{
m_LastRes->FreeHandle();
}
m_LastRes = (SqliteResultSet *)info->rs;
return res;
}
bool SqliteQuery::Execute2(QueryInfo *info, char *error, size_t maxlength)
{
bool res = ExecuteR(info, error, maxlength);
if (m_LastRes)
m_LastRes->FreeHandle();
m_LastRes = (SqliteResultSet *)info->rs;
if (info->success)
{
info->insert_id = sqlite3_last_insert_rowid(m_pDatabase->m_pSql);
} else {
info->insert_id = 0;
}
return res;
}
const char *SqliteQuery::GetQueryString()
{
return m_QueryString;
}
bool SqliteQuery::ExecuteR(QueryInfo *info, char *error, size_t maxlength)
{
int err;
char *errmsg;
char **results;
int rows, cols;
err = sqlite3_get_table(m_pDatabase->m_pSql, m_QueryString, &results, &rows, &cols, &errmsg);
if (err != SQLITE_OK)
{
if (error && maxlength && errmsg)
{
UTIL_Format(error, maxlength, "%s", errmsg);
}
info->affected_rows = 0;
info->errorcode = err;
info->rs = NULL;
info->success = false;
} else {
info->affected_rows = sqlite3_changes(m_pDatabase->m_pSql);
info->errorcode = 0;
info->success = true;
if (cols)
{
SqliteResults data;
data.cols = cols;
data.rows = rows;
data.results = results;
SqliteResultSet *pRes = new SqliteResultSet(data);
info->rs = static_cast<IResultSet *>(pRes);
} else {
info->rs = NULL;
if (results)
{
sqlite3_free_table(results);
}
}
}
return info->success;
}

View File

@ -0,0 +1,49 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_SQLITE_QUERY_H
#define _INCLUDE_SOURCEMOD_SQLITE_QUERY_H
#include "SqliteHeaders.h"
namespace SourceMod
{
class SqliteDatabase;;
class SqliteResultSet;
class SqliteQuery : public IQuery
{
public:
struct SqliteResults
{
char **results;
int rows;
int cols;
};
public:
SqliteQuery(SqliteDatabase *db, const char *query);
~SqliteQuery();
public:
void FreeHandle();
bool Execute(QueryInfo *info, char *error, size_t maxlength);
bool ExecuteR(QueryInfo *info, char *error, size_t maxlength);
bool Execute2(QueryInfo *info, char *error, size_t maxlength);
const char *GetQueryString();
private:
SqliteDatabase *m_pDatabase;
SqliteResultSet *m_LastRes;
char *m_QueryString;
};
};
#endif //_INCLUDE_SOURCEMOD_SQLITE_QUERY_H

View File

@ -0,0 +1,184 @@
// 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
//
#include <string.h>
#include <stdlib.h>
#include "SqliteResultSet.h"
using namespace SourceMod;
SqliteResultSet::SqliteResultSet(SqliteQuery::SqliteResults &res)
{
m_pResults = res.results;
m_Columns = res.cols;
m_Rows = res.rows;
m_CurRow = 1;
m_CurIndex = (m_CurRow * m_Columns);
}
SqliteResultSet::~SqliteResultSet()
{
if (m_pResults)
{
sqlite3_free_table(m_pResults);
m_pResults = NULL;
}
}
const char *SqliteResultSet::GetStringSafe(unsigned int columnId)
{
if (columnId > m_Columns)
{
return "";
}
const char *data = m_pResults[m_CurIndex + columnId];
return data ? data : "";
}
const char *SqliteResultSet::GetString(unsigned int columnId)
{
if (columnId > m_Columns)
{
return NULL;
}
return m_pResults[m_CurIndex + columnId];
}
bool SqliteResultSet::IsNull(unsigned int columnId)
{
return (GetString(columnId) == NULL);
}
double SqliteResultSet::GetDouble(unsigned int columnId)
{
return atof(GetStringSafe(columnId));
}
float SqliteResultSet::GetFloat(unsigned int columnId)
{
return (float)atof(GetStringSafe(columnId));
}
int SqliteResultSet::GetInt(unsigned int columnId)
{
return atoi(GetStringSafe(columnId));
}
/**
* :TODO: - convert this whole beast to sqlite3_prepare/step
* that way we get finer control and actual raw/null data.
*/
const char *SqliteResultSet::GetRaw(unsigned int columnId, size_t *length)
{
if (columnId >= m_Columns)
{
if (length)
{
*length = 0;
}
return NULL;
}
const char *str = GetString(columnId);
if (!str)
{
if (length)
{
*length = 0;
}
return NULL;
} else {
if (length)
{
*length = strlen(str);
}
return str;
}
}
void SqliteResultSet::FreeHandle()
{
delete this;
}
IResultRow *SqliteResultSet::GetRow()
{
return static_cast<IResultRow *>(this);
}
unsigned int SqliteResultSet::RowCount()
{
return m_Rows;
}
const char *SqliteResultSet::FieldNumToName(unsigned int num)
{
if (num >= m_Columns)
{
return NULL;
}
return m_pResults[num];
}
bool SqliteResultSet::FieldNameToNum(const char *name, unsigned int *columnId)
{
for (unsigned int i=0; i<m_Columns; i++)
{
if (strcmp(m_pResults[i], name) == 0)
{
if (columnId)
{
*columnId = i;
}
return true;
}
}
if (columnId)
{
*reinterpret_cast<int *>(columnId) = -1;
}
return false;
}
unsigned int SqliteResultSet::FieldCount()
{
return m_Columns;
}
bool SqliteResultSet::IsDone()
{
return (m_CurRow > m_Rows);
}
void SqliteResultSet::NextRow()
{
m_CurIndex = (++m_CurRow * m_Columns);
}
void SqliteResultSet::Rewind()
{
m_CurRow = 1;
m_CurIndex = (m_CurRow * m_Columns);
}
bool SqliteResultSet::NextResultSet()
{
return false;
}

View File

@ -0,0 +1,68 @@
// 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
//
#ifndef _INCLUDE_SOURCEMOD_SQLITE_RESULTSET_H
#define _INCLUDE_SOURCEMOD_SQLITE_RESULTSET_H
#include "SqliteHeaders.h"
#include "SqliteDriver.h"
#include "SqliteDatabase.h"
#include "SqliteQuery.h"
namespace SourceMod
{
class SqliteResultSet : public IResultSet, public IResultRow
{
/**
* IResultSet
*/
public:
SqliteResultSet(SqliteQuery::SqliteResults &res);
~SqliteResultSet();
public:
void FreeHandle();
public:
unsigned int RowCount();
unsigned int FieldCount();
const char *FieldNumToName(unsigned int num);
bool FieldNameToNum(const char *name, unsigned int *columnId);
public:
bool IsDone();
IResultRow *GetRow();
void NextRow();
void Rewind();
public:
/**
* IResultRow
*/
public:
const char *GetString(unsigned int columnId);
double GetDouble(unsigned int columnId);
float GetFloat(unsigned int columnId);
int GetInt(unsigned int columnId);
bool IsNull(unsigned int columnId);
const char *GetRaw(unsigned int columnId, size_t *length);
bool NextResultSet();
private:
const char *GetStringSafe(unsigned int columnId);
private:
char **m_pResults;
unsigned int m_Columns;
unsigned int m_Rows;
unsigned int m_CurRow;
unsigned int m_CurIndex;
};
};
#endif //_INCLUDE_SOURCEMOD_SQLITE_RESULTSET_H