예제 #1
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

            if (sequence.SqlQuery.Select.TakeValue != null || sequence.SqlQuery.Select.SkipValue != null)
            {
                sequence = new SubQueryContext(sequence);
            }

            var lambda  = (LambdaExpression)methodCall.Arguments[1].Unwrap();
            var sparent = sequence.Parent;
            var order   = new ExpressionContext(buildInfo.Parent, sequence, lambda);
            var body    = lambda.Body.Unwrap();
            var sql     = builder.ConvertExpressions(order, body, ConvertFlags.Key);

            builder.ReplaceParent(order, sparent);

            if (!methodCall.Method.Name.StartsWith("Then"))
            {
                sequence.SqlQuery.OrderBy.Items.Clear();
            }

            foreach (var expr in sql)
            {
                var e = builder.ConvertSearchCondition(sequence, expr.Sql);
                sequence.SqlQuery.OrderBy.Expr(e, methodCall.Method.Name.EndsWith("Descending"));
            }

            return(sequence);
        }
예제 #2
0
		protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
		{
			var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));

			if (sequence.SqlQuery.Select.TakeValue != null || sequence.SqlQuery.Select.SkipValue != null)
				sequence = new SubQueryContext(sequence);

			var lambda  = (LambdaExpression)methodCall.Arguments[1].Unwrap();
			var sparent = sequence.Parent;
			var order   = new ExpressionContext(buildInfo.Parent, sequence, lambda);
			var body    = lambda.Body.Unwrap();
			var sql     = builder.ConvertExpressions(order, body, ConvertFlags.Key);

			builder.ReplaceParent(order, sparent);

			//if (!methodCall.Method.Name.StartsWith("Then"))
			//	sequence.SqlQuery.OrderBy.Items.Clear();

			foreach (var expr in sql)
			{
				var e = builder.ConvertSearchCondition(sequence, expr.Sql);
				sequence.SqlQuery.OrderBy.Expr(e, methodCall.Method.Name.EndsWith("Descending"));
			}

			return sequence;
		}
예제 #3
0
        internal static void BuildSetter(
            ExpressionBuilder builder,
            BuildInfo buildInfo,
            LambdaExpression setter,
            IBuildContext into,
            List <SqlQuery.SetExpression> items,
            IBuildContext sequence)
        {
            var path = Expression.Parameter(setter.Body.Type, "p");
            var ctx  = new ExpressionContext(buildInfo.Parent, sequence, setter);

            if (setter.Body.NodeType == ExpressionType.MemberInit)
            {
                var ex = (MemberInitExpression)setter.Body;
                var p  = sequence.Parent;

                BuildSetter(builder, into, items, ctx, ex, path);

                builder.ReplaceParent(ctx, p);
            }
            else
            {
                var sqlInfo = ctx.ConvertToSql(setter.Body, 0, ConvertFlags.All);

                foreach (var info in sqlInfo)
                {
                    if (info.Members.Count == 0)
                    {
                        throw new LinqException("Object initializer expected for insert statement.");
                    }

                    if (info.Members.Count != 1)
                    {
                        throw new InvalidOperationException();
                    }

                    var member = info.Members[0];
                    var pe     = Expression.MakeMemberAccess(path, member);
                    var column = into.ConvertToSql(pe, 1, ConvertFlags.Field);
                    var expr   = info.Sql;

                    if (expr is SqlParameter)
                    {
                        SetConverter(builder.MappingSchema, member, expr);
                        //var type = member.MemberType == MemberTypes.Field ?
                        //	((FieldInfo)   member).FieldType :
                        //	((PropertyInfo)member).PropertyType;

                        //if (TypeHelper.IsEnumOrNullableEnum(type))
                        //{
                        //	var memberAccessor = TypeAccessor.GetAccessor(member.DeclaringType)[member.Name];
                        //	((SqlParameter)expr).SetEnumConverter(memberAccessor, builder.MappingSchema);
                        //}
                    }

                    items.Add(new SqlQuery.SetExpression(column[0].Sql, expr));
                }
            }
        }
예제 #4
0
        internal static void BuildSetter(
            ExpressionBuilder builder,
            BuildInfo buildInfo,
            LambdaExpression setter,
            IBuildContext into,
            List <SqlQuery.SetExpression> items,
            IBuildContext sequence)
        {
            if (setter.Body.NodeType != ExpressionType.MemberInit)
            {
                throw new LinqException("Object initializer expected for insert statement.");
            }

            var ex  = (MemberInitExpression)setter.Body;
            var p   = sequence.Parent;
            var ctx = new ExpressionContext(buildInfo.Parent, sequence, setter);

            BuildSetter(builder, into, items, ctx, ex, Expression.Parameter(ex.Type, "p"));

            builder.ReplaceParent(ctx, p);
        }
