language-extensions/R/src/RLibrarySession.cpp
//--------------------------------------------------------------------------------------------------
// Name: RLibrarySession::UninstallLibrary
//
// Description:
//  Uninstalls from the specified library directory.
//  We use remove.packages R function with its 'lib' argument pointing to the directory from which
//  to install. If that fails for some reason, we try to manually uninstall the package
//  by deleting the top level package folder.
//
// Returns:
//  SQL_SUCCESS on complete uninstallation. In case of any failures, returns SQL_ERROR.
//
:UninstallLibrary
Description:
Uninstalls from the specified library directory.
We use remove.packages R function with its 'lib' argument pointing to the directory from which
to install. If that fails for some reason, we try to manually uninstall the package
by deleting the top level package folder.

SQL_SUCCESS on complete uninstallation. In case of any failures, returns SQL_ERROR.
: UninstallLibrary Description : Uninstalls from the specified library directory . We use remove . packages R function with its ' lib ' argument pointing to the directory from which to install . If that fails for some reason we try to manually uninstall the package by deleting the top level package folder . SQL_SUCCESS on complete uninstallation . In case of any failures returns SQL_ERROR .
SQLRETURN RLibrarySession::UninstallLibrary(
	const SQLCHAR *libraryInstallDirectory,
	SQLINTEGER    libraryInstallDirectoryLength)
{
	LOG("RLibrarySession::UninstallLibrary");
	SQLRETURN result = SQL_ERROR;
	string errorString;
	string installDir = string(static_cast<const char*>(
			static_cast<const void*>(libraryInstallDirectory)), libraryInstallDirectoryLength);
	installDir = Utilities::NormalizePathString(installDir);
	try
	{
		string uninstallScript = "remove.packages(pkgs = '" + m_libraryName +
			"', lib = '" + installDir + "');";
		ExecuteScript(uninstallScript);
		bool isInstalled = IsLibraryInstalledAtPath(installDir);
		if (isInstalled)
		{
			throw runtime_error("remove.packages() failed to uninstall the package " +
				m_libraryName + ".");
		}
		result = SQL_SUCCESS;
	}
	catch (const exception & ex)
	{
		result = SQL_ERROR;
		errorString = string(ex.what());
	}
	catch (...)
	{
		result = SQL_ERROR;
		errorString = "Unexpected exception occurred in function UninstallLibrary().";
	}
	 If remove.packages fails for some reason, we try to manually uninstall the package
	 by deleting the top level package folder.
	if (result != SQL_SUCCESS && fs::exists(installDir))
	{
		LOG_ERROR(errorString);
		string libraryDir = Utilities::NormalizePathString(
			fs::path(installDir).append(m_libraryName).string());
		 Clean up the library directory folder
		if (fs::exists(libraryDir))
		{
			LOG("Failed to completely uninstall " + m_libraryName + " with remove.packages,"
				" so deleting files manually...");
			error_code errorCode;
			 returnValue is SQL_ERROR(-1) if there is an error, otherwise it is the
			 number of files deleted.
			uintmax_t returnValue = fs::remove_all(libraryDir, errorCode);
			if (returnValue == static_cast<uintmax_t>(SQL_ERROR))
			{
				result = errorCode.value();
				LOG_ERROR("Failed to manually delete the directory with error code "
					+ to_string(result) + ".");
			}
			else
			{
				 If we successfully removed all the files, then we have a SUCCESS result.
				result = SQL_SUCCESS;
				LOG("Manual deletion succeeded.");
			}
		}
		else
		{
			result = SQL_SUCCESS;
			LOG("Even though there was an error, library directory does not exist.");
		}
	}
	return result;
}
SQLRETURN RLibrarySession :: UninstallLibrary ( const SQLCHAR * libraryInstallDirectory , SQLINTEGER libraryInstallDirectoryLength ) { LOG ( " " ) ; SQLRETURN result = SQL_ERROR ; string errorString ; string installDir = string ( static_cast < const char * > ( static_cast < const void * > ( libraryInstallDirectory ) ) , libraryInstallDirectoryLength ) ; installDir = Utilities :: NormalizePathString ( installDir ) ; try { string uninstallScript = " " + m_libraryName + " " + installDir + " " ; ExecuteScript ( uninstallScript ) ; bool isInstalled = IsLibraryInstalledAtPath ( installDir ) ; if ( isInstalled ) { throw runtime_error ( " " + m_libraryName + " " ) ; } result = SQL_SUCCESS ; } catch ( const exception & ex ) { result = SQL_ERROR ; errorString = string ( ex . what ( ) ) ; } catch ( ... ) { result = SQL_ERROR ; errorString = " " ; } if ( result != SQL_SUCCESS && fs :: exists ( installDir ) ) { LOG_ERROR ( errorString ) ; string libraryDir = Utilities :: NormalizePathString ( fs :: path ( installDir ) . append ( m_libraryName ) . string ( ) ) ; if ( fs :: exists ( libraryDir ) ) { LOG ( " " + m_libraryName + " " " " ) ; error_code errorCode ; uintmax_t returnValue = fs :: remove_all ( libraryDir , errorCode ) ; if ( returnValue == static_cast < uintmax_t > ( SQL_ERROR ) ) { result = errorCode . value ( ) ; LOG_ERROR ( " " + to_string ( result ) + " " ) ; } else { result = SQL_SUCCESS ; LOG ( " " ) ; } } else { result = SQL_SUCCESS ; LOG ( " " ) ; } } return result ; }
Name: RLibrarySession::UninstallLibrary
Description:
Uninstalls from the specified library directory.
Name : RLibrarySession :: UninstallLibrary Description : Uninstalls from the specified library directory .
// Uninstall the given libName from the installDir path. // // If remove.packages fails for some reason, we try to manually uninstall the package // by deleting the top level package folder. // // Clean up the library directory folder // // returnValue is SQL_ERROR(-1) if there is an error, otherwise it is the // number of files deleted. // // If we successfully removed all the files, then we have a SUCCESS result. //