public/IDBDriver.h
author Matt Woodrow <pred@alliedmods.net>
Sun Jul 06 03:30:06 2008 +0000 (2008-07-06)
changeset 2165 ca3757e676f6
parent 2134 f1d8a7cd3c94
child 2289 44045ead61cd
permissions -rw-r--r--
Fixed requiring dependencies at the wrong time.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SOURCEMOD_INTERFACE_DBDRIVER_H_
ds@1952
    33
#define _INCLUDE_SOURCEMOD_INTERFACE_DBDRIVER_H_
ds@1952
    34
ds@1952
    35
#include <IShareSys.h>
ds@1952
    36
#include <IHandleSys.h>
ds@1952
    37
#include <string.h>
ds@1952
    38
ds@1952
    39
/**
ds@1952
    40
 * @file IDBDriver.h
ds@1952
    41
 * @brief Defines interfaces for interacting with relational databases.
ds@1952
    42
 */
ds@1952
    43
ds@1952
    44
#define SMINTERFACE_DBI_NAME		"IDBI"
dvander@2134
    45
#define SMINTERFACE_DBI_VERSION		7
ds@1952
    46
ds@1952
    47
namespace SourceMod
ds@1952
    48
{
ds@1952
    49
	/**
ds@1952
    50
	 * @brief Describes a database field value.
ds@1952
    51
	 */
ds@1952
    52
	enum DBResult
ds@1952
    53
	{
ds@1952
    54
		DBVal_Error = 0,		/**< Column number/field is invalid */
ds@1952
    55
		DBVal_TypeMismatch = 1,	/**< You cannot retrieve this data with this type */
ds@1952
    56
		DBVal_Null = 2,			/**< Field has no data (NULL) */
ds@1952
    57
		DBVal_Data = 3,			/**< Field has data */
ds@1952
    58
	};
ds@1952
    59
ds@1952
    60
	/**
ds@1952
    61
	 * @brief Describes a primitive database type.
ds@1952
    62
	 */
ds@1952
    63
	enum DBType
ds@1952
    64
	{
ds@1952
    65
		DBType_Unknown = 0,		/**< Type could not be inferred */
ds@1952
    66
		DBType_String,			/**< NULL-terminated string (variable length) */
ds@1952
    67
		DBType_Blob,			/**< Raw binary data (variable length) */
ds@1952
    68
		DBType_Integer,			/**< 4-byte signed integer */
ds@1952
    69
		DBType_Float,			/**< 4-byte floating point  */
ds@1952
    70
		DBType_NULL,			/**< NULL (no data) */
ds@1952
    71
		/* --------- */
ds@1952
    72
		DBTypes_TOTAL,			/**< Total number of database types known */
ds@1952
    73
	};
ds@1952
    74
ds@1952
    75
	/**
ds@1952
    76
	 * @brief Represents a one database result row.
ds@1952
    77
	 *
ds@1952
    78
	 * Note that type mismatches will only occur when type safety is being
ds@1952
    79
	 * enforced.  So far this is only the case for prepared statements in 
ds@1952
    80
	 * MySQL and SQLite.
ds@1952
    81
	 *
ds@1952
    82
	 * Also, it is worth noting that retrieving as raw data will never cause a
ds@1952
    83
	 * type mismatch.
ds@1952
    84
	 */
ds@1952
    85
	class IResultRow
ds@1952
    86
	{
ds@1952
    87
	public:
ds@1952
    88
		/**
ds@1952
    89
		 * @brief Retrieves a database field result as a string.
ds@1952
    90
		 *
ds@1952
    91
		 * For NULL values, the resulting string pointer will be non-NULL but 
ds@1952
    92
		 * empty.  The pointer returned will become invalid after  advancing to
ds@1952
    93
		 * the next row.
ds@1952
    94
		 *
ds@1952
    95
		 * @param columnId		Column to use, starting from 0.
ds@1952
    96
		 * @param pString		Pointer to store a pointer to the string.
ds@1952
    97
		 * @param length		Optional pointer to store the string length.
ds@1952
    98
		 * @return				A DBResult return code.
ds@1952
    99
		 */
ds@1952
   100
		virtual DBResult GetString(unsigned int columnId, const char **pString, size_t *length) =0;
ds@1952
   101
ds@1952
   102
		/**
ds@1952
   103
		 * @brief Retrieves a database field result as a string, using a 
ds@1952
   104
		 * user-supplied buffer.  If the field is NULL, an empty string
ds@1952
   105
		 * will be copied.
ds@1952
   106
		 *
ds@1952
   107
		 * @param columnId		Column to use, starting from 0.
ds@1952
   108
		 * @param buffer		Buffer to store string in.
ds@1952
   109
		 * @param maxlength		Maximum length of the buffer.
ds@1952
   110
		 * @param written		Optional pointer to store the number of bytes 
ds@1952
   111
		 *						written, excluding the null terminator.
ds@1952
   112
		 * @return				A DBResult return code.
ds@1952
   113
		 */
ds@1952
   114
		virtual DBResult CopyString(unsigned int columnId, 
ds@1952
   115
									char *buffer, 
ds@1952
   116
									size_t maxlength, 
ds@1952
   117
									size_t *written) =0;
ds@1952
   118
		
ds@1952
   119
		/**
ds@1952
   120
		 * @brief Retrieves a database field result as a float.
ds@1952
   121
		 *
ds@1952
   122
		 * For NULL entries, the returned float value will be 0.0.
ds@1952
   123
		 *
ds@1952
   124
		 * @param columnId		Column to use, starting from 0.
ds@1952
   125
		 * @param pFloat		Pointer to a floating point number to set.
ds@1952
   126
		 * @return				A DBResult return code.
ds@1952
   127
		 */
ds@1952
   128
		virtual DBResult GetFloat(unsigned int columnId, float *pFloat) =0;
ds@1952
   129
ds@1952
   130
		/**
ds@1952
   131
		 * @brief Retrieves a database field result as an integer.
ds@1952
   132
		 *
ds@1952
   133
		 * For NULL entries, the returned integer value will be 0.
ds@1952
   134
		 *
ds@1952
   135
		 * @param columnId		Column to use, starting from 0.
ds@1952
   136
		 * @param pInt			Pointer to an integer number to set.
ds@1952
   137
		 * @return				A DBResult return code.
ds@1952
   138
		 */
ds@1952
   139
		virtual DBResult GetInt(unsigned int columnId, int *pInt) =0;
ds@1952
   140
ds@1952
   141
		/**
ds@1952
   142
		 * @brief Returns whether or not a field is NULL.
ds@1952
   143
		 *
ds@1952
   144
		 * @param columnId		Column to use, starting from 0.
ds@1952
   145
		 * @return				True if field is NULL, false otherwise.
ds@1952
   146
		 */
ds@1952
   147
		virtual bool IsNull(unsigned int columnId) =0;
ds@1952
   148
ds@1952
   149
		/**
ds@1952
   150
		 * @brief Returns the size of a field (text/raw/blob) in bytes.
ds@1952
   151
		 * For strings, this returned size will not include the null
ds@1952
   152
		 * terminator.
ds@1952
   153
		 * 
ds@1952
   154
		 * When used on fields that are not of variable length,
ds@1952
   155
		 * the size returned will be the number of bytes required
ds@1952
   156
		 * to store the internal data.  Note that the data size 
ds@1952
   157
		 * will correspond to the ACTUAL data type, not the 
ds@1952
   158
		 * COLUMN type.
ds@1952
   159
		 *
ds@1952
   160
		 * @param columnId		Column to use, starting from 0.
ds@1952
   161
		 * @return				Number of bytes required to store
ds@1952
   162
		 *						the data, or 0 on failure.
ds@1952
   163
		 */
ds@1952
   164
		virtual size_t GetDataSize(unsigned int columnId) =0;
ds@1952
   165
ds@1952
   166
		/**
ds@1952
   167
		 * @brief Retrieves field data as a raw bitstream.  The pointer returned
ds@1952
   168
		 * will become invalid after advancing to the next row.
ds@1952
   169
		 *
ds@1952
   170
		 * @param columnId		Column to use, starting from 0.
ds@1952
   171
		 * @param pData			Pointer to store the raw bit stream.  If the 
ds@1952
   172
		 *						data is NULL, a NULL pointer will be returned.
ds@1952
   173
		 * @param length		Pointer to store the data length.
ds@1952
   174
		 * @return				A DBResult return code.
ds@1952
   175
		 */
ds@1952
   176
		virtual DBResult GetBlob(unsigned int columnId, const void **pData, size_t *length) =0;
ds@1952
   177
ds@1952
   178
		/**
ds@1952
   179
		 * @brief Copies field data as a raw bitstream.
ds@1952
   180
		 *
ds@1952
   181
		 * @param columnId		Column to use, starting from 0.
ds@1952
   182
		 * @param buffer		Pointer to copy the data to.  If the data is
ds@1952
   183
		 *						NULL, no data will be copied.
ds@1952
   184
		 * @param maxlength		Maximum length of the buffer.
ds@1952
   185
		 * @param written		Optional pointer to store the number of bytes 
ds@1952
   186
		 *						written.
ds@1952
   187
		 * @return				A DBResult return code.
ds@1952
   188
		 */
ds@1952
   189
		virtual DBResult CopyBlob(unsigned int columnId, void *buffer, size_t maxlength, size_t *written) =0;
ds@1952
   190
	};
ds@1952
   191
ds@1952
   192
	/**
ds@1952
   193
	 * @brief Describes a set of database results.
ds@1952
   194
	 */
ds@1952
   195
	class IResultSet
ds@1952
   196
	{
ds@1952
   197
	public:
ds@1952
   198
		/**
ds@1952
   199
		 * @brief Returns the number of rows in the set.
ds@1952
   200
		 *
ds@1952
   201
		 * @return				Number of rows in the set.
ds@1952
   202
		 */
ds@1952
   203
		virtual unsigned int GetRowCount() =0;
ds@1952
   204
ds@1952
   205
		/**
ds@1952
   206
		 * @brief Returns the number of fields in the set.
ds@1952
   207
		 *
ds@1952
   208
		 * @return				Number of fields in the set.
ds@1952
   209
		 */
ds@1952
   210
		virtual unsigned int GetFieldCount() =0;
ds@1952
   211
ds@1952
   212
		/**
ds@1952
   213
		 * @brief Converts a column number to a column name.
ds@1952
   214
		 *
ds@1952
   215
		 * @param columnId		Column to use, starting from 0.
ds@1952
   216
		 * @return				Pointer to column name, or NULL if not found.
ds@1952
   217
		 */
ds@1952
   218
		virtual const char *FieldNumToName(unsigned int columnId) =0;
ds@1952
   219
ds@1952
   220
		/**
ds@1952
   221
		 * @brief Converts a column name to a column id.
ds@1952
   222
		 *
ds@1952
   223
		 * @param name			Column name (case sensitive).
ds@1952
   224
		 * @param columnId		Pointer to store the column id.  If the
ds@1952
   225
		 *						name is not found, the value will be
ds@1952
   226
		 *						undefined.
ds@1952
   227
		 * @return				True on success, false if not found.
ds@1952
   228
		 */
ds@1952
   229
		virtual bool FieldNameToNum(const char *name, unsigned int *columnId) =0;
ds@1952
   230
ds@1952
   231
		/**
ds@1952
   232
		 * @brief Returns if there is still data in the result set.
ds@1952
   233
		 *
ds@1952
   234
		 * @return				False if there is more data to be read, 
ds@1952
   235
		 *						true, otherwise.
ds@1952
   236
		 */
ds@1952
   237
		virtual bool MoreRows() =0;
ds@1952
   238
ds@1952
   239
		/**
ds@1952
   240
		 * @brief Returns a pointer to the current row and advances
ds@1952
   241
		 * the internal row pointer/counter to the next row available.
ds@1952
   242
		 *
ds@1952
   243
		 * @return				IResultRow pointer to the current row,
ds@1952
   244
		 *						or NULL if there is no more data.
ds@1952
   245
		 */
ds@1952
   246
		virtual IResultRow *FetchRow() =0;
ds@1952
   247
ds@1952
   248
		/**
ds@1952
   249
		 * @brief Returns a pointer to the current row.
ds@1952
   250
		 *
ds@1952
   251
		 * @return				IResultRow pointer to the current row,
ds@1952
   252
		 *						or NULL if the current row is invalid.
ds@1952
   253
		 */
ds@1952
   254
		virtual IResultRow *CurrentRow() =0;
ds@1952
   255
ds@1952
   256
		/**
ds@1952
   257
		 * @brief Rewinds back to the beginning of the row iteration.
ds@1952
   258
		 * 
ds@1952
   259
		 * @return				True on success, false otherwise.
ds@1952
   260
		 */
ds@1952
   261
		virtual bool Rewind() =0;
ds@1952
   262
ds@1952
   263
		/**
ds@1952
   264
		 * @brief Returns a field's type as it should be interpreted
ds@1952
   265
		 * by the user.
ds@1952
   266
		 *
ds@1952
   267
		 * @param field			Field number (starting from 0).
ds@1952
   268
		 * @return				A DBType value.
ds@1952
   269
		 */
ds@1952
   270
		virtual DBType GetFieldType(unsigned int field) =0;
ds@1952
   271
ds@1952
   272
		/**
ds@1952
   273
		 * @brief Returns a field's type as it will be interpreted
ds@1952
   274
		 * by the GetDataSize() function.  For example, MySQL
ds@1952
   275
		 * for non-prepared queries will store all results as
ds@1952
   276
		 * strings internally.
ds@1952
   277
		 *
ds@1952
   278
		 * @param field			Field number (starting from 0).
ds@1952
   279
		 * @return				A DBType value.
ds@1952
   280
		 */
ds@1952
   281
		virtual DBType GetFieldDataType(unsigned int field) =0;
ds@1952
   282
	};
ds@1952
   283
ds@1952
   284
	class IDBDriver;
ds@1952
   285
ds@1952
   286
	class IQuery
ds@1952
   287
	{
ds@1952
   288
	public:
ds@1952
   289
		/**
ds@1952
   290
		 * @brief Returns a pointer to the current result set, if any.  
ds@1952
   291
		 *
ds@1952
   292
		 * @return				An IResultSet pointer on success,
ds@1952
   293
		 *						NULL if no result set exists.
ds@1952
   294
		 */
ds@1952
   295
		virtual IResultSet *GetResultSet() =0;
ds@1952
   296
ds@1952
   297
		/**
ds@1952
   298
		 * @brief Advances to the next result set if one exists.  This
ds@1952
   299
		 * is for checking for MORE result sets, and should not be used
ds@1952
   300
		 * on the first result set.
ds@1952
   301
		 *
ds@1952
   302
		 * Multiple results only happen in certain cases, such as CALLing
ds@1952
   303
		 * stored procedure that have a SELECTs, where MySQL will return 
ds@1952
   304
		 * both the CALL status and one or more SELECT result sets.  If
ds@1952
   305
		 * you do not process these results, they will be automatically
ds@1952
   306
		 * processed for you.  However, the behaviour of creating a new
ds@1952
   307
		 * query from the same connection while results are left 
ds@1952
   308
		 * unprocessed is undefined, and may result in a dropped 
ds@1952
   309
		 * connection.  Therefore, process all extra results or destroy the
ds@1952
   310
		 * IQuery pointer before starting a new query.
ds@1952
   311
		 *
ds@1952
   312
		 * Again, this only happens in very specific cases, so there is
ds@1952
   313
		 * no need to call this for normal queries.
ds@1952
   314
		 *
ds@1952
   315
		 * After calling this function, GetResultSet() must be called
ds@1952
   316
		 * again to return the result set.  The previous result set
ds@1952
   317
		 * is automatically destroyed and will be unusable.
ds@1952
   318
		 *
ds@1952
   319
		 * @return				True if another result set is
ds@1952
   320
		 *						available, false otherwise.
ds@1952
   321
		 */
ds@1952
   322
		virtual bool FetchMoreResults() =0;
ds@1952
   323
ds@1952
   324
		/**
ds@1952
   325
		 * @brief Frees resources created by this query.
ds@1952
   326
		 */
ds@1952
   327
		virtual void Destroy() =0;
ds@1952
   328
	};
ds@1952
   329
ds@1952
   330
	class IPreparedQuery : public IQuery
ds@1952
   331
	{
ds@1952
   332
	public:
ds@1952
   333
		/** 
ds@1952
   334
		 * @brief Binds an integer parameter.
ds@1952
   335
		 *
ds@1952
   336
		 * @param param			Parameter index, starting from 0.
ds@1952
   337
		 * @param num			Number to bind as a value.
ds@1952
   338
		 * @param signd			True to write as signed, false to write as 
ds@1952
   339
		 *						unsigned.
ds@1952
   340
		 * @return				True on success, false otherwise.
ds@1952
   341
		 */
ds@1952
   342
		virtual bool BindParamInt(unsigned int param, int num, bool signd=true) =0;
ds@1952
   343
ds@1952
   344
		/**
ds@1952
   345
		 * @brief Binds a float parameter.
ds@1952
   346
		 *
ds@1952
   347
		 * @param param			Parameter index, starting from 0.
ds@1952
   348
		 * @param f				Float to bind as a value.
ds@1952
   349
		 * @return				True on success, false otherwise.
ds@1952
   350
		 */
ds@1952
   351
		virtual bool BindParamFloat(unsigned int param, float f) =0;
ds@1952
   352
ds@1952
   353
		/**
ds@1952
   354
		 * @brief Binds an SQL NULL type as a parameter.
ds@1952
   355
		 *
ds@1952
   356
		 * @param param			Parameter index, starting from 0.
ds@1952
   357
		 * @return				True on success, false otherwise.
ds@1952
   358
		 */
ds@1952
   359
		virtual bool BindParamNull(unsigned int param) =0;
ds@1952
   360
ds@1952
   361
		/**
ds@1952
   362
		 * @brief Binds a string as a parameter.
ds@1952
   363
		 *
ds@1952
   364
		 * @param param			Parameter index, starting from 0.
ds@1952
   365
		 * @param text			Pointer to a null-terminated string.
ds@1952
   366
		 * @param copy			If true, the pointer is assumed to be
ds@1952
   367
		 *						volatile and a temporary copy may be
ds@1952
   368
		 *						made for safety.
ds@1952
   369
		 * @return				True on success, false otherwise.
ds@1952
   370
		 */
ds@1952
   371
		virtual bool BindParamString(unsigned int param, const char *text, bool copy) =0;
ds@1952
   372
ds@1952
   373
		/**
ds@1952
   374
		 * @brief Binds a blob of raw data as a parameter.
ds@1952
   375
		 *
ds@1952
   376
		 * @param param			Parameter index, starting from 0.
ds@1952
   377
		 * @param data			Pointer to a blob of memory.
ds@1952
   378
		 * @param length		Number of bytes to copy.
ds@1952
   379
		 * @param copy			If true, the pointer is assumed to be
ds@1952
   380
		 *						volatile and a temporary copy may be
ds@1952
   381
		 *						made for safety.
ds@1952
   382
		 * @return				True on success, false otherwise.
ds@1952
   383
		 */
ds@1952
   384
		virtual bool BindParamBlob(unsigned int param, 
ds@1952
   385
									const void *data, 
ds@1952
   386
									size_t length, 
ds@1952
   387
									bool copy) =0;
ds@1952
   388
ds@1952
   389
		/**
ds@1952
   390
		 * @brief Executes the query with the currently bound parameters.
ds@1952
   391
ds@1952
   392
		 * @return				True on success, false otherwise.
ds@1952
   393
		 */
ds@1952
   394
		virtual bool Execute() =0;
ds@1952
   395
ds@1952
   396
		/**
ds@1952
   397
		 * @brief Returns the last error message from this statement.
ds@1952
   398
		 *
ds@1952
   399
		 * @param errCode		Optional pointer to store the driver-specific
ds@1952
   400
		 *						error code.
ds@1952
   401
		 * @return				Error message string.
ds@1952
   402
		 */
ds@1952
   403
		virtual const char *GetError(int *errCode=NULL) =0;
ds@1952
   404
ds@1952
   405
		/**
ds@1952
   406
		 * @brief Number of rows affected by the last execute.
ds@1952
   407
		 *
ds@1952
   408
		 * @return				Number of rows affected by the last execute.
ds@1952
   409
		 */
ds@1952
   410
		virtual unsigned int GetAffectedRows() =0;
ds@1952
   411
ds@1952
   412
		/**
ds@1952
   413
		 * @brief Retrieves the last insert ID on this database connection.
ds@1952
   414
		 *
ds@1952
   415
		 * @return				Row insertion ID of the last execute, if any.
ds@1952
   416
		 */
ds@1952
   417
		virtual unsigned int GetInsertID() =0;
ds@1952
   418
	};
ds@1952
   419
ds@1952
   420
	class IDBDriver;
ds@1952
   421
ds@1952
   422
	/**
ds@1952
   423
	 * @brief Encapsulates a database connection.
ds@1952
   424
	 */
ds@1952
   425
	class IDatabase
ds@1952
   426
	{
ds@1952
   427
	public:
ds@1952
   428
		/**
ds@1952
   429
		 * @brief Disconnects the database and frees its associated memory.
ds@1952
   430
		 * Note that the actual object will not be freed until all open 
ds@1952
   431
		 * references have been closed.
ds@1952
   432
		 *
ds@1952
   433
		 * It is guaranteed that an IDatabase pointer won't be destroyed until
ds@1952
   434
		 * all open IQuery or IPreparedQuery pointers are closed.
ds@1952
   435
		 *
ds@1952
   436
		 * This function is thread safe.
ds@1952
   437
		 * 
ds@1952
   438
		 * @return				True if object was destroyed, false if 
ds@1952
   439
		 *						references are remaining.
ds@1952
   440
		 */
ds@1952
   441
		virtual bool Close() =0;
ds@1952
   442
ds@1952
   443
		/**
ds@1952
   444
		 * @brief Error code and string returned by the last operation on this
ds@1952
   445
		 * connection.
ds@1952
   446
		 *
ds@1952
   447
		 * This function is not thread safe and must be included in any locks.
ds@1952
   448
		 *
ds@1952
   449
		 * @param errorCode		Optional pointer to retrieve an error code.
ds@1952
   450
		 * @return				Error string pointer (empty if none).
ds@1952
   451
		 */
ds@1952
   452
		virtual const char *GetError(int *errorCode=NULL) =0;
ds@1952
   453
ds@1952
   454
		/**
ds@1952
   455
		 * @brief Prepares and executes a query in one step, and discards
ds@1952
   456
		 * any return data.
ds@1952
   457
		 *
ds@1952
   458
		 * This function is not thread safe and must be included in any locks.
ds@1952
   459
		 *
ds@1952
   460
		 * @param query			Query string.
ds@1952
   461
		 * @return				True on success, false otherwise.
ds@1952
   462
		 */
ds@1952
   463
		virtual bool DoSimpleQuery(const char *query) =0;
ds@1952
   464
ds@1952
   465
		/**
ds@1952
   466
		 * @brief Prepares and executes a query in one step, and returns
ds@1952
   467
		 * the resultant data set.
ds@1952
   468
		 *
ds@1952
   469
		 * Note: If a query contains more than one result set, each
ds@1952
   470
		 * result set must be processed before a new query is started.
ds@1952
   471
		 *
ds@1952
   472
		 * This function is not thread safe and must be included in any locks.
ds@1952
   473
		 *
ds@1952
   474
		 * @param query			Query string.
ds@1952
   475
		 * @return				IQuery pointer on success, NULL otherwise.
ds@1952
   476
		 */
ds@1952
   477
		virtual IQuery *DoQuery(const char *query) =0;
ds@1952
   478
ds@1952
   479
		/** 
ds@1952
   480
		 * @brief Prepares a query statement for multiple executions and/or
ds@1952
   481
		 * binding marked parameters (? in MySQL/sqLite, $n in PostgreSQL).
ds@1952
   482
		 *
ds@1952
   483
		 * This function is not thread safe and must be included in any locks.
ds@1952
   484
		 *
ds@1952
   485
		 * @param query			Query string.
ds@1952
   486
		 * @param error			Error buffer.
ds@1952
   487
		 * @param maxlength		Maximum length of the error buffer.
ds@1952
   488
		 * @param errCode		Optional pointer to store a driver-specific error code.
ds@1952
   489
		 * @return				IPreparedQuery pointer on success, NULL
ds@1952
   490
		 *						otherwise.
ds@1952
   491
		 */
ds@1952
   492
		virtual IPreparedQuery *PrepareQuery(const char *query, char *error, size_t maxlength, int *errCode=NULL) =0;
ds@1952
   493
ds@1952
   494
		/**
ds@1952
   495
		 * Quotes a string for insertion into a query.
ds@1952
   496
		 *
ds@1952
   497
		 * @param str			Source string.
ds@1952
   498
		 * @param buffer		Buffer to store new string (should not overlap source string).
ds@1952
   499
		 * @param maxlen		Maximum length of the output buffer.
ds@1952
   500
		 * @param newSize		Pointer to store the output size.
ds@1952
   501
		 * @return				True on success, false if the output buffer is not big enough.
ds@1952
   502
		 *						If not big enough, the required buffer size is passed through
ds@1952
   503
		 *						newSize.
ds@1952
   504
		 */
ds@1952
   505
		virtual bool QuoteString(const char *str, char buffer[], size_t maxlen, size_t *newSize) =0;
ds@1952
   506
ds@1952
   507
		/**
ds@1952
   508
		 * @brief Number of rows affected by the last execute.
ds@1952
   509
		 *
ds@1952
   510
		 * This function is not thread safe and must be included in any locks.
ds@1952
   511
		 *
ds@1952
   512
		 * @return				Number of rows affected by the last execute.
ds@1952
   513
		 */
ds@1952
   514
		virtual unsigned int GetAffectedRows() =0;
ds@1952
   515
ds@1952
   516
		/**
ds@1952
   517
		 * @brief Retrieves the last insert ID on this database connection.
ds@1952
   518
		 *
ds@1952
   519
		 * This function is not thread safe and must be included in any locks.
ds@1952
   520
		 *
ds@1952
   521
		 * @return				Row insertion ID of the last execute, if any.
ds@1952
   522
		 */
ds@1952
   523
		virtual unsigned int GetInsertID() =0;
ds@1952
   524
ds@1952
   525
		/**
ds@1952
   526
		 * @brief Locks the database for an atomic query+retrieval operation.
ds@1952
   527
		 *
ds@1952
   528
		 * @return				True on success, false if not supported.
ds@1952
   529
		 */
ds@1952
   530
		virtual bool LockForFullAtomicOperation() =0;
ds@1952
   531
ds@1952
   532
		/**
ds@1952
   533
		 * @brief Unlocks a locked atomic fetch.
ds@1952
   534
		 */
ds@1952
   535
		virtual void UnlockFromFullAtomicOperation() =0;
ds@1952
   536
ds@1952
   537
		/**
ds@1952
   538
		 * @brief Increases the reference count on the database.
ds@1952
   539
		 *
ds@1952
   540
		 * This function is thread safe.
ds@1952
   541
		 */
ds@1952
   542
		virtual void IncReferenceCount() =0;
ds@1952
   543
ds@1952
   544
		/**
ds@1952
   545
		 * @brief Returns the parent driver.
ds@1952
   546
		 *
ds@1952
   547
		 * This function is thread safe.
ds@1952
   548
		 */
ds@1952
   549
		virtual IDBDriver *GetDriver() =0;
ds@1952
   550
ds@1952
   551
		/**
ds@1952
   552
		 * @brief Prepares and executes a binary query in one step, and discards
ds@1952
   553
		 * any return data.
ds@1952
   554
		 *
ds@1952
   555
		 * This function is not thread safe and must be included in any locks.
ds@1952
   556
		 *
ds@1952
   557
		 * @param query			Query string.
ds@1952
   558
		 * @param length		Length of query string.
ds@1952
   559
		 * @return				True on success, false otherwise.
ds@1952
   560
		 */
ds@1952
   561
		virtual bool DoSimpleQueryEx(const char *query, size_t len) =0;
ds@1952
   562
ds@1952
   563
		/**
ds@1952
   564
		 * @brief Prepares and executes a binary query in one step, and returns
ds@1952
   565
		 * the resultant data set.
ds@1952
   566
		 *
ds@1952
   567
		 * Note: If a query contains more than one result set, each
ds@1952
   568
		 * result set must be processed before a new query is started.
ds@1952
   569
		 *
ds@1952
   570
		 * This function is not thread safe and must be included in any locks.
ds@1952
   571
		 *
ds@1952
   572
		 * @param query			Query string.
ds@1952
   573
		 * @return				IQuery pointer on success, NULL otherwise.
ds@1952
   574
		 */
ds@1952
   575
		virtual IQuery *DoQueryEx(const char *query, size_t len) =0;
ds@1952
   576
	};
ds@1952
   577
ds@1952
   578
	/** 
ds@1952
   579
	 * @brief Describes database connection info.
ds@1952
   580
	 */
ds@1952
   581
	struct DatabaseInfo
ds@1952
   582
	{
ds@1952
   583
		DatabaseInfo()
ds@1952
   584
		{
ds@1952
   585
			dbiVersion = SMINTERFACE_DBI_VERSION;
ds@1952
   586
			port = 0;
ds@1952
   587
			maxTimeout = 0;
ds@1952
   588
		}
ds@1952
   589
		unsigned int dbiVersion;		/**< DBI Version for backwards compatibility */
ds@1952
   590
		const char *host;				/**< Host string */
ds@1952
   591
		const char *database;			/**< Database name string */
ds@1952
   592
		const char *user;				/**< User to authenticate as */
ds@1952
   593
		const char *pass;				/**< Password to authenticate with */
ds@1952
   594
		const char *driver;				/**< Driver to use */
ds@1952
   595
		unsigned int port;				/**< Port to use, 0=default */
ds@1952
   596
		unsigned int maxTimeout;		/**< Maximum timeout, 0=default */
ds@1952
   597
	};
ds@1952
   598
ds@1952
   599
	/**
ds@1952
   600
	 * @brief Describes an SQL driver.
ds@1952
   601
	 */
ds@1952
   602
	class IDBDriver
ds@1952
   603
	{
ds@1952
   604
	public:
ds@1952
   605
		virtual unsigned int GetDBIVersion()
ds@1952
   606
		{
ds@1952
   607
			return SMINTERFACE_DBI_VERSION;
ds@1952
   608
		}
ds@1952
   609
	public:
ds@1952
   610
		/**
ds@1952
   611
		 * @brief Initiates a database connection.
ds@1952
   612
		 *
ds@1952
   613
		 * Note: Persistent connections should never be created from a thread.
ds@1952
   614
		 * 
ds@1952
   615
		 * @param info			Database connection info pointer.
ds@1952
   616
		 * @param persistent	If true, a previous persistent connection will
ds@1952
   617
		 *						be re-used if possible.
ds@1952
   618
		 * @param error			Buffer to store error message.
ds@1952
   619
		 * @param maxlength		Maximum size of the error buffer.
ds@1952
   620
		 * @return				A new IDatabase pointer, or NULL on failure.
ds@1952
   621
		 */
ds@1952
   622
		virtual IDatabase *Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength) =0;
ds@1952
   623
ds@1952
   624
		/**
ds@1952
   625
		 * @brief Returns a case insensitive database identifier string.
ds@1952
   626
		 *
ds@1952
   627
		 * @return				String containing an identifier.
ds@1952
   628
		 */
ds@1952
   629
		virtual const char *GetIdentifier() =0;
ds@1952
   630
ds@1952
   631
		/**
ds@1952
   632
		 * @brief Returns a case sensitive implementation name.
ds@1952
   633
		 *
ds@1952
   634
		 * @return				String containing an implementation name.
ds@1952
   635
		 */
ds@1952
   636
		virtual const char *GetProductName() =0;
ds@1952
   637
ds@1952
   638
		/**
ds@1952
   639
		 * @brief Retrieves a Handle_t handle of the IDBDriver type.
ds@1952
   640
		 *
ds@1952
   641
		 * @return				A Handle_t handle.
ds@1952
   642
		 */
ds@1952
   643
		virtual Handle_t GetHandle() =0;
ds@1952
   644
ds@1952
   645
		/**
ds@1952
   646
		 * @brief Returns the driver's controlling identity (must be the same 
ds@1952
   647
		 * as from IExtension::GetIdentity).
ds@1952
   648
		 *
ds@1952
   649
		 * @return				An IdentityToken_t identity.
ds@1952
   650
		 */
ds@1952
   651
		virtual IdentityToken_t *GetIdentity() =0;
ds@1952
   652
ds@1952
   653
		/**
ds@1952
   654
		 * @brief Returns whether the driver is thread safe.
ds@1952
   655
		 *
ds@1952
   656
		 * @return				True if thread safe, false otherwise.
ds@1952
   657
		 */
ds@1952
   658
		virtual bool IsThreadSafe() =0;
ds@1952
   659
ds@1952
   660
		/**
ds@1952
   661
		 * @brief Initializes thread safety for the calling thread.
ds@1952
   662
		 *
ds@1952
   663
		 * @return				True on success, false otherwise.
ds@1952
   664
		 */
ds@1952
   665
		virtual bool InitializeThreadSafety() =0;
ds@1952
   666
ds@1952
   667
		/**
ds@1952
   668
		 * @brief Shuts down thread safety for the calling thread.
ds@1952
   669
		 */
ds@1952
   670
		virtual void ShutdownThreadSafety() =0;
ds@1952
   671
	};
