mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Peek definition for Azure DB objects (#179)
* Working... * Call refresh on SmoObject to switch status from creating to existing. * Add logging to exeception handlers
This commit is contained in:
@@ -728,11 +728,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
bindingContext.MetadataDisplayInfoProvider);
|
bindingContext.MetadataDisplayInfoProvider);
|
||||||
|
|
||||||
// Match token with the suggestions(declaration items) returned
|
// Match token with the suggestions(declaration items) returned
|
||||||
string schemaName = this.GetSchemaName(scriptParseInfo, textDocumentPosition.Position, scriptFile);
|
string schemaName = GetSchemaName(scriptParseInfo, textDocumentPosition.Position, scriptFile);
|
||||||
PeekDefinition peekDefinition = new PeekDefinition(connInfo);
|
PeekDefinition peekDefinition = new PeekDefinition(bindingContext.ServerConnection);
|
||||||
return peekDefinition.GetScript(declarationItems, tokenText, schemaName);
|
return peekDefinition.GetScript(declarationItems, tokenText, schemaName);
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// wait for the queue item
|
// wait for the queue item
|
||||||
|
|||||||
@@ -23,7 +23,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal class PeekDefinition
|
internal class PeekDefinition
|
||||||
{
|
{
|
||||||
private ConnectionInfo connectionInfo;
|
private ServerConnection serverConnection;
|
||||||
|
|
||||||
|
private Database database;
|
||||||
|
|
||||||
private string tempPath;
|
private string tempPath;
|
||||||
|
|
||||||
internal delegate StringCollection ScriptGetter(string objectName, string schemaName);
|
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)
|
// Dictionary that holds the object name (as appears on the TSQL create statement)
|
||||||
private Dictionary<DeclarationType, string> sqlObjectTypes = new Dictionary<DeclarationType, string>();
|
private Dictionary<DeclarationType, string> sqlObjectTypes = new Dictionary<DeclarationType, string>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize a Peek Definition helper object
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serverConnection">SMO Server connection</param>
|
||||||
|
internal PeekDefinition(ServerConnection serverConnection)
|
||||||
|
{
|
||||||
|
this.serverConnection = serverConnection;
|
||||||
|
|
||||||
|
DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(Path.GetTempPath() + "mssql_definition");
|
||||||
|
this.tempPath = tempScriptDirectory.FullName;
|
||||||
|
Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
private Database Database
|
private Database Database
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (this.connectionInfo.SqlConnection != null)
|
if (this.database == null)
|
||||||
|
{
|
||||||
|
if (this.serverConnection != null && !string.IsNullOrEmpty(this.serverConnection.DatabaseName))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Get server object from connection
|
// Get server object from connection
|
||||||
string connectionString = ConnectionService.BuildConnectionString(this.connectionInfo.ConnectionDetails);
|
SqlConnection sqlConn = new SqlConnection(this.serverConnection.ConnectionString);
|
||||||
SqlConnection sqlConn = new SqlConnection(connectionString);
|
|
||||||
sqlConn.Open();
|
sqlConn.Open();
|
||||||
ServerConnection serverConn = new ServerConnection(sqlConn);
|
ServerConnection peekConnection = new ServerConnection(sqlConn);
|
||||||
Server server = new Server(serverConn);
|
Server server = new Server(peekConnection);
|
||||||
return server.Databases[this.connectionInfo.SqlConnection.Database];
|
this.database = new Database(server, peekConnection.DatabaseName);
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Error, "Exception at PeekDefinition Database.get() : " + ex.Message);
|
Logger.Write(LogLevel.Error, "Exception at PeekDefinition Database.get() : " + ex.Message);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal PeekDefinition(ConnectionInfo connInfo)
|
}
|
||||||
{
|
|
||||||
this.connectionInfo = connInfo;
|
return this.database;
|
||||||
DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(Path.GetTempPath() + "mssql_definition");
|
}
|
||||||
this.tempPath = tempScriptDirectory.FullName;
|
|
||||||
Initialize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -208,8 +219,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// <returns>String collection of scripts</returns>
|
/// <returns>String collection of scripts</returns>
|
||||||
internal StringCollection GetTableScripts(string tableName, string schemaName)
|
internal StringCollection GetTableScripts(string tableName, string schemaName)
|
||||||
{
|
{
|
||||||
return (schemaName != null) ? Database?.Tables[tableName, schemaName]?.Script()
|
try
|
||||||
: Database?.Tables[tableName]?.Script();
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -220,8 +244,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// <returns>String collection of scripts</returns>
|
/// <returns>String collection of scripts</returns>
|
||||||
internal StringCollection GetViewScripts(string viewName, string schemaName)
|
internal StringCollection GetViewScripts(string viewName, string schemaName)
|
||||||
{
|
{
|
||||||
return (schemaName != null) ? Database?.Views[viewName, schemaName]?.Script()
|
try
|
||||||
: Database?.Views[viewName]?.Script();
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -230,10 +267,23 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// <param name="storedProcedureName">Stored Procedure name</param>
|
/// <param name="storedProcedureName">Stored Procedure name</param>
|
||||||
/// <param name="schemaName">Schema Name</param>
|
/// <param name="schemaName">Schema Name</param>
|
||||||
/// <returns>String collection of scripts</returns>
|
/// <returns>String collection of scripts</returns>
|
||||||
internal StringCollection GetStoredProcedureScripts(string viewName, string schemaName)
|
internal StringCollection GetStoredProcedureScripts(string sprocName, string schemaName)
|
||||||
{
|
{
|
||||||
return (schemaName != null) ? Database?.StoredProcedures[viewName, schemaName]?.Script()
|
try
|
||||||
: Database?.StoredProcedures[viewName]?.Script();
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user