提交 4a364589 编写于 作者: S Sam Saffron

added support for enumeration, so you can work through big lists if needed

上级 e0017edc
......@@ -45,7 +45,6 @@ static SqlMapper()
typeMap[typeof(DateTime)] = DbType.DateTime;
typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
typeMap[typeof(byte[])] = DbType.Binary;
typeMap[typeof(byte?)] = DbType.Byte;
typeMap[typeof(sbyte?)] = DbType.SByte;
typeMap[typeof(short?)] = DbType.Int16;
......@@ -62,7 +61,6 @@ static SqlMapper()
typeMap[typeof(Guid?)] = DbType.Guid;
typeMap[typeof(DateTime?)] = DbType.DateTime;
typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
}
private static DbType LookupDbType(Type type)
......@@ -84,7 +82,6 @@ private static DbType LookupDbType(Type type)
throw new NotSupportedException("The type : " + type.ToString() + " is not supported by the mapper");
}
class ParamInfo
{
private ParamInfo()
......@@ -150,6 +147,43 @@ static class DynamicStub
public static Type Type = typeof(DynamicStub);
}
/// <summary>
/// Enumerates the query, keeping the reader open after it is called. Use when iterating through huge result sets . You should usually use ExecuteMapperQuery instead.
/// </summary>
public static IEnumerable<T> EnumerateMapperQuery<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
{
var identity = new Identity(sql, cnn, typeof(T));
using (var reader = GetReader(cnn, transaction, sql, GetParamInfo(param)))
{
Func<IDataReader, T> deserializer = GetDeserializer<T>(identity, reader);
while (reader.Read())
{
yield return deserializer(reader);
}
}
}
/// <summary>
/// Enumerates the query, keeping the reader open after it is called. Use when iterating through huge result sets. You should usually use ExecuteMapperQuery instead
/// </summary>
public static IEnumerable<dynamic> EnumerateMapperQuery(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
{
var identity = new Identity(sql, cnn, DynamicStub.Type);
using (var reader = GetReader(cnn, transaction, sql, GetParamInfo(param)))
{
Func<IDataReader, ExpandoObject> deserializer = GetDeserializer<ExpandoObject>(identity, reader);
while (reader.Read())
{
yield return deserializer(reader);
}
}
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static List<dynamic> ExecuteMapperQuery(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
{
var identity = new Identity(sql, cnn, DynamicStub.Type);
......@@ -166,7 +200,9 @@ public static List<dynamic> ExecuteMapperQuery(this IDbConnection cnn, string sq
return list;
}
/// <summary>
/// Return a typed list of objects, reader is closed after the call
/// </summary>
public static List<T> ExecuteMapperQuery<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null)
{
var identity = new Identity(sql, cnn, typeof(T));
......@@ -180,10 +216,7 @@ public static List<T> ExecuteMapperQuery<T>(this IDbConnection cnn, string sql,
{
rval.Add(deserializer(reader));
}
// ignore any other grids; note that this might mean we miss exceptions that happen
// late in the TDS stream, but that is bad design anyhow
}
return rval;
}
......@@ -480,6 +513,5 @@ private static void EmitInt32(ILGenerator il, int value)
default: il.Emit(OpCodes.Ldc_I4, value); break;
}
}
}
}
......@@ -34,6 +34,14 @@ public static void IsFalse(this bool b)
}
}
public static void IsTrue(this bool b)
{
if (b)
{
throw new ApplicationException("Expected true");
}
}
public static void IsNull(this object obj)
{
if (obj != null)
......@@ -187,7 +195,48 @@ public void TestSetPrivate()
connection.ExecuteMapperQuery<TestObj>("select 10 as [Priv]").First()._priv.IsEqualTo(10);
}
public void TestEnumeration()
{
var en = connection.EnumerateMapperQuery<int>("select 1 as one");
bool gotException = false;
try
{
connection.EnumerateMapperQuery<int>("select 1 as one");
}
catch (Exception)
{
gotException = true;
}
var realItems = en.ToList();
// should not exception, since enumertated
en = connection.EnumerateMapperQuery<int>("select 1 as one");
gotException.IsTrue();
}
public void TestEnumerationDynamic()
{
var en = connection.EnumerateMapperQuery("select 1 as one");
bool gotException = false;
try
{
connection.EnumerateMapperQuery("select 1 as one");
}
catch (Exception)
{
gotException = true;
}
int i = en.First().one;
i.IsEqualTo(1);
// should not exception, since enumertated
en = connection.EnumerateMapperQuery("select 1 as one");
gotException.IsTrue();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册