ds@1952
   672
ds@1952
   673
	/**
ds@1952
   674
	 * @brief Priority queue level.
ds@1952
   675
	 */
ds@1952
   676
	enum PrioQueueLevel
ds@1952
   677
	{
ds@1952
   678
		PrioQueue_High,			/**< High priority */
ds@1952
   679
		PrioQueue_Normal,		/**< Normal priority */
ds@1952
   680
		PrioQueue_Low			/**< Low priority */
ds@1952
   681
	};
ds@1952
   682
ds@1952
   683
	/**
ds@1952
   684
	 * Specification for a threaded database operation.
ds@1952
   685
	 */
ds@1952
   686
	class IDBThreadOperation
ds@1952
   687
	{
ds@1952
   688
	public:
ds@1952
   689
		/**
ds@1952
   690
		 * @brief Must return the driver this operation is using, or 
ds@1952
   691
		 * NULL if not using any driver.  This is not never inside 
ds@1952
   692
		 * the thread.
ds@1952
   693
		 *
ds@1952
   694
		 * @return			IDBDriver pointer.
ds@1952
   695
		 */
ds@1952
   696
		virtual IDBDriver *GetDriver() =0;
ds@1952
   697
ds@1952
   698
		/**
ds@1952
   699
		 * @brief Must return the object owning this threaded operation.
ds@1952
   700
		 * This is never called inside the thread.
ds@1952
   701
		 *
ds@1952
   702
		 * @return			IdentityToken_t pointer.
ds@1952
   703
		 */
ds@1952
   704
		virtual IdentityToken_t *GetOwner() =0;
ds@1952
   705
ds@1952
   706
		/**
ds@1952
   707
		 * @brief Called inside the thread; this is where any blocking
ds@1952
   708
		 * or threaded operations must occur.
ds@1952
   709
		 */
ds@1952
   710
		virtual void RunThreadPart() =0;
ds@1952
   711
ds@1952
   712
		/**
ds@1952
   713
		 * @brief Called in a server frame after the thread operation 
ds@1952
   714
		 * has completed.  This is the non-threaded completion callback,
ds@1952
   715
		 * which although optional, is useful for pumping results back
ds@1952
   716
		 * to normal game API.
ds@1952
   717
		 */
ds@1952
   718
		virtual void RunThinkPart() =0;
ds@1952
   719
ds@1952
   720
		/**
ds@1952
   721
		 * @brief If RunThinkPart() is not called, this will be called
ds@1952
   722
		 * instead.  Note that RunThreadPart() is ALWAYS called regardless,
ds@1952
   723
		 * and this is only called when Core requests that the operation
ds@1952
   724
		 * be scrapped (for example, the database driver might be unloading).
ds@1952
   725
		 */
ds@1952
   726
		virtual void CancelThinkPart() =0;
ds@1952
   727
ds@1952
   728
		/**
ds@1952
   729
		 * @brief Called when the operation is finalized and any resources
ds@1952
   730
		 * can be released.
ds@1952
   731
		 */
ds@1952
   732
		virtual void Destroy() =0;
ds@1952
   733
	};
