Ejemplo n.º 1
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.º 2
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.º 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 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.º 4
0
 /// <summary>
 /// Execute the query and return a <see cref="System.Data.DataSet"/> object filled with data table results.
 /// </summary>
 /// <returns>Classic <see cref="System.Data.DataSet"/> object.</returns>
 public virtual System.Data.DataSet ExecuteDataSet()
 {
     return(ExecuteInternalCommand(() =>
     {
         using (DbDataReader dr = this.Command.ExecuteReader(System.Data.CommandBehavior.KeyInfo))
         {
             return DataReaderConvertor.ToSystemDataSet(dr);
         }
     }));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Execute the query and return a <see cref="System.Data.DataSet"/> object filled with data table results.
 /// </summary>
 /// <returns>Classic <see cref="System.Data.DataSet"/> object.</returns>
 public async virtual Task <System.Data.DataSet> ExecuteDataSetAsync()
 {
     return(await ExecuteInternalCommand(async() =>
     {
         using (DbDataReader dr = await this.Command.ExecuteReaderAsync(System.Data.CommandBehavior.KeyInfo))
         {
             return DataReaderConvertor.ToSystemDataSet(dr);
         }
     }));
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Execute the query and return a list or array of new instances of typed results filled with data table results.
        /// </summary>
        /// <typeparam name="T">Object type for first table</typeparam>
        /// <typeparam name="U">Object type for second table</typeparam>
        /// <returns>List of array of typed results</returns>
        /// <example>
        /// <code>
        ///   var data = cmd.ExecuteDataSet&lt;Employee, Department&gt;();
        ///   var employees = data.Item1;
        ///   var departments = data.Item2;
        /// </code>
        /// </example>
        public virtual Tuple <IEnumerable <T>, IEnumerable <U> > ExecuteDataSet <T, U>(T typeOfItem1, U typeOfItem2)
        {
            var datasets = ExecuteInternalCommand(() =>
            {
                using (DbDataReader dr = this.Command.ExecuteReader())
                {
                    return(DataReaderConvertor.ToMultipleTypes <T, U, NoType, NoType, NoType>(dr, forAnonymousTypes: true));
                }
            });

            return(new Tuple <IEnumerable <T>, IEnumerable <U> >
                   (
                       datasets.Item1,
                       datasets.Item2
                   ));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Execute the query and return a list or array of new instances of typed results filled with data table results.
        /// </summary>
        /// <typeparam name="T">Object type for first table</typeparam>
        /// <typeparam name="U">Object type for second table</typeparam>
        /// <returns>List of array of typed results</returns>
        /// <example>
        /// <code>
        ///   var data = cmd.ExecuteDataSet&lt;Employee, Department&gt;();
        ///   var employees = data.Item1;
        ///   var departments = data.Item2;
        /// </code>
        /// </example>
        public async virtual Task <Tuple <IEnumerable <T>, IEnumerable <U> > > ExecuteDataSetAsync <T, U>(T typeOfItem1, U typeOfItem2)
        {
            var datasets = await ExecuteInternalCommand(async() =>
            {
                using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
                {
                    return(await DataReaderConvertor.ToMultipleTypesAsync <T, U, NoType, NoType, NoType>(dr, forAnonymousTypes: true));
                }
            });

            return(new Tuple <IEnumerable <T>, IEnumerable <U> >
                   (
                       datasets.Item1,
                       datasets.Item2
                   ));
        }
Ejemplo n.º 8
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.º 9
0
        /// <summary>
        /// Execute the query and return a list or array of new instances of typed results filled with data table results.
        /// </summary>
        /// <typeparam name="T">Object type for first table</typeparam>
        /// <typeparam name="U">Object type for second table</typeparam>
        /// <typeparam name="V">Object type for third table</typeparam>
        /// <returns>List of array of typed results</returns>
        /// <example>
        /// <code>
        ///   var data = cmd.ExecuteDataSet&lt;Employee, Department&gt;();
        ///   var employees = data.Item1;
        ///   var departments = data.Item2;
        /// </code>
        /// </example>
        public virtual Tuple <IEnumerable <T>, IEnumerable <U>, IEnumerable <V> > ExecuteDataSet <T, U, V>()
        {
            var datasets = ExecuteInternalCommand(() =>
            {
                using (DbDataReader dr = this.Command.ExecuteReader())
                {
                    return(DataReaderConvertor.ToMultipleTypes <T, U, V, NoType, NoType>(dr));
                }
            });

            return(new Tuple <IEnumerable <T>, IEnumerable <U>, IEnumerable <V> >
                   (
                       datasets.Item1,
                       datasets.Item2,
                       datasets.Item3
                   ));
        }
Ejemplo n.º 10
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.º 11
0
        /// <summary>
        /// Execute the query and return a list or array of new instances of typed results filled with data table results.
        /// </summary>
        /// <typeparam name="T">Object type for first table</typeparam>
        /// <typeparam name="U">Object type for second table</typeparam>
        /// <typeparam name="V">Object type for third table</typeparam>
        /// <returns>List of array of typed results</returns>
        /// <example>
        /// <code>
        ///   var data = cmd.ExecuteDataSet&lt;Employee, Department&gt;();
        ///   var employees = data.Item1;
        ///   var departments = data.Item2;
        /// </code>
        /// </example>
        public async virtual Task <Tuple <IEnumerable <T>, IEnumerable <U>, IEnumerable <V> > > ExecuteDataSetAsync <T, U, V>()
        {
            var datasets = await ExecuteInternalCommand(async() =>
            {
                using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
                {
                    return(await DataReaderConvertor.ToMultipleTypesAsync <T, U, V, NoType, NoType>(dr));
                }
            });

            return(new Tuple <IEnumerable <T>, IEnumerable <U>, IEnumerable <V> >
                   (
                       datasets.Item1,
                       datasets.Item2,
                       datasets.Item3
                   ));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="converter"></param>
        /// <returns>Array of typed results</returns>
        public virtual IEnumerable <T> ExecuteTable <T>(Func <Schema.DataRow, T> converter)
        {
            var table = ExecuteInternalCommand(() =>
            {
                using (DbDataReader dr = this.Command.ExecuteReader())
                {
                    return(DataReaderConvertor.ToDataTable(dr));
                }
            });

            if (table != null && table.Rows != null)
            {
                return(table.Rows.Select(row => converter.Invoke(row)));
            }
            else
            {
                return new T[] { }
            };
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Execute the query and return an array of new instances of typed results filled with data table result.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="converter"></param>
        /// <returns>Array of typed results</returns>
        public async virtual Task <IEnumerable <T> > ExecuteTableAsync <T>(Func <Schema.DataRow, Task <T> > converter)
        {
            var table = await ExecuteInternalCommand(async() =>
            {
                using (DbDataReader dr = await this.Command.ExecuteReaderAsync())
                {
                    return(await DataReaderConvertor.ToDataTableAsync(dr));
                }
            });

            if (table != null && table.Rows != null)
            {
                return(table.Rows.Select(async row => await converter.Invoke(row)).Select(i => i.GetAwaiter().GetResult()));
            }
            else
            {
                return new T[] { }
            };
        }
Ejemplo n.º 14
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.º 15
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.º 16
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));
            }
        }