Ejemplo n.º 1
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var isHaving = methodCall.Method.Name == "Having";
            var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            if (methodCall.Arguments[1].Type.Name.Equals("String"))
            {
                return(builder.BuildWhere(buildInfo.Parent, sequence, methodCall.Arguments[1].ToString(), !isHaving, isHaving, methodCall.Arguments.Count > 2? (ConstantExpression)methodCall.Arguments[2]:null));
            }
            var condition = (LambdaExpression)methodCall.Arguments[1].Unwrap();
            var result    = builder.BuildWhere(buildInfo.Parent, sequence, condition, !isHaving, isHaving);

            result.SetAlias(condition.Parameters[0].Name);

            return(result);
        }
Ejemplo n.º 2
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0])
            {
                CopyTable = true
            });

            if (methodCall.Arguments.Count == 2)
            {
                if (sequence.SelectQuery.Select.TakeValue != null ||
                    sequence.SelectQuery.Select.SkipValue != null)
                {
                    sequence = new SubQueryContext(sequence);
                }
                var condition = (LambdaExpression)methodCall.Arguments[1].Unwrap();

                if (methodCall.Method.Name == "All")
                {
                    condition = Expression.Lambda(Expression.Not(condition.Body), condition.Name, condition.Parameters);
                }

                sequence = builder.BuildWhere(buildInfo.Parent, sequence, condition, true);
                sequence.SetAlias(condition.Parameters[0].Name);
            }

            return(new AllAnyContext(buildInfo.Parent, methodCall, sequence));
        }
Ejemplo n.º 3
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var isHaving  = methodCall.Method.Name == "Having";
            var sequence  = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));
            var condition = (LambdaExpression)methodCall.Arguments[1].Unwrap();
            var result    = builder.BuildWhere(buildInfo.Parent, sequence, condition, !isHaving, isHaving);

            result.SetAlias(condition.Parameters[0].Name);

            return(result);
        }
Ejemplo n.º 4
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            if (methodCall.Arguments.Count == 2)
            {
                sequence = builder.BuildWhere(buildInfo.Parent, sequence, (LambdaExpression)methodCall.Arguments[1].Unwrap(), false);
            }

            sequence.SelectQuery.QueryType = QueryType.Delete;

            // Check association.
            //
            var ctx = sequence as SelectContext;

            if (ctx != null && ctx.IsScalar)
            {
                var res = ctx.IsExpression(null, 0, RequestFor.Association);

                if (res.Result && res.Context is TableBuilder.AssociatedTableContext)
                {
                    var atc = (TableBuilder.AssociatedTableContext)res.Context;
                    sequence.SelectQuery.Delete.Table = atc.SqlTable;
                }
                else
                {
                    res = ctx.IsExpression(null, 0, RequestFor.Table);

                    if (res.Result && res.Context is TableBuilder.TableContext)
                    {
                        var tc = (TableBuilder.TableContext)res.Context;

                        if (sequence.SelectQuery.From.Tables.Count == 0 || sequence.SelectQuery.From.Tables[0].Source != tc.SelectQuery)
                        {
                            sequence.SelectQuery.Delete.Table = tc.SqlTable;
                        }
                    }
                }
            }

            return(new DeleteContext(buildInfo.Parent, sequence));
        }
Ejemplo n.º 5
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            switch (methodCall.Arguments.Count)
            {
            case 1:                      // int Update<T>(this IUpdateable<T> source)
                CheckAssociation(sequence);
                break;

            case 2:                      // int Update<T>(this IQueryable<T> source, Expression<Func<T,T>> setter)
            {
                CheckAssociation(sequence);

                BuildSetter(
                    builder,
                    buildInfo,
                    (LambdaExpression)methodCall.Arguments[1].Unwrap(),
                    sequence,
                    sequence.SelectQuery.Update.Items,
                    sequence);
                break;
            }

            case 3:
            {
                var expr = methodCall.Arguments[1].Unwrap();

                if (expr is LambdaExpression && ((LambdaExpression)expr).ReturnType == typeof(bool))
                {
                    CheckAssociation(sequence);

                    // int Update<T>(this IQueryable<T> source, Expression<Func<T,bool>> predicate, Expression<Func<T,T>> setter)
                    //
                    sequence = builder.BuildWhere(buildInfo.Parent, sequence, (LambdaExpression)methodCall.Arguments[1].Unwrap(), false);

                    BuildSetter(
                        builder,
                        buildInfo,
                        (LambdaExpression)methodCall.Arguments[2].Unwrap(),
                        sequence,
                        sequence.SelectQuery.Update.Items,
                        sequence);
                }
                else
                {
                    IBuildContext into;

                    if (expr is LambdaExpression)
                    {
                        // static int Update<TSource,TTarget>(this IQueryable<TSource> source, Expression<Func<TSource,TTarget>> target, Expression<Func<TSource,TTarget>> setter)
                        //
                        var body      = ((LambdaExpression)expr).Body;
                        int level     = body.GetLevel();
                        var tableInfo = sequence.IsExpression(body, level, RequestFor.Table);

                        if (tableInfo.Result == false)
                        {
                            throw new LinqException("Expression '{0}' mast be a table.");
                        }

                        into = tableInfo.Context;
                    }
                    else
                    {
                        // static int Update<TSource,TTarget>(this IQueryable<TSource> source, Table<TTarget> target, Expression<Func<TSource,TTarget>> setter)
                        //
                        into = builder.BuildSequence(new BuildInfo(buildInfo, expr, new SelectQuery()));
                    }

                    sequence.ConvertToIndex(null, 0, ConvertFlags.All);
                    new SelectQueryOptimizer(builder.DataContextInfo.SqlProviderFlags, sequence.SelectQuery)
                    .ResolveWeakJoins(new List <ISqlTableSource>());
                    sequence.SelectQuery.Select.Columns.Clear();

                    BuildSetter(
                        builder,
                        buildInfo,
                        (LambdaExpression)methodCall.Arguments[2].Unwrap(),
                        into,
                        sequence.SelectQuery.Update.Items,
                        sequence);

                    var sql = sequence.SelectQuery;

                    sql.Select.Columns.Clear();

                    foreach (var item in sql.Update.Items)
                    {
                        sql.Select.Columns.Add(new SelectQuery.Column(sql, item.Expression));
                    }

                    sql.Update.Table = ((TableBuilder.TableContext)into).SqlTable;
                }

                break;
            }
            }

            sequence.SelectQuery.QueryType = QueryType.Update;

            return(new UpdateContext(buildInfo.Parent, sequence));
        }