Ejemplo n.º 1
0
        /// <summary>
        /// Creates a dictionary from the rows of two columns of a data reader.
        /// </summary>
        /// <typeparam name="TKey">The key type.</typeparam>
        /// <typeparam name="TValue">The key type.</typeparam>
        /// <param name="reader">An open reader ready to be read to a list.</param>
        /// <returns>A dictionary created from the first two columns of the data reader.</returns>
        public static IDictionary <TKey, TValue> CreateRowKeyedDictionary <TKey, TValue>(this DbDataReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if (!EntityMapping.IsSimpleType <TKey>())
            {
                throw new ArgumentException("The key type of the dictionary must be a simple type.");
            }
            if (!EntityMapping.IsSimpleType <TValue>())
            {
                throw new ArgumentException("The value type of the dictionary must be a simple type.");
            }

            var result = new Dictionary <TKey, TValue>();

            while (reader.Read())
            {
                result.Add(EntityMapping.ReadHelper.Get <TKey>(reader, 0), EntityMapping.ReadHelper.Get <TValue>(reader, 1));
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Processes the result as a single entity result.
        /// </summary>
        /// <param name="reader">An open data reader queued to the appropriate result set.</param>
        /// <returns>The result for this query.</returns>
        public object Process(DbDataReader reader)
        {
            if (EntityMapping.IsSimpleType <TResult>())
            {
                // requested type can be mapped directly to a CLR type

                if (reader.Read())
                {
                    return(EntityMapping.ReadHelper.Get(reader, 0, _defaultResult));
                }
                else
                {
                    return(_defaultResult);
                }
            }
            else
            {
                // else if requested type must be mapped from the result row

                var result = reader.CreateInstance(_defaultResult);

                // Populate collections
                if (_properties != null)
                {
                    foreach (var collection in _properties)
                    {
                        if (!reader.NextResult())
                        {
                            throw new InvalidOperationException("Not enough result sets were returned by the query to assign all the requested properties.");
                        }

                        // Don't try to assign the result if it is null
                        if (result != null)
                        {
                            var genericReadData   = readData.MakeGenericMethod(collection.PropertyType.GetCollectionType());
                            var collectionResults = genericReadData.Invoke(null, new object[] { reader });
                            collection.SetValue(result, collectionResults);
                        }
                    }
                }

                return(result);
            }
        }