예제 #1
0
 protected void AssertQuery <T>(IQueryable <T> query, string expectedQueryText)
 {
     lastQuery = null;
     Assert.That(query.ToArray().Length, Is.EqualTo(0));
     Assert.IsNotNull(lastQuery);
     Assert.That(lastQuery.Query.ToString(), Is.EqualTo(expectedQueryText));
 }
예제 #2
0
        private IEnumerable Execute(BuiltQuery builtQuery)
        {
            if (EntityHelpers.IsConstant(builtQuery.EntityType))
            {
                var constantWrapType = builtQuery.EntityType.BaseType;
                if (constantWrapType == null)
                {
                    throw new InvalidOperationException("assertion failure");
                }
                var constantWrap = (Constant)FormatterServices.GetUninitializedObject(builtQuery.EntityType);
                var constants    = ComHelpers.GetProperty(globalContext.ComObject(), "Константы");
                var constant     = ComHelpers.GetProperty(constants, builtQuery.EntityType.Name);
                var value        = ComHelpers.Invoke(constant, "Получить");
                constantWrap.ЗначениеНетипизированное = comObjectMapper.MapFrom1C(value,
                                                                                  constantWrapType.GetGenericArguments()[0]);
                yield return(constantWrap);

                yield break;
            }
            var queryText  = builtQuery.QueryText;
            var parameters = builtQuery.Parameters;

            parametersConverter.ConvertParametersTo1C(parameters);
            var hasReference = ConfigurationName.Get(builtQuery.EntityType).HasReference;
            var queryResult  = globalContext.Execute(queryText, parameters);

            if (hasReference || builtQuery.Projection != null)
            {
                var projection = builtQuery.Projection != null
                    ? ProjectionMapperFactory.GetMapper(builtQuery.Projection, comObjectMapper)
                    : null;

                var selection = queryResult.Select();
                while (selection.Next())
                {
                    if (projection != null)
                    {
                        yield return(projection(selection.ComObject));
                    }
                    else if (builtQuery.IsCount)
                    {
                        yield return(comObjectMapper.MapFrom1C(selection["src_Count"], typeof(int)));
                    }
                    else
                    {
                        yield return(comObjectMapper.MapFrom1C(selection["Ссылка"], builtQuery.EntityType));
                    }
                }
            }
            else
            {
                var valueTable = queryResult.Unload();
                foreach (var r in valueTable)
                {
                    yield return(comObjectMapper.MapFrom1C(r.ComObject(), builtQuery.EntityType));
                }
            }
        }
예제 #3
0
 protected void AssertQueryCount <T>(IQueryable <T> query, string expectedQueryText,
                                     params KeyValuePair <string, object>[] expectedParameters)
 {
     lastQuery = null;
     Assert.That(query.Count(), Is.EqualTo(0));
     Assert.IsNotNull(lastQuery);
     Assert.That(lastQuery.QueryText, Is.EqualTo(expectedQueryText));
     Assert.That(DumpParametersToString(lastQuery.Parameters),
                 Is.EqualTo(DumpParametersToString(expectedParameters)));
 }
예제 #4
0
        protected IQueryable <T> SourceForCount <T>(string sourceName = null)
        {
            var queryProvider = RelinqHelpers.CreateQueryProvider(new TypeRegistry(typeof(Контрагенты).Assembly),
                                                                  delegate(BuiltQuery query)
            {
                lastQuery = query;
                return(new int[1]);
            });

            return(new RelinqQueryable <T>(queryProvider, sourceName));
        }
예제 #5
0
        private IQueryable <T> Source <T>()
        {
            var queryProvider = RelinqHelpers.CreateQueryProvider(new JiraMetadataProvider(new[] { typeof(T) }),
                                                                  delegate(BuiltQuery query)
            {
                lastQuery = query;
                return(new T[0]);
            });

            return(new RelinqQueryable <T>(queryProvider));
        }
