mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-20 17:24:00 -05:00
Add support for using generic SQL queries to filter EditData rows. (#605)
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Utility.SqlScriptFormatters;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
@@ -55,6 +58,40 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Filters out metadata that is not present in the result set, and matches metadata ordering to resultset.
|
||||
/// </summary>
|
||||
public static EditColumnMetadata[] FilterColumnMetadata(EditColumnMetadata[] metaColumns, DbColumnWrapper[] resultColumns)
|
||||
{
|
||||
if (metaColumns.Length == 0)
|
||||
{
|
||||
return metaColumns;
|
||||
}
|
||||
|
||||
bool escapeColName = FromSqlScript.IsIdentifierBracketed(metaColumns[0].EscapedName);
|
||||
Dictionary<string, int> columnNameOrdinalMap = new Dictionary<string, int>(capacity: resultColumns.Length);
|
||||
for (int i = 0; i < resultColumns.Length; i++)
|
||||
{
|
||||
DbColumnWrapper column = resultColumns[i];
|
||||
string columnName = column.ColumnName;
|
||||
if (escapeColName && !FromSqlScript.IsIdentifierBracketed(columnName))
|
||||
{
|
||||
columnName = ToSqlScript.FormatIdentifier(columnName);
|
||||
}
|
||||
columnNameOrdinalMap.Add(columnName, column.ColumnOrdinal ?? i);
|
||||
}
|
||||
|
||||
HashSet<string> resultColumnNames = columnNameOrdinalMap.Keys.ToHashSet();
|
||||
metaColumns = Array.FindAll(metaColumns, column => resultColumnNames.Contains(column.EscapedName));
|
||||
foreach (EditColumnMetadata metaCol in metaColumns)
|
||||
{
|
||||
metaCol.Ordinal = columnNameOrdinalMap[metaCol.EscapedName];
|
||||
}
|
||||
Array.Sort(metaColumns, (x, y) => (Comparer<int>.Default).Compare(x.Ordinal, y.Ordinal));
|
||||
|
||||
return metaColumns;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts extended column properties from the database columns from SQL Client
|
||||
/// </summary>
|
||||
@@ -63,6 +100,8 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData
|
||||
{
|
||||
Validate.IsNotNull(nameof(dbColumnWrappers), dbColumnWrappers);
|
||||
|
||||
Columns = EditTableMetadata.FilterColumnMetadata(Columns, dbColumnWrappers);
|
||||
|
||||
// Iterate over the column wrappers and improve the columns we have
|
||||
for (int i = 0; i < Columns.Length; i++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user