1 #include "sm_globals.h"
4 #include "ExtensionSys.h"
7 HandleType_t hQueryType;
8 HandleType_t hStmtType;
10 class DatabaseHelpers :
12 public IHandleTypeDispatch
15 virtual void OnSourceModAllInitialized()
20 g_HandleSys.InitAccessDefaults(NULL, &acc);
21 acc.access[HandleAccess_Clone] = HANDLE_RESTRICT_OWNER|HANDLE_RESTRICT_IDENTITY;
25 g_HandleSys.InitAccessDefaults(&tacc, NULL);
26 tacc.ident = g_pCoreIdent;
28 hQueryType = g_HandleSys.CreateType("IQuery", this, 0, &tacc, &acc, g_pCoreIdent, NULL);
29 hStmtType = g_HandleSys.CreateType("IPreparedQuery", this, hQueryType, &tacc, &acc, g_pCoreIdent, NULL);
32 virtual void OnSourceModShutdown()
34 g_HandleSys.RemoveType(hStmtType, g_pCoreIdent);
35 g_HandleSys.RemoveType(hQueryType, g_pCoreIdent);
38 virtual void OnHandleDestroy(HandleType_t type, void *object)
40 if (type == hQueryType)
42 IQuery *query = (IQuery *)object;
44 } else if (type == hStmtType) {
45 IPreparedQuery *query = (IPreparedQuery *)object;
49 } s_DatabaseNativeHelpers;
51 //is this safe for stmt handles? i think since it's single inheritance, it always will be.
52 inline HandleError ReadQueryHndl(Handle_t hndl, IPluginContext *pContext, IQuery **query)
55 sec.pOwner = pContext->GetIdentity();
56 sec.pIdentity = g_pCoreIdent;
58 return g_HandleSys.ReadHandle(hndl, hQueryType, &sec, (void **)query);
61 inline HandleError ReadStmtHndl(Handle_t hndl, IPluginContext *pContext, IPreparedQuery **query)
64 sec.pOwner = pContext->GetIdentity();
65 sec.pIdentity = g_pCoreIdent;
67 return g_HandleSys.ReadHandle(hndl, hStmtType, &sec, (void **)query);
70 inline HandleError ReadDbOrStmtHndl(Handle_t hndl, IPluginContext *pContext, IDatabase **db, IPreparedQuery **query)
73 if ((err = g_DBMan.ReadHandle(hndl, DBHandle_Database, (void **)db)) == HandleError_Type)
76 return ReadStmtHndl(hndl, pContext, query);
81 static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params)
85 size_t maxlength = (size_t)params[4];
86 bool persistent = params[2] ? true : false;
87 pContext->LocalToString(params[1], &conf);
88 pContext->LocalToString(params[3], &err);
92 if (!g_DBMan.Connect(conf, &driver, &db, persistent, err, maxlength))
97 /* HACK! Add us to the dependency list */
98 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
101 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
104 return db->GetHandle();
107 static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
110 if (params[1] == BAD_HANDLE)
112 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
114 return pContext->ThrowNativeError("Could not find any default driver");
118 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
121 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
125 char *host, *user, *pass, *database, *error;
126 size_t maxlength = (size_t)params[7];
127 bool persistent = params[8] ? true : false;
128 unsigned int port = params[9];
129 unsigned int maxTimeout = params[10];
130 pContext->LocalToString(params[2], &host);
131 pContext->LocalToString(params[3], &user);
132 pContext->LocalToString(params[4], &pass);
133 pContext->LocalToString(params[5], &database);
134 pContext->LocalToString(params[6], &error);
137 info.database = database;
138 info.driver = driver->GetIdentifier();
140 info.maxTimeout = maxTimeout;
145 IDatabase *db = driver->Connect(&info, persistent, error, maxlength);
149 /* HACK! Add us to the dependency list */
150 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
153 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
156 return db->GetHandle();
162 static cell_t SQL_GetDriverIdent(IPluginContext *pContext, const cell_t *params)
165 if (params[1] == BAD_HANDLE)
167 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
169 return pContext->ThrowNativeError("Could not find any default driver");
173 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
176 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
180 pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
185 static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params)
188 pContext->LocalToString(params[1], &name);
190 IDBDriver *driver = NULL;
193 driver = g_DBMan.GetDefaultDriver();
195 driver = g_DBMan.FindOrLoadDriver(name);
198 return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE;
201 static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params)
204 if (params[1] == BAD_HANDLE)
206 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
208 return pContext->ThrowNativeError("Could not find any default driver");
212 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
215 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
219 pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL);
224 static cell_t SQL_GetAffectedRows(IPluginContext *pContext, const cell_t *params)
226 IDatabase *db = NULL;
227 IPreparedQuery *stmt = NULL;
230 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
232 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
237 return db->GetAffectedRows();
239 return stmt->GetAffectedRows();
242 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
245 static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params)
247 IDatabase *db = NULL;
248 IPreparedQuery *stmt = NULL;
251 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
253 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
258 return db->GetInsertID();
260 return stmt->GetInsertID();
263 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
266 static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params)
268 IDatabase *db = NULL;
269 IPreparedQuery *stmt = NULL;
272 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
274 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
277 const char *error = "";
280 error = db->GetError();
282 error = stmt->GetError();
285 if (error[0] == '\0')
290 pContext->StringToLocalUTF8(params[2], params[3], error, NULL);
295 static cell_t SQL_QuoteString(IPluginContext *pContext, const cell_t *params)
297 IDatabase *db = NULL;
300 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
303 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
306 char *input, *output;
307 size_t maxlength = (size_t)params[4];
308 pContext->LocalToString(params[2], &input);
309 pContext->LocalToString(params[3], &output);
312 bool s = db->QuoteString(input, output, maxlength, &written);
315 pContext->LocalToPhysAddr(params[5], &addr);
316 *addr = (cell_t)written;
321 static cell_t SQL_FastQuery(IPluginContext *pContext, const cell_t *params)
323 IDatabase *db = NULL;
326 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
329 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
333 pContext->LocalToString(params[2], &query);
335 return db->DoSimpleQuery(query) ? 1 : 0;
338 static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params)
340 IDatabase *db = NULL;
343 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
346 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
350 pContext->LocalToString(params[2], &query);
352 IQuery *qr = db->DoQuery(query);
359 Handle_t hndl = g_HandleSys.CreateHandle(hQueryType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
360 if (hndl == BAD_HANDLE)
369 static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params)
371 IDatabase *db = NULL;
374 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
377 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
381 size_t maxlength = (size_t)params[4];
382 pContext->LocalToString(params[2], &query);
383 pContext->LocalToString(params[3], &error);
385 IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength);
392 Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
393 if (hndl == BAD_HANDLE)
402 static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params)
407 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
409 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
412 return query->FetchMoreResults() ? 1 : 0;
415 static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params)
420 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
422 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
425 return query->GetResultSet() != NULL ? true : false;
428 static cell_t SQL_GetRowCount(IPluginContext *pContext, const cell_t *params)
433 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
435 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
438 IResultSet *rs = query->GetResultSet();
444 return rs->GetRowCount();
447 static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params)
452 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
454 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
457 IResultSet *rs = query->GetResultSet();
463 return rs->GetFieldCount();
466 static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params)
471 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
473 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
476 IResultSet *rs = query->GetResultSet();
479 return pContext->ThrowNativeError("No current result set");
482 unsigned int field = params[2];
485 if ((fldname = rs->FieldNumToName(field)) == NULL)
487 return pContext->ThrowNativeError("Invalid field index %d", field);
490 pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL);
495 static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params)
500 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
502 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
505 IResultSet *rs = query->GetResultSet();
508 return pContext->ThrowNativeError("No current result set");
512 pContext->LocalToString(params[2], &field);
515 pContext->LocalToPhysAddr(params[3], &num);
517 return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0;
520 static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params)
525 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
527 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
530 IResultSet *rs = query->GetResultSet();
533 return pContext->ThrowNativeError("No current result set");
536 return (rs->FetchRow() != NULL) ? true : false;
539 static cell_t SQL_MoreRows(IPluginContext *pContext, const cell_t *params)
544 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
546 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
549 IResultSet *rs = query->GetResultSet();
552 return pContext->ThrowNativeError("No current result set");
555 return rs->MoreRows() ? 1 : 0;
558 static cell_t SQL_Rewind(IPluginContext *pContext, const cell_t *params)
563 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
565 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
568 IResultSet *rs = query->GetResultSet();
571 return pContext->ThrowNativeError("No current result set");
574 return rs->Rewind() ? 1 : 0;
577 static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params)
582 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
584 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
587 IResultSet *rs = query->GetResultSet();
590 return pContext->ThrowNativeError("No current result set");
593 IResultRow *row = rs->CurrentRow();
596 return pContext->ThrowNativeError("Current result set has no fetched rows");
601 DBResult res = row->GetString(params[2], &str, &length);
603 if (res == DBVal_Error)
605 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
606 } else if (res == DBVal_TypeMismatch) {
607 return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]);
610 pContext->StringToLocalUTF8(params[3], params[4], str, &length);
613 pContext->LocalToPhysAddr(params[5], &addr);
616 return (cell_t)length;
619 static cell_t SQL_FetchFloat(IPluginContext *pContext, const cell_t *params)
624 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
626 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
629 IResultSet *rs = query->GetResultSet();
632 return pContext->ThrowNativeError("No current result set");
635 IResultRow *row = rs->CurrentRow();
638 return pContext->ThrowNativeError("Current result set has no fetched rows");
642 DBResult res = row->GetFloat(params[2], &f);
644 if (res == DBVal_Error)
646 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
647 } else if (res == DBVal_TypeMismatch) {
648 return pContext->ThrowNativeError("Could not fetch data in field %d as a float", params[2]);
652 pContext->LocalToPhysAddr(params[5], &addr);
658 static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params)
663 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
665 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
668 IResultSet *rs = query->GetResultSet();
671 return pContext->ThrowNativeError("No current result set");
674 IResultRow *row = rs->CurrentRow();
677 return pContext->ThrowNativeError("Current result set has no fetched rows");
681 DBResult res = row->GetInt(params[2], &iv);
683 if (res == DBVal_Error)
685 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
686 } else if (res == DBVal_TypeMismatch) {
687 return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]);
691 pContext->LocalToPhysAddr(params[5], &addr);
697 static cell_t SQL_IsFieldNull(IPluginContext *pContext, const cell_t *params)
702 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
704 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
707 IResultSet *rs = query->GetResultSet();
710 return pContext->ThrowNativeError("No current result set");
713 IResultRow *row = rs->CurrentRow();
716 return pContext->ThrowNativeError("Current result set has no fetched rows");
719 if ((unsigned)params[2] >= rs->GetFieldCount())
721 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
724 return row->IsNull(params[2]) ? 1 : 0;
727 static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params)
732 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
734 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
737 IResultSet *rs = query->GetResultSet();
740 return pContext->ThrowNativeError("No current result set");
743 IResultRow *row = rs->CurrentRow();
746 return pContext->ThrowNativeError("Current result set has no fetched rows");
749 if ((unsigned)params[2] >= rs->GetFieldCount())
751 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
754 return row->GetDataSize(params[2]);
757 static cell_t SQL_BindParamInt(IPluginContext *pContext, const cell_t *params)
759 IPreparedQuery *stmt;
762 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
764 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
767 if (!stmt->BindParamInt(params[2], params[3], params[4] ? true : false))
769 return pContext->ThrowNativeError("Could not bind parameter %d as an integer", params[2]);
775 static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params)
777 IPreparedQuery *stmt;
780 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
782 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
785 if (!stmt->BindParamFloat(params[2], sp_ctof(params[3])))
787 return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]);
793 static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params)
795 IPreparedQuery *stmt;
798 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
800 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
804 pContext->LocalToString(params[3], &str);
806 if (!stmt->BindParamString(params[2], str, params[4] ? true : false))
808 return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]);
814 static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params)
816 IPreparedQuery *stmt;
819 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
821 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
824 return stmt->Execute() ? 1 : 0;
827 REGISTER_NATIVES(dbNatives)
829 {"SQL_BindParamInt", SQL_BindParamInt},
830 {"SQL_BindParamFloat", SQL_BindParamFloat},
831 {"SQL_BindParamString", SQL_BindParamString},
832 {"SQL_Connect", SQL_Connect},
833 {"SQL_ConnectEx", SQL_ConnectEx},
834 {"SQL_Execute", SQL_Execute},
835 {"SQL_FastQuery", SQL_FastQuery},
836 {"SQL_FetchFloat", SQL_FetchFloat},
837 {"SQL_FetchInt", SQL_FetchInt},
838 {"SQL_FetchMoreResults", SQL_FetchMoreResults},
839 {"SQL_FetchRow", SQL_FetchRow},
840 {"SQL_FetchSize", SQL_FetchSize},
841 {"SQL_FetchString", SQL_FetchString},
842 {"SQL_FieldNameToNum", SQL_FieldNameToNum},
843 {"SQL_FieldNumToName", SQL_FieldNumToName},
844 {"SQL_GetAffectedRows", SQL_GetAffectedRows},
845 {"SQL_GetDriver", SQL_GetDriver},
846 {"SQL_GetDriverIdent", SQL_GetDriverIdent},
847 {"SQL_GetDriverProduct", SQL_GetDriverProduct},
848 {"SQL_GetError", SQL_GetError},
849 {"SQL_GetFieldCount", SQL_GetFieldCount},
850 {"SQL_GetInsertId", SQL_GetInsertId},
851 {"SQL_GetRowCount", SQL_GetRowCount},
852 {"SQL_HasResultSet", SQL_HasResultSet},
853 {"SQL_IsFieldNull", SQL_IsFieldNull},
854 {"SQL_MoreRows", SQL_MoreRows},
855 {"SQL_Query", SQL_Query},
856 {"SQL_QuoteString", SQL_QuoteString},
857 {"SQL_PrepareQuery", SQL_PrepareQuery},
858 {"SQL_Rewind", SQL_Rewind},