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_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_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; }