Ejemplo n.º 1
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>Array of typed results</returns>
        /// <example>
        /// <code>
        ///   Employee[] emp = cmd.ExecuteTable&lt;Employee&gt;();
        ///   var x = cmd.ExecuteTable&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public virtual IEnumerable <T> ExecuteTable <T>()
        {
            return(ExecuteInternalCommand(() =>
            {
                using (DbDataReader dr = this.Command.ExecuteReader())
                {
                    // Primitive type: Executable<string>()
                    if (TypeExtension.IsPrimitive(typeof(T)))
                    {
                        return DataReaderConvertor.ToPrimitives <T>(dr);
                    }

                    // Dynamic type: Executable<dynamic>()
                    else if (DynamicConvertor.IsDynamic(typeof(T)))
                    {
                        return DataReaderConvertor.ToDynamic <T>(dr);
                    }

                    // Object type: Executable<Employee>()
                    else
                    {
                        return DataReaderConvertor.ToType <T>(dr).Rows;
                    }
                }
            }));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Execute the query and return a new instance of T with the first row of results
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>First row of results</returns>
        /// <example>
        /// <code>
        ///   Employee emp = cmd.ExecuteRow&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public virtual T ExecuteRow <T>()
        {
            // Primitive type: Executable<string>()
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(this.ExecuteScalar <T>());
            }
            else
            {
                // Get DataTable
                var rows = ExecuteInternalCommand(() =>
                {
                    using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        // Dynamic type: Executable<dynamic>()
                        if (DynamicConvertor.IsDynamic(typeof(T)))
                        {
                            return(DataReaderConvertor.ToDynamic <T>(dr));
                        }

                        // Object type: Executable<Employee>()
                        else
                        {
                            return(DataReaderConvertor.ToType <T>(dr).Rows);
                        }
                    }
                });

                // Return
                return(rows?.Any() == true?rows.First() : default(T));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <returns>Array of typed results</returns>
        /// <example>
        /// <code>
        ///   Employee[] emp = cmd.ExecuteTable&lt;Employee&gt;();
        ///   var x = cmd.ExecuteTable&lt;Employee&gt;();
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public async virtual Task <IEnumerable <T> > ExecuteTableAsync <T>()
        {
            return(await ExecuteInternalCommand(async() =>
            {
                using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
                {
                    // Primitive type: Executable<string>()
                    if (TypeExtension.IsPrimitive(typeof(T)))
                    {
                        return await DataReaderConvertor.ToPrimitivesAsync <T>(dr);
                    }

                    // Dynamic type: Executable<dynamic>()
                    else if (DynamicConvertor.IsDynamic(typeof(T)))
                    {
                        return await DataReaderConvertor.ToDynamicAsync <T>(dr);
                    }

                    // Object type: Executable<Employee>()
                    else
                    {
                        return (await DataReaderConvertor.ToTypeAsync <T>(dr)).Rows;
                    }
                }
            }));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new instance of DbParameter[] with ParameterName, Value and IsNullable properties
        /// sets to value's properties.
        /// </summary>
        /// <typeparam name="T">Type of object with properties to convert in Parameters</typeparam>
        /// <typeparam name="U">DbParameter type (SqlParameter, ...)</typeparam>
        /// <param name="command"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static IEnumerable <DbParameter> ToParameters <T, U>(DbCommand command, T value) where U : DbParameter
        {
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                throw new ArgumentException("The value can not be a simple type (string, int, ...), but an object with simple properties.", "value");
            }
            else
            {
                List <DbParameter> parameters = new List <DbParameter>();
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    if (TypeExtension.IsPrimitive(property.PropertyType))
                    {
                        // Data type
                        Type propType = TypeExtension.GetNullableSubType(property.PropertyType);

                        // Value
                        DbParameter parameter = command != null?
                                                command.CreateParameter() :
                                                    Activator.CreateInstance(typeof(U)) as DbParameter;

                        parameter.Value      = typeof(T).GetProperty(property.Name).GetValue(value, null);
                        parameter.IsNullable = TypeExtension.IsNullable(property.PropertyType);
                        parameter.DbType     = DbTypeMap.FirstDbType(propType);

                        if (parameter.IsNullable && parameter.Value == null)
                        {
                            parameter.Value = DBNull.Value;
                        }

                        // Parameter name
                        string attribute = Apps72.Dev.Data.Annotations.ColumnAttribute.GetColumnAttributeName(property);
                        if (string.IsNullOrEmpty(attribute))
                        {
                            parameter.ParameterName = property.Name;
                        }
                        else
                        {
                            parameter.ParameterName = attribute;
                        }

                        parameters.Add(parameter);
                    }
                }

                return(parameters.AsEnumerable());
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Execute the query and return an array of new instances of typed results filled with data table result.
 /// </summary>
 /// <typeparam name="T">Object type</typeparam>
 /// <param name="itemOftype"></param>
 /// <returns>Array of typed results</returns>
 /// <example>
 /// <code>
 ///   Employee emp = new Employee();
 ///   var x = cmd.ExecuteTable(new { emp.Age, emp.Name });
 ///   var y = cmd.ExecuteTable(new { Age = 0, Name = "" });
 /// </code>
 /// <remarks>
 ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
 ///   to set which column name (ex. [Column("Name")] must be associated to this property.
 /// </remarks>
 /// </example>
 public virtual IEnumerable <T> ExecuteTable <T>(T itemOftype)
 {
     return(ExecuteInternalCommand(() =>
     {
         using (DbDataReader dr = this.Command.ExecuteReader())
         {
             if (TypeExtension.IsPrimitive(typeof(T)))
             {
                 return DataReaderConvertor.ToPrimitives <T>(dr);
             }
             else
             {
                 return DataReaderConvertor.ToAnonymous <T>(dr);
             }
         }
     }));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Execute the query and return an array of new instances of typed results filled with data table result.
 /// </summary>
 /// <typeparam name="T">Object type</typeparam>
 /// <param name="itemOftype"></param>
 /// <returns>Array of typed results</returns>
 /// <example>
 /// <code>
 ///   Employee emp = new Employee();
 ///   var x = cmd.ExecuteTable(new { emp.Age, emp.Name });
 ///   var y = cmd.ExecuteTable(new { Age = 0, Name = "" });
 /// </code>
 /// <remarks>
 ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
 ///   to set which column name (ex. [Column("Name")] must be associated to this property.
 /// </remarks>
 /// </example>
 public async virtual Task <IEnumerable <T> > ExecuteTableAsync <T>(T itemOftype)
 {
     return(await ExecuteInternalCommand(async() =>
     {
         using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
         {
             if (TypeExtension.IsPrimitive(typeof(T)))
             {
                 return await DataReaderConvertor.ToPrimitivesAsync <T>(dr);
             }
             else
             {
                 return await DataReaderConvertor.ToAnonymousAsync <T>(dr);
             }
         }
     }));
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Execute the query and fill the specified T object with the first row of results
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="itemOftype"></param>
        /// <returns>First row of results</returns>
        /// <example>
        /// <code>
        ///   Employee emp = new Employee();
        ///   var x = cmd.ExecuteRow(new { emp.Age, emp.Name });
        ///   var y = cmd.ExecuteRow(new { Age = 0, Name = "" });
        ///   var z = cmd.ExecuteRow(emp);
        /// </code>
        /// <remarks>
        ///   Result object property (ex. Employee.Name) may be tagged with the ColumnAttribute
        ///   to set which column name (ex. [Column("Name")] must be associated to this property.
        /// </remarks>
        /// </example>
        public virtual T ExecuteRow <T>(T itemOftype)
        {
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(this.ExecuteScalar <T>());
            }
            else
            {
                // Get DataTable
                var rows = ExecuteInternalCommand(() =>
                {
                    using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        return(DataReaderConvertor.ToAnonymous <T>(dr));
                    }
                });

                // Return
                return(rows?.Any() == true?rows.First() : default(T));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Execute the query and fill the specified T object with the first row of results
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="converter"></param>
        /// <returns>First row of results</returns>
        public virtual T ExecuteRow <T>(Func <Schema.DataRow, T> converter)
        {
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(this.ExecuteScalar <T>());
            }
            else
            {
                // Get DataRow
                var table = ExecuteInternalCommand(() =>
                {
                    using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        return(DataReaderConvertor.ToDataTable(dr));
                    }
                });
                var row = table?.Rows?.FirstOrDefault();

                // Return
                return(row != null?converter.Invoke(row) : default(T));
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Execute the query and fill the specified T object with the first row of results
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="converter"></param>
        /// <returns>First row of results</returns>
        public async virtual Task <T> ExecuteRowAsync <T>(Func <Schema.DataRow, Task <T> > converter)
        {
            if (TypeExtension.IsPrimitive(typeof(T)))
            {
                return(await this.ExecuteScalarAsync <T>());
            }
            else
            {
                // Get DataRow
                var table = await ExecuteInternalCommand(async() =>
                {
                    using (DbDataReader dr = await this.Command.ExecuteReaderAsync(System.Data.CommandBehavior.SingleRow))
                    {
                        return(await DataReaderConvertor.ToDataTableAsync(dr));
                    }
                });

                var row = table?.Rows?.FirstOrDefault();

                // Return
                return(row != null ? await converter.Invoke(row) : default(T));
            }
        }