ds@1952
   734
ds@1952
   735
	/**
ds@1952
   736
	 * @brief Database-related Handle types.
ds@1952
   737
	 */
ds@1952
   738
	enum DBHandleType
ds@1952
   739
	{
ds@1952
   740
		DBHandle_Driver = 0,		/**< Driver Handle */
ds@1952
   741
		DBHandle_Database = 1,		/**< Database Handle */
ds@1952
   742
	};
ds@1952
   743
ds@1952
   744
	/**
ds@1952
   745
	 * @brief Describes the DBI manager.
ds@1952
   746
	 */
ds@1952
   747
	class IDBManager : public SMInterface
ds@1952
   748
	{
ds@1952
   749
	public:
ds@1952
   750
		virtual const char *GetInterfaceName() =0;
ds@1952
   751
		virtual unsigned int GetInterfaceVersion() =0;
ds@1952
   752
	public:
ds@1952
   753
		/**
ds@1952
   754
		 * @brief Adds a driver to the DBI system.  Not thread safe.
ds@1952
   755
		 *
ds@1952
   756
		 * @param pDriver		Database driver.
ds@1952
   757
		 */
ds@1952
   758
		virtual void AddDriver(IDBDriver *pDriver) =0;
ds@1952
   759
ds@1952
   760
		/**
ds@1952
   761
		 * @brief Removes a driver from the DBI system.  Not thread safe.
ds@1952
   762
		 *
ds@1952
   763
		 * @param pDriver		Database driver.
ds@1952
   764
		 */
ds@1952
   765
		virtual void RemoveDriver(IDBDriver *pDriver) =0;
ds@1952
   766
ds@1952
   767
		/**
ds@1952
   768
		 * @brief Searches for database info by name.  Both the return pointer
ds@1952
   769
		 * and all pointers contained therein should be considered volatile.
ds@1952
   770
		 *
ds@1952
   771
		 * @param name			Named database info.
ds@1952
   772
		 * @return				DatabaseInfo pointer.
ds@1952
   773
		 */
ds@1952
   774
		virtual const DatabaseInfo *FindDatabaseConf(const char *name) =0;
ds@1952
   775
ds@1952
   776
		/**
ds@1952
   777
		 * @brief Tries to connect to a named database.  Not thread safe.
ds@1952
   778
		 *
ds@1952
   779
		 * @param name			Named database info.
ds@1952
   780
		 * @param pdr			Pointer to store the IDBDriver pointer in.
ds@1952
   781
		 *						If driver is not found, NULL will be stored.
ds@1952
   782
		 * @param pdb			Pointer to store the IDatabase pointer in.
ds@1952
   783
		 *						If connection fails, NULL will be stored.
ds@1952
   784
		 * @param persistent	If true, the dbmanager will attempt to PConnect
ds@1952
   785
		 *						instead of connect.
ds@1952
   786
		 * @param error			Error buffer to store a driver's error message.
ds@1952
   787
		 * @param maxlength		Maximum length of the error buffer.
ds@1952
   788
		 * @return				True on success, false otherwise.
ds@1952
   789
		 */
ds@1952
   790
		virtual bool Connect(const char *name, 
ds@1952
   791
							IDBDriver **pdr, 
ds@1952
   792
							IDatabase **pdb, 
ds@1952
   793
							bool persistent, 
ds@1952
   794
							char *error, 
ds@1952
   795
							size_t maxlength) =0;
ds@1952
   796
ds@1952
   797
		/**
ds@1952
   798
		 * @brief Returns the number of drivers loaded.  Not thread safe.
ds@1952
   799
		 *
ds@1952
   800
		 * @return				Number of drivers loaded.
ds@1952
   801
		 */
ds@1952
   802
		virtual unsigned int GetDriverCount() =0;
ds@1952
   803
ds@1952
   804
		/**
ds@1952
   805
		 * @brief Returns a driver by index.  Not thread safe.
ds@1952
   806
		 *
ds@1952
   807
		 * @param index			Driver index, starting from 0.
ds@1952
   808
		 * @return				IDBDriver pointer for the given index.
ds@1952
   809
		 */
ds@1952
   810
		virtual IDBDriver *GetDriver(unsigned int index) =0;
ds@1952
   811
ds@1952
   812
		/**
ds@1952
   813
		 * @brief Creates a Handle_t of the IDBDriver type.  Not thread safe.
ds@1952
   814
		 *
ds@1952
   815
		 * @param type			A DBHandleType value.
ds@1952
   816
		 * @param ptr			A pointer corrresponding to a DBHandleType 
ds@1952
   817
		 *						object.
ds@1952
   818
		 * @param pToken		Identity pointer of the owning identity.
ds@1952
   819
		 * @return				A new Handle_t handle, or 0 on failure.
ds@1952
   820
		 */
ds@1952
   821
		virtual Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken) =0;
