Commit 70884bab authored by Marc Gravell's avatar Marc Gravell

When doing list expansions (6=>10 etc), use the last non-trivial value - do...

When doing list expansions (6=>10 etc), use the last non-trivial value - do *NOT* use null, as it causes problems with "not in" clauses; add test for the same
parent d2ee19c4
......@@ -236,16 +236,19 @@ private void TestListExpansionPadding(bool enabled)
private void TestListForExpansion(List<int> list, bool enabled)
{
var row = connection.QuerySingle(@"
declare @hits int;
declare @hits int, @misses int, @count int;
select @count = count(1) from #ListExpansion;
select @hits = count(1) from #ListExpansion where id in @ids ;
select @misses = count(1) from #ListExpansion where not id in @ids ;
declare @query nvarchar(max) = N' in @ids '; -- ok, I confess to being pleased with this hack ;p
select @hits as [Hits], @query as [Query];
select @hits as [Hits], (@count - @misses) as [Misses], @query as [Query];
", new { ids = list });
int hits = row.Hits;
int hits = row.Hits, misses = row.Misses;
string query = row.Query;
int argCount = Regex.Matches(query, "@ids[0-9]").Count;
int expectedCount = GetExpectedListExpansionCount(list.Count, enabled);
hits.IsEqualTo(list.Count);
misses.IsEqualTo(list.Count);
argCount.IsEqualTo(expectedCount);
}
......
......@@ -1862,6 +1862,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
DbType dbType = 0;
if (list != null)
{
object lastValue = null;
foreach (var item in list)
{
if (++count == 1) // first item: fetch some type info
......@@ -1876,24 +1877,29 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
dbType = LookupDbType(item.GetType(), "", true, out handler);
}
}
var listParam = command.CreateParameter();
listParam.ParameterName = namePrefix + count.ToString();
if (isString)
{
listParam.Size = DbString.DefaultLength;
if (item != null && ((string) item).Length > DbString.DefaultLength)
{
listParam.Size = -1;
}
}
var nextName = namePrefix + count.ToString();
if (isDbString && item as DbString != null)
{
var str = item as DbString;
str.AddParameter(command, listParam.ParameterName);
str.AddParameter(command, nextName);
}
else
{
listParam.Value = SanitizeParameterValue(item);
var listParam = command.CreateParameter();
listParam.ParameterName = nextName;
if (isString)
{
listParam.Size = DbString.DefaultLength;
if (item != null && ((string)item).Length > DbString.DefaultLength)
{
listParam.Size = -1;
}
}
var tmp = listParam.Value = SanitizeParameterValue(item);
if (tmp != null && !(tmp is DBNull))
lastValue = tmp; // only interested in non-trivial values for padding
if (listParam.DbType != dbType)
{
listParam.DbType = dbType;
......@@ -1901,17 +1907,17 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
command.Parameters.Add(listParam);
}
}
if (Settings.PadListExpansions && !isDbString)
if (Settings.PadListExpansions && !isDbString && lastValue != null)
{
int padCount = GetListPaddingExtraCount(count);
for(int i = 0; i < padCount; i++)
for (int i = 0; i < padCount; i++)
{
count++;
var padParam = command.CreateParameter();
padParam.ParameterName = namePrefix + count.ToString();
if(isString) padParam.Size = DbString.DefaultLength;
padParam.DbType = dbType;
padParam.Value = DBNull.Value;
padParam.Value = lastValue;
command.Parameters.Add(padParam);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment