mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-13 17:23:02 -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);
|
||||
|
||||
// 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
|
||||
|
||||
@@ -23,7 +23,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
/// </summary>
|
||||
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<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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -208,8 +219,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
/// <returns>String collection of scripts</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -220,8 +244,21 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
/// <returns>String collection of scripts</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -230,10 +267,23 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||
/// <param name="storedProcedureName">Stored Procedure name</param>
|
||||
/// <param name="schemaName">Schema Name</param>
|
||||
/// <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()
|
||||
: 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user