mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
ML - Verifying ODBC Driver (#11587)
* Verifying ODBC is installed before opening package manager dialog
This commit is contained in:
@@ -120,6 +120,8 @@ export class PackageManager {
|
||||
await utils.executeTasks(this._apiWrapper, constants.installPackageMngDependenciesMsgTaskName, [
|
||||
this.installRequiredPythonPackages(this._config.requiredSqlPythonPackages),
|
||||
this.installRequiredRPackages()], true);
|
||||
|
||||
await this.verifyOdbcInstalled();
|
||||
}
|
||||
|
||||
private async installRequiredRPackages(): Promise<void> {
|
||||
@@ -186,6 +188,45 @@ export class PackageManager {
|
||||
}
|
||||
}
|
||||
|
||||
private async verifyOdbcInstalled(): Promise<void> {
|
||||
let connection = await this.getCurrentConnection();
|
||||
if (connection) {
|
||||
let credentials = await this._apiWrapper.getCredentials(connection.connectionId);
|
||||
const separator = '=';
|
||||
let connectionParts: string[] = [];
|
||||
if (connection) {
|
||||
connectionParts.push(utils.getKeyValueString('DRIVER', `{${constants.supportedODBCDriver}}`, separator));
|
||||
|
||||
if (connection.userName) {
|
||||
connectionParts.push(utils.getKeyValueString('UID', connection.userName, separator));
|
||||
connectionParts.push(utils.getKeyValueString('PWD', credentials[azdata.ConnectionOptionSpecialType.password], separator));
|
||||
} else {
|
||||
connectionParts.push(utils.getKeyValueString('Trusted_Connection', 'yes', separator));
|
||||
|
||||
}
|
||||
|
||||
connectionParts.push(utils.getKeyValueString('SERVER', connection.serverName, separator));
|
||||
}
|
||||
|
||||
let scripts: string[] = [
|
||||
'import pyodbc',
|
||||
`connection = pyodbc.connect('${connectionParts.join(';')}')`,
|
||||
'cursor = connection.cursor()',
|
||||
'cursor.execute("SELECT @@version;")'
|
||||
];
|
||||
let pythonExecutable = await this._config.getPythonExecutable(true);
|
||||
try {
|
||||
await this._processService.execScripts(pythonExecutable, scripts, [], this._outputChannel);
|
||||
} catch (err) {
|
||||
const result = await this._apiWrapper.showErrorMessage(constants.verifyOdbcDriverError, constants.learnMoreTitle);
|
||||
if (result === constants.learnMoreTitle) {
|
||||
await this._apiWrapper.openExternal(vscode.Uri.parse(constants.odbcDriverDocuments));
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async getInstalledPipPackages(): Promise<nbExtensionApis.IPackageDetails[]> {
|
||||
try {
|
||||
let pythonExecutable = await this.getPythonExecutable();
|
||||
|
||||
@@ -13,6 +13,7 @@ import { SqlPackageManageProviderBase, ScriptMode } from './packageManageProvide
|
||||
import { HttpClient } from '../common/httpClient';
|
||||
import * as utils from '../common/utils';
|
||||
import { PackageManagementService } from './packageManagementService';
|
||||
import * as constants from '../common/constants';
|
||||
|
||||
/**
|
||||
* Manage Package Provider for python packages inside SQL server databases
|
||||
@@ -62,26 +63,31 @@ export class SqlPythonPackageManageProvider extends SqlPackageManageProviderBase
|
||||
protected async executeScripts(scriptMode: ScriptMode, packageDetails: nbExtensionApis.IPackageDetails, databaseName: string): Promise<void> {
|
||||
let connection = await this.getCurrentConnection();
|
||||
let credentials = await this._apiWrapper.getCredentials(connection.connectionId);
|
||||
let connectionParts: string[] = [];
|
||||
|
||||
if (connection) {
|
||||
let port = '1433';
|
||||
let server = connection.serverName;
|
||||
let database = databaseName ? `, database="${databaseName}"` : '';
|
||||
const auth = connection.userName ? `, uid="${connection.userName}", pwd="${credentials[azdata.ConnectionOptionSpecialType.password]}"` : '';
|
||||
let index = connection.serverName.indexOf(',');
|
||||
if (index > 0) {
|
||||
port = connection.serverName.substring(index + 1);
|
||||
server = connection.serverName.substring(0, index);
|
||||
connectionParts.push(utils.getKeyValueString('driver', `"${constants.supportedODBCDriver}"`));
|
||||
|
||||
let port = utils.getServerPort(connection);
|
||||
let server = utils.getServerName(connection);
|
||||
if (databaseName) {
|
||||
connectionParts.push(utils.getKeyValueString('database', `"${databaseName}"`));
|
||||
}
|
||||
if (connection.userName) {
|
||||
connectionParts.push(utils.getKeyValueString('uid', `"${connection.userName}"`));
|
||||
connectionParts.push(utils.getKeyValueString('pwd', `"${credentials[azdata.ConnectionOptionSpecialType.password]}"`));
|
||||
}
|
||||
|
||||
let pythonConnectionParts = `server="${server}", port=${port}${auth}${database})`;
|
||||
connectionParts.push(utils.getKeyValueString('server', `"${server}"`));
|
||||
connectionParts.push(utils.getKeyValueString('port', port));
|
||||
|
||||
let pythonCommandScript = scriptMode === ScriptMode.Install ?
|
||||
`pkgmanager.install(package="${packageDetails.name}", version="${packageDetails.version}")` :
|
||||
`pkgmanager.uninstall(package_name="${packageDetails.name}")`;
|
||||
|
||||
let scripts: string[] = [
|
||||
'import sqlmlutils',
|
||||
`connection = sqlmlutils.ConnectionInfo(driver="ODBC Driver 17 for SQL Server", ${pythonConnectionParts}`,
|
||||
`connection = sqlmlutils.ConnectionInfo(${connectionParts.join(',')})`,
|
||||
'pkgmanager = sqlmlutils.SQLPackageManager(connection)',
|
||||
pythonCommandScript
|
||||
];
|
||||
|
||||
@@ -14,7 +14,7 @@ import { SqlPackageManageProviderBase, ScriptMode } from './packageManageProvide
|
||||
import { HttpClient } from '../common/httpClient';
|
||||
import * as constants from '../common/constants';
|
||||
import { PackageManagementService } from './packageManagementService';
|
||||
|
||||
import * as utils from '../common/utils';
|
||||
|
||||
|
||||
/**
|
||||
@@ -66,18 +66,26 @@ export class SqlRPackageManageProvider extends SqlPackageManageProviderBase impl
|
||||
protected async executeScripts(scriptMode: ScriptMode, packageDetails: nbExtensionApis.IPackageDetails, databaseName: string): Promise<void> {
|
||||
let connection = await this.getCurrentConnection();
|
||||
let credentials = await this._apiWrapper.getCredentials(connection.connectionId);
|
||||
let connectionParts: string[] = [];
|
||||
|
||||
if (connection) {
|
||||
connectionParts.push(utils.getKeyValueString('driver', constants.supportedODBCDriver));
|
||||
let server = connection.serverName.replace('\\', '\\\\');
|
||||
let database = databaseName ? `, database="${databaseName}"` : '';
|
||||
const auth = connection.userName ? `, uid="${connection.userName}", pwd="${credentials[azdata.ConnectionOptionSpecialType.password]}"` : '';
|
||||
let connectionParts = `server="${server}"${auth}${database}`;
|
||||
if (databaseName) {
|
||||
connectionParts.push(utils.getKeyValueString('database', `"${databaseName}"`));
|
||||
}
|
||||
if (connection.userName) {
|
||||
connectionParts.push(utils.getKeyValueString('uid', `"${connection.userName}"`));
|
||||
connectionParts.push(utils.getKeyValueString('pwd', `"${credentials[azdata.ConnectionOptionSpecialType.password]}"`));
|
||||
}
|
||||
connectionParts.push(utils.getKeyValueString('server', `"${server}"`));
|
||||
|
||||
let rCommandScript = scriptMode === ScriptMode.Install ? 'sql_install.packages' : 'sql_remove.packages';
|
||||
|
||||
let scripts: string[] = [
|
||||
'formals(quit)$save <- formals(q)$save <- "no"',
|
||||
'library(sqlmlutils)',
|
||||
`connection <- connectionInfo(driver= "ODBC Driver 17 for SQL Server", ${connectionParts})`,
|
||||
`connection <- connectionInfo(${connectionParts.join(', ')})`,
|
||||
`r = getOption("repos")`,
|
||||
`r["CRAN"] = "${this._config.rPackagesRepository}"`,
|
||||
`options(repos = r)`,
|
||||
|
||||
Reference in New Issue
Block a user