Skip to content

Commit

Permalink
Fix WhereClause.IsIn with empty List
Browse files Browse the repository at this point in the history
  • Loading branch information
NetDwarf committed Sep 30, 2023
1 parent 37a6288 commit c83aabf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 5 additions & 4 deletions DOLDatabase/WhereClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public virtual QueryParameter[] Parameters
}
}

internal abstract List<TextAtom> IntermediateRepresentation { get; }
internal abstract IEnumerable<TextAtom> IntermediateRepresentation { get; }

public virtual WhereClause And(WhereClause rightExpression)
=> rightExpression.Equals(Empty) ? this : new ChainingExpression(this, "AND", rightExpression);
Expand Down Expand Up @@ -134,13 +134,14 @@ internal FilterExpression(string columnName, string op, object val)
this.val = val;
}

internal override List<TextAtom> IntermediateRepresentation
internal override IEnumerable<TextAtom> IntermediateRepresentation
{
get
{
if (val is IEnumerable<object> valueCollection && valueCollection.Any())
if (val is IEnumerable<object> valueCollection)
{
var result = new List<TextAtom>() { new TextAtom(columnName), new TextAtom(op), new TextAtom("(") };
if(!valueCollection.Any()) return result.Append(new TextAtom(")"));
result.Add(new ValueAtom(valueCollection.ElementAt(0)));
foreach(var element in valueCollection.Skip(1))
{
Expand Down Expand Up @@ -208,7 +209,7 @@ internal ChainingExpression(WhereClause left, string chainingOperator, WhereClau
this.chainingOperator = chainingOperator;
}

internal override List<TextAtom> IntermediateRepresentation
internal override IEnumerable<TextAtom> IntermediateRepresentation
{
get
{
Expand Down
13 changes: 11 additions & 2 deletions Tests/UnitTests/UT_WhereClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void ParameterizedText_FooIsEqualToOneAndBarIsEqualToOne_WhereFooEqualAtA
[Test]
public void ParameterizedText_FooIsInOneAndTwo_FooIsInAtACommaAtB()
{
var expr = DB.Column("foo").IsIn(new [] { 1, 2 });
var expr = DB.Column("foo").IsIn(new[] { 1, 2 });
var placeHolder1 = expr.Parameters[0].Item1;
var placeHolder2 = expr.Parameters[1].Item1;
var actual = expr.ParameterizedText;
Expand All @@ -70,14 +70,23 @@ public void ParameterizedText_FooIsInOneAndTwo_FooIsInAtACommaAtB()
[Test]
public void ParameterizedText_FooIsInAandB_FooIsInAtACommaAtB()
{
var expr = DB.Column("foo").IsIn(new [] { "a", "b" });
var expr = DB.Column("foo").IsIn(new[] { "a", "b" });
var placeHolder1 = expr.Parameters[0].Item1;
var placeHolder2 = expr.Parameters[1].Item1;
var actual = expr.ParameterizedText;
var expected = $"WHERE foo IN ( {placeHolder1} , {placeHolder2} )";
Assert.AreEqual(expected, actual);
}

[Test]
public void ParameterizedText_FooIsInEmptyArray_FooInEmptyParenthesis()
{
var expr = DB.Column("foo").IsIn(Array.Empty<object>());
var actual = expr.ParameterizedText;
var expected = $"WHERE foo IN ( )";
Assert.AreEqual(expected, actual);
}

[Test]
public void Parameters_Expression1AndExpression2_ConcatenationOfBothParameters()
{
Expand Down

0 comments on commit c83aabf

Please sign in to comment.