Skip to content

Commit

Permalink
Fixed bug using parameters in UPDATE SET clause
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Apr 6, 2022
1 parent 6c787b2 commit e90d7d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/ExecutionPlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4254,5 +4254,37 @@ INSERT INTO contact (firstname, lastname)
</entity>
</fetch>");
}

[TestMethod]
public void UpdateParameters()
{
var metadata = new AttributeMetadataCache(_service);
var planBuilder = new ExecutionPlanBuilder(metadata, new StubTableSizeCache(), this);

var query = @"declare @name varchar(100) = 'Data8', @employees int = 10
UPDATE account SET employees = @employees WHERE name = @name";

var plans = planBuilder.Build(query, null, out _);

Assert.AreEqual(4, plans.Length);

AssertNode<DeclareVariablesNode>(plans[0]);
AssertNode<AssignVariablesNode>(plans[1]);
AssertNode<AssignVariablesNode>(plans[2]);
var update = AssertNode<UpdateNode>(plans[3]);
var compute = AssertNode<ComputeScalarNode>(update.Source);
Assert.AreEqual(compute.Columns[update.ColumnMappings["employees"]].ToSql(), "@employees");
var fetch = AssertNode<FetchXmlScan>(compute.Source);

AssertFetchXml(fetch, @"
<fetch xmlns:generator='MarkMpn.SQL4CDS'>
<entity name='account'>
<attribute name='accountid' />
<filter>
<condition attribute='name' operator='eq' value='@name' generator:IsVariable='true' />
</filter>
</entity>
</fetch>");
}
}
}
2 changes: 1 addition & 1 deletion MarkMpn.Sql4Cds.Engine/ExecutionPlanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ private UpdateNode ConvertSetClause(IList<SetClause> setClauses, DataSource data
update.Source = select.Source;
update.PrimaryIdSource = $"{targetAlias}.{targetMetadata.PrimaryIdAttribute}";

var schema = select.Source.GetSchema(DataSources, null);
var schema = select.Source.GetSchema(DataSources, _parameterTypes);

foreach (var assignment in setClauses.Cast<AssignmentSetClause>())
{
Expand Down

0 comments on commit e90d7d3

Please sign in to comment.