Commit 148f2f96 authored by Joao Silva's avatar Joao Silva

Added dynamic query like for CSharp30. The behavior is simulated with a Dictionary<string,object>

parent 809594cf
......@@ -625,6 +625,41 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
{
return Query<FastExpando>(cnn, sql, param as object, transaction, buffered, commandTimeout, commandType);
}
#else
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static IEnumerable<IDictionary<string, object>> Query(this IDbConnection cnn, string sql, object param) {
return Query(cnn, sql, param, null, true, null, null);
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static IEnumerable<IDictionary<string, object>> Query(this IDbConnection cnn, string sql, object param, IDbTransaction transaction) {
return Query(cnn, sql, param, transaction, true, null, null);
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static IEnumerable<IDictionary<string, object>> Query(this IDbConnection cnn, string sql, object param, CommandType? commandType) {
return Query(cnn, sql, param, null, true, null, commandType);
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static IEnumerable<IDictionary<string, object>> Query(this IDbConnection cnn, string sql, object param, IDbTransaction transaction, CommandType? commandType) {
return Query(cnn, sql, param, transaction, true, null, commandType);
}
/// <summary>
/// Return a list of dynamic objects, reader is closed after the call
/// </summary>
public static IEnumerable<IDictionary<string,object>> Query(this IDbConnection cnn, string sql, object param, IDbTransaction transaction, bool buffered, int? commandTimeout, CommandType? commandType) {
return Query<IDictionary<string, object>>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);
}
#endif
/// <summary>
......@@ -1016,6 +1051,11 @@ private static CacheInfo GetCacheInfo(Identity identity)
{
return GetDynamicDeserializer(reader, startBound, length, returnNullIfFirstMissing);
}
#else
if(type.IsAssignableFrom(typeof(Dictionary<string,object>)))
{
return GetDictionaryDeserializer(reader, startBound, length, returnNullIfFirstMissing);
}
#endif
Type underlyingType = null;
if (!(typeMap.ContainsKey(type) || type.IsEnum || type.FullName == LinqBinary ||
......@@ -1159,9 +1199,14 @@ IEnumerator IEnumerable.GetEnumerator()
#endregion
}
#endif
#if !CSHARP30
private static Func<IDataReader, object> GetDynamicDeserializer(IDataRecord reader, int startBound, int length, bool returnNullIfFirstMissing)
#else
private static Func<IDataReader, object> GetDictionaryDeserializer(IDataRecord reader, int startBound, int length, bool returnNullIfFirstMissing)
#endif
{
var fieldCount = reader.FieldCount;
if (length == -1)
......@@ -1188,11 +1233,15 @@ IEnumerator IEnumerable.GetEnumerator()
return null;
}
}
#if !CSHARP30
//we know this is an object so it will not box
return FastExpando.Attach(row);
#else
return row;
#endif
};
}
#endif
/// <summary>
/// Internal use only
/// </summary>
......
......@@ -23,5 +23,22 @@ class BasicType
{
public string Value { get; set; }
}
public void TestDynamicSimulatedQuery() {
var rows = connection.Query("select 1 A, 2 B union all select 3, 4", null).ToList();
((int)rows[0]["A"])
.IsEqualTo(1);
((int)rows[0]["B"])
.IsEqualTo(2);
((int)rows[1]["A"])
.IsEqualTo(3);
((int)rows[1]["B"])
.IsEqualTo(4);
}
}
}
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