예제 #6
0
 private IEnumerable Execute(BuiltQuery builtQuery, Stopwatch stopwatch)
 {
     if (builtQuery.IsAny.HasValue && builtQuery.IsAny.Value)
     {
         return(ExecuteAny(builtQuery, stopwatch));
     }
     if (builtQuery.Count.HasValue && builtQuery.Count.Value)
     {
         return(ExecuteCount(builtQuery, stopwatch));
     }
     return(ExecuteCollection(builtQuery, stopwatch));
 }
예제 #7
0
 private void AssertQuery <T>(IQueryable <T> query, string expectedQueryText, string[] fields)
 {
     lastQuery = null;
     Assert.That(query.ToArray().Length, Is.EqualTo(0));
     Assert.IsNotNull(lastQuery);
     Assert.That(lastQuery.Query.ToString(), Is.EqualTo(expectedQueryText));
     if (fields == null)
     {
         Assert.That(lastQuery.Projection, Is.Null);
     }
     else
     {
         Assert.That(lastQuery.Projection.fields.Select(x => x.Expression).ToArray(),
                     Is.EquivalentTo(fields));
     }
 }
예제 #8
0
        /// <inheritdoc />
        public async Task <QueryResult <TModel> > InternalGetAsync(
            TDbContext db,
            TSession session,
            QueryInfo queryInfo         = null,
            BenchmarkSection benchmarks = null)
        {
            var dbSet       = GetDbSet(db).AsNoTracking();
            var dbDataQuery = FilterDbModelCollection(dbSet);

            BuiltQuery <TDbMetaData> builtResultsQuery;

            if (QueryOnMetaData)
            {
                builtResultsQuery = SelectDbMetaData(db, session, dbDataQuery
                                                     .BuildSecuredQuery(session.PermissionResolver))
                                    .BuildUnsecuredDbQuery <TDbMetaData, TModel>(queryInfo);
            }
            else
            {
                var builtQuery = dbDataQuery.BuildDbQuery <TDbModel, TModel>(queryInfo, session.PermissionResolver);
                builtResultsQuery = new BuiltQuery <TDbMetaData>(
                    SelectDbMetaData(db, session, builtQuery.Query),
                    SelectDbMetaData(db, session, builtQuery.PagedQuery),
                    builtQuery.PageSize);
            }

            var queryResult = await builtResultsQuery.ExecuteAsync(session.Benchmarks, x => x.ToArrayAsync(), x => x.LongCountAsync());

            TModel[] models;
            using (session.Benchmarks.CreateBenchmark("Map models"))
            {
                models = queryResult.Data
                         .Select(x =>
                {
                    var model = DbModelToModel(x);
                    model.AttachService(Service, session);
                    return(model);
                })
                         .ToArray();
            }

            return(new QueryResult <TModel>(models, queryResult.RecordCount, queryResult.PageCount));
        }
예제 #9
0
        private JiraIssuesResponse SelectIssuesInfo(BuiltQuery builtQuery, Stopwatch stopwatch)
        {
            loggingSettings.Log(LogLevel.Debug,
                                $"Getting the count of issues is starting execution, query [{builtQuery.Query}] ");

            try
            {
                var request = new JiraIssuesRequest
                {
                    Jql        = builtQuery.Query?.ToString() ?? "",
                    StartAt    = 0,
                    MaxResults = 1,
                    Fields     = new[] { "created" }
                };

                var response = jira.SelectIssuesAsync(request, builtQuery.IssueType, CancellationToken.None)
                               .GetAwaiter().GetResult();
                if (stopwatch != null)
                {
                    stopwatch.Stop();
                    loggingSettings.Log(LogLevel.Trace,
                                        $"Getting the count of issues is finished, query [{builtQuery.Query}], took [{stopwatch.Elapsed.TotalMilliseconds}] ms");
                }

                return(response);
            }
            catch (Exception)
            {
                if (stopwatch != null)
                {
                    stopwatch.Stop();
                    loggingSettings.Log(LogLevel.Trace,
                                        $"Getting the count of issues is finished, query [{builtQuery.Query}]. Threw an exception, took [{stopwatch.Elapsed.TotalMilliseconds}] ms");
                }

                throw;
            }
        }