ds@1952
   822
ds@1952
   823
		/**
ds@1952
   824
		 * @brief Reads an IDBDriver pointer from an IDBDriver handle.  Not 
ds@1952
   825
		 * thread safe.
ds@1952
   826
		 *
ds@1952
   827
		 * @param hndl			Handle_t handle to read.
ds@1952
   828
		 * @param type			A DBHandleType value.
ds@1952
   829
		 * @param ptr			Pointer to store the object pointer.
ds@1952
   830
		 * @return				HandleError value.
ds@1952
   831
		 */
ds@1952
   832
		virtual HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr) =0;
ds@1952
   833
ds@1952
   834
		/**
ds@1952
   835
		 * @brief Releases an IDBDriver handle.
ds@1952
   836
		 *
ds@1952
   837
		 * @param hndl			Handle_t handle to release.
ds@1952
   838
		 * @param type			A DBHandleType value.
ds@1952
   839
		 * @param token			Identity pointer of the owning identity.
ds@1952
   840
		 * @return				HandleError value.
ds@1952
   841
		 */
ds@1952
   842
		virtual HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token) =0;
ds@1952
   843
ds@1952
   844
		/**
ds@1952
   845
		 * @brief Given a driver name, attempts to find it.  If it is not found, SourceMod
ds@1952
   846
		 * will attempt to load it.  This function is not thread safe.
ds@1952
   847
		 *
ds@1952
   848
		 * @param driver		Driver identifier name.
ds@1952
   849
		 * @return				IDBDriver pointer on success, NULL otherwise.
ds@1952
   850
		 */
