Skip to content
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
45 changes: 45 additions & 0 deletions src/LinqTests/Acceptance/nullable_types.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Shouldly;
Expand Down Expand Up @@ -70,6 +71,50 @@ public async Task query_against_null_3()
.ShouldBe(1);
}

[Fact]
public async Task query_against_nullable_bool_not_true()
{
var target1 = new Target { NullableBoolean = null };
theSession.Store(target1);

var target2 = new Target { NullableBoolean = true };
theSession.Store(target2);

var target3 = new Target { NullableBoolean = false };
theSession.Store(target3);

await theSession.SaveChangesAsync();

theSession.Logger = new TestOutputMartenLogger(_output);

var list = await theSession.Query<Target>().Where(x => x.NullableBoolean != true).ToListAsync();
list.Count.ShouldBe(2);
list.Any(x => x.Id == target1.Id).ShouldBeTrue();
list.Any(x => x.Id == target3.Id).ShouldBeTrue();
}

[Fact]
public async Task query_against_nullable_bool_not_false()
{
var target1 = new Target { NullableBoolean = null };
theSession.Store(target1);

var target2 = new Target { NullableBoolean = true };
theSession.Store(target2);

var target3 = new Target { NullableBoolean = false };
theSession.Store(target3);

await theSession.SaveChangesAsync();

theSession.Logger = new TestOutputMartenLogger(_output);

var list = await theSession.Query<Target>().Where(x => x.NullableBoolean != false).ToListAsync();
list.Count.ShouldBe(2);
list.Any(x => x.Id == target1.Id).ShouldBeTrue();
list.Any(x => x.Id == target2.Id).ShouldBeTrue();
}

[Fact]
public async Task query_against_null_4()
{
Expand Down
29 changes: 29 additions & 0 deletions src/Marten/Linq/Members/BooleanMember.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
#nullable enable
using System;
using System.Linq.Expressions;
using System.Reflection;
using JasperFx.Core.Reflection;
using Marten.Linq.SqlGeneration.Filters;
using Weasel.Postgresql.SqlGeneration;

namespace Marten.Linq.Members;

internal class BooleanMember: QueryableMember, IComparableMember, IBooleanMember
{
private readonly bool _isNullable;

public BooleanMember(IQueryableMember parent, Casing casing, MemberInfo member, string pgType): base(parent,
casing, member)
{
TypedLocator = $"CAST({RawLocator} as {pgType})";

_isNullable = member.GetRawMemberType().IsNullable();
}

public ISqlFragment BuildIsTrueFragment()
{
return new BooleanFieldIsTrue(this);
}

public override ISqlFragment CreateComparison(string op, ConstantExpression constant)
{
if (constant.Value == null)
{
return op == "=" ? new IsNullFilter(this) : new IsNotNullFilter(this);
}

if (_isNullable && op == "!=")
{
if (constant.Value.Equals(true))
{
return CompoundWhereFragment.Or(new IsNullFilter(this), base.CreateComparison(op, constant));
}
else
{
return CompoundWhereFragment.Or(new IsNullFilter(this), base.CreateComparison(op, constant));
}
}

return base.CreateComparison(op, constant);
}
}
Loading