예제 #10
0
        private IEnumerable ExecuteCollection(BuiltQuery builtQuery, Stopwatch stopwatch)
        {
            var keys   = new HashSet <string>();
            var fields = builtQuery.Projection?.fields
                         .Select(x => x.Expression)
                         .ToArray();
            var skip       = builtQuery.Skip ?? 0;
            var take       = builtQuery.Take ?? int.MaxValue;
            var projection = builtQuery.Projection != null
                ? ProjectionMapperFactory.GetMapper(builtQuery.Projection)
                : null;

            if (projection == null && loggingSettings.NeedLogging(LogLevel.Warning))
            {
                if (warnings.TryAdd(Environment.StackTrace, 0))
                {
                    loggingSettings.Log(LogLevel.Warning,
                                        $"Query [{builtQuery.Query}] doesn't have .Select(...) construction. It may be critical to the performance because Jira should return all fields of issues.");
                }
            }

            loggingSettings.Log(LogLevel.Debug, $"Query [{builtQuery.Query}] is starting execution");

            int got;
            int maxResults;

            do
            {
                maxResults = Math.Min(take, packetSize);

                Stopwatch partialStopwatch = null;
                if (loggingSettings.NeedLogging(LogLevel.Trace))
                {
                    partialStopwatch = Stopwatch.StartNew();
                    loggingSettings.Log(LogLevel.Trace,
                                        $"Query's part (startAt: [{skip}], maxResults: [{maxResults}], jql: [{builtQuery.Query}]) is starting execution");
                }

                var request = new JiraIssuesRequest
                {
                    Jql        = builtQuery.Query?.ToString() ?? "",
                    StartAt    = skip,
                    MaxResults = maxResults,
                    Fields     = fields
                };
                JiraIssuesResponse response;
                try
                {
                    response = jira.SelectIssues(request, builtQuery.IssueType);
                    if (partialStopwatch != null)
                    {
                        partialStopwatch.Stop();
                        loggingSettings.Log(LogLevel.Trace,
                                            $"Query's partial execution (startAt: [{skip}], maxResults: [{maxResults}], jql: [{builtQuery.Query}]) is finished. Returned [{response.Issues.Length}] issues, took [{partialStopwatch.Elapsed.TotalMilliseconds}] ms");
                    }
                }
                catch (Exception)
                {
                    if (partialStopwatch != null)
                    {
                        partialStopwatch.Stop();
                        loggingSettings.Log(LogLevel.Trace,
                                            $"Query's partial execution (startAt: [{skip}], maxResults: [{maxResults}], jql: [{builtQuery.Query}]) is finished. Threw an exception, took [{partialStopwatch.Elapsed.TotalMilliseconds}] ms");
                    }

                    stopwatch?.Stop();

                    throw;
                }

                got = response.Issues.Length;
                for (var i = 0; i < response.Issues.Length && take > 0; ++i)
                {
                    var issue = response.Issues[i];
                    if (keys.Add(issue.Key))
                    {
                        yield return(projection != null?projection(issue) : issue);

                        take--;
                    }
                }

                skip += packetSize;
            } while (take > 0 && got == maxResults);

            if (stopwatch != null)
            {
                stopwatch.Stop();
                loggingSettings.Log(LogLevel.Trace,
                                    $"Query's execution [{builtQuery.Query}] is finished. Returned [{keys.Count}] issues, took [{stopwatch.Elapsed.TotalMilliseconds}] ms");
            }
        }
예제 #11
0
        private IEnumerable ExecuteCount(BuiltQuery builtQuery, Stopwatch stopwatch)
        {
            var response = SelectIssuesInfo(builtQuery, stopwatch);

            yield return(response.Total);
        }