language-extensions/R/src/RLibrarySession.cpp
//--------------------------------------------------------------------------------------------------
// Name: RLibrarySession::InstallLibrary
//
// Description:
//  Installs the contents of the libraryFile which is of the form <dbId>_<userId>_libName.zip
//  to the given installation directory. We copy the same contents into a new zip
//  whose name is of the form libName.zip. We use the R function install.packages specifying this
//  new zip as the value for its 'pkgs' argument and 'lib' pointing to the install directory.
//
// Returns:
//  SQL_SUCCESS if installation completes, throws an exception otherwise.
//
:InstallLibrary
Description:
Installs the contents of the libraryFile which is of the form __libName.zip
to the given installation directory. We copy the same contents into a new zip
whose name is of the form libName.zip. We use the R function install.packages specifying this
new zip as the value for its 'pkgs' argument and 'lib' pointing to the install directory.

SQL_SUCCESS if installation completes, throws an exception otherwise.
: InstallLibrary Description : Installs the contents of the libraryFile which is of the form __libName . zip to the given installation directory . We copy the same contents into a new zip whose name is of the form libName . zip . We use the R function install . packages specifying this new zip as the value for its ' pkgs ' argument and ' lib ' pointing to the install directory . SQL_SUCCESS if installation completes throws an exception otherwise .
SQLRETURN RLibrarySession::InstallLibrary(
	const SQLCHAR *libraryFile,
	SQLINTEGER    libraryFileLength,
	const SQLCHAR *libraryInstallDirectory,
	SQLINTEGER    libraryInstallDirectoryLength)
{
	LOG("RLibrarySession::InstallLibrary");
	string installDir = string(static_cast<const char*>(
			static_cast<const void*>(libraryInstallDirectory)), libraryInstallDirectoryLength);
	installDir = Utilities::NormalizePathString(installDir);
	string libFilePathStr = string(static_cast<const char*>(
			static_cast<const void*>(libraryFile)), libraryFileLength);
	libFilePathStr = Utilities::NormalizePathString(libFilePathStr);
	string extension = GetFileExtension(libFilePathStr);
	string renamedLibFilePathStr = Utilities::NormalizePathString(
		fs::path(installDir).append(m_libraryName + extension).string());
	try
	{
		 Copy the library file to the install directory with the expected name.
		string copyScript = "file.copy(from = '" + libFilePathStr + "', to = '"
			+ renamedLibFilePathStr + "');";
		ExecuteScript(copyScript);
		 Install the package using the new renamedLibFilePathStr.
		string installScript =
			"install.packages(pkgs = '" + renamedLibFilePathStr + "', "
				"lib = '" + installDir + "', repos = NULL, clean = TRUE, quiet = FALSE, "
				"verbose = TRUE, INSTALL_opts=c('--debug'));";
		ExecuteScript(installScript);
		 Confirm the installation.
		bool isInstalled = IsLibraryInstalledAtPath(installDir);
		if (!isInstalled)
		{
			throw runtime_error("install.packages() failed to install the package "
				+ m_libraryName + ".");
		}
	}
	catch (exception &ex)
	{
		 Clean up the copied library file path with the new name.
		if (fs::exists(renamedLibFilePathStr))
		{
			fs::remove_all(renamedLibFilePathStr);
		}
		 Rethrow this exception, since the install has failed.
		 We denote SQL_ERROR, by rethrowing this exception so that it can be caught by the caller
		 and sent back to SQL Server to record the errors.
		throw(ex);
	}
	return SQL_SUCCESS;
}
SQLRETURN RLibrarySession :: InstallLibrary ( const SQLCHAR * libraryFile , SQLINTEGER libraryFileLength , const SQLCHAR * libraryInstallDirectory , SQLINTEGER libraryInstallDirectoryLength ) { LOG ( " " ) ; string installDir = string ( static_cast < const char * > ( static_cast < const void * > ( libraryInstallDirectory ) ) , libraryInstallDirectoryLength ) ; installDir = Utilities :: NormalizePathString ( installDir ) ; string libFilePathStr = string ( static_cast < const char * > ( static_cast < const void * > ( libraryFile ) ) , libraryFileLength ) ; libFilePathStr = Utilities :: NormalizePathString ( libFilePathStr ) ; string extension = GetFileExtension ( libFilePathStr ) ; string renamedLibFilePathStr = Utilities :: NormalizePathString ( fs :: path ( installDir ) . append ( m_libraryName + extension ) . string ( ) ) ; try { string copyScript = " " + libFilePathStr + " " + renamedLibFilePathStr + " " ; ExecuteScript ( copyScript ) ; string installScript = " " + renamedLibFilePathStr + " " " " + installDir + " " " " ; ExecuteScript ( installScript ) ; bool isInstalled = IsLibraryInstalledAtPath ( installDir ) ; if ( ! isInstalled ) { throw runtime_error ( " " + m_libraryName + " " ) ; } } catch ( exception & ex ) { if ( fs :: exists ( renamedLibFilePathStr ) ) { fs :: remove_all ( renamedLibFilePathStr ) ; } throw ( ex ) ; } return SQL_SUCCESS ; }
Name: RLibrarySession::InstallLibrary
Description:
Installs the contents of the libraryFile which is of the form <dbId>_<userId>_libName.zip
to the given installation directory.
Name : RLibrarySession :: InstallLibrary Description : Installs the contents of the libraryFile which is of the form <dbId > _<userId > _libName . zip to the given installation directory .
// The original library file name is of the form <dbId>_<userId>_libName.zip // Generate the new name of the library file in the form of libName.zip or libName.tar.gz // // Copy the library file to the install directory with the expected name. // // Install the package using the new renamedLibFilePathStr. // // Confirm the installation. // // Clean up the copied library file path with the new name. // // Rethrow this exception, since the install has failed. // We denote SQL_ERROR, by rethrowing this exception so that it can be caught by the caller // and sent back to SQL Server to record the errors. //