static cell_t SQL_PrepareQuery(IPluginContext *pContext, const cell_t *params) { IDatabase *db = NULL; HandleError err; if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db)) != HandleError_None) { return pContext->ThrowNativeError("Invalid database Handle %x (error: %d)", params[1], err); } char *query, *error; size_t maxlength = (size_t)params[4]; pContext->LocalToString(params[2], &query); pContext->LocalToString(params[3], &error); IPreparedQuery *qr = db->PrepareQuery(query, error, maxlength); if (!qr) { return BAD_HANDLE; } Handle_t hndl = g_HandleSys.CreateHandle(hStmtType, qr, pContext->GetIdentity(), g_pCoreIdent, NULL); if (hndl == BAD_HANDLE) { qr->Destroy(); return BAD_HANDLE; } return hndl; }
static cell_t SQL_GetError(IPluginContext *pContext, const cell_t *params) { IDatabase *db = NULL; IPreparedQuery *stmt = NULL; HandleError err; if ((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None) { return pContext->ThrowNativeError("Invalid statement or db Handle %x (error: %d)", params[1], err); } const char *error = ""; if (db) { error = db->GetError(); } else if (stmt) { error = stmt->GetError(); } if (error[0] == '\0') { return false; } pContext->StringToLocalUTF8(params[2], params[3], error, NULL); return 1; }
static cell_t SQL_GetInsertId(IPluginContext *pContext, const cell_t *params) { IDatabase *db = NULL; IQuery *query = NULL; IPreparedQuery *stmt = NULL; HandleError err; if (((err = ReadDbOrStmtHndl(params[1], pContext, &db, &stmt)) != HandleError_None) && ((err = ReadQueryAndDbHndl(params[1], pContext, &query, &db)) != HandleError_None)) { return pContext->ThrowNativeError("Invalid statement, db, or query Handle %x (error: %d)", params[1], err); } if (query) { return db->GetInsertIDForQuery(query); } else if (db) { return db->GetInsertID(); } else if (stmt) { return stmt->GetInsertID(); } return pContext->ThrowNativeError("Unknown error reading db/stmt/query handles"); }
virtual void OnHandleDestroy(HandleType_t type, void *object) { if (type == hCombinedQueryType) { CombinedQuery *combined = (CombinedQuery *)object; combined->query->Destroy(); delete combined; } else if (type == hStmtType) { IPreparedQuery *query = (IPreparedQuery *)object; query->Destroy(); } }
static cell_t SQL_Execute(IPluginContext *pContext, const cell_t *params) { IPreparedQuery *stmt; HandleError err; if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None) { return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err); } return stmt->Execute() ? 1 : 0; }
IQuery *SqDatabase::DoQueryEx(const char *query, size_t len) { IPreparedQuery *pQuery = PrepareQueryEx(query, len, NULL, 0, NULL); if (!pQuery) { return NULL; } if (!pQuery->Execute()) { pQuery->Destroy(); return NULL; } return pQuery; }
static cell_t SQL_BindParamFloat(IPluginContext *pContext, const cell_t *params) { IPreparedQuery *stmt; HandleError err; if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None) { return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err); } if (!stmt->BindParamFloat(params[2], sp_ctof(params[3]))) { return pContext->ThrowNativeError("Could not bind parameter %d as a float", params[2]); } return 1; }
static cell_t SQL_BindParamString(IPluginContext *pContext, const cell_t *params) { IPreparedQuery *stmt; HandleError err; if ((err = ReadStmtHndl(params[1], pContext, &stmt)) != HandleError_None) { return pContext->ThrowNativeError("Invalid statement Handle %x (error: %d)", params[1], err); } char *str; pContext->LocalToString(params[3], &str); if (!stmt->BindParamString(params[2], str, params[4] ? true : false)) { return pContext->ThrowNativeError("Could not bind parameter %d as a string", params[2]); } return 1; }