/// <summary> /// Creates an enumerable list of instances of the specified type create from /// a data reader. /// </summary> /// <typeparam name="T"> /// The data object type. This should be a class with properties matching the reader /// value. /// </typeparam> /// <param name="reader">An open reader ready to be read to a list.</param> /// <returns>An enumerable list of instances of type 'T' created from the data reader.</returns> public static IEnumerable <T> ReadData <T>(this DbDataReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } var result = new List <T>(); if (EntityMapping.IsSimpleType <T>()) { while (reader.Read()) { result.Add(EntityMapping.ReadHelper.Get <T>(reader, 0)); } } else { var map = EntityMapping.MapDbReaderColumns(reader); var readEntity = EntityMapping.GetEntityFunc <T>(); while (reader.Read()) { result.Add(readEntity(reader, map)); } } return(result); }
public ResultStreamEnumerator(IContext context, SqlCommandManager manager) { _isSimpleType = EntityMapping.IsSimpleType <T>(); _readEntity = _isSimpleType ? GetSimple : EntityMapping.GetEntityFunc <T>(); _connection = AsyncHelper.RunSync(() => manager.CreateConnectionAsync()); _command = _connection.CreateCommand(); _command.Connection = _connection; _command.CommandText = context.CommandText; manager.AddParametersToCommand(_command, context); _connection.Open(); _reader = _command.ExecuteReader(); _map = _isSimpleType ? null : EntityMapping.MapDbReaderColumns(_reader); }
/// <summary> /// Creates a dictionary from a single row of a data reader, setting the dictionary key to the column names /// and the value to the values from the row. /// </summary> /// <param name="reader">An open reader with at least one row result (subsequent rows are ignored.)</param> /// <param name="columns">The columns to read. If this value is null, all columns will be returned.</param> /// <returns>A dictionary created from the first row of the data reader.</returns> /// <remarks>Keep in mind that result may contain DbNull type values.</remarks> public static IDictionary <string, object> CreateColumnKeyedDictionary(this DbDataReader reader, IEnumerable <string> columns) { if (reader.Read()) { if (columns?.Any() != true) { var map = EntityMapping.MapDbReaderColumns(reader); return(map.ToDictionary(k => k.Key, v => reader[v.Value])); } else { return(columns.ToDictionary(k => k, v => reader[v])); } } return(null); }
/// <summary>Processes the command and generates a custom result.</summary> /// <param name="reader">An open data reader queued to the appropriate result set.</param> /// <returns>The result of the processor for this query.</returns> public object Process(DbDataReader reader) { var map = EntityMapping.MapDbReaderColumns(reader); return(_valueProvider(reader, map)); }