Esempio n. 1
0
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();
}
Esempio n. 2
0
static cell_t SQL_GetDriverIdent(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->GetIdentifier(), NULL);

	return 1;
}
Esempio n. 3
0
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;
}
Esempio n. 4
0
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;
}