Skip to content

Commit

Permalink
Moved duplicate column name handling to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Feb 24, 2022
1 parent 3abbff2 commit 6178d61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
2 changes: 0 additions & 2 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2636,10 +2636,8 @@ public void SelectDuplicateColumnNames()
Assert.AreEqual(2, select.ColumnSet.Count);
Assert.AreEqual("contact.fullname", select.ColumnSet[0].SourceColumn);
Assert.AreEqual("fullname", select.ColumnSet[0].OutputColumn);
Assert.AreEqual("fullname", select.ColumnSet[0].PhysicalOutputColumn);
Assert.AreEqual("Expr1", select.ColumnSet[1].SourceColumn);
Assert.AreEqual("fullname", select.ColumnSet[1].OutputColumn);
Assert.AreEqual("fullname1", select.ColumnSet[1].PhysicalOutputColumn);
var computeScalar = AssertNode<ComputeScalarNode>(select.Source);
var fetch = AssertNode<FetchXmlScan>(computeScalar.Source);
AssertFetchXml(fetch, @"
Expand Down
24 changes: 0 additions & 24 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/SelectNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,6 @@ public IRootExecutionPlanNodeInternal[] FoldQuery(IDictionary<string, DataSource

ExpandWildcardColumns(dataSources, parameterTypes);

// Ensure column names are unique
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

foreach (var col in ColumnSet)
{
col.PhysicalOutputColumn = col.OutputColumn;

if (!names.Add(col.PhysicalOutputColumn))
{
var suffix = 1;

while (!names.Add(col.OutputColumn + suffix))
suffix++;

col.PhysicalOutputColumn += suffix;
}
}

return new[] { this };
}

Expand Down Expand Up @@ -300,12 +282,6 @@ class SelectColumn
/// </summary>
public string OutputColumn { get; set; }

/// <summary>
/// A unique name for the column in the output data
/// </summary>
[Browsable(false)]
public string PhysicalOutputColumn { get; set; }

/// <summary>
/// Indicates this is a placeholder for all columns from the source data (SELECT *)
/// </summary>
Expand Down
22 changes: 21 additions & 1 deletion MarkMpn.Sql4Cds/SqlQueryControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComplet
if (error is QueryExecutionException queryExecution)
{
if (plan == null)
plan = queryExecution.Node as IRootExecutionPlanNode;
plan = GetRootNode(queryExecution.Node);

messageSuffix = "\r\nSee the Execution Plan tab for details of where this error occurred";
ShowResult(plan, new ExecuteParams { Execute = true, IncludeFetchXml = true, Sql = plan?.Sql }, null, null, queryExecution);
Expand Down Expand Up @@ -747,6 +747,19 @@ private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerComplet
_editor.Focus();
}

private IRootExecutionPlanNode GetRootNode(IExecutionPlanNode node)
{
while (node != null)
{
if (node is IRootExecutionPlanNode root)
return root;

node = node.Parent;
}

return null;
}

private string GetErrorMessage(Exception error)
{
string msg;
Expand Down Expand Up @@ -886,9 +899,16 @@ private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (!reader.IsClosed)
{
var columnNames = new List<string>();
for (var i = 0; i < reader.FieldCount; i++)
columnNames.Add(reader.GetName(i));

var dataTable = new DataTable();
dataTable.Load(reader);

for (var i = 0; i < columnNames.Count; i++)
dataTable.Columns[i].Caption = columnNames[i];

Execute(() => ShowResult(null, args, dataTable, null, null));
}
}
Expand Down

0 comments on commit 6178d61

Please sign in to comment.