sebastienros/yessql

Exception when having boolean constants in the index query.

Closed this issue · 2 comments

alafi commented

The following exception happens when having a boolean constant in the index query. It was tested with release (3.3.0) connecting to SQL Server (this issue might be related to #476 )

Message: 
Microsoft.Data.SqlClient.SqlException : An expression of non-boolean type specified in a context where a condition is expected, near 'or'.
Stack Trace: 

I have created the following test to reproduce the issue:

[Fact]
public async Task ShouldHandleBooleanConstants()
{
    _store.RegisterIndexes<PersonWithAIndexProvider>();
    using (var session = _store.CreateSession())
    {
        session.Save(new Person { Firstname = "Alex" });
        session.Save(new Person { Firstname = "Bill" });
        session.Save(new Person { Firstname = "assan" });
        await session.SaveChangesAsync();
    }
    using (var session = _store.CreateSession())
    {
        string firstName = null;
        Assert.Null(await session.QueryIndex<PersonByBothNamesCol>(x => 
                firstName == null || 
                firstName == x.Firstname
            ).FirstOrDefaultAsync());
    }
}

I believe it is specific to SQL Server due to the fact that Boolean parameters needs an equal (=) operator to be interpreted correctly.

The produced query is

'SELECT TOP (1) * FROM [BobaFett].[tpPersonByBothNamesCol] AS [PersonByBothNamesCol_a1] WHERE (@p0 or ([PersonByBothNamesCol_a1].[Firstname] IS NULL))

This can be fixed by adding =1 for params in where clause if and only if the parameter without operator

@sebastienros assign this to me because I don't have access, I'm planning to push a PR. It's better than what I suggested earlier