Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "sm_globals.h"
34 #include "HandleSys.h"
36 #include "ExtensionSys.h"
37 #include "PluginSys.h"
38 #include "sm_stringutil.h"
40 HandleType_t hQueryType;
41 HandleType_t hStmtType;
43 class DatabaseHelpers :
45 public IHandleTypeDispatch
48 virtual void OnSourceModAllInitialized()
53 g_HandleSys.InitAccessDefaults(NULL, &acc);
54 acc.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
58 g_HandleSys.InitAccessDefaults(&tacc, NULL);
59 tacc.ident = g_pCoreIdent;
61 hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL);
62 hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL);
65 virtual void OnSourceModShutdown()
67 g_HandleSys.RemoveType(hStmtType, g_pCoreIdent);
68 g_HandleSys.RemoveType(hQueryType, g_pCoreIdent);
71 virtual void OnHandleDestroy(HandleType_t type, void *object)
73 if (type == hQueryType)
75 IQuery *query = (IQuery *)object;
77 } else if (type == hStmtType) {
78 IPreparedQuery *query = (IPreparedQuery *)object;
82 } s_DatabaseNativeHelpers;
84 //is this safe for stmt handles? i think since it's single inheritance, it always will be.
85 inline HandleError ReadQueryHndl(Handle_t hndl, IPluginContext *pContext, IQuery **query)
88 sec.pOwner = pContext->GetIdentity();
89 sec.pIdentity = g_pCoreIdent;
91 return g_HandleSys.ReadHandle(hndl, hQueryType, &sec, (void **)query);
94 inline HandleError ReadStmtHndl(Handle_t hndl, IPluginContext *pContext, IPreparedQuery **query)
97 sec.pOwner = pContext->GetIdentity();
98 sec.pIdentity = g_pCoreIdent;
100 return g_HandleSys.ReadHandle(hndl, hStmtType, &sec, (void **)query);
103 inline HandleError ReadDbOrStmtHndl(Handle_t hndl, IPluginContext *pContext, IDatabase **db, IPreparedQuery **query)
106 if ((err = g_DBMan.ReadHandle(hndl, DBHandle_Database, (void **)db)) == HandleError_Type)
109 return ReadStmtHndl(hndl, pContext, query);
114 class TQueryOp : public IDBThreadOperation
117 TQueryOp(IDatabase *db, IPluginFunction *pf, const char *query, cell_t data) :
118 m_pDatabase(db), m_pFunction(pf), m_Query(query), m_Data(data),
119 me(g_PluginSys.GetPluginByCtx(pf->GetParentContext()->GetContext())),
122 /* We always increase the reference count because this is potentially
123 * asynchronous. Otherwise the original handle could be closed while
124 * we're still latched onto it.
126 m_pDatabase->IncReferenceCount();
128 /* Now create our own Handle such that it can only be closed by us.
129 * We allow cloning just in case someone wants to hold onto it.
131 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
133 g_HandleSys.InitAccessDefaults(NULL, &access);
134 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
135 m_MyHandle = g_HandleSys.CreateHandleEx(g_DBMan.GetDatabaseType(),
148 /* Close our Handle if it's valid. */
149 if (m_MyHandle != BAD_HANDLE)
151 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
152 g_HandleSys.FreeHandle(m_MyHandle, &sec);
154 /* Otherwise, there is an open ref to the db */
155 m_pDatabase->Close();
158 IdentityToken_t *GetOwner()
160 return me->GetIdentity();
162 IDBDriver *GetDriver()
164 return m_pDatabase->GetDriver();
168 m_pDatabase->LockForFullAtomicOperation();
169 m_pQuery = m_pDatabase->DoQuery(m_Query.c_str());
172 UTIL_Format(error, sizeof(error), "%s", m_pDatabase->GetError());
174 m_pDatabase->UnlockFromFullAtomicOperation();
176 void CancelThinkPart()
178 m_pFunction->PushCell(BAD_HANDLE);
179 m_pFunction->PushCell(BAD_HANDLE);
180 m_pFunction->PushString("Driver is unloading");
181 m_pFunction->PushCell(m_Data);
182 m_pFunction->Execute(NULL);
186 /* Create a Handle for our query */
187 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
189 g_HandleSys.InitAccessDefaults(NULL, &access);
190 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
192 Handle_t qh = BAD_HANDLE;
196 qh = g_HandleSys.CreateHandle(hQueryType, m_pQuery, me->GetIdentity(), g_pCoreIdent, NULL);
197 if (qh != BAD_HANDLE)
201 UTIL_Format(error, sizeof(error), "Could not alloc handle");
205 m_pFunction->PushCell(m_MyHandle);
206 m_pFunction->PushCell(qh);
207 m_pFunction->PushString(qh == BAD_HANDLE ? error : "");
208 m_pFunction->PushCell(m_Data);
209 m_pFunction->Execute(NULL);
211 if (qh != BAD_HANDLE)
213 g_HandleSys.FreeHandle(qh, &sec);
221 IDatabase *m_pDatabase;
222 IPluginFunction *m_pFunction;
231 class TConnectOp : public IDBThreadOperation
234 TConnectOp(IPluginFunction *func, IDBDriver *driver, const char *_dbname)
240 strncopy(dbname, _dbname, sizeof(dbname));
241 me = g_PluginSys.GetPluginByCtx(m_pFunction->GetParentContext()->GetContext());
243 IdentityToken_t *GetOwner()
245 return me->GetIdentity();
247 IDBDriver *GetDriver()
253 g_DBMan.LockConfig();
254 const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(dbname);
257 UTIL_Format(error, sizeof(error), "Could not find database config \"%s\"", dbname);
259 m_pDatabase = m_pDriver->Connect(pInfo, false, error, sizeof(error));
261 g_DBMan.UnlockConfig();
263 void CancelThinkPart()
267 m_pDatabase->Close();
269 m_pFunction->PushCell(BAD_HANDLE);
270 m_pFunction->PushCell(BAD_HANDLE);
271 m_pFunction->PushString("Driver is unloading");
272 m_pFunction->PushCell(0);
273 m_pFunction->Execute(NULL);
277 Handle_t hndl = BAD_HANDLE;
281 if ((hndl = g_DBMan.CreateHandle(DBHandle_Database, m_pDatabase, me->GetIdentity()))
284 m_pDatabase->Close();
285 UTIL_Format(error, sizeof(error), "Unable to allocate Handle");
289 m_pFunction->PushCell(m_pDriver->GetHandle());
290 m_pFunction->PushCell(hndl);
291 m_pFunction->PushString(hndl == BAD_HANDLE ? error : "");
292 m_pFunction->PushCell(0);
293 m_pFunction->Execute(NULL);
301 IPluginFunction *m_pFunction;
302 IDBDriver *m_pDriver;
303 IDatabase *m_pDatabase;
308 static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
312 size_t maxlength = (size_t)params[4];
313 bool persistent = params[2] ? true : false;
314 pContext->LocalToString(params[1], &conf);
315 pContext->LocalToString(params[3], &err);
319 if (!g_DBMan.Connect(conf, &driver, &db, persistent, err, maxlength))
324 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
331 /* HACK! Add us to the dependency list */
332 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
335 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
341 static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
343 IPluginFunction *pf = pContext->GetFunctionById(params[1]);
346 return pContext->ThrowNativeError("Function id %x is invalid", params[1]);
350 pContext->LocalToString(params[2], &conf);
352 IDBDriver *driver = NULL;
353 const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(conf);
357 if (pInfo->driver[0] == '\0')
359 driver = g_DBMan.GetDefaultDriver();
361 driver = g_DBMan.FindOrLoadDriver(pInfo->driver);
367 "Could not find driver \"%s\"",
368 pInfo->driver[0] == '\0' ? g_DBMan.GetDefaultDriverName() : pInfo->driver);
369 } else if (!driver->IsThreadSafe()) {
372 "Driver \"%s\" is not thread safe!",
373 driver->GetIdentifier());
376 UTIL_Format(error, sizeof(error), "Could not find database conf \"%s\"", conf);
379 if (!pInfo || !driver)
381 pf->PushCell(BAD_HANDLE);
382 pf->PushCell(BAD_HANDLE);
383 pf->PushString(error);
389 /* HACK! Add us to the dependency list */
390 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
393 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
396 /* Finally, add to the thread if we can */
397 TConnectOp *op = new TConnectOp(pf, driver, conf);
398 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
399 if (pPlugin->GetProperty("DisallowDBThreads", NULL)
400 || !g_DBMan.AddToThreadQueue(op, PrioQueue_High))
402 /* Do everything right now */
411 static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
414 if (params[1] == BAD_HANDLE)
416 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
418 return pContext->ThrowNativeError("Could not find any default driver");
422 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
425 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
429 char *host, *user, *pass, *database, *error;
430 size_t maxlength = (size_t)params[7];
431 bool persistent = params[8] ? true : false;
432 unsigned int port = params[9];
433 unsigned int maxTimeout = params[10];
434 pContext->LocalToString(params[2], &host);
435 pContext->LocalToString(params[3], &user);
436 pContext->LocalToString(params[4], &pass);
437 pContext->LocalToString(params[5], &database);
438 pContext->LocalToString(params[6], &error);
441 info.database = database;
442 info.driver = driver->GetIdentifier();
444 info.maxTimeout = maxTimeout;
449 IDatabase *db = driver->Connect(&info, persistent, error, maxlength);
453 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
460 /* HACK! Add us to the dependency list */
461 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
464 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
473 static cell_t SQL_GetDriverIdent(IPluginContext *pContext, const cell_t *params)
476 if (params[1] == BAD_HANDLE)
478 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
480 return pContext->ThrowNativeError("Could not find any default driver");
484 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
487 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
491 pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
496 static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params)
499 pContext->LocalToString(params[1], &name);
501 IDBDriver *driver = NULL;
504 driver = g_DBMan.GetDefaultDriver();
506 driver = g_DBMan.FindOrLoadDriver(name);
509 return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE;
512 static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params)
515 if (params[1] == BAD_HANDLE)
517 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
519 return pContext->ThrowNativeError("Could not find any default driver");
523 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
526 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
530 pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL);
535 static cell_t SQL_GetAffectedRows(IPluginContext *pContext, const cell_t *params)
537 IDatabase *db = NULL;
538 IPreparedQuery *stmt = NULL;
541 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
543 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
548 return db->GetAffectedRows();
550 return stmt->GetAffectedRows();
553 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
556 static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params)
558 IDatabase *db = NULL;
559 IPreparedQuery *stmt = NULL;
562 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
564 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
569 return db->GetInsertID();
571 return stmt->GetInsertID();
574 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
577 static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params)
579 IDatabase *db = NULL;
580 IPreparedQuery *stmt = NULL;
583 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
585 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
588 const char *error = "";
591 error = db->GetError();
593 error = stmt->GetError();
596 if (error[0] == '\0')
601 pContext->StringToLocalUTF8(params[2], params[3], error, NULL);
606 static cell_t SQL_QuoteString(IPluginContext *pContext, const cell_t *params)
608 IDatabase *db = NULL;
611 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
614 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
617 char *input, *output;
618 size_t maxlength = (size_t)params[4];
619 pContext->LocalToString(params[2], &input);
620 pContext->LocalToString(params[3], &output);
623 bool s = db->QuoteString(input, output, maxlength, &written);
626 pContext->LocalToPhysAddr(params[5], &addr);
627 *addr = (cell_t)written;
632 static cell_t SQL_FastQuery(IPluginContext *pContext, const cell_t *params)
634 IDatabase *db = NULL;
637 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
640 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
644 pContext->LocalToString(params[2], &query);
646 return db->DoSimpleQuery(query) ? 1 : 0;
649 static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params)
651 IDatabase *db = NULL;
654 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
657 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
661 pContext->LocalToString(params[2], &query);
663 IQuery *qr = db->DoQuery(query);
670 Handle_t hndl = g_HandleSys.CreateHandle(hQueryType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
671 if (hndl == BAD_HANDLE)
680 static cell_t SQL_TQuery(IPluginContext *pContext, const cell_t *params)
682 IDatabase *db = NULL;
685 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
688 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
691 if (!db->GetDriver()->IsThreadSafe())
693 return pContext->ThrowNativeError("Driver \"%s\" is not thread safe!", db->GetDriver()->GetIdentifier());
696 IPluginFunction *pf = pContext->GetFunctionById(params[2]);
699 return pContext->ThrowNativeError("Function id %x is invalid", params[2]);
703 pContext->LocalToString(params[3], &query);
705 cell_t data = params[4];
706 PrioQueueLevel level = PrioQueue_Normal;
707 if (params[5] == (cell_t)PrioQueue_High)
709 level = PrioQueue_High;
710 } else if (params[5] == (cell_t)PrioQueue_Low) {
711 level = PrioQueue_Low;
714 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
716 TQueryOp *op = new TQueryOp(db, pf, query, data);
717 if (pPlugin->GetProperty("DisallowDBThreads", NULL)
718 || !g_DBMan.AddToThreadQueue(op, level))
720 /* Do everything right now */
729 static cell_t SQL_LockDatabase(IPluginContext *pContext, const cell_t *params)
731 IDatabase *db = NULL;
734 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
737 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
740 db->LockForFullAtomicOperation();
745 static cell_t SQL_UnlockDatabase(IPluginContext *pContext, const cell_t *params)
747 IDatabase *db = NULL;
750 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
753 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
756 db->UnlockFromFullAtomicOperation();
761 static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params)
763 IDatabase *db = NULL;
766 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
769 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
773 size_t maxlength = (size_t)params[4];
774 pContext->LocalToString(params[2], &query);
775 pContext->LocalToString(params[3], &error);
777 IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength);
784 Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
785 if (hndl == BAD_HANDLE)
794 static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params)
799 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
801 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
804 return query->FetchMoreResults() ? 1 : 0;
807 static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params)
812 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
814 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
817 return query->GetResultSet() != NULL ? true : false;
820 static cell_t SQL_GetRowCount(IPluginContext *pContext, const cell_t *params)
825 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
827 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
830 IResultSet *rs = query->GetResultSet();
836 return rs->GetRowCount();
839 static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params)
844 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
846 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
849 IResultSet *rs = query->GetResultSet();
855 return rs->GetFieldCount();
858 static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params)
863 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
865 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
868 IResultSet *rs = query->GetResultSet();
871 return pContext->ThrowNativeError("No current result set");
874 unsigned int field = params[2];
877 if ((fldname = rs->FieldNumToName(field)) == NULL)
879 return pContext->ThrowNativeError("Invalid field index %d", field);
882 pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL);
887 static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params)
892 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
894 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
897 IResultSet *rs = query->GetResultSet();
900 return pContext->ThrowNativeError("No current result set");
904 pContext->LocalToString(params[2], &field);
907 pContext->LocalToPhysAddr(params[3], &num);
909 return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0;
912 static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params)
917 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
919 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
922 IResultSet *rs = query->GetResultSet();
925 return pContext->ThrowNativeError("No current result set");
928 return (rs->FetchRow() != NULL) ? true : false;
931 static cell_t SQL_MoreRows(IPluginContext *pContext, const cell_t *params)
936 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
938 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
941 IResultSet *rs = query->GetResultSet();
944 return pContext->ThrowNativeError("No current result set");
947 return rs->MoreRows() ? 1 : 0;
950 static cell_t SQL_Rewind(IPluginContext *pContext, const cell_t *params)
955 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
957 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
960 IResultSet *rs = query->GetResultSet();
963 return pContext->ThrowNativeError("No current result set");
966 return rs->Rewind() ? 1 : 0;
969 static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params)
974 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
976 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
979 IResultSet *rs = query->GetResultSet();
982 return pContext->ThrowNativeError("No current result set");
985 IResultRow *row = rs->CurrentRow();
988 return pContext->ThrowNativeError("Current result set has no fetched rows");
993 DBResult res = row->GetString(params[2], &str, &length);
995 if (res == DBVal_Error)
997 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
998 } else if (res == DBVal_TypeMismatch) {
999 return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]);
1002 pContext->StringToLocalUTF8(params[3], params[4], str, &length);
1005 pContext->LocalToPhysAddr(params[5], &addr);
1006 *addr = (cell_t)res;
1008 return (cell_t)length;
1011 static cell_t SQL_FetchFloat(IPluginContext *pContext, const cell_t *params)
1016 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1018 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1021 IResultSet *rs = query->GetResultSet();
1024 return pContext->ThrowNativeError("No current result set");
1027 IResultRow *row = rs->CurrentRow();
1030 return pContext->ThrowNativeError("Current result set has no fetched rows");
1034 DBResult res = row->GetFloat(params[2], &f);
1036 if (res == DBVal_Error)
1038 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
1039 } else if (res == DBVal_TypeMismatch) {
1040 return pContext->ThrowNativeError("Could not fetch data in field %d as a float", params[2]);
1044 pContext->LocalToPhysAddr(params[3], &addr);
1045 *addr = (cell_t)res;
1050 static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params)
1055 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1057 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1060 IResultSet *rs = query->GetResultSet();
1063 return pContext->ThrowNativeError("No current result set");
1066 IResultRow *row = rs->CurrentRow();
1069 return pContext->ThrowNativeError("Current result set has no fetched rows");
1073 DBResult res = row->GetInt(params[2], &iv);
1075 if (res == DBVal_Error)
1077 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
1078 } else if (res == DBVal_TypeMismatch) {
1079 return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]);
1083 pContext->LocalToPhysAddr(params[3], &addr);
1084 *addr = (cell_t)res;
1089 static cell_t SQL_IsFieldNull(IPluginContext *pContext, const cell_t *params)
1094 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1096 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1099 IResultSet *rs = query->GetResultSet();
1102 return pContext->ThrowNativeError("No current result set");
1105 IResultRow *row = rs->CurrentRow();
1108 return pContext->ThrowNativeError("Current result set has no fetched rows");
1111 if ((unsigned)params[2] >= rs->GetFieldCount())
1113 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
1116 return row->IsNull(params[2]) ? 1 : 0;
1119 static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params)
1124 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1126 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1129 IResultSet *rs = query->GetResultSet();
1132 return pContext->ThrowNativeError("No current result set");
1135 IResultRow *row = rs->CurrentRow();
1138 return pContext->ThrowNativeError("Current result set has no fetched rows");
1141 if ((unsigned)params[2] >= rs->GetFieldCount())
1143 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
1146 return row->GetDataSize(params[2]);
1149 static cell_t SQL_BindParamInt(IPluginContext *pContext, const cell_t *params)
1151 IPreparedQuery *stmt;
1154 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1156 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1159 if (!stmt->BindParamInt(params[2], params[3], params[4] ? true : false))
1161 return pContext->ThrowNativeError("Could not bind parameter %d as an integer", params[2]);
1167 static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params)
1169 IPreparedQuery *stmt;
1172 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1174 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1177 if (!stmt->BindParamFloat(params[2], sp_ctof(params[3])))
1179 return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]);
1185 static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params)
1187 IPreparedQuery *stmt;
1190 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1192 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1196 pContext->LocalToString(params[3], &str);
1198 if (!stmt->BindParamString(params[2], str, params[4] ? true : false))
1200 return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]);
1206 static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params)
1208 IPreparedQuery *stmt;
1211 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1213 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1216 return stmt->Execute() ? 1 : 0;
1219 static cell_t SQL_IsSameConnection(IPluginContext *pContext, const cell_t *params)
1221 IDatabase *db1=NULL, *db2=NULL;
1224 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
1225 != HandleError_None)
1227 return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
1230 if ((err = g_DBMan.ReadHandle(params[2], DBHandle_Database, (void **)&db2))
1231 != HandleError_None)
1233 return pContext->ThrowNativeError("Invalid database Handle 2/%x (error: %d)", params[2], err);
1236 return (db1 == db2) ? true : false;
1239 REGISTER_NATIVES(dbNatives)
1241 {"SQL_BindParamInt", SQL_BindParamInt},
1242 {"SQL_BindParamFloat", SQL_BindParamFloat},
1243 {"SQL_BindParamString", SQL_BindParamString},
1244 {"SQL_Connect", SQL_Connect},
1245 {"SQL_ConnectEx", SQL_ConnectEx},
1246 {"SQL_Execute", SQL_Execute},
1247 {"SQL_FastQuery", SQL_FastQuery},
1248 {"SQL_FetchFloat", SQL_FetchFloat},
1249 {"SQL_FetchInt", SQL_FetchInt},
1250 {"SQL_FetchMoreResults", SQL_FetchMoreResults},
1251 {"SQL_FetchRow", SQL_FetchRow},
1252 {"SQL_FetchSize", SQL_FetchSize},
1253 {"SQL_FetchString", SQL_FetchString},
1254 {"SQL_FieldNameToNum", SQL_FieldNameToNum},
1255 {"SQL_FieldNumToName", SQL_FieldNumToName},
1256 {"SQL_GetAffectedRows", SQL_GetAffectedRows},
1257 {"SQL_GetDriver", SQL_GetDriver},
1258 {"SQL_GetDriverIdent", SQL_GetDriverIdent},
1259 {"SQL_GetDriverProduct", SQL_GetDriverProduct},
1260 {"SQL_GetError", SQL_GetError},
1261 {"SQL_GetFieldCount", SQL_GetFieldCount},
1262 {"SQL_GetInsertId", SQL_GetInsertId},
1263 {"SQL_GetRowCount", SQL_GetRowCount},
1264 {"SQL_HasResultSet", SQL_HasResultSet},
1265 {"SQL_IsFieldNull", SQL_IsFieldNull},
1266 {"SQL_IsSameConnection", SQL_IsSameConnection},
1267 {"SQL_LockDatabase", SQL_LockDatabase},
1268 {"SQL_MoreRows", SQL_MoreRows},
1269 {"SQL_PrepareQuery", SQL_PrepareQuery},
1270 {"SQL_Query", SQL_Query},
1271 {"SQL_QuoteString", SQL_QuoteString},
1272 {"SQL_Rewind", SQL_Rewind},
1273 {"SQL_TConnect", SQL_TConnect},
1274 {"SQL_TQuery", SQL_TQuery},
1275 {"SQL_UnlockDatabase", SQL_UnlockDatabase},