/// <summary> /// 异步的,执行查询文本并将结果并返回动态序列。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <param name="cancellationToken">取消操作的通知。</param> /// <returns>一个动态对象的枚举器。</returns> public virtual async Task <IEnumerable <dynamic> > ExecuteEnumerableAsync(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, CancellationToken cancellationToken = default) { Guard.ArgumentNull(queryCommand, nameof(queryCommand)); cancellationToken.ThrowIfCancellationRequested(); var result = new List <dynamic>(); using var reader = (InternalDataReader) await ExecuteReaderAsync(queryCommand, segment, parameters, CommandBehavior.Default, cancellationToken); var wrapper = Provider.GetService <IRecordWrapper>(); TypeDescriptorUtility.AddDefaultDynamicProvider(); while (await reader.ReadAsync(cancellationToken)) { var expando = new ExpandoObject(); var dictionary = (IDictionary <string, object>)expando; for (int i = 0, n = reader.FieldCount; i < n; i++) { var name = wrapper.GetFieldName(reader, i); if (name.Equals("ROW_NUM")) { continue; } dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i)); } result.Add(expando); } return(result); }
/// <summary> /// 根据自定义的SQL语句查询返回一组动态对象。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>一个动态对象的枚举器。</returns> public virtual IEnumerable <object> ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, "queryCommand"); using (var reader = ExecuteReader(queryCommand, segment, parameters)) { var wrapper = Provider.GetService <IRecordWrapper>(); #if !N35 && DYNAMIC TypeDescriptorUtility.AddDefaultDynamicProvider(); while (reader.Read()) { var expando = new ExpandoObject(); var dictionary = (IDictionary <string, object>)expando; for (var i = 0; i < reader.FieldCount; i++) { dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i)); } yield return(expando); } #else var builder = new DataReaderTypeBuilder(reader); var elementType = builder.CreateType(); while (reader.Read()) { yield return(elementType.New(reader, wrapper)); } #endif } }
/// <summary> /// 根据自定义的SQL语句查询返回一组动态对象。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <returns>一个动态对象的枚举器。</returns> public virtual IEnumerable <dynamic> ExecuteEnumerable(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null) { Guard.ArgumentNull(queryCommand, nameof(queryCommand)); using var reader = ExecuteReader(queryCommand, segment, parameters, CommandBehavior.Default); var wrapper = Provider.GetService <IRecordWrapper>(); TypeDescriptorUtility.AddDefaultDynamicProvider(); while (reader.Read()) { var expando = new ExpandoObject(); var dictionary = (IDictionary <string, object>)expando; for (int i = 0, n = reader.FieldCount; i < n; i++) { var name = wrapper.GetFieldName(reader, i); if (name.Equals("ROW_NUM")) { continue; } dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i)); } yield return(expando); } }
/// <summary> /// 异步的,执行查询文本并将结果并返回动态序列。 /// </summary> /// <param name="queryCommand">查询命令。</param> /// <param name="segment">数据分段对象。</param> /// <param name="parameters">查询参数集合。</param> /// <param name="cancellationToken">取消操作的通知。</param> /// <returns>一个动态对象的枚举器。</returns> public virtual async IAsyncEnumerable <dynamic> ExecuteEnumerableAsync(IQueryCommand queryCommand, IDataSegment segment = null, ParameterCollection parameters = null, CancellationToken cancellationToken = default) { Guard.ArgumentNull(queryCommand, nameof(queryCommand)); using (var reader = (DbDataReader)(await ExecuteReaderAsync(queryCommand, segment, parameters, cancellationToken))) { var wrapper = Provider.GetService <IRecordWrapper>(); TypeDescriptorUtility.AddDefaultDynamicProvider(); while (await reader.ReadAsync(cancellationToken)) { var expando = new ExpandoObject(); var dictionary = (IDictionary <string, object>)expando; for (var i = 0; i < reader.FieldCount; i++) { var name = wrapper.GetFieldName(reader, i); if (name.Equals("ROW_NUM")) { continue; } dictionary.Add(wrapper.GetFieldName(reader, i), RecordWrapHelper.GetValue(wrapper, reader, i)); } yield return(expando); } } }
private static Type InternalCreateType(IDataReader reader, IEnumerable <Type> implInterfaceTypes) { var guid = Guid.NewGuid().ToString("N"); var typeBuilder = new DynamicAssemblyBuilder("_Dynamic_" + guid).DefineType("$<>" + guid); foreach (var type in implInterfaceTypes) { typeBuilder.ImplementInterface(type); } var length = reader.FieldCount; var fields = new DynamicFieldBuilder[length]; for (var i = 0; i < length; i++) { var name = GetFieldName(reader.GetName(i)); var type = reader.GetFieldType(i); var fieldBuilder = typeBuilder.DefineField("<>__" + name, type); typeBuilder.DefineProperty(name, type).DefineGetSetMethods(fieldBuilder); fields[i] = fieldBuilder; } var constructorBuilder = typeBuilder.DefineConstructor(new[] { typeof(IDataReader), typeof(IRecordWrapper) }, ilCoding: bc => bc.Emitter .For(0, length, (e, i) => e.ldarg_0 .ldarg_2 .ldarg_1 .ldc_i4(i) .callvirt(RecordWrapHelper.GetGetValueMethod(reader.GetFieldType(i))) .stfld(fields[i].FieldBuilder)) .ret()); constructorBuilder.DefineParameter("reader"); constructorBuilder.DefineParameter("wrapper"); return(typeBuilder.CreateType()); }