From c83aabf1bc247f29c6b0f5c24fc31f0aadce75aa Mon Sep 17 00:00:00 2001 From: NetDwarf Date: Wed, 20 Sep 2023 16:55:14 +0200 Subject: [PATCH] Fix WhereClause.IsIn with empty List --- DOLDatabase/WhereClause.cs | 9 +++++---- Tests/UnitTests/UT_WhereClause.cs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/DOLDatabase/WhereClause.cs b/DOLDatabase/WhereClause.cs index f0b422b944..6fee427070 100644 --- a/DOLDatabase/WhereClause.cs +++ b/DOLDatabase/WhereClause.cs @@ -81,7 +81,7 @@ public virtual QueryParameter[] Parameters } } - internal abstract List IntermediateRepresentation { get; } + internal abstract IEnumerable IntermediateRepresentation { get; } public virtual WhereClause And(WhereClause rightExpression) => rightExpression.Equals(Empty) ? this : new ChainingExpression(this, "AND", rightExpression); @@ -134,13 +134,14 @@ internal FilterExpression(string columnName, string op, object val) this.val = val; } - internal override List IntermediateRepresentation + internal override IEnumerable IntermediateRepresentation { get { - if (val is IEnumerable valueCollection && valueCollection.Any()) + if (val is IEnumerable valueCollection) { var result = new List() { 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)) { @@ -208,7 +209,7 @@ internal ChainingExpression(WhereClause left, string chainingOperator, WhereClau this.chainingOperator = chainingOperator; } - internal override List IntermediateRepresentation + internal override IEnumerable IntermediateRepresentation { get { diff --git a/Tests/UnitTests/UT_WhereClause.cs b/Tests/UnitTests/UT_WhereClause.cs index f5f3151d5b..44aebc8aad 100644 --- a/Tests/UnitTests/UT_WhereClause.cs +++ b/Tests/UnitTests/UT_WhereClause.cs @@ -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; @@ -70,7 +70,7 @@ 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; @@ -78,6 +78,15 @@ public void ParameterizedText_FooIsInAandB_FooIsInAtACommaAtB() Assert.AreEqual(expected, actual); } + [Test] + public void ParameterizedText_FooIsInEmptyArray_FooInEmptyParenthesis() + { + var expr = DB.Column("foo").IsIn(Array.Empty()); + var actual = expr.ParameterizedText; + var expected = $"WHERE foo IN ( )"; + Assert.AreEqual(expected, actual); + } + [Test] public void Parameters_Expression1AndExpression2_ConcatenationOfBothParameters() {