Skip to content
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

Bugs/fix 2487 allow contains whitespaces #2498

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
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" });
JKamsker marked this conversation as resolved.
Show resolved Hide resolved
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
18 changes: 10 additions & 8 deletions LiteDB/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;

using static LiteDB.Constants;

namespace LiteDB
{
internal static class StringExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrWhiteSpace(this string str)
{
return str == null || str.Trim().Length == 0;
return string.IsNullOrWhiteSpace(str);
pictos marked this conversation as resolved.
Show resolved Hide resolved
}

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

/// <summary>
Expand All @@ -28,13 +37,6 @@ public static bool IsWord(this string str)
return true;
}

public static string TrimToNull(this string str)
{
var v = str.Trim();

return v.Length == 0 ? null : v;
}

public static string Sha1(this string value)
{
var data = Encoding.UTF8.GetBytes(value);
Expand Down