Skip to content

Fix decimal equality comparison for Sqlite #2807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/NHibernate.Test/Async/Linq/OperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public async Task UnaryMinusAsync()
Assert.AreEqual(1, await (session.Query<TimesheetEntry>().CountAsync(a => -a.NumberOfHours == -7)));
}

[Test]
public async Task DecimalAddAsync()
{
decimal offset = 5.5m;
decimal test = 10248 + offset;
var result = await (session.Query<Order>().Where(e => offset + e.OrderId == test).ToListAsync());
Assert.That(result, Has.Count.EqualTo(1));

offset = 5.5m;
test = 32.38m + offset;
result = await (session.Query<Order>().Where(e => offset + e.Freight == test).ToListAsync());
Assert.That(result, Has.Count.EqualTo(1));
}

[Test]
public async Task UnaryPlusAsync()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Async/Linq/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public async Task CompareFloatingPointParametersAndColumnsAsync()
totalParameters,
sql =>
{
Assert.That(sql, Does.Not.Contain("cast"));
Assert.That(sql, pair.Value == "Decimal" && Dialect.IsDecimalStoredAsFloatingPointNumber ? Does.Contain("cast") : Does.Not.Contain("cast"));
Assert.That(GetTotalOccurrences(sql, $"Type: {pair.Value}"), Is.EqualTo(totalParameters));
}));
}
Expand Down
14 changes: 14 additions & 0 deletions src/NHibernate.Test/Linq/OperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public void UnaryMinus()
Assert.AreEqual(1, session.Query<TimesheetEntry>().Count(a => -a.NumberOfHours == -7));
}

[Test]
public void DecimalAdd()
{
decimal offset = 5.5m;
decimal test = 10248 + offset;
var result = session.Query<Order>().Where(e => offset + e.OrderId == test).ToList();
Assert.That(result, Has.Count.EqualTo(1));

offset = 5.5m;
test = 32.38m + offset;
result = session.Query<Order>().Where(e => offset + e.Freight == test).ToList();
Assert.That(result, Has.Count.EqualTo(1));
}

[Test]
public void UnaryPlus()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Linq/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void CompareFloatingPointParametersAndColumns()
totalParameters,
sql =>
{
Assert.That(sql, Does.Not.Contain("cast"));
Assert.That(sql, pair.Value == "Decimal" && Dialect.IsDecimalStoredAsFloatingPointNumber ? Does.Contain("cast") : Does.Not.Contain("cast"));
Assert.That(GetTotalOccurrences(sql, $"Type: {pair.Value}"), Is.EqualTo(totalParameters));
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Engine.Query;
using NHibernate.SqlTypes;
using NHibernate.Util;
using NSubstitute;

Expand Down Expand Up @@ -525,6 +526,8 @@ protected void ClearQueryPlanCache()
var forPartsOfMethod = ReflectHelper.GetMethodDefinition(() => Substitute.ForPartsOf<object>());
var substitute = (Dialect.Dialect) forPartsOfMethod.MakeGenericMethod(origDialect.GetType())
.Invoke(null, new object[] { new object[0] });
substitute.GetCastTypeName(Arg.Any<SqlType>())
.ReturnsForAnyArgs(x => origDialect.GetCastTypeName(x.ArgAt<SqlType>(0)));

dialectProperty.SetValue(Sfi.Settings, substitute);

Expand Down
5 changes: 5 additions & 0 deletions src/NHibernate/Dialect/Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,11 @@ public virtual bool SupportsSqlBatches
get { return false; }
}

/// <summary>
/// Whether <see cref="decimal"/> is stored as a floating point number.
/// </summary>
public virtual bool IsDecimalStoredAsFloatingPointNumber => false;

public virtual bool IsKnownToken(string currentToken, string nextToken)
{
return false;
Expand Down
3 changes: 3 additions & 0 deletions src/NHibernate/Dialect/SQLiteDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ public override bool GenerateTablePrimaryKeyConstraintForIdentityColumn
get { return false; }
}

/// <inheritdoc />
public override bool IsDecimalStoredAsFloatingPointNumber => true;

public override string Qualify(string catalog, string schema, string table)
{
StringBuilder qualifiedName = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,12 @@ protected HqlTreeNode VisitConstantExpression(ConstantExpression expression)
if (_parameters.ConstantToParameterMap.TryGetValue(expression, out namedParameter))
{
_parameters.RequiredHqlParameters.Add(new NamedParameterDescriptor(namedParameter.Name, null, false));
var parameter = _hqlTreeBuilder.Parameter(namedParameter.Name).AsExpression();

return _hqlTreeBuilder.Parameter(namedParameter.Name).AsExpression();
// SQLite driver binds decimal parameters to text, which can cause unexpected results in arithmetic operations.
return _parameters.SessionFactory.Dialect.IsDecimalStoredAsFloatingPointNumber && expression.Type.UnwrapIfNullable() == typeof(decimal)
? _hqlTreeBuilder.TransparentCast(parameter, expression.Type)
: parameter;
}

return _hqlTreeBuilder.Constant(expression.Value);
Expand Down