1.1 --- a/core/smn_database.cpp Thu Oct 09 20:19:22 2008 +1300
1.2 +++ b/core/smn_database.cpp Mon Oct 13 01:13:39 2008 -0500
1.3 @@ -1277,6 +1277,76 @@
1.4 return (g_DBMan.FindDatabaseConf(name) != NULL) ? 1 : 0;
1.5 }
1.6
1.7 +static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params)
1.8 +{
1.9 + KeyValues *kv;
1.10 + HandleError err;
1.11 +
1.12 + kv = g_SourceMod.ReadKeyValuesHandle(params[1], &err, false);
1.13 + if (kv == NULL)
1.14 + {
1.15 + return pContext->ThrowNativeError("Invalid KeyValues handle %x (error: %d)",
1.16 + params[1],
1.17 + err);
1.18 + }
1.19 +
1.20 + DatabaseInfo info;
1.21 + info.database = kv->GetString("database", "");
1.22 + info.driver = kv->GetString("driver", "default");
1.23 + info.host = kv->GetString("host", "");
1.24 + info.maxTimeout = kv->GetInt("timeout", 0);
1.25 + info.pass = kv->GetString("pass", "");
1.26 + info.port = kv->GetInt("port", 0);
1.27 + info.user = kv->GetString("user", "");
1.28 +
1.29 + IDBDriver *driver;
1.30 + if (info.driver[0] == '\0' || strcmp(info.driver, "default") == 0)
1.31 + {
1.32 + driver = g_DBMan.GetDefaultDriver();
1.33 + }
1.34 + else
1.35 + {
1.36 + driver = g_DBMan.FindOrLoadDriver(info.driver);
1.37 + }
1.38 +
1.39 + if (driver == NULL)
1.40 + {
1.41 + char buffer[255];
1.42 +
1.43 + UTIL_Format(buffer, sizeof(buffer), "Could not find driver \"%s\"", info.driver);
1.44 + pContext->StringToLocalUTF8(params[2], params[3], buffer, NULL);
1.45 +
1.46 + return BAD_HANDLE;
1.47 + }
1.48 +
1.49 + char *buffer;
1.50 + IDatabase *db;
1.51 +
1.52 + pContext->LocalToString(params[2], &buffer);
1.53 +
1.54 + db = driver->Connect(&info, params[4] ? true : false, buffer, params[3]);
1.55 + if (db == NULL)
1.56 + {
1.57 + return BAD_HANDLE;
1.58 + }
1.59 +
1.60 + Handle_t hndl = g_DBMan.CreateHandle(DBHandle_Database, db, pContext->GetIdentity());
1.61 + if (!hndl)
1.62 + {
1.63 + db->Close();
1.64 + return pContext->ThrowNativeError("Out of handles!");
1.65 + }
1.66 +
1.67 + /* HACK! Add us to the dependency list */
1.68 + CExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity());
1.69 + if (pExt)
1.70 + {
1.71 + g_Extensions.BindChildPlugin(pExt, g_PluginSys.GetPluginByCtx(pContext->GetContext()));
1.72 + }
1.73 +
1.74 + return hndl;
1.75 +}
1.76 +
1.77 REGISTER_NATIVES(dbNatives)
1.78 {
1.79 {"SQL_BindParamInt", SQL_BindParamInt},
1.80 @@ -1317,5 +1387,7 @@
1.81 {"SQL_TConnect", SQL_TConnect},
1.82 {"SQL_TQuery", SQL_TQuery},
1.83 {"SQL_UnlockDatabase", SQL_UnlockDatabase},
1.84 + {"SQL_ConnectCustom", SQL_ConnectCustom},
1.85 {NULL, NULL},
1.86 };
1.87 +