Ejemplo n.º 1
0
        /// <summary>
        /// Executes the CommandBuilder using a provided database manager and optionally a filter to read parameters from and explicit
        /// parameters.
        /// </summary>
        /// <param name="databaseManager">The database manager.</param>
        /// <param name="executableCommandInfo">The executable command information.</param>
        /// <returns>Tuple&lt;
        /// IEnumerable&lt;TResult1&gt;,
        /// IEnumerable&lt;TResult2&gt;&gt;.</returns>
        /// <exception cref="SusanooExecutionException">Any exception occured during execution.</exception>
        public override IResultSetCollection Execute(IDatabaseManager databaseManager,
                                                     IExecutableCommandInfo executableCommandInfo)
        {
            var results = new IEnumerable[_mappers.Length];
            IResultSetCollection finalResults;

            try
            {
                var record = databaseManager
                             .ExecuteDataReader(
                    executableCommandInfo.CommandText,
                    executableCommandInfo.DbCommandType,
                    Timeout,
                    executableCommandInfo.Parameters);

                var ix = 0;

                do
                {
                    results[ix] = _mappers[ix].Deserialize(record, RetrieveColumnIndexInfo());
                    ix++;
                } while (ix < results.Length);

                if (StreamingScope.Current == null)
                {
                    try
                    {
                        for (int i = 0; i < results.Length; i++)
                        {
                            results[i] = results[i].Cast <object>().ToList();

                            if (!record.NextResult())
                            {
                                for (int j = i + 1; j < results.Length; j++)
                                {
                                    results[j] = null;
                                }

                                break;
                            }
                        }

                        finalResults = new ResultSetCollection(results, null);
                    }
                    finally
                    {
                        record.Dispose();
                    }
                }
                else
                {
                    StreamingScope.Current.Enlist(record);
                    finalResults = new ResultSetCollection(results, record);
                }
            }
            catch (Exception ex)
            {
                throw new SusanooExecutionException(ex, executableCommandInfo, Timeout, executableCommandInfo.Parameters);
            }

            return(finalResults);
        }