Ejemplo n.º 1
0
        public async Task <object> ExecuteAsync(CancellationToken token)
        {
            using (new EntityCache())
                using (Transaction tr = new Transaction())
                {
                    object result;
                    using (var retriever = EntityCache.NewRetriever())
                    {
                        if (EagerProjections.Any() || LazyChildProjections.Any())
                        {
                            lookups = new Dictionary <LookupToken, IEnumerable>();
                        }

                        foreach (var child in EagerProjections)
                        {
                            child.Fill(lookups, retriever);
                        }

                        using (HeavyProfiler.Log("SQL", () => MainCommand.PlainSql()))
                            using (DbDataReader reader = await Executor.UnsafeExecuteDataReaderAsync(MainCommand, token: token))
                            {
                                ProjectionRowEnumerator <T> enumerator = new ProjectionRowEnumerator <T>(reader, ProjectorExpression, lookups, retriever, token);

                                IEnumerable <T> enumerable = new ProjectionRowEnumerable <T>(enumerator);

                                try
                                {
                                    if (Unique == null)
                                    {
                                        result = enumerable.ToList();
                                    }
                                    else
                                    {
                                        result = UniqueMethod(enumerable, Unique.Value);
                                    }
                                }
                                catch (Exception ex) when(!(ex is OperationCanceledException))
                                {
                                    FieldReaderException fieldEx = enumerator.Reader.CreateFieldReaderException(ex);

                                    fieldEx.Command   = MainCommand;
                                    fieldEx.Row       = enumerator.Row;
                                    fieldEx.Projector = ProjectorExpression;
                                    throw fieldEx;
                                }
                            }

                        foreach (var child in LazyChildProjections)
                        {
                            child.Fill(lookups, retriever);
                        }

                        retriever.CompleteAll();
                    }

                    return(tr.Commit(result));
                }
        }
Ejemplo n.º 2
0
        public object?Execute()
        {
            return(SqlServerRetry.Retry(() =>
            {
                using (new EntityCache())
                    using (Transaction tr = new Transaction())
                    {
                        object?result;
                        using (var retriever = EntityCache.NewRetriever())
                        {
                            var lookups = new Dictionary <LookupToken, IEnumerable>();

                            foreach (var child in EagerProjections)
                            {
                                child.Fill(lookups, retriever);
                            }

                            using (HeavyProfiler.Log("SQL", () => MainCommand.sp_executesql()))
                                using (var reader = Executor.UnsafeExecuteDataReader(MainCommand))
                                {
                                    ProjectionRowEnumerator <T> enumerator = new ProjectionRowEnumerator <T>(reader.Reader, ProjectorExpression, lookups, retriever, CancellationToken.None);

                                    IEnumerable <T> enumerable = new ProjectionRowEnumerable <T>(enumerator);

                                    try
                                    {
                                        if (Unique == null)
                                        {
                                            result = enumerable.ToList();
                                        }
                                        else
                                        {
                                            result = UniqueMethod(enumerable, Unique.Value);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        FieldReaderException fieldEx = enumerator.Reader.CreateFieldReaderException(ex);
                                        fieldEx.Command = MainCommand;
                                        fieldEx.Row = enumerator.Row;
                                        fieldEx.Projector = ProjectorExpression;
                                        throw fieldEx;
                                    }
                                }

                            foreach (var child in LazyChildProjections)
                            {
                                child.Fill(lookups, retriever);
                            }

                            retriever.CompleteAll();
                        }

                        return tr.Commit(result);
                    }
            }));
        }