Ejemplo n.º 1
0
            ISqlExpression ConvertEnumerable(MethodCallExpression call)
            {
                if (AggregationBuilder.MethodNames.Contains(call.Method.Name))
                {
                    if (call.Arguments[0].NodeType == ExpressionType.Call)
                    {
                        var arg = (MethodCallExpression)call.Arguments[0];

                        if (arg.Method.Name == "Select")
                        {
                            if (arg.Arguments[0].NodeType != ExpressionType.Call)
                            {
                                var l     = (LambdaExpression)arg.Arguments[1].Unwrap();
                                var largs = l.Type.GetGenericArguments();

                                if (largs.Length == 2)
                                {
                                    var p   = _element.Parent;
                                    var ctx = new ExpressionContext(Parent, _element, l);
                                    var sql = Builder.ConvertToSql(ctx, l.Body, true);

                                    Builder.ReplaceParent(ctx, p);

                                    return(new SqlFunction(call.Type, call.Method.Name, sql));
                                }
                            }
                        }
                    }
                }

                if (call.Arguments[0].NodeType == ExpressionType.Call)
                {
                    var ctx = Builder.GetSubQuery(this, call);

                    if (Builder.SqlProvider.IsSubQueryColumnSupported)
                    {
                        return(ctx.SqlQuery);
                    }

                    var join = ctx.SqlQuery.CrossApply();

                    SqlQuery.From.Tables[0].Joins.Add(join.JoinedTable);

                    return(ctx.SqlQuery.Select.Columns[0]);
                }

                var args = new ISqlExpression[call.Arguments.Count - 1];

                if (CountBuilder.MethodNames.Contains(call.Method.Name))
                {
                    if (args.Length > 0)
                    {
                        throw new InvalidOperationException();
                    }

                    return(SqlFunction.CreateCount(call.Type, SqlQuery));
                }

                if (call.Arguments.Count > 1)
                {
                    for (var i = 1; i < call.Arguments.Count; i++)
                    {
                        var ex = call.Arguments[i].Unwrap();

                        if (ex is LambdaExpression)
                        {
                            var l   = (LambdaExpression)ex;
                            var p   = _element.Parent;
                            var ctx = new ExpressionContext(Parent, _element, l);

                            args[i - 1] = Builder.ConvertToSql(ctx, l.Body, true);

                            Builder.ReplaceParent(ctx, p);
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
                else
                {
                    args = _element.ConvertToSql(null, 0, ConvertFlags.Field).Select(_ => _.Sql).ToArray();
                }

                return(new SqlFunction(call.Type, call.Method.Name, args));
            }
Ejemplo n.º 2
0
            ISqlExpression ConvertEnumerable(MethodCallExpression call)
            {
                var expr = call;


                //	if (AggregationBuilder.MethodNames.Contains(expr.Method.Name))
                //		while (arg.NodeType == ExpressionType.Call && ((MethodCallExpression)arg).Method.Name == "Select")
                //			arg = ((MethodCallExpression)arg).Arguments[0];


                if (call.Arguments[0].NodeType == ExpressionType.Call)
                {
                    var ctx = Builder.GetSubQuery(this, call);

                    if (Builder.SqlProvider.IsSubQueryColumnSupported)
                    {
                        return(ctx.SqlQuery);
                    }

                    var join = ctx.SqlQuery.CrossApply();

                    SqlQuery.From.Tables[0].Joins.Add(join.JoinedTable);

                    return(ctx.SqlQuery.Select.Columns[0]);
                }

                var args = new ISqlExpression[call.Arguments.Count - 1];

                if (CountBuilder.MethodNames.Contains(call.Method.Name))
                {
                    if (args.Length > 0)
                    {
                        throw new InvalidOperationException();

                        /*
                         * var ctx = _element;
                         * var l   = (LambdaExpression)call.Arguments[1].Unwrap();
                         * var cnt = Builder.BuildWhere(Parent, ctx, l, false);
                         * var sql = cnt.SqlQuery.Clone((_ => !(_ is SqlParameter)));
                         *
                         * sql.ParentSql = SqlQuery;
                         * sql.Select.Columns.Clear();
                         *
                         * if (ctx == cnt)
                         *      ctx.SqlQuery.Where.SearchCondition.Conditions.RemoveAt(ctx.SqlQuery.Where.SearchCondition.Conditions.Count - 1);
                         *
                         * if (Builder.SqlProvider.IsSubQueryColumnSupported && Builder.SqlProvider.IsCountSubQuerySupported)
                         * {
                         *      for (var i = 0; i < sql.GroupBy.Items.Count; i++)
                         *      {
                         *              var item1 = sql.GroupBy.Items[i];
                         *              var item2 = SqlQuery.GroupBy.Items[i];
                         *              var pr    = Builder.Convert(this, new SqlQuery.Predicate.ExprExpr(item1, SqlQuery.Predicate.Operator.Equal, item2));
                         *
                         *              sql.Where.SearchCondition.Conditions.Add(new SqlQuery.Condition(false, pr));
                         *      }
                         *
                         *      sql.GroupBy.Items.Clear();
                         *      sql.Select.Expr(SqlFunction.CreateCount(call.Type, sql));
                         *
                         *      return sql;
                         * }
                         *
                         * var join = sql.WeakLeftJoin();
                         *
                         * SqlQuery.From.Tables[0].Joins.Add(join.JoinedTable);
                         *
                         * for (var i = 0; i < sql.GroupBy.Items.Count; i++)
                         * {
                         *      var item1 = sql.GroupBy.Items[i];
                         *      var item2 = SqlQuery.GroupBy.Items[i];
                         *      var col   = sql.Select.Columns[sql.Select.Add(item1)];
                         *      var pr    = Builder.Convert(this, new SqlQuery.Predicate.ExprExpr(col, SqlQuery.Predicate.Operator.Equal, item2));
                         *
                         *      join.JoinedTable.Condition.Conditions.Add(new SqlQuery.Condition(false, pr));
                         * }
                         *
                         * return new SqlFunction(call.Type, "Count", sql.Select.Columns[0]);
                         */
                    }

                    return(SqlFunction.CreateCount(call.Type, SqlQuery));
                }

                if (call.Arguments.Count > 1)
                {
                    for (var i = 1; i < call.Arguments.Count; i++)
                    {
                        var ex = call.Arguments[i].Unwrap();

                        if (ex is LambdaExpression)
                        {
                            var l   = (LambdaExpression)ex;
                            var p   = _element.Parent;
                            var ctx = new ExpressionContext(Parent, _element, l);

                            _element.Parent = p;

                            args[i - 1] = Builder.ConvertToSql(ctx, l.Body.Unwrap());
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                }
                else
                {
                    args = _element.ConvertToSql(null, 0, ConvertFlags.Field).Select(_ => _.Sql).ToArray();
                }

                return(new SqlFunction(call.Type, call.Method.Name, args));
            }