Ejemplo n.º 1
0
        private static bool CheckShortCircuitSum(SubQueryExpression expression, MainQueryParts queryParts, Action<Expression> visitExpression)
        {
            var subquery = SubqueryGeneratorQueryModelVisitor.ParseSubquery(expression.QueryModel, queryParts, true);
            if (subquery.ShouldQueryInMemory)
            {
                throw new NotImplementedException("Unsupported subquery. Please provide more info about query.");
                //return false;
            }

            var sql = subquery.BuildSqlString(false);
            var cnt = queryParts.CurrentSelectIndex;

            var selector = expression.QueryModel.SelectClause.Selector as MemberExpression;
            if (selector == null)
                return false;

            var type = expression.QueryModel.ResultTypeOverride;
            if (type.IsNullable())
                type = type.GetGenericArguments()[0];

            queryParts.AddSelectPart(
                expression.QueryModel.MainFromClause,
                @"(SELECT SUM((""{1}"").""{3}"") FROM ({2}) ""{1}"") AS ""{0}""".With(
                    "_subquery_" + cnt,
                    expression.QueryModel.MainFromClause.ItemName,
                    sql,
                    selector.Member.Name),
                "_sum_" + cnt,
                expression.QueryModel.ResultTypeOverride,
                (_, dr) => dr.IsDBNull(cnt) ? 0 : Convert.ChangeType(dr.GetValue(cnt), type));
            return true;
        }
Ejemplo n.º 2
0
        private static bool CheckShortCircuitCount(SubQueryExpression expression, MainQueryParts queryParts, Action<Expression> visitExpression)
        {
            var subquery = SubqueryGeneratorQueryModelVisitor.ParseSubquery(expression.QueryModel, queryParts, true);
            if (subquery.ShouldQueryInMemory)
            {
                throw new NotImplementedException("Unsupported subquery. Please provide more info about query.");
                //return false;
            }

            var cnt = queryParts.CurrentSelectIndex;

            var mq = subquery.MainFrom.FromExpression as QuerySourceReferenceExpression;
            if (mq != null && subquery.Joins.Count == 0 && subquery.AdditionalJoins.Count == 0 && subquery.Conditions.Count == 0)
            {
                if (mq.ReferencedQuerySource.ItemType.IsGrouping())
                {
                    queryParts.AddSelectPart(
                        expression.QueryModel.MainFromClause,
                        "ARRAY_UPPER(\"{0}\".\"Values\", 1) AS \"_count_helper_{1}\"".With(mq.ReferencedQuerySource.ItemName, cnt),
                        "_count_helper_" + cnt,
                        expression.QueryModel.ResultTypeOverride,
                        (_, dr) => dr.IsDBNull(cnt) ? 0 : Convert.ChangeType(dr.GetValue(cnt), expression.QueryModel.ResultTypeOverride));
                    return true;
                }
            }

            var sql = subquery.BuildSqlString(false);

            queryParts.AddSelectPart(
                expression.QueryModel.MainFromClause,
                @"(SELECT COUNT(""{1}"") FROM ({2}) ""{1}"") AS ""{0}"" ".With(
                    "_subquery_" + cnt,
                    expression.QueryModel.MainFromClause.ItemName,
                    sql),
                "_count_" + cnt,
                expression.QueryModel.ResultTypeOverride,
                (_, dr) => dr.IsDBNull(cnt) ? 0 : Convert.ChangeType(dr.GetValue(cnt), expression.QueryModel.ResultTypeOverride));
            return true;
        }