diff --git a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs index c5f3ae3b..d87eab9a 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/LanguageService.cs @@ -728,11 +728,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices bindingContext.MetadataDisplayInfoProvider); // Match token with the suggestions(declaration items) returned - string schemaName = this.GetSchemaName(scriptParseInfo, textDocumentPosition.Position, scriptFile); - PeekDefinition peekDefinition = new PeekDefinition(connInfo); - return peekDefinition.GetScript(declarationItems, tokenText, schemaName); - - + string schemaName = GetSchemaName(scriptParseInfo, textDocumentPosition.Position, scriptFile); + PeekDefinition peekDefinition = new PeekDefinition(bindingContext.ServerConnection); + return peekDefinition.GetScript(declarationItems, tokenText, schemaName); }); // wait for the queue item diff --git a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/PeekDefinition.cs b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/PeekDefinition.cs index 07293ba6..bc0eca35 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/PeekDefinition.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/LanguageServices/PeekDefinition.cs @@ -23,7 +23,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices /// internal class PeekDefinition { - private ConnectionInfo connectionInfo; + private ServerConnection serverConnection; + + private Database database; + private string tempPath; internal delegate StringCollection ScriptGetter(string objectName, string schemaName); @@ -35,38 +38,46 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices // Dictionary that holds the object name (as appears on the TSQL create statement) private Dictionary sqlObjectTypes = new Dictionary(); + /// + /// Initialize a Peek Definition helper object + /// + /// SMO Server connection + internal PeekDefinition(ServerConnection serverConnection) + { + this.serverConnection = serverConnection; + + DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(Path.GetTempPath() + "mssql_definition"); + this.tempPath = tempScriptDirectory.FullName; + Initialize(); + } + private Database Database { get { - if (this.connectionInfo.SqlConnection != null) + if (this.database == null) { - try + if (this.serverConnection != null && !string.IsNullOrEmpty(this.serverConnection.DatabaseName)) { - // Get server object from connection - string connectionString = ConnectionService.BuildConnectionString(this.connectionInfo.ConnectionDetails); - SqlConnection sqlConn = new SqlConnection(connectionString); - sqlConn.Open(); - ServerConnection serverConn = new ServerConnection(sqlConn); - Server server = new Server(serverConn); - return server.Databases[this.connectionInfo.SqlConnection.Database]; + try + { + // Get server object from connection + SqlConnection sqlConn = new SqlConnection(this.serverConnection.ConnectionString); + sqlConn.Open(); + ServerConnection peekConnection = new ServerConnection(sqlConn); + Server server = new Server(peekConnection); + this.database = new Database(server, peekConnection.DatabaseName); + } + catch (Exception ex) + { + Logger.Write(LogLevel.Error, "Exception at PeekDefinition Database.get() : " + ex.Message); + } } - catch(Exception ex) - { - Logger.Write(LogLevel.Error, "Exception at PeekDefinition Database.get() : " + ex.Message); - return null; - } - } - return null; - } - } - internal PeekDefinition(ConnectionInfo connInfo) - { - this.connectionInfo = connInfo; - DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(Path.GetTempPath() + "mssql_definition"); - this.tempPath = tempScriptDirectory.FullName; - Initialize(); + } + + return this.database; + } } /// @@ -208,8 +219,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices /// String collection of scripts internal StringCollection GetTableScripts(string tableName, string schemaName) { - return (schemaName != null) ? Database?.Tables[tableName, schemaName]?.Script() - : Database?.Tables[tableName]?.Script(); + try + { + Table table = string.IsNullOrEmpty(schemaName) + ? new Table(this.Database, tableName) + : new Table(this.Database, tableName, schemaName); + + table.Refresh(); + + return table.Script(); + } + catch (Exception ex) + { + Logger.Write(LogLevel.Error, "Exception at PeekDefinition GetTableScripts : " + ex.Message); + return null; + } } /// @@ -220,8 +244,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices /// String collection of scripts internal StringCollection GetViewScripts(string viewName, string schemaName) { - return (schemaName != null) ? Database?.Views[viewName, schemaName]?.Script() - : Database?.Views[viewName]?.Script(); + try + { + View view = string.IsNullOrEmpty(schemaName) + ? new View(this.Database, viewName) + : new View(this.Database, viewName, schemaName); + + view.Refresh(); + + return view.Script(); + } + catch (Exception ex) + { + Logger.Write(LogLevel.Error, "Exception at PeekDefinition GetViewScripts : " + ex.Message); + return null; + } } /// @@ -230,10 +267,23 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices /// Stored Procedure name /// Schema Name /// String collection of scripts - internal StringCollection GetStoredProcedureScripts(string viewName, string schemaName) + internal StringCollection GetStoredProcedureScripts(string sprocName, string schemaName) { - return (schemaName != null) ? Database?.StoredProcedures[viewName, schemaName]?.Script() - : Database?.StoredProcedures[viewName]?.Script(); + try + { + StoredProcedure sproc = string.IsNullOrEmpty(schemaName) + ? new StoredProcedure(this.Database, sprocName) + : new StoredProcedure(this.Database, sprocName, schemaName); + + sproc.Refresh(); + + return sproc.Script(); + } + catch (Exception ex) + { + Logger.Write(LogLevel.Error, "Exception at PeekDefinition GetStoredProcedureScripts : " + ex.Message); + return null; + } } ///