Skip to content

Commit

Permalink
Fixed null type conversion
Browse files Browse the repository at this point in the history
Fixes #314
  • Loading branch information
MarkMpn committed Jun 28, 2023
1 parent da6c9f3 commit 0c27af7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 21 additions & 0 deletions MarkMpn.Sql4Cds.Engine.Tests/AdoProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,5 +1224,26 @@ SELECT 2 + 2
Assert.AreEqual("<row>4</row>", actual);
}
}

[TestMethod]
public void UpdateCase()
{
// https://github.com/MarkMpn/Sql4Cds/issues/314
using (var con = new Sql4CdsConnection(_localDataSource))
using (var cmd = con.CreateCommand())
{
cmd.CommandTimeout = 0;

cmd.CommandText = "INSERT INTO account (employees) VALUES (1)";
cmd.ExecuteNonQuery();

cmd.CommandText = "UPDATE a SET a.employees = (CASE WHEN a.employees IS NULL THEN NULL ELSE a.employees + 1 END) FROM account AS a WHERE a.accountid IS NOT NULL";
cmd.ExecuteNonQuery();

cmd.CommandText = "SELECT employees FROM account";
var actual = cmd.ExecuteScalar();
Assert.AreEqual(2, actual);
}
}
}
}
10 changes: 5 additions & 5 deletions MarkMpn.Sql4Cds.Engine/ExecutionPlan/SqlTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,19 @@ private static void AddNullableTypeConversion<TSql, TNet>(Func<DataSource, TNet,
/// <returns><c>true</c> if the two values can be converted to a consistent type, or <c>false</c> otherwise</returns>
public static bool CanMakeConsistentTypes(DataTypeReference lhs, DataTypeReference rhs, DataSource primaryDataSource, out DataTypeReference consistent)
{
if (lhs.IsSameAs(rhs))
if (lhs == DataTypeHelpers.ImplicitIntForNullLiteral)
{
consistent = lhs;
consistent = rhs;
return true;
}

if (lhs == DataTypeHelpers.ImplicitIntForNullLiteral)
if (rhs == DataTypeHelpers.ImplicitIntForNullLiteral)
{
consistent = rhs;
consistent = lhs;
return true;
}

if (rhs == DataTypeHelpers.ImplicitIntForNullLiteral)
if (lhs.IsSameAs(rhs))
{
consistent = lhs;
return true;
Expand Down

0 comments on commit 0c27af7

Please sign in to comment.