ds@1952
   851
		virtual IDBDriver *FindOrLoadDriver(const char *driver) =0;
ds@1952
   852
ds@1952
   853
		/**
ds@1952
   854
		 * @brief Returns the default driver, or NULL if none is set.  This 
ds@1952
   855
		 * function is not thread safe.
ds@1952
   856
		 *
ds@1952
   857
		 * @return				IDBDriver pointer on success, NULL otherwise.
ds@1952
   858
		 */
ds@1952
   859
		virtual IDBDriver *GetDefaultDriver() =0;
ds@1952
   860
ds@1952
   861
		/**
ds@1952
   862
		 * @brief Adds a threaded database operation to the priority queue.
ds@1952
   863
		 * This function is not thread safe.
ds@1952
   864
		 *
ds@1952
   865
		 * @param op			Instance of an IDBThreadOperation.
ds@1952
   866
		 * @param prio			Priority level to run at.
ds@1952
   867
		 * @return				True on success, false on failure.
ds@1952
   868
		 */
ds@1952
   869
		virtual bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio) =0;
dvander@2134
   870
dvander@2134
   871
		/**
dvander@2134
   872
		 * @brief Adds a dependency from one extension to the owner of a driver.
dvander@2134
   873
		 * 
dvander@2134
   874
		 * @param myself		Extension that is using the IDBDriver.
dvander@2134
   875
		 * @param driver		Driver that is being used.
dvander@2134
   876
		 */
dvander@2134
   877
		virtual void AddDependency(IExtension *myself, IDBDriver *driver) =0;
ds@1952
   878
	};
ds@1952
   879
}
ds@1952
   880
ds@1952
   881
#endif //_INCLUDE_SOURCEMOD_INTERFACE_DBDRIVER_H_
dvander@2134
   882