2006-04-23 05:10:06 +04:00
|
|
|
#ifndef _INCLUDE_SOURCEMOD_DATABASE2_H
|
|
|
|
#define _INCLUDE_SOURCEMOD_DATABASE2_H
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
namespace SourceMod
|
|
|
|
{
|
|
|
|
class IResultRow
|
|
|
|
{
|
2006-08-27 06:17:18 +04:00
|
|
|
public:
|
|
|
|
virtual ~IResultRow() { };
|
2006-04-23 05:10:06 +04:00
|
|
|
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
|
|
|
|
{
|
2006-08-27 06:17:18 +04:00
|
|
|
public:
|
|
|
|
virtual ~IResultSet() { };
|
2006-04-23 05:10:06 +04:00
|
|
|
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;
|
2007-04-24 19:46:33 +04:00
|
|
|
/**
|
|
|
|
* Rewinds to the first row.
|
|
|
|
*/
|
|
|
|
virtual void Rewind() =0;
|
2007-10-23 01:31:02 +04:00
|
|
|
/**
|
|
|
|
* Transitions to the next result set, if any is available.
|
|
|
|
*/
|
|
|
|
virtual bool NextResultSet() =0;
|
2006-04-23 05:10:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct QueryInfo
|
|
|
|
{
|
|
|
|
IResultSet *rs;
|
|
|
|
unsigned long long affected_rows;
|
|
|
|
int errorcode;
|
|
|
|
bool success;
|
2006-08-28 23:17:26 +04:00
|
|
|
unsigned long long insert_id;
|
2006-04-23 05:10:06 +04:00
|
|
|
};
|
|
|
|
|
2006-08-28 23:17:26 +04:00
|
|
|
class IQuery
|
2006-04-23 05:10:06 +04:00
|
|
|
{
|
2006-08-27 06:17:18 +04:00
|
|
|
public:
|
|
|
|
virtual ~IQuery() { };
|
2006-04-23 05:10:06 +04:00
|
|
|
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;
|
2006-08-18 06:19:38 +04:00
|
|
|
/**
|
|
|
|
* Returns the query string.
|
|
|
|
*/
|
|
|
|
virtual const char *GetQueryString() =0;
|
2006-08-28 23:17:26 +04:00
|
|
|
/**
|
|
|
|
* Same as execute, but supports insert_id
|
|
|
|
*/
|
|
|
|
virtual bool Execute2(QueryInfo *info, char *error, size_t maxlength) =0;
|
2006-04-23 05:10:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ISQLDriver;
|
|
|
|
|
|
|
|
class IDatabase
|
|
|
|
{
|
2006-08-27 06:17:18 +04:00
|
|
|
public:
|
|
|
|
virtual ~IDatabase() { };
|
2006-04-23 05:10:06 +04:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DatabaseInfo
|
|
|
|
{
|
2006-10-01 23:46:56 +04:00
|
|
|
DatabaseInfo() : max_timeout(0) { };
|
2006-04-23 05:10:06 +04:00
|
|
|
const char *host;
|
|
|
|
const char *database;
|
|
|
|
const char *user;
|
|
|
|
const char *pass;
|
|
|
|
unsigned int port;
|
2006-10-01 23:46:56 +04:00
|
|
|
unsigned int max_timeout;
|
2006-04-23 05:10:06 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ISQLDriver
|
|
|
|
{
|
2006-08-27 06:17:18 +04:00
|
|
|
public:
|
|
|
|
virtual ~ISQLDriver() { };
|
2006-04-23 05:10:06 +04:00
|
|
|
public:
|
|
|
|
virtual IDatabase *Connect(DatabaseInfo *info, int *errcode, char *error, size_t maxlength) =0;
|
2006-10-01 23:46:56 +04:00
|
|
|
//Supports the timeout clause
|
|
|
|
virtual IDatabase *Connect2(DatabaseInfo *info, int *errcode, char *error, size_t maxlength) =0;
|
2006-04-23 05:10:06 +04:00
|
|
|
virtual const char *NameString() =0;
|
|
|
|
virtual bool IsCompatDriver(const char *namestring) =0;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif //_INCLUDE_SOURCEMOD_DATABASE2_H
|