static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params) { char *conf, *err; size_t maxlength = (size_t)params[4]; bool persistent = params[2] ? true : false; pContext->LocalToString(params[1], &conf); pContext->LocalToString(params[3], &err); IDBDriver *driver; IDatabase *db; if (!g_DBMan.Connect(conf, &driver, &db, persistent, err, maxlength)) { return BAD_HANDLE; } Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity()); if (!hndl) { db->Close(); return BAD_HANDLE; } /* HACK! Add us to the dependency list */ CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext())); } return hndl; }
static cell_t SQL_GetDriver(IPluginContext *pContext, const cell_t *params) { char *name; pContext->LocalToString(params[1], &name); IDBDriver *driver = NULL; if (name[0] == '\0') { driver = g_DBMan.GetDefaultDriver(); } else { driver = g_DBMan.FindOrLoadDriver(name); } return (driver != NULL) ? driver->GetHandle() : BAD_HANDLE; }
static cell_t SQL_ReadDriver(IPluginContext *pContext, const cell_t *params) { IDatabase *db1=NULL; HandleError err; if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Database, (void **)&db1)) != HandleError_None) { return pContext->ThrowNativeError("Invalid database Handle 1/%x (error: %d)", params[1], err); } IDBDriver *driver = db1->GetDriver(); pContext->StringToLocalUTF8(params[2], params[3], driver->GetIdentifier(), NULL); return driver->GetHandle(); }
static cell_t SQL_GetDriverProduct(IPluginContext *pContext, const cell_t *params) { IDBDriver *driver; if (params[1] == BAD_HANDLE) { if ((driver = g_DBMan.GetDefaultDriver()) == NULL) { return pContext->ThrowNativeError("Could not find any default driver"); } } else { HandleError err; if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver)) != HandleError_None) { return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err); } } pContext->StringToLocalUTF8(params[2], params[3], driver->GetProductName(), NULL); return 1; }
static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params) { IDBDriver *driver; if (params[1] == BAD_HANDLE) { if ((driver = g_DBMan.GetDefaultDriver()) == NULL) { return pContext->ThrowNativeError("Could not find any default driver"); } } else { HandleError err; if ((err = g_DBMan.ReadHandle(params[1], DBHandle_Driver, (void **)&driver)) != HandleError_None) { return pContext->ThrowNativeError("Invalid driver Handle %x (error: %d)", params[1], err); } } char *host, *user, *pass, *database, *error; size_t maxlength = (size_t)params[7]; bool persistent = params[8] ? true : false; unsigned int port = params[9]; unsigned int maxTimeout = params[10]; pContext->LocalToString(params[2], &host); pContext->LocalToString(params[3], &user); pContext->LocalToString(params[4], &pass); pContext->LocalToString(params[5], &database); pContext->LocalToString(params[6], &error); DatabaseInfo info; info.database = database; info.driver = driver->GetIdentifier(); info.host = host; info.maxTimeout = maxTimeout; info.pass = pass; info.port = port; info.user = user; IDatabase *db = driver->Connect(&info, persistent, error, maxlength); if (db) { Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity()); if (!hndl) { db->Close(); return BAD_HANDLE; } /* HACK! Add us to the dependency list */ CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext())); } return hndl; } return BAD_HANDLE; }
static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params) { IPluginFunction *pf = pContext->GetFunctionById(params[1]); if (!pf) { return pContext->ThrowNativeError("Function id %x is invalid", params[1]); } char *conf; pContext->LocalToString(params[2], &conf); IDBDriver *driver = NULL; const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(conf); char error[255]; if (pInfo != NULL) { if (pInfo->driver[0] == '\0') { driver = g_DBMan.GetDefaultDriver(); } else { driver = g_DBMan.FindOrLoadDriver(pInfo->driver); } if (!driver) { UTIL_Format(error, sizeof(error), "Could not find driver \"%s\"", pInfo->driver[0] == '\0' ? g_DBMan.GetDefaultDriverName() : pInfo->driver); } else if (!driver->IsThreadSafe()) { UTIL_Format(error, sizeof(error), "Driver \"%s\" is not thread safe!", driver->GetIdentifier()); } } else { UTIL_Format(error, sizeof(error), "Could not find database conf \"%s\"", conf); } if (!pInfo || !driver) { pf->PushCell(BAD_HANDLE); pf->PushCell(BAD_HANDLE); pf->PushString(error); pf->PushCell(0); pf->Execute(NULL); return 0; } /* HACK! Add us to the dependency list */ CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext())); } /* Finally, add to the thread if we can */ TConnectOp *op = new TConnectOp(pf, driver, conf, params[3]); CPlugin *pPlugin = g_PluginSys.GetPluginByCtx(pContext->GetContext()); if (pPlugin->GetProperty("DisallowDBThreads", NULL) || !g_DBMan.AddToThreadQueue(op, PrioQueue_High)) { /* Do everything right now */ op->RunThreadPart(); op->RunThinkPart(); op->Destroy(); } return 1; }
static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) { KeyValues *kv; HandleError err; kv = g_SourceMod.ReadKeyValuesHandle(params[1], &err, false); if (kv == NULL) { return pContext->ThrowNativeError("Invalid KeyValues handle %x (error: %d)", params[1], err); } DatabaseInfo info; info.database = kv->GetString("database", ""); info.driver = kv->GetString("driver", "default"); info.host = kv->GetString("host", ""); info.maxTimeout = kv->GetInt("timeout", 0); info.pass = kv->GetString("pass", ""); info.port = kv->GetInt("port", 0); info.user = kv->GetString("user", ""); IDBDriver *driver; if (info.driver[0] == '\0' || strcmp(info.driver, "default") == 0) { driver = g_DBMan.GetDefaultDriver(); } else { driver = g_DBMan.FindOrLoadDriver(info.driver); } if (driver == NULL) { char buffer[255]; UTIL_Format(buffer, sizeof(buffer), "Could not find driver \"%s\"", info.driver); pContext->StringToLocalUTF8(params[2], params[3], buffer, NULL); return BAD_HANDLE; } char *buffer; IDatabase *db; pContext->LocalToString(params[2], &buffer); db = driver->Connect(&info, params[4] ? true : false, buffer, params[3]); if (db == NULL) { return BAD_HANDLE; } Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity()); if (!hndl) { db->Close(); return pContext->ThrowNativeError("Out of handles!"); } /* HACK! Add us to the dependency list */ CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext())); } return hndl; }
static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) { KeyValues *kv; HandleError err; kv = g_pSM->ReadKeyValuesHandle(params[1], &err, false); if (kv == NULL) { return pContext->ThrowNativeError("Invalid KeyValues handle %x (error: %d)", params[1], err); } DatabaseInfo info = smcore.GetDBInfoFromKeyValues(kv); IDBDriver *driver; if (info.driver[0] == '\0' || strcmp(info.driver, "default") == 0) { driver = g_DBMan.GetDefaultDriver(); } else { driver = g_DBMan.FindOrLoadDriver(info.driver); } if (driver == NULL) { char buffer[255]; g_pSM->Format(buffer, sizeof(buffer), "Could not find driver \"%s\"", info.driver); pContext->StringToLocalUTF8(params[2], params[3], buffer, NULL); return BAD_HANDLE; } char *buffer; IDatabase *db; pContext->LocalToString(params[2], &buffer); db = driver->Connect(&info, params[4] ? true : false, buffer, params[3]); if (db == NULL) { return BAD_HANDLE; } Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity()); if (!hndl) { db->Close(); return pContext->ThrowNativeError("Out of handles!"); } /* HACK! Add us to the dependency list */ IExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { g_Extensions.BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); } return hndl; }