Skip to content

Commit

Permalink
Query.Contains allows whitespace value now
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed Jun 6, 2024
1 parent 6b32cfb commit fad6af3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
41 changes: 41 additions & 0 deletions LiteDB.Tests/Issues/Issue2487_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using FluentAssertions;

using Xunit;

namespace LiteDB.Tests.Issues;

public class Issue2487_tests
{
private class DataClass
{
[BsonId]
public int Id { get; set; }

public string Foo { get; set; }

public string Bar { get; set; }
}

[Fact]
public void Test_Contains_EmptyStrings()
{
using var engine = new ConnectionString(":memory:").CreateEngine();

using var db = new LiteDatabase(engine);
var collection = db.GetCollection<DataClass>("data");

collection.Insert(new DataClass { Foo = "bar", Bar = "abc" });
collection.Insert(new DataClass { Foo = " ", Bar = "def" });
collection.Insert(new DataClass { Foo = "fo bar", Bar = "def" });

var containsAction = () => collection.FindOne(x => x.Foo.Contains(" "));
containsAction.Should().NotThrow();

var def = containsAction();
def.Should().NotBeNull();
def.Bar.Should().Be("def");

var shouldExecute = () => engine.Query("data", Query.All(Query.Contains("Foo", " ")));
shouldExecute.Should().NotThrow();
}
}
5 changes: 3 additions & 2 deletions LiteDB/Client/Structures/Query.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using LiteDB.Engine;

using System;
using System.Collections.Generic;
using System.Linq;

using static LiteDB.Constants;

namespace LiteDB
Expand Down Expand Up @@ -83,7 +85,6 @@ public static BsonExpression GT(string field, BsonValue value)
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));

return BsonExpression.Create($"{field} > {value ?? BsonValue.Null}");

}

/// <summary>
Expand Down Expand Up @@ -123,7 +124,7 @@ public static BsonExpression StartsWith(string field, string value)
public static BsonExpression Contains(string field, string value)
{
if (field.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(field));
if (value.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(value));
if (value.IsNullOrEmpty()) throw new ArgumentNullException(nameof(value));

return BsonExpression.Create($"{field} LIKE {(new BsonValue("%" + value + "%"))}");
}
Expand Down
1 change: 1 addition & 0 deletions LiteDB/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static bool IsNullOrWhiteSpace(this string str)
return string.IsNullOrWhiteSpace(str);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
Expand Down

0 comments on commit fad6af3

Please sign in to comment.