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 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
104 /* HACK! Add us to the dependency list */
105 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
108 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
114 static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params)
117 if (params[1] == BAD_HANDLE)
119 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
121 return pContext->ThrowNativeError("Could not find any default driver");
125 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
128 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
132 char *host, *user, *pass, *database, *error;
133 size_t maxlength = (size_t)params[7];
134 bool persistent = params[8] ? true : false;
135 unsigned int port = params[9];
136 unsigned int maxTimeout = params[10];
137 pContext->LocalToString(params[2], &host);
138 pContext->LocalToString(params[3], &user);
139 pContext->LocalToString(params[4], &pass);
140 pContext->LocalToString(params[5], &database);
141 pContext->LocalToString(params[6], &error);
144 info.database = database;
145 info.driver = driver->GetIdentifier();
147 info.maxTimeout = maxTimeout;
152 IDatabase *db = driver->Connect(&info, persistent, error, maxlength);
156 Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
163 /* HACK! Add us to the dependency list */
164 CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
167 g_Extensions.BindChildPlugin(pExt, g_PluginSys.FindPluginByContext(pContext->GetContext()));
176 static cell_t SQL_GetDriverIdent(IPluginContext *pContext, const cell_t *params)
179 if (params[1] == BAD_HANDLE)
181 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
183 return pContext->ThrowNativeError("Could not find any default driver");
187 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
190 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
194 pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL);
199 static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params)
202 pContext->LocalToString(params[1], &name);
204 IDBDriver *driver = NULL;
207 driver = g_DBMan.GetDefaultDriver();
209 driver = g_DBMan.FindOrLoadDriver(name);
212 return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE;
215 static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params)
218 if (params[1] == BAD_HANDLE)
220 if ((driver = g_DBMan.GetDefaultDriver()) == NULL)
222 return pContext->ThrowNativeError("Could not find any default driver");
226 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver))
229 return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err);
233 pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL);
238 static cell_t SQL_GetAffectedRows(IPluginContext *pContext, const cell_t *params)
240 IDatabase *db = NULL;
241 IPreparedQuery *stmt = NULL;
244 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
246 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
251 return db->GetAffectedRows();
253 return stmt->GetAffectedRows();
256 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
259 static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params)
261 IDatabase *db = NULL;
262 IPreparedQuery *stmt = NULL;
265 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
267 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
272 return db->GetInsertID();
274 return stmt->GetInsertID();
277 return pContext->ThrowNativeError("Unknown error reading db/stmt handles");
280 static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params)
282 IDatabase *db = NULL;
283 IPreparedQuery *stmt = NULL;
286 if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None)
288 return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err);
291 const char *error = "";
294 error = db->GetError();
296 error = stmt->GetError();
299 if (error[0] == '\0')
304 pContext->StringToLocalUTF8(params[2], params[3], error, NULL);
309 static cell_t SQL_QuoteString(IPluginContext *pContext, const cell_t *params)
311 IDatabase *db = NULL;
314 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
317 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
320 char *input, *output;
321 size_t maxlength = (size_t)params[4];
322 pContext->LocalToString(params[2], &input);
323 pContext->LocalToString(params[3], &output);
326 bool s = db->QuoteString(input, output, maxlength, &written);
329 pContext->LocalToPhysAddr(params[5], &addr);
330 *addr = (cell_t)written;
335 static cell_t SQL_FastQuery(IPluginContext *pContext, const cell_t *params)
337 IDatabase *db = NULL;
340 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
343 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
347 pContext->LocalToString(params[2], &query);
349 return db->DoSimpleQuery(query) ? 1 : 0;
352 static cell_t SQL_Query(IPluginContext *pContext, const cell_t *params)
354 IDatabase *db = NULL;
357 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
360 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
364 pContext->LocalToString(params[2], &query);
366 IQuery *qr = db->DoQuery(query);
373 Handle_t hndl = g_HandleSys.CreateHandle(hQueryType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
374 if (hndl == BAD_HANDLE)
383 static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params)
385 IDatabase *db = NULL;
388 if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db))
391 return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err);
395 size_t maxlength = (size_t)params[4];
396 pContext->LocalToString(params[2], &query);
397 pContext->LocalToString(params[3], &error);
399 IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength);
406 Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL);
407 if (hndl == BAD_HANDLE)
416 static cell_t SQL_FetchMoreResults(IPluginContext *pContext, const cell_t *params)
421 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
423 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
426 return query->FetchMoreResults() ? 1 : 0;
429 static cell_t SQL_HasResultSet(IPluginContext *pContext, const cell_t *params)
434 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
436 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
439 return query->GetResultSet() != NULL ? true : false;
442 static cell_t SQL_GetRowCount(IPluginContext *pContext, const cell_t *params)
447 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
449 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
452 IResultSet *rs = query->GetResultSet();
458 return rs->GetRowCount();
461 static cell_t SQL_GetFieldCount(IPluginContext *pContext, const cell_t *params)
466 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
468 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
471 IResultSet *rs = query->GetResultSet();
477 return rs->GetFieldCount();
480 static cell_t SQL_FieldNumToName(IPluginContext *pContext, const cell_t *params)
485 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
487 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
490 IResultSet *rs = query->GetResultSet();
493 return pContext->ThrowNativeError("No current result set");
496 unsigned int field = params[2];
499 if ((fldname = rs->FieldNumToName(field)) == NULL)
501 return pContext->ThrowNativeError("Invalid field index %d", field);
504 pContext->StringToLocalUTF8(params[3], params[4], fldname, NULL);
509 static cell_t SQL_FieldNameToNum(IPluginContext *pContext, const cell_t *params)
514 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
516 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
519 IResultSet *rs = query->GetResultSet();
522 return pContext->ThrowNativeError("No current result set");
526 pContext->LocalToString(params[2], &field);
529 pContext->LocalToPhysAddr(params[3], &num);
531 return rs->FieldNameToNum(field, (unsigned int *)num) ? 1 : 0;
534 static cell_t SQL_FetchRow(IPluginContext *pContext, const cell_t *params)
539 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
541 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
544 IResultSet *rs = query->GetResultSet();
547 return pContext->ThrowNativeError("No current result set");
550 return (rs->FetchRow() != NULL) ? true : false;
553 static cell_t SQL_MoreRows(IPluginContext *pContext, const cell_t *params)
558 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
560 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
563 IResultSet *rs = query->GetResultSet();
566 return pContext->ThrowNativeError("No current result set");
569 return rs->MoreRows() ? 1 : 0;
572 static cell_t SQL_Rewind(IPluginContext *pContext, const cell_t *params)
577 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
579 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
582 IResultSet *rs = query->GetResultSet();
585 return pContext->ThrowNativeError("No current result set");
588 return rs->Rewind() ? 1 : 0;
591 static cell_t SQL_FetchString(IPluginContext *pContext, const cell_t *params)
596 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
598 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
601 IResultSet *rs = query->GetResultSet();
604 return pContext->ThrowNativeError("No current result set");
607 IResultRow *row = rs->CurrentRow();
610 return pContext->ThrowNativeError("Current result set has no fetched rows");
615 DBResult res = row->GetString(params[2], &str, &length);
617 if (res == DBVal_Error)
619 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
620 } else if (res == DBVal_TypeMismatch) {
621 return pContext->ThrowNativeError("Could not fetch data in field %d as a string", params[2]);
624 pContext->StringToLocalUTF8(params[3], params[4], str, &length);
627 pContext->LocalToPhysAddr(params[5], &addr);
630 return (cell_t)length;
633 static cell_t SQL_FetchFloat(IPluginContext *pContext, const cell_t *params)
638 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
640 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
643 IResultSet *rs = query->GetResultSet();
646 return pContext->ThrowNativeError("No current result set");
649 IResultRow *row = rs->CurrentRow();
652 return pContext->ThrowNativeError("Current result set has no fetched rows");
656 DBResult res = row->GetFloat(params[2], &f);
658 if (res == DBVal_Error)
660 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
661 } else if (res == DBVal_TypeMismatch) {
662 return pContext->ThrowNativeError("Could not fetch data in field %d as a float", params[2]);
666 pContext->LocalToPhysAddr(params[5], &addr);
672 static cell_t SQL_FetchInt(IPluginContext *pContext, const cell_t *params)
677 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
679 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
682 IResultSet *rs = query->GetResultSet();
685 return pContext->ThrowNativeError("No current result set");
688 IResultRow *row = rs->CurrentRow();
691 return pContext->ThrowNativeError("Current result set has no fetched rows");
695 DBResult res = row->GetInt(params[2], &iv);
697 if (res == DBVal_Error)
699 return pContext->ThrowNativeError("Error fetching data from field %d", params[2]);
700 } else if (res == DBVal_TypeMismatch) {
701 return pContext->ThrowNativeError("Could not fetch data in field %d as an integer", params[2]);
705 pContext->LocalToPhysAddr(params[5], &addr);
711 static cell_t SQL_IsFieldNull(IPluginContext *pContext, const cell_t *params)
716 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
718 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
721 IResultSet *rs = query->GetResultSet();
724 return pContext->ThrowNativeError("No current result set");
727 IResultRow *row = rs->CurrentRow();
730 return pContext->ThrowNativeError("Current result set has no fetched rows");
733 if ((unsigned)params[2] >= rs->GetFieldCount())
735 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
738 return row->IsNull(params[2]) ? 1 : 0;
741 static cell_t SQL_FetchSize(IPluginContext *pContext, const cell_t *params)
746 if ((err = ReadQueryHndl(params[1], pContext, &query)) != HandleError_None)
748 return pContext->ThrowNativeError("Invalid query Handle %x (error: %d)", params[1], err);
751 IResultSet *rs = query->GetResultSet();
754 return pContext->ThrowNativeError("No current result set");
757 IResultRow *row = rs->CurrentRow();
760 return pContext->ThrowNativeError("Current result set has no fetched rows");
763 if ((unsigned)params[2] >= rs->GetFieldCount())
765 return pContext->ThrowNativeError("Invalid field index %d", params[2]);
768 return row->GetDataSize(params[2]);
771 static cell_t SQL_BindParamInt(IPluginContext *pContext, const cell_t *params)
773 IPreparedQuery *stmt;
776 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
778 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
781 if (!stmt->BindParamInt(params[2], params[3], params[4] ? true : false))
783 return pContext->ThrowNativeError("Could not bind parameter %d as an integer", params[2]);
789 static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params)
791 IPreparedQuery *stmt;
794 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
796 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
799 if (!stmt->BindParamFloat(params[2], sp_ctof(params[3])))
801 return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]);
807 static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params)
809 IPreparedQuery *stmt;
812 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
814 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
818 pContext->LocalToString(params[3], &str);
820 if (!stmt->BindParamString(params[2], str, params[4] ? true : false))
822 return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]);
828 static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params)
830 IPreparedQuery *stmt;
833 if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None)
835 return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err);
838 return stmt->Execute() ? 1 : 0;
841 REGISTER_NATIVES(dbNatives)
843 {"SQL_BindParamInt", SQL_BindParamInt},
844 {"SQL_BindParamFloat", SQL_BindParamFloat},
845 {"SQL_BindParamString", SQL_BindParamString},
846 {"SQL_Connect", SQL_Connect},
847 {"SQL_ConnectEx", SQL_ConnectEx},
848 {"SQL_Execute", SQL_Execute},
849 {"SQL_FastQuery", SQL_FastQuery},
850 {"SQL_FetchFloat", SQL_FetchFloat},
851 {"SQL_FetchInt", SQL_FetchInt},
852 {"SQL_FetchMoreResults", SQL_FetchMoreResults},
853 {"SQL_FetchRow", SQL_FetchRow},
854 {"SQL_FetchSize", SQL_FetchSize},
855 {"SQL_FetchString", SQL_FetchString},
856 {"SQL_FieldNameToNum", SQL_FieldNameToNum},
857 {"SQL_FieldNumToName", SQL_FieldNumToName},
858 {"SQL_GetAffectedRows", SQL_GetAffectedRows},
859 {"SQL_GetDriver", SQL_GetDriver},
860 {"SQL_GetDriverIdent", SQL_GetDriverIdent},
861 {"SQL_GetDriverProduct", SQL_GetDriverProduct},
862 {"SQL_GetError", SQL_GetError},
863 {"SQL_GetFieldCount", SQL_GetFieldCount},
864 {"SQL_GetInsertId", SQL_GetInsertId},
865 {"SQL_GetRowCount", SQL_GetRowCount},
866 {"SQL_HasResultSet", SQL_HasResultSet},
867 {"SQL_IsFieldNull", SQL_IsFieldNull},
868 {"SQL_MoreRows", SQL_MoreRows},
869 {"SQL_Query", SQL_Query},
870 {"SQL_QuoteString", SQL_QuoteString},
871 {"SQL_PrepareQuery", SQL_PrepareQuery},
872 {"SQL_Rewind", SQL_Rewind},