예제 #5
0
        internal static void ParseSet(
            ExpressionBuilder builder,
            BuildInfo buildInfo,
            LambdaExpression extract,
            LambdaExpression update,
            IBuildContext select,
            SqlTable table,
            List <SqlQuery.SetExpression> items)
        {
            var ext = extract.Body;

            while (ext.NodeType == ExpressionType.Convert || ext.NodeType == ExpressionType.ConvertChecked)
            {
                ext = ((UnaryExpression)ext).Operand;
            }

            if (ext.NodeType != ExpressionType.MemberAccess || ext.GetRootObject() != extract.Parameters[0])
            {
                throw new LinqException("Member expression expected for the 'Set' statement.");
            }

            var body   = (MemberExpression)ext;
            var member = body.Member;

            if (member is MethodInfo)
            {
                member = TypeHelper.GetPropertyByMethod((MethodInfo)member);
            }

            var members = body.GetMembers();
            var name    = members
                          .Skip(1)
                          .Select(ex =>
            {
                var me = ex as MemberExpression;

                if (me == null)
                {
                    return(null);
                }

                var m = me.Member;

                if (m is MethodInfo)
                {
                    m = TypeHelper.GetPropertyByMethod((MethodInfo)m);
                }

                return(m);
            })
                          .Where(m => m != null && !TypeHelper.IsNullableValueMember(m))
                          .Select(m => m.Name)
                          .Aggregate((s1, s2) => s1 + "." + s2);

            if (table != null && !table.Fields.ContainsKey(name))
            {
                throw new LinqException("Member '{0}.{1}' is not a table column.", member.DeclaringType.Name, name);
            }

            var column = table != null ?
                         table.Fields[name] :
                         select.ConvertToSql(
                body, 1, ConvertFlags.Field)[0].Sql;
            //Expression.MakeMemberAccess(Expression.Parameter(member.DeclaringType, "p"), member), 1, ConvertFlags.Field)[0].Sql;
            var sp   = select.Parent;
            var ctx  = new ExpressionContext(buildInfo.Parent, select, update);
            var expr = builder.ConvertToSqlExpression(ctx, update.Body, false);

            builder.ReplaceParent(ctx, sp);

            SetConverter(builder.MappingSchema, member, expr);

            items.Add(new SqlQuery.SetExpression(column, expr));
        }
예제 #6
0
        protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
        {
            var isGroup      = methodCall.Method.Name == "GroupJoin";
            var outerContext = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0], buildInfo.SqlQuery));
            var innerContext = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[1], new SqlQuery()));
            var countContext = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[1], new SqlQuery()));

            var context = new SubQueryContext(outerContext);

            innerContext = isGroup ? new GroupJoinSubQueryContext(innerContext, methodCall) : new SubQueryContext(innerContext);
            countContext = new SubQueryContext(countContext);

            var join = isGroup ? innerContext.SqlQuery.WeakLeftJoin() : innerContext.SqlQuery.InnerJoin();
            var sql  = context.SqlQuery;

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

            var selector = (LambdaExpression)methodCall.Arguments[4].Unwrap();

            context.SetAlias(selector.Parameters[0].Name);
            innerContext.SetAlias(selector.Parameters[1].Name);

            var outerKeyLambda = ((LambdaExpression)methodCall.Arguments[2].Unwrap());
            var innerKeyLambda = ((LambdaExpression)methodCall.Arguments[3].Unwrap());

            var outerKeySelector = outerKeyLambda.Body.Unwrap();
            var innerKeySelector = innerKeyLambda.Body.Unwrap();

            var outerParent = context.Parent;
            var innerParent = innerContext.Parent;
            var countParent = countContext.Parent;

            var outerKeyContext = new ExpressionContext(buildInfo.Parent, context, outerKeyLambda);
            var innerKeyContext = new InnerKeyContext(buildInfo.Parent, innerContext, innerKeyLambda);
            var countKeyContext = new ExpressionContext(buildInfo.Parent, countContext, innerKeyLambda);

            // Process counter.
            //
            var counterSql = ((SubQueryContext)countContext).SqlQuery;

            // Make join and where for the counter.
            //
            if (outerKeySelector.NodeType == ExpressionType.New)
            {
                var new1 = (NewExpression)outerKeySelector;
                var new2 = (NewExpression)innerKeySelector;

                for (var i = 0; i < new1.Arguments.Count; i++)
                {
                    var arg1 = new1.Arguments[i];
                    var arg2 = new2.Arguments[i];

                    BuildJoin(builder, join, outerKeyContext, arg1, innerKeyContext, arg2, countKeyContext, counterSql);
                }
            }
            else if (outerKeySelector.NodeType == ExpressionType.MemberInit)
            {
                var mi1 = (MemberInitExpression)outerKeySelector;
                var mi2 = (MemberInitExpression)innerKeySelector;

                for (var i = 0; i < mi1.Bindings.Count; i++)
                {
                    if (mi1.Bindings[i].Member != mi2.Bindings[i].Member)
                    {
                        throw new LinqException(string.Format("List of member inits does not match for entity type '{0}'.", outerKeySelector.Type));
                    }

                    var arg1 = ((MemberAssignment)mi1.Bindings[i]).Expression;
                    var arg2 = ((MemberAssignment)mi2.Bindings[i]).Expression;

                    BuildJoin(builder, join, outerKeyContext, arg1, innerKeyContext, arg2, countKeyContext, counterSql);
                }
            }
            else
            {
                BuildJoin(builder, join, outerKeyContext, outerKeySelector, innerKeyContext, innerKeySelector, countKeyContext, counterSql);
            }

            builder.ReplaceParent(outerKeyContext, outerParent);
            builder.ReplaceParent(innerKeyContext, innerParent);
            builder.ReplaceParent(countKeyContext, countParent);

            if (isGroup)
            {
                counterSql.ParentSql = sql;
                counterSql.Select.Columns.Clear();

                var inner = (GroupJoinSubQueryContext)innerContext;

                inner.Join       = join.JoinedTable;
                inner.CounterSql = counterSql;
                return(new GroupJoinContext(
                           buildInfo.Parent, selector, context, inner, methodCall.Arguments[1], outerKeyLambda, innerKeyLambda));
            }

            return(new JoinContext(buildInfo.Parent, selector, context, innerContext)
#if DEBUG
            {
                MethodCall = methodCall
            }
#endif
                   );
        }