mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-19 09:35:36 -05:00
Remove SELECT * from edit/initialize Query (#288)
* Major refactor of EditDataMetadata providers * EditMetadataFactory generates "basic" EditTableMetadata objects based entirely on SMO metadata * SmoEditTableMetadata no longer depends on SMO, making it unecessary to mock it * Renamed SmoEditTableMetadata to EditTableMetadata * EditTableMetadata can be extended with DbColumnWrappers * Moving logic for extending a EditColumnMetadata into that class * I *think* this will work for async execution of initialize tasks * Fixing unit tests for new Edit(Table|Column)Metadata classes * Async stuff that works! And passes unit tests * Adding unit tests Adding .idea to gitignore * Adding message to the EditSessionReadyEvent * Fixes from dev merge * Fixing unit tests that Rider didn't catch as failing May have been a bit heavy-handed with the async/await stuff * Couple changes as per PR comments
This commit is contained in:
@@ -4,12 +4,13 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
{
|
||||
@@ -22,11 +23,10 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
/// Generates a edit-ready metadata object using SMO
|
||||
/// </summary>
|
||||
/// <param name="connection">Connection to use for getting metadata</param>
|
||||
/// <param name="columns">List of columns from a query against the object</param>
|
||||
/// <param name="objectName">Name of the object to return metadata for</param>
|
||||
/// <param name="objectType">Type of the object to return metadata for</param>
|
||||
/// <returns>Metadata about the object requested</returns>
|
||||
public IEditTableMetadata GetObjectMetadata(DbConnection connection, DbColumnWrapper[] columns, string objectName, string objectType)
|
||||
public EditTableMetadata GetObjectMetadata(DbConnection connection, string objectName, string objectType)
|
||||
{
|
||||
// Get a connection to the database for SMO purposes
|
||||
SqlConnection sqlConn = connection as SqlConnection;
|
||||
@@ -44,25 +44,59 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
sqlConn = reliableConn.GetUnderlyingConnection();
|
||||
}
|
||||
|
||||
// Connect with SMO and get the metadata for the table
|
||||
Server server = new Server(new ServerConnection(sqlConn));
|
||||
TableViewTableTypeBase result;
|
||||
TableViewTableTypeBase smoResult;
|
||||
switch (objectType.ToLowerInvariant())
|
||||
{
|
||||
case "table":
|
||||
result = server.Databases[sqlConn.Database].Tables[objectName];
|
||||
smoResult = server.Databases[sqlConn.Database].Tables[objectName];
|
||||
break;
|
||||
case "view":
|
||||
result = server.Databases[sqlConn.Database].Views[objectName];
|
||||
smoResult = server.Databases[sqlConn.Database].Views[objectName];
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(objectType), SR.EditDataUnsupportedObjectType(objectType));
|
||||
}
|
||||
if (result == null)
|
||||
if (smoResult == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(objectName), SR.EditDataObjectMetadataNotFound);
|
||||
}
|
||||
|
||||
return new SmoEditTableMetadata(columns, result);
|
||||
// Generate the edit column metadata
|
||||
List<EditColumnMetadata> editColumns = new List<EditColumnMetadata>();
|
||||
for (int i = 0; i < smoResult.Columns.Count; i++)
|
||||
{
|
||||
Column smoColumn = smoResult.Columns[i];
|
||||
|
||||
// The default value may be escaped
|
||||
string defaultValue = smoColumn.DefaultConstraint == null
|
||||
? null
|
||||
: SqlScriptFormatter.UnwrapLiteral(smoColumn.DefaultConstraint.Text);
|
||||
|
||||
EditColumnMetadata column = new EditColumnMetadata
|
||||
{
|
||||
DefaultValue = defaultValue,
|
||||
EscapedName = SqlScriptFormatter.FormatIdentifier(smoColumn.Name),
|
||||
Ordinal = i,
|
||||
};
|
||||
editColumns.Add(column);
|
||||
}
|
||||
|
||||
// Only tables can be memory-optimized
|
||||
Table smoTable = smoResult as Table;
|
||||
bool isMemoryOptimized = smoTable != null && smoTable.IsMemoryOptimized;
|
||||
|
||||
// Escape the parts of the name
|
||||
string[] objectNameParts = {smoResult.Schema, smoResult.Name};
|
||||
string escapedMultipartName = SqlScriptFormatter.FormatMultipartIdentifier(objectNameParts);
|
||||
|
||||
return new EditTableMetadata
|
||||
{
|
||||
Columns = editColumns.ToArray(),
|
||||
EscapedMultipartName = escapedMultipartName,
|
||||
IsMemoryOptimized = isMemoryOptimized,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user