Initial Import of beta structs extension.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include "HandleSys.h"
35 #include "ExtensionSys.h"
36 #include "PluginSys.h"
37 #include "sm_stringutil.h"
39 HandleType_t hQueryType;
40 HandleType_t hStmtType;
42 class DatabaseHelpers :
44 public IHandleTypeDispatch
47 virtual void OnSourceModAllInitialized()
52 g_HandleSys.InitAccessDefaults(NULL, &acc);
53 acc.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
57 g_HandleSys.InitAccessDefaults(&tacc, NULL);
58 tacc.ident = g_pCoreIdent;
60 hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL);
61 hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL);
64 virtual void OnSourceModShutdown()
66 g_HandleSys.RemoveType(hStmtType, g_pCoreIdent);
67 g_HandleSys.RemoveType(hQueryType, g_pCoreIdent);
70 virtual void OnHandleDestroy(HandleType_t type, void *object)
72 if (type == hQueryType)
74 IQuery *query = (IQuery *)object;
76 } else if (type == hStmtType) {
77 IPreparedQuery *query = (IPreparedQuery *)object;
81 } s_DatabaseNativeHelpers;
83 //is this safe for stmt handles? i think since it's single inheritance, it always will be.
84 inline HandleError ReadQueryHndl(Handle_t hndl, IPluginContext *pContext, IQuery **query)
87 sec.pOwner = pContext->GetIdentity();
88 sec.pIdentity = g_pCoreIdent;
90 return g_HandleSys.ReadHandle(hndl, hQueryType, &sec, (void **)query);
93 inline HandleError ReadStmtHndl(Handle_t hndl, IPluginContext *pContext, IPreparedQuery **query)
96 sec.pOwner = pContext->GetIdentity();
97 sec.pIdentity = g_pCoreIdent;
99 return g_HandleSys.ReadHandle(hndl, hStmtType, &sec, (void **)query);
102 inline HandleError ReadDbOrStmtHndl(Handle_t hndl, IPluginContext *pContext, IDatabase **db, IPreparedQuery **query)
105 if ((err = g_DBMan.ReadHandle(hndl, DBHandle_Database, (void **)db)) == HandleError_Type)
108 return ReadStmtHndl(hndl, pContext, query);
113 class TQueryOp : public IDBThreadOperation
116 TQueryOp(IDatabase *db, IPluginFunction *pf, const char *query, cell_t data) :
117 m_pDatabase(db), m_pFunction(pf), m_Query(query), m_Data(data),
118 me(g_PluginSys.GetPluginByCtx(pf->GetParentContext()->GetContext())),
121 /* We always increase the reference count because this is potentially
122 * asynchronous. Otherwise the original handle could be closed while
123 * we're still latched onto it.
125 m_pDatabase->IncReferenceCount();
127 /* Now create our own Handle such that it can only be closed by us.
128 * We allow cloning just in case someone wants to hold onto it.
130 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
132 g_HandleSys.InitAccessDefaults(NULL, &access);
133 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
134 m_MyHandle = g_HandleSys.CreateHandleEx(g_DBMan.GetDatabaseType(),
147 /* Close our Handle if it's valid. */
148 if (m_MyHandle != BAD_HANDLE)
150 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
151 g_HandleSys.FreeHandle(m_MyHandle, &sec);
153 /* Otherwise, there is an open ref to the db */
154 m_pDatabase->Close();
157 IdentityToken_t *GetOwner()
159 return me->GetIdentity();
161 IDBDriver *GetDriver()
163 return m_pDatabase->GetDriver();
167 m_pDatabase->LockForFullAtomicOperation();
168 m_pQuery = m_pDatabase->DoQuery(m_Query.c_str());
171 UTIL_Format(error, sizeof(error), "%s", m_pDatabase->GetError());
173 m_pDatabase->UnlockFromFullAtomicOperation();
175 void CancelThinkPart()
177 m_pFunction->PushCell(BAD_HANDLE);
178 m_pFunction->PushCell(BAD_HANDLE);
179 m_pFunction->PushString("Driver is unloading");
180 m_pFunction->PushCell(m_Data);
181 m_pFunction->Execute(NULL);
185 /* Create a Handle for our query */
186 HandleSecurity sec(me->GetIdentity(), g_pCoreIdent);
188 g_HandleSys.InitAccessDefaults(NULL, &access);
189 access.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY|HANDLE_RESTRICT_OWNER;
191 Handle_t qh = BAD_HANDLE;
195 qh = g_HandleSys.CreateHandle(hQueryType, m_pQuery, me->GetIdentity(), g_pCoreIdent, NULL);
196 if (qh != BAD_HANDLE)
200 UTIL_Format(error, sizeof(error), "Could not alloc handle");
204 m_pFunction->PushCell(m_MyHandle);
205 m_pFunction->PushCell(qh);
206 m_pFunction->PushString(qh == BAD_HANDLE ? error : "");
207 m_pFunction->PushCell(m_Data);
208 m_pFunction->Execute(NULL);
210 if (qh != BAD_HANDLE)
212 g_HandleSys.FreeHandle(qh, &sec);
220 IDatabase *m_pDatabase;
221 IPluginFunction *m_pFunction;
230 class TConnectOp : public IDBThreadOperation
233 TConnectOp(IPluginFunction *func, IDBDriver *driver, const char *_dbname, cell_t data)
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(m_Data);
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(m_Data);
293 m_pFunction->Execute(NULL);
301 IPluginFunction *m_pFunction;
302 IDBDriver *m_pDriver;
303 IDatabase *m_pDatabase;
309 static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
313 size_t maxlength = (size_t)params[4];
314 bool persistent = params[2] ? true : false;
315 pContext->LocalToString(params[1], &conf);
316 pContext->LocalToString(params[3], &err);
320 if (!g_DBMan.Connect(conf, &driver, &db, persistent, err, maxlength))
325 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
332 /* HACK! Add us to the dependency list */
333 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
336 g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext()));
342 static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params)
344 IPluginFunction *pf = pContext->GetFunctionById(params[1]);
347 return pContext->ThrowNativeError("Function id %x is invalid", params[1]);
351 pContext->LocalToString(params[2], &conf);
353 IDBDriver *driver = NULL;
354 const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(conf);
358 if (pInfo->driver[0] == '\0')
360 driver = g_DBMan.GetDefaultDriver();
362 driver = g_DBMan.FindOrLoadDriver(pInfo->driver);
368 "Could not find driver \"%s\"",
369 pInfo->driver[0] == '\0' ? g_DBMan.GetDefaultDriverName() : pInfo->driver);
370 } else if (!driver->IsThreadSafe()) {
373 "Driver \"%s\" is not thread safe!",
374 driver->GetIdentifier());
377 UTIL_Format(error, sizeof(error), "Could not find database conf \"%s\"", conf);
380 if (!pInfo || !driver)
382 pf->PushCell(BAD_HANDLE);
383 pf->PushCell(BAD_HANDLE);
384 pf->PushString(error);
390 /* HACK! Add us to the dependency list */
391 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
394 g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext()));
397 /* Finally, add to the thread if we can */
398 TConnectOp *op = new TConnectOp(pf, driver, conf, params[3]);
399 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
400 if (pPlugin->GetProperty("DisallowDBThreads", NULL)
401 || !g_DBMan.AddToThreadQueue(op, PrioQueue_High))
403 /* Do everything right now */
412 static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
415 if (params[1] == BAD_HANDLE)
417 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
419 return pContext->ThrowNativeError("Could not find any default driver");
423 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
426 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
430 char *host, *user, *pass, *database, *error;
431 size_t maxlength = (size_t)params[7];
432 bool persistent = params[8] ? true : false;
433 unsigned int port = params[9];
434 unsigned int maxTimeout = params[10];
435 pContext->LocalToString(params[2], &host);
436 pContext->LocalToString(params[3], &user);
437 pContext->LocalToString(params[4], &pass);
438 pContext->LocalToString(params[5], &database);
439 pContext->LocalToString(params[6], &error);
442 info.database = database;
443 info.driver = driver->GetIdentifier();
445 info.maxTimeout = maxTimeout;
450 IDatabase *db = driver->Connect(&info, persistent, error, maxlength);
454 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
461 /* HACK! Add us to the dependency list */
462 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
465 g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext()));
474 static cell_t SQL_GetDriverIdent(IPluginContext *pContext, const cell_t *params)
477 if (params[1] == BAD_HANDLE)
479 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
481 return pContext->ThrowNativeError("Could not find any default driver");
485 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
488 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
492 pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
497 static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params)
500 pContext->LocalToString(params[1], &name);
502 IDBDriver *driver = NULL;
505 driver = g_DBMan.GetDefaultDriver();
507 driver = g_DBMan.FindOrLoadDriver(name);
510 return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE;
513 static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params)
516 if (params[1] == BAD_HANDLE)
518 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
520 return pContext->ThrowNativeError("Could not find any default driver");
524 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
527 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
531 pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL);
536 static cell_t SQL_GetAffectedRows(IPluginContext *pContext, const cell_t *params)
538 IDatabase *db = NULL;
539 IPreparedQuery *stmt = NULL;
542 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
544 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
549 return db->GetAffectedRows();
551 return stmt->GetAffectedRows();
554 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
557 static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params)
559 IDatabase *db = NULL;
560 IPreparedQuery *stmt = NULL;
563 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
565 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
570 return db->GetInsertID();
572 return stmt->GetInsertID();
575 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
578 static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params)
580 IDatabase *db = NULL;
581 IPreparedQuery *stmt = NULL;
584 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
586 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
589 const char *error = "";
592 error = db->GetError();
594 error = stmt->GetError();
597 if (error[0] == '\0')
602 pContext->StringToLocalUTF8(params[2], params[3], error, NULL);
607 static cell_t SQL_QuoteString(IPluginContext *pContext, const cell_t *params)
609 IDatabase *db = NULL;
612 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
615 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
618 char *input, *output;
619 size_t maxlength = (size_t)params[4];
620 pContext->LocalToString(params[2], &input);
621 pContext->LocalToString(params[3], &output);
624 bool s = db->QuoteString(input, output, maxlength, &written);
627 pContext->LocalToPhysAddr(params[5], &addr);
628 *addr = (cell_t)written;
633 static cell_t SQL_FastQuery(IPluginContext *pContext, const cell_t *params)
635 IDatabase *db = NULL;
638 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
641 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
645 pContext->LocalToString(params[2], &query);
647 if (params[0] >= 3 && params[3] != -1)
649 return db->DoSimpleQueryEx(query, params[3]) ? 1 : 0;
652 return db->DoSimpleQuery(query) ? 1 : 0;
655 static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params)
657 IDatabase *db = NULL;
660 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
663 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
667 pContext->LocalToString(params[2], &query);
671 if (params[0] >= 3 && params[3] != -1)
673 qr = db->DoQueryEx(query, params[3]);
677 qr = db->DoQuery(query);
685 Handle_t hndl = g_HandleSys.CreateHandle(hQueryType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
686 if (hndl == BAD_HANDLE)
695 static cell_t SQL_TQuery(IPluginContext *pContext, const cell_t *params)
697 IDatabase *db = NULL;
700 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
703 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
706 if (!db->GetDriver()->IsThreadSafe())
708 return pContext->ThrowNativeError("Driver \"%s\" is not thread safe!", db->GetDriver()->GetIdentifier());
711 IPluginFunction *pf = pContext->GetFunctionById(params[2]);
714 return pContext->ThrowNativeError("Function id %x is invalid", params[2]);
718 pContext->LocalToString(params[3], &query);
720 cell_t data = params[4];
721 PrioQueueLevel level = PrioQueue_Normal;
722 if (params[5] == (cell_t)PrioQueue_High)
724 level = PrioQueue_High;
725 } else if (params[5] == (cell_t)PrioQueue_Low) {
726 level = PrioQueue_Low;
729 CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext());
731 TQueryOp *op = new TQueryOp(db, pf, query, data);
732 if (pPlugin->GetProperty("DisallowDBThreads", NULL)
733 || !g_DBMan.AddToThreadQueue(op, level))
735 /* Do everything right now */
744 static cell_t SQL_LockDatabase(IPluginContext *pContext, const cell_t *params)
746 IDatabase *db = NULL;
749 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
752 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
755 db->LockForFullAtomicOperation();
760 static cell_t SQL_UnlockDatabase(IPluginContext *pContext, const cell_t *params)
762 IDatabase *db = NULL;
765 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
768 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
771 db->UnlockFromFullAtomicOperation();
776 static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params)
778 IDatabase *db = NULL;
781 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
784 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
788 size_t maxlength = (size_t)params[4];
789 pContext->LocalToString(params[2], &query);
790 pContext->LocalToString(params[3], &error);
792 IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength);
799 Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
800 if (hndl == BAD_HANDLE)
809 static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params)
814 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
816 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
819 return query->FetchMoreResults() ? 1 : 0;
822 static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params)
827 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
829 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
832 return query->GetResultSet() != NULL ? true : false;
835 static cell_t SQL_GetRowCount(IPluginContext *pContext, const cell_t *params)
840 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
842 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
845 IResultSet *rs = query->GetResultSet();
851 return rs->GetRowCount();
854 static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params)
859 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
861 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
864 IResultSet *rs = query->GetResultSet();
870 return rs->GetFieldCount();
873 static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params)
878 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
880 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
883 IResultSet *rs = query->GetResultSet();
886 return pContext->ThrowNativeError("No current result set");
889 unsigned int field = params[2];
892 if ((fldname = rs->FieldNumToName(field)) == NULL)
894 return pContext->ThrowNativeError("Invalid field index %d", field);
897 pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL);
902 static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params)
907 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
909 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
912 IResultSet *rs = query->GetResultSet();
915 return pContext->ThrowNativeError("No current result set");
919 pContext->LocalToString(params[2], &field);
922 pContext->LocalToPhysAddr(params[3], &num);
924 return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0;
927 static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params)
932 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
934 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
937 IResultSet *rs = query->GetResultSet();
940 return pContext->ThrowNativeError("No current result set");
943 return (rs->FetchRow() != NULL) ? true : false;
946 static cell_t SQL_MoreRows(IPluginContext *pContext, const cell_t *params)
951 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
953 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
956 IResultSet *rs = query->GetResultSet();
959 return pContext->ThrowNativeError("No current result set");
962 return rs->MoreRows() ? 1 : 0;
965 static cell_t SQL_Rewind(IPluginContext *pContext, const cell_t *params)
970 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
972 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
975 IResultSet *rs = query->GetResultSet();
978 return pContext->ThrowNativeError("No current result set");
981 return rs->Rewind() ? 1 : 0;
984 static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params)
989 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
991 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
994 IResultSet *rs = query->GetResultSet();
997 return pContext->ThrowNativeError("No current result set");
1000 IResultRow *row = rs->CurrentRow();
1003 return pContext->ThrowNativeError("Current result set has no fetched rows");
1008 DBResult res = row->GetString(params[2], &str, &length);
1010 if (res == DBVal_Error)
1012 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
1013 } else if (res == DBVal_TypeMismatch) {
1014 return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]);
1017 pContext->StringToLocalUTF8(params[3], params[4], str, &length);
1020 pContext->LocalToPhysAddr(params[5], &addr);
1021 *addr = (cell_t)res;
1023 return (cell_t)length;
1026 static cell_t SQL_FetchFloat(IPluginContext *pContext, const cell_t *params)
1031 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1033 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1036 IResultSet *rs = query->GetResultSet();
1039 return pContext->ThrowNativeError("No current result set");
1042 IResultRow *row = rs->CurrentRow();
1045 return pContext->ThrowNativeError("Current result set has no fetched rows");
1049 DBResult res = row->GetFloat(params[2], &f);
1051 if (res == DBVal_Error)
1053 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
1054 } else if (res == DBVal_TypeMismatch) {
1055 return pContext->ThrowNativeError("Could not fetch data in field %d as a float", params[2]);
1059 pContext->LocalToPhysAddr(params[3], &addr);
1060 *addr = (cell_t)res;
1065 static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params)
1070 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1072 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1075 IResultSet *rs = query->GetResultSet();
1078 return pContext->ThrowNativeError("No current result set");
1081 IResultRow *row = rs->CurrentRow();
1084 return pContext->ThrowNativeError("Current result set has no fetched rows");
1088 DBResult res = row->GetInt(params[2], &iv);
1090 if (res == DBVal_Error)
1092 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
1093 } else if (res == DBVal_TypeMismatch) {
1094 return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]);
1098 pContext->LocalToPhysAddr(params[3], &addr);
1099 *addr = (cell_t)res;
1104 static cell_t SQL_IsFieldNull(IPluginContext *pContext, const cell_t *params)
1109 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1111 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1114 IResultSet *rs = query->GetResultSet();
1117 return pContext->ThrowNativeError("No current result set");
1120 IResultRow *row = rs->CurrentRow();
1123 return pContext->ThrowNativeError("Current result set has no fetched rows");
1126 if ((unsigned)params[2] >= rs->GetFieldCount())
1128 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
1131 return row->IsNull(params[2]) ? 1 : 0;
1134 static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params)
1139 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
1141 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
1144 IResultSet *rs = query->GetResultSet();
1147 return pContext->ThrowNativeError("No current result set");
1150 IResultRow *row = rs->CurrentRow();
1153 return pContext->ThrowNativeError("Current result set has no fetched rows");
1156 if ((unsigned)params[2] >= rs->GetFieldCount())
1158 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
1161 return row->GetDataSize(params[2]);
1164 static cell_t SQL_BindParamInt(IPluginContext *pContext, const cell_t *params)
1166 IPreparedQuery *stmt;
1169 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1171 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1174 if (!stmt->BindParamInt(params[2], params[3], params[4] ? true : false))
1176 return pContext->ThrowNativeError("Could not bind parameter %d as an integer", params[2]);
1182 static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params)
1184 IPreparedQuery *stmt;
1187 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1189 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1192 if (!stmt->BindParamFloat(params[2], sp_ctof(params[3])))
1194 return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]);
1200 static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params)
1202 IPreparedQuery *stmt;
1205 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1207 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1211 pContext->LocalToString(params[3], &str);
1213 if (!stmt->BindParamString(params[2], str, params[4] ? true : false))
1215 return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]);
1221 static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params)
1223 IPreparedQuery *stmt;
1226 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
1228 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
1231 return stmt->Execute() ? 1 : 0;
1234 static cell_t SQL_IsSameConnection(IPluginContext *pContext, const cell_t *params)
1236 IDatabase *db1=NULL, *db2=NULL;
1239 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
1240 != HandleError_None)
1242 return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
1245 if ((err = g_DBMan.ReadHandle(params[2], DBHandle_Database, (void **)&db2))
1246 != HandleError_None)
1248 return pContext->ThrowNativeError("Invalid database Handle 2/%x (error: %d)", params[2], err);
1251 return (db1 == db2) ? true : false;
1254 static cell_t SQL_ReadDriver(IPluginContext *pContext, const cell_t *params)
1256 IDatabase *db1=NULL;
1259 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1))
1260 != HandleError_None)
1262 return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err);
1265 IDBDriver *driver = db1->GetDriver();
1267 pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
1269 return driver->GetHandle();
1272 static cell_t SQL_CheckConfig(IPluginContext *pContext, const cell_t *params)
1275 pContext->LocalToString(params[1], &name);
1277 return (g_DBMan.FindDatabaseConf(name) != NULL) ? 1 : 0;
1280 REGISTER_NATIVES(dbNatives)
1282 {"SQL_BindParamInt", SQL_BindParamInt},
1283 {"SQL_BindParamFloat", SQL_BindParamFloat},
1284 {"SQL_BindParamString", SQL_BindParamString},\
1285 {"SQL_CheckConfig", SQL_CheckConfig},
1286 {"SQL_Connect", SQL_Connect},
1287 {"SQL_ConnectEx", SQL_ConnectEx},
1288 {"SQL_EscapeString", SQL_QuoteString},
1289 {"SQL_Execute", SQL_Execute},
1290 {"SQL_FastQuery", SQL_FastQuery},
1291 {"SQL_FetchFloat", SQL_FetchFloat},
1292 {"SQL_FetchInt", SQL_FetchInt},
1293 {"SQL_FetchMoreResults", SQL_FetchMoreResults},
1294 {"SQL_FetchRow", SQL_FetchRow},
1295 {"SQL_FetchSize", SQL_FetchSize},
1296 {"SQL_FetchString", SQL_FetchString},
1297 {"SQL_FieldNameToNum", SQL_FieldNameToNum},
1298 {"SQL_FieldNumToName", SQL_FieldNumToName},
1299 {"SQL_GetAffectedRows", SQL_GetAffectedRows},
1300 {"SQL_GetDriver", SQL_GetDriver},
1301 {"SQL_GetDriverIdent", SQL_GetDriverIdent},
1302 {"SQL_GetDriverProduct", SQL_GetDriverProduct},
1303 {"SQL_GetError", SQL_GetError},
1304 {"SQL_GetFieldCount", SQL_GetFieldCount},
1305 {"SQL_GetInsertId", SQL_GetInsertId},
1306 {"SQL_GetRowCount", SQL_GetRowCount},
1307 {"SQL_HasResultSet", SQL_HasResultSet},
1308 {"SQL_IsFieldNull", SQL_IsFieldNull},
1309 {"SQL_IsSameConnection", SQL_IsSameConnection},
1310 {"SQL_LockDatabase", SQL_LockDatabase},
1311 {"SQL_MoreRows", SQL_MoreRows},
1312 {"SQL_PrepareQuery", SQL_PrepareQuery},
1313 {"SQL_Query", SQL_Query},
1314 {"SQL_QuoteString", SQL_QuoteString},
1315 {"SQL_ReadDriver", SQL_ReadDriver},
1316 {"SQL_Rewind", SQL_Rewind},
1317 {"SQL_TConnect", SQL_TConnect},
1318 {"SQL_TQuery", SQL_TQuery},
1319 {"SQL_UnlockDatabase", SQL_UnlockDatabase},