Ejemplo n.º 1
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)
                break;

            case 2:                      // int Update<T>(this IQueryable<T> source, Expression<Func<T,T>> setter)
            {
                BuildSetter(
                    builder, buildInfo, (LambdaExpression)methodCall.Arguments[1].Unwrap(), sequence, sequence);
                break;
            }

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

                if (expr is LambdaExpression)
                {
                    // 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);
                }
                else
                {
                    // static int Update<TSource,TTarget>(this IQueryable<TSource> source, Table<TTarget> target, Expression<Func<TSource,TTarget>> setter)
                    //
                    var into = builder.BuildSequence(new BuildInfo(buildInfo, expr, new SqlQuery()));

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

                    var sql = sequence.SqlQuery;

                    sql.Select.Columns.Clear();

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

                    sql.Set.Into = ((TableBuilder.TableContext)into).SqlTable;
                }

                break;
            }
            }

            sequence.SqlQuery.QueryType = QueryType.Update;

            return(new UpdateContext(buildInfo.Parent, sequence));
        }
Ejemplo n.º 2
0
		protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
		{
			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, true);

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

			return result;
		}
Ejemplo n.º 3
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var sequence  = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));
            var condition = (LambdaExpression)ExpressionBuilder.GetPredicate(methodCall.Arguments[1]);
            var result    = builder.BuildWhere(buildInfo.Parent, sequence, condition, true);

            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.SqlQuery.QueryType = QueryType.Delete;

            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]));

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

            sequence.SqlQuery.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.SqlQuery.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.SqlQuery.From.Tables.Count == 0 || sequence.SqlQuery.From.Tables[0].Source != tc.SqlQuery)
                        {
                            sequence.SqlQuery.Delete.Table = tc.SqlTable;
                        }
                    }
                }
            }

            return(new DeleteContext(buildInfo.Parent, sequence));
        }
Ejemplo n.º 6
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)
            {
                var condition = (LambdaExpression)methodCall.Arguments[1].Unwrap();

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

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

            return(new AllAnyContext(buildInfo.Parent, methodCall, sequence));
        }