Build fixes.
3 * =============================================================================
4 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
5 * =============================================================================
7 * This file is part of the SourceMod/SourcePawn SDK.
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, version 3.0, as published by the
11 * Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
21 * As a special exception, AlliedModders LLC gives you permission to link the
22 * code of this program (as well as its derivative works) to "Half-Life 2," the
23 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
24 * by the Valve Corporation. You must obey the GNU General Public License in
25 * all respects for all other code used. Additionally, AlliedModders LLC grants
26 * this exception to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
28 * or <http://www.sourcemod.net/license.php>.
33 #if defined _dbi_included
41 * Contains information about an SQL driver.
47 * Contains information about a database connection.
53 * Contains information about an active query and its
58 * @handle Statement : Query
60 * Extends a Query Handle and can be used as a Query Handle.
61 * Statement Handles are for prepared queries and contain
62 * their own function for binding parameters. Statement
63 * Handles can be used instead of database Handles in a few
68 * Describes a database field fetch status.
72 DBVal_Error = 0, /**< Column number/field is invalid. */
73 DBVal_TypeMismatch = 1, /**< You cannot retrieve this data with this type. */
74 DBVal_Null = 2, /**< Field has no data (NULL) */
75 DBVal_Data = 3, /**< Field has data */
79 * Describes binding types.
83 DBBind_Int = 0, /**< Bind an integer. */
84 DBBind_Float = 1, /**< Bind a float. */
85 DBBind_String = 2, /**< Bind a string. */
89 * Threading priority level.
93 DBPrio_High = 0, /**< High priority. */
94 DBPrio_Normal = 1, /**< Normal priority. */
95 DBPrio_Low = 2, /**< Low priority. */
99 * Creates an SQL connection from a named configuration.
101 * @param confname Named configuration.
102 * @param persistent True to re-use a previous persistent connection if
103 * possible, false otherwise.
104 * @param error Error buffer.
105 * @param maxlength Maximum length of the error buffer.
106 * @return A database connection Handle, or INVALID_HANDLE on failure.
108 native Handle:SQL_Connect(const String:confname[], bool:persistent, String:error[], maxlength);
111 * Creates a default SQL connection.
113 * @param error Error buffer.
114 * @param maxlength Maximum length of the error buffer.
115 * @param persistent True to re-use a previous persistent connection
116 * if possible, false otherwise.
117 * @return A database connection Handle, or INVALID_HANDLE on failure.
118 * On failure the error buffer will be filled with a message.
120 stock Handle:SQL_DefConnect(String:error[], maxlength, bool:persistent=true)
122 return SQL_Connect("default", persistent, error, maxlength);
126 * Connects to a database using key value pairs containing the database info.
127 * The key/value pairs should match what would be in databases.cfg.
129 * I.e. "driver" should be "default" or a driver name (or ommitted for
130 * the default). For SQLite, only the "database" parameter is needed in addition.
131 * For drivers which require external connections, more of the parameters may be
134 * In general it is discouraged to use this function. Connections should go through
135 * databases.cfg for greatest flexibility on behalf of users.
137 * @param keyvalues Key/value pairs from a KeyValues handle, describing the connection.
138 * @param error Error buffer.
139 * @param maxlength Maximum length of the error buffer.
140 * @return A database connection Handle, or INVALID_HANDLE on failure.
141 * On failure the error buffer will be filled with a message.
142 * @error Invalid KeyValues handle.
144 native Handle:SQL_ConnectCustom(Handle:keyvalues,
150 * Grabs a handle to an SQLite database, creating one if it does not exist.
152 * Unless there are extenuating circumstances, you should consider using "sourcemod-local" as the
153 * database name. This provides some unification between plugins on behalf of users.
155 * As a precaution, you should always create some sort of unique prefix to your table names so
156 * there are no conflicts, and you should never drop or modify tables that you do not own.
158 * @param database Database name.
159 * @param error Error buffer.
160 * @param maxlength Maximum length of the error buffer.
161 * @return A database connection Handle, or INVALID_HANDLE on failure.
162 * On failure the error buffer will be filled with a message.
164 stock Handle:SQLite_UseDatabase(const String:database[],
168 new Handle:kv, Handle:db;
170 kv = CreateKeyValues("");
171 KvSetString(kv, "driver", "sqlite");
172 KvSetString(kv, "database", database);
174 db = SQL_ConnectCustom(kv, error, maxlength, false);
182 * This function is deprecated. Use SQL_ConnectCustom or SQLite_UseDatabase instead.
184 #pragma deprecated Use SQL_ConnectCustom instead.
185 native Handle:SQL_ConnectEx(Handle:driver,
189 const String:database[],
192 bool:persistent=true,
197 * Returns if a named configuration is present in databases.cfg.
199 * @param name Configuration name.
200 * @return True if it exists, false otherwise.
202 native bool:SQL_CheckConfig(const String:name[]);
205 * Returns a driver Handle from a name string.
207 * If the driver is not found, SourceMod will attempt
208 * to load an extension named dbi.<name>.ext.[dll|so].
210 * @param name Driver identification string, or an empty
211 * string to return the default driver.
212 * @return Driver Handle, or INVALID_HANDLE on failure.
214 native Handle:SQL_GetDriver(const String:name[]="");
217 * Reads the driver of an opened database.
219 * @param database Database Handle.
220 * @param ident Option buffer to store the identification string.
221 * @param ident_length Maximum length of the buffer.
222 * @return Driver Handle.
224 native Handle:SQL_ReadDriver(Handle:database, String:ident[]="", ident_length=0);
227 * Retrieves a driver's identification string.
229 * Example: "mysql", "sqlite"
231 * @param driver Driver Handle, or INVALID_HANDLE for the default driver.
232 * @param ident Identification string buffer.
233 * @param maxlength Maximum length of the buffer.
235 * @error Invalid Handle other than INVALID_HANDLE.
237 native SQL_GetDriverIdent(Handle:driver, String:ident[], maxlength);
240 * Retrieves a driver's product string.
242 * Example: "MySQL", "SQLite"
244 * @param driver Driver Handle, or INVALID_HANDLE for the default driver.
245 * @param product Product string buffer.
246 * @param maxlength Maximum length of the buffer.
248 * @error Invalid Handle other than INVALID_HANDLE.
250 native SQL_GetDriverProduct(Handle:driver, String:product[], maxlength);
253 * Returns the number of affected rows from the last query.
255 * @param hndl A database OR statement Handle.
256 * @return Number of rows affected by the last query.
257 * @error Invalid database or statement Handle.
259 native SQL_GetAffectedRows(Handle:hndl);
262 * Returns the last query's insertion id.
264 * @param hndl A database OR statement Handle.
265 * @return Last query's insertion id.
266 * @error Invalid database or statement Handle.
268 native SQL_GetInsertId(Handle:hndl);
271 * Returns the error reported by the last query.
273 * @param hndl A database OR statement Handle.
274 * @param error Error buffer.
275 * @param maxlength Maximum length of the buffer.
276 * @return True if there was an error, false otherwise.
277 * @error Invalid database or statement Handle.
279 native bool:SQL_GetError(Handle:hndl, String:error[], maxlength);
282 * Escapes a database string for literal insertion. This is not needed
283 * for binding strings in prepared statements.
285 * Generally, database strings are inserted into queries enclosed in
286 * single quotes ('). If user input has a single quote in it, the
287 * quote needs to be escaped. This function ensures that any unsafe
288 * characters are safely escaped according to the database engine and
289 * the database's character set.
291 * NOTE: SourceMod only guarantees properly escaped strings when the query
292 * encloses the string in ''. While drivers tend to allow " instead, the string
293 * may be not be escaped (for example, on SQLite)!
295 * @param hndl A database Handle.
296 * @param string String to quote.
297 * @param buffer Buffer to store quoted string in.
298 * @param maxlength Maximum length of the buffer.
299 * @param written Optionally returns the number of bytes written.
300 * @return True on success, false if buffer is not big enough.
301 * The buffer must be at least 2*strlen(string)+1.
302 * @error Invalid database or statement Handle.
304 native bool:SQL_EscapeString(Handle:database,
305 const String:string[],
311 * This is a backwards compatibility stock. You should use SQL_EscapeString()
312 * instead, as this function will probably be deprecated in SourceMod 1.1.
314 stock bool:SQL_QuoteString(Handle:database,
315 const String:string[],
320 return SQL_EscapeString(database, string, buffer, maxlength, written);
324 * Executes a query and ignores the result set.
326 * @param database A database Handle.
327 * @param query Query string.
328 * @param len Optional parameter to specify the query length, in
329 * bytes. This can be used to send binary queries that
330 * have a premature terminator.
331 * @return True if query succeeded, false otherwise. Use
332 * SQL_GetError to find the last error.
333 * @error Invalid database Handle.
335 native bool:SQL_FastQuery(Handle:database, const String:query[], len=-1);
338 * Executes a simple query and returns a new query Handle for
339 * receiving the results.
341 * @param database A database Handle.
342 * @param query Query string.
343 * @param len Optional parameter to specify the query length, in
344 * bytes. This can be used to send binary queries that
345 * have a premature terminator.
346 * @return A new Query Handle on success, INVALID_HANDLE
347 * otherwise. The Handle must be freed with CloseHandle().
348 * @error Invalid database Handle.
350 native Handle:SQL_Query(Handle:database, const String:query[], len=-1);
353 * Creates a new prepared statement query. Prepared statements can
354 * be executed any number of times. They can also have placeholder
355 * parameters, similar to variables, which can be bound safely and
356 * securely (for example, you do not need to quote bound strings).
358 * Statement handles will work in any function that accepts a Query handle.
360 * @param database A database Handle.
361 * @param query Query string.
362 * @param error Error buffer.
363 * @param maxlength Maximum size of the error buffer.
364 * @return A new statement Handle on success, INVALID_HANDLE
365 * otherwise. The Handle must be freed with CloseHandle().
366 * @error Invalid database Handle.
368 native Handle:SQL_PrepareQuery(Handle:database, const String:query[], String:error[], maxlength);
371 * Advances to the next set of results.
373 * In some SQL implementations, multiple result sets can exist on one query.
374 * This is possible in MySQL with simple queries when executing a CALL
375 * query. If this is the case, all result sets must be processed before
376 * another query is made.
378 * @param query A query Handle.
379 * @return True if there was another result set, false otherwise.
380 * @error Invalid query Handle.
382 native bool:SQL_FetchMoreResults(Handle:query);
385 * Returns whether or not a result set exists. This will
386 * return true even if 0 results were returned, but false
387 * on queries like UPDATE, INSERT, or DELETE.
389 * @param query A query (or statement) Handle.
390 * @return True if there is a result set, false otherwise.
391 * @error Invalid query Handle.
393 native bool:SQL_HasResultSet(Handle:query);
396 * Retrieves the number of rows in the last result set.
398 * @param query A query (or statement) Handle.
399 * @return Number of rows in the current result set.
400 * @error Invalid query Handle.
402 native SQL_GetRowCount(Handle:query);
405 * Retrieves the number of fields in the last result set.
407 * @param query A query (or statement) Handle.
408 * @return Number of fields in the current result set.
409 * @error Invalid query Handle.
411 native SQL_GetFieldCount(Handle:query);
414 * Retrieves the name of a field by index.
416 * @param query A query (or statement) Handle.
417 * @param field Field number (starting from 0).
418 * @param name Name buffer.
419 * @param maxlength Maximum length of the name buffer.
421 * @error Invalid query Handle, invalid field index, or
422 * no current result set.
424 native SQL_FieldNumToName(Handle:query, field, String:name[], maxlength);
427 * Retrieves a field index by name.
429 * @param query A query (or statement) Handle.
430 * @param name Name of the field (case sensitive).
431 * @param field Variable to store field index in.
432 * @return True if found, false if not found.
433 * @error Invalid query Handle or no current result set.
435 native bool:SQL_FieldNameToNum(Handle:query, const String:name[], &field);
438 * Fetches a row from the current result set. This must be
439 * successfully called before any results are fetched.
441 * If this function fails, SQL_MoreResults() can be used to
442 * tell if there was an error or the result set is finished.
444 * @param query A query (or statement) Handle.
445 * @return True if a row was fetched, false otherwise.
446 * @error Invalid query Handle.
448 native bool:SQL_FetchRow(Handle:query);
451 * Returns if there are more rows.
453 * @param query A query (or statement) Handle.
454 * @return True if there are more rows, false otherwise.
455 * @error Invalid query Handle.
457 native bool:SQL_MoreRows(Handle:query);
460 * Rewinds a result set back to the first result.
462 * @param query A query (or statement) Handle.
463 * @return True on success, false otherwise.
464 * @error Invalid query Handle or no current result set.
466 native bool:SQL_Rewind(Handle:query);
469 * Fetches a string from a field in the current row of a result set.
470 * If the result is NULL, an empty string will be returned. A NULL
471 * check can be done with the result parameter, or SQL_IsFieldNull().
473 * @param query A query (or statement) Handle.
474 * @param field The field index (starting from 0).
475 * @param buffer String buffer.
476 * @param maxlength Maximum size of the string buffer.
477 * @param result Optional variable to store the status of the return value.
478 * @return Number of bytes written.
479 * @error Invalid query Handle or field index, invalid
480 * type conversion requested from the database,
481 * or no current result set.
483 native SQL_FetchString(Handle:query, field, String:buffer[], maxlength, &DBResult:result=DBVal_Error);
486 * Fetches a float from a field in the current row of a result set.
487 * If the result is NULL, a value of 0.0 will be returned. A NULL
488 * check can be done with the result parameter, or SQL_IsFieldNull().
490 * @param query A query (or statement) Handle.
491 * @param field The field index (starting from 0).
492 * @param result Optional variable to store the status of the return value.
493 * @return A float value.
494 * @error Invalid query Handle or field index, invalid
495 * type conversion requested from the database,
496 * or no current result set.
498 native Float:SQL_FetchFloat(Handle:query, field, &DBResult:result=DBVal_Error);
501 * Fetches an integer from a field in the current row of a result set.
502 * If the result is NULL, a value of 0 will be returned. A NULL
503 * check can be done with the result parameter, or SQL_IsFieldNull().
505 * @param query A query (or statement) Handle.
506 * @param field The field index (starting from 0).
507 * @param result Optional variable to store the status of the return value.
508 * @return An integer value.
509 * @error Invalid query Handle or field index, invalid
510 * type conversion requested from the database,
511 * or no current result set.
513 native SQL_FetchInt(Handle:query, field, &DBResult:result=DBVal_Error);
516 * Returns whether a field's data in the current row of a result set is
517 * NULL or not. NULL is an SQL type which means "no data."
519 * @param query A query (or statement) Handle.
520 * @param field The field index (starting from 0).
521 * @return True if data is NULL, false otherwise.
522 * @error Invalid query Handle or field index, or no
523 * current result set.
525 native bool:SQL_IsFieldNull(Handle:query, field);
528 * Returns the length of a field's data in the current row of a result
529 * set. This only needs to be called for strings to determine how many
530 * bytes to use. Note that the return value does not include the null
533 * @param query A query (or statement) Handle.
534 * @param field The field index (starting from 0).
535 * @return Number of bytes for the field's data size.
536 * @error Invalid query Handle or field index or no
537 * current result set.
539 native SQL_FetchSize(Handle:query, field);
542 * Binds a parameter in a prepared statement to a given integer value.
544 * @param statement A statement (prepared query) Handle.
545 * @param param The parameter index (starting from 0).
546 * @param number The number to bind.
547 * @param signed True to bind the number as signed, false to
548 * bind it as unsigned.
550 * @error Invalid statement Handle or parameter index, or
553 native SQL_BindParamInt(Handle:statement, param, number, bool:signed=true);
556 * Binds a parameter in a prepared statement to a given float value.
558 * @param statement A statement (prepared query) Handle.
559 * @param param The parameter index (starting from 0).
560 * @param float The float number to bind.
562 * @error Invalid statement Handle or parameter index, or
565 native SQL_BindParamFloat(Handle:statement, param, Float:value);
568 * Binds a parameter in a prepared statement to a given string value.
570 * @param statement A statement (prepared query) Handle.
571 * @param param The parameter index (starting from 0).
572 * @param value The string to bind.
573 * @param copy Whether or not SourceMod should copy the value
574 * locally if necessary. If the string contents
575 * won't change before calling SQL_Execute(), this
576 * can be set to false for optimization.
578 * @error Invalid statement Handle or parameter index, or
581 native SQL_BindParamString(Handle:statement, param, const String:value[], bool:copy);
584 * Executes a prepared statement. All parameters must be bound beforehand.
586 * @param statement A statement (prepared query) Handle.
587 * @return True on success, false on failure.
588 * @error Invalid statement Handle.
590 native bool:SQL_Execute(Handle:statement);
593 * Locks a database so threading operations will not interrupt.
595 * If you are using a database Handle for both threading and non-threading,
596 * this MUST be called before doing any set of non-threading DB operations.
597 * Otherwise you risk corrupting the database driver's memory or network
600 * Leaving a lock on a database and then executing a threaded query results
601 * in a dead lock! Make sure to call SQL_UnlockDatabase()!
603 * If the lock cannot be acquired, the main thread will pause until the
604 * threaded operation has concluded.
606 * @param database A database Handle.
608 * @error Invalid database Handle.
610 native SQL_LockDatabase(Handle:database);
613 * Unlocks a database so threading operations may continue.
615 * @param database A database Handle.
617 * @error Invalid database Handle.
619 native SQL_UnlockDatabase(Handle:database);
622 * General callback for threaded SQL stuff.
624 * @param db Parent object of the Handle (or INVALID_HANDLE if none).
625 * @param hndl Handle to the child object (or INVALID_HANDLE if none).
626 * @param error Error string if there was an error. The error could be
627 * empty even if an error condition exists, so it is important
628 * to check the actual Handle value instead.
629 * @param data Data passed in via the original threaded invocation.
632 functag public SQLTCallback(Handle:owner, Handle:hndl, const String:error[], any:data);
635 * Tells whether two database handles both point to the same database
638 * @param hndl1 First database Handle.
639 * @param hndl2 Second database Handle.
640 * @return True if the Handles point to the same
641 * connection, false otherwise.
642 * @error Invalid Handle.
644 native bool:SQL_IsSameConnection(Handle:hndl1, Handle:hndl2);
647 * Connects to a database via a thread. This can be used instead of
648 * SQL_Connect() if you wish for non-blocking functionality.
650 * It is not necessary to use this to use threaded queries. However, if you
651 * don't (or you mix threaded/non-threaded queries), you should see
652 * SQL_LockDatabase().
654 * @param callback Callback; new Handle will be in hndl, owner is the driver.
655 * If no driver was found, the owner is INVALID_HANDLE.
656 * @param name Database name.
659 native SQL_TConnect(SQLTCallback:callback, const String:name[]="default", any:data=0);
662 * Executes a simple query via a thread. The query Handle is passed through
665 * The database Handle returned through the callback is always a new Handle,
666 * and if necessary, SQL_IsSameConnection() should be used to test against
669 * The query Handle returned through the callback is temporary and destroyed
670 * at the end of the callback. If you need to hold onto it, use CloneHandle().
672 * @param database A database Handle.
673 * @param callback Callback; database is in "owner" and the query Handle
674 * is passed in "hndl".
675 * @param query Query string.
676 * @param data Extra data value to pass to the callback.
677 * @param prio Priority queue to use.
679 * @error Invalid database Handle.
681 native SQL_TQuery(Handle:database, SQLTCallback:callback, const String:query[], any:data=0, DBPriority:prio=DBPrio_Normal);