sapiens/SqlFu

Improper exception on adding a where clause with a call to IList.Contains method with an empty list instance

Closed this issue · 2 comments

When you supply an empty collection, say you have:

var ids = new List();
connection.Query().Where(entity => ids.Contains(entity.Id));

the result is an exception, supplying no valuable information about the error.

PrepareStatement.SetupParameters method assumes there are values in the list, so it tries to remove the trailing comma, which in the case of empty list is non-existant, and raises an exception:

System.ArgumentOutOfRangeException: StartIndex cannot be less than zero. Parameter name: startIndex at System.Text.StringBuilder.Remove(Int32 startIndex, Int32 length) at SqlFu.PrepareStatement.SetupParameters(DbCommand cmd) in e:\Projects.Net\SqlFu\src\SqlFu\PrepareStatement.cs:line 79 at ...

Perhaps the behavior should be either a more informational exception or maybe do what some other ORMs like for instance Entity Framework does, i.e the code emitted when executing the same query using EF:

SELECT
CAST(NULL AS varchar(1)) AS [C1],
CAST(NULL AS bigint) AS [C2],
.......
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0

Resulting in an empty result set, not an error, or another possible solution maybe not sending SELECT to the SQL server, but just giving back an empty result list.

I 'm quite busy at the moment, but I can accept a PR into the devel branch

fixed in ver 2.3.11