Example #1
0
        public override Expression GetUpdateExpression(MappingEntity entity, Expression instance, LambdaExpression updateCheck, LambdaExpression selector, Expression @else)
        {
            TableAlias      alias       = new TableAlias();
            TableExpression root        = new TableExpression(alias, entity, this.mapping.GetTableName(entity));
            Expression      expression2 = this.GetIdentityCheck(root, entity, instance);

            if (updateCheck != null)
            {
                Expression entityExpression = this.GetEntityExpression(root, entity);
                Expression expression4      = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], entityExpression);
                expression2 = expression2.And(expression4);
            }
            IEnumerable <ColumnAssignment> assignments = this.GetColumnAssignments(root, instance, entity, (e, m) => this.mapping.IsUpdatable(e, m));
            Expression command = new UpdateCommand(root, expression2, assignments);

            if (selector != null)
            {
                return(new BlockCommand(new Expression[] { command, new IFCommand(this.translator.Linguist.Language.GetRowsAffectedExpression(command).GreaterThan(Expression.Constant(0)), this.GetUpdateResult(entity, instance, selector), @else) }));
            }
            if (@else != null)
            {
                return(new BlockCommand(new Expression[] { command, new IFCommand(this.translator.Linguist.Language.GetRowsAffectedExpression(command).LessThanOrEqual(Expression.Constant(0)), @else, null) }));
            }
            return(command);
        }
        protected override Expression VisitClientJoin(ClientJoinExpression join)
        {
            Expression           expression = this.MakeJoinKey(join.InnerKey as IList <Expression>);
            Expression           exp        = this.MakeJoinKey(join.OuterKey as IList <Expression>);
            Expression           projector  = Expression.New(typeof(KeyValuePair <,>).MakeGenericType(new Type[] { expression.Type, join.Projection.Projector.Type }).GetConstructor(new Type[] { expression.Type, join.Projection.Projector.Type }), new Expression[] { expression, join.Projection.Projector });
            ProjectionExpression projection = new ProjectionExpression(join.Projection.Select, projector);
            int                 num         = ++this.nLookup;
            Expression          expression5 = this.ExecuteProjection(projection, false);
            ParameterExpression expression6 = Expression.Parameter(projector.Type, "kvp");

            if (join.Projection.Projector.NodeType == ((ExpressionType)0x3f9))
            {
                LambdaExpression expression7 = Expression.Lambda(Expression.PropertyOrField(expression6, "Value").NotEqual(King.Framework.Linq.TypeHelper.GetNullConstant(join.Projection.Projector.Type)), new ParameterExpression[] { expression6 });
                expression5 = Expression.Call(typeof(Enumerable), "Where", new Type[] { expression6.Type }, new Expression[] { expression5, expression7 });
            }
            LambdaExpression    expression8 = Expression.Lambda(Expression.PropertyOrField(expression6, "Key"), new ParameterExpression[] { expression6 });
            LambdaExpression    expression9 = Expression.Lambda(Expression.PropertyOrField(expression6, "Value"), new ParameterExpression[] { expression6 });
            Expression          item        = Expression.Call(typeof(Enumerable), "ToLookup", new Type[] { expression6.Type, exp.Type, join.Projection.Projector.Type }, new Expression[] { expression5, expression8, expression9 });
            ParameterExpression instance    = Expression.Parameter(item.Type, "lookup" + num);
            Expression          replaceWith = Expression.Call(instance, instance.Type.GetProperty("Item").GetGetMethod(), new Expression[] { this.Visit(exp) });

            if (join.Projection.Aggregator != null)
            {
                replaceWith = DbExpressionReplacer.Replace(join.Projection.Aggregator.Body, join.Projection.Aggregator.Parameters[0], replaceWith);
            }
            this.variables.Add(instance);
            this.initializers.Add(item);
            return(replaceWith);
        }
Example #3
0
        protected virtual Expression GetInsertResult(MappingEntity entity, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map)
        {
            Expression expression2;
            Func <MemberInfo, bool>            func  = null;
            Func <MemberInfo, int, Expression> func2 = null;
            TableAlias         alias      = new TableAlias();
            TableExpression    tex        = new TableExpression(alias, entity, this.mapping.GetTableName(entity));
            LambdaExpression   aggregator = Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(new Type[] { selector.Body.Type }));
            DeclarationCommand command    = null;
            List <MemberInfo>  source     = (from m in this.mapping.GetMappedMembers(entity)
                                             where this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)
                                             select m).ToList <MemberInfo>();

            if (source.Count > 0)
            {
                if (map != null)
                {
                }
                if (!((func == null) && source.Any <MemberInfo>((func = m => map.ContainsKey(m)))))
                {
                    Dictionary <MemberInfo, Expression> dictionary = new Dictionary <MemberInfo, Expression>();
                    command = this.GetGeneratedIdCommand(entity, source.ToList <MemberInfo>(), dictionary);
                    map     = dictionary;
                }
                MemberExpression body = selector.Body as MemberExpression;
                if (((body != null) && this.mapping.IsPrimaryKey(entity, body.Member)) && this.mapping.IsGenerated(entity, body.Member))
                {
                    if (command != null)
                    {
                        return(new ProjectionExpression(command.Source, new ColumnExpression(body.Type, command.Variables[0].QueryType, command.Source.Alias, command.Source.Columns[0].Name), aggregator));
                    }
                    TableAlias alias2     = new TableAlias();
                    QueryType  columnType = this.GetColumnType(entity, body.Member);
                    return(new ProjectionExpression(new SelectExpression(alias2, new ColumnDeclaration[] { new ColumnDeclaration("", map[body.Member], columnType) }, null, null), new ColumnExpression(King.Framework.Linq.TypeHelper.GetMemberType(body.Member), columnType, alias2, ""), aggregator));
                }
                if (func2 == null)
                {
                    func2 = (m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m]);
                }
                expression2 = source.Select <MemberInfo, Expression>(func2).Aggregate <Expression>((x, y) => x.And(y));
            }
            else
            {
                expression2 = this.GetIdentityCheck(tex, entity, instance);
            }
            Expression           entityExpression = this.GetEntityExpression(tex, entity);
            Expression           expression       = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], entityExpression);
            TableAlias           newAlias         = new TableAlias();
            ProjectedColumns     columns          = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, expression, null, newAlias, new TableAlias[] { alias });
            ProjectionExpression expression6      = new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, tex, expression2), columns.Projector, aggregator);

            if (command != null)
            {
                return(new BlockCommand(new Expression[] { command, expression6 }));
            }
            return(expression6);
        }
Example #4
0
        protected virtual Expression GetEntityStateTest(MappingEntity entity, Expression instance, LambdaExpression updateCheck)
        {
            ProjectionExpression queryExpression = this.GetQueryExpression(entity);
            Expression           expression2     = this.GetIdentityCheck(queryExpression.Select, entity, instance);
            Expression           expression3     = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], queryExpression.Projector);

            expression2 = expression2.And(expression3);
            return(new ExistsExpression(new SelectExpression(new TableAlias(), null, queryExpression.Select, expression2)));
        }
Example #5
0
        protected virtual Expression GetUpdateResult(MappingEntity entity, Expression instance, LambdaExpression selector)
        {
            ProjectionExpression queryExpression = this.GetQueryExpression(entity);

            Expression where = this.GetIdentityCheck(queryExpression.Select, entity, instance);
            Expression       expression = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], queryExpression.Projector);
            TableAlias       newAlias   = new TableAlias();
            ProjectedColumns columns    = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, expression, null, newAlias, new TableAlias[] { queryExpression.Select.Alias });

            return(new ProjectionExpression(new SelectExpression(newAlias, columns.Columns, queryExpression.Select, where), columns.Projector, Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(new Type[] { selector.Body.Type }))));
        }
Example #6
0
        public override Expression GetDeleteExpression(MappingEntity entity, Expression instance, LambdaExpression deleteCheck)
        {
            TableExpression root        = new TableExpression(new TableAlias(), entity, this.mapping.GetTableName(entity));
            Expression      expression2 = null;

            if (instance != null)
            {
                expression2 = this.GetIdentityCheck(root, entity, instance);
            }
            if (deleteCheck != null)
            {
                Expression entityExpression = this.GetEntityExpression(root, entity);
                Expression expression4      = DbExpressionReplacer.Replace(deleteCheck.Body, deleteCheck.Parameters[0], entityExpression);
                expression2 = (expression2 != null) ? expression2.And(expression4) : expression4;
            }
            return(new DeleteCommand(root, expression2));
        }
        private Expression ExecuteProjection(ProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values)
        {
            okayToDefer &= (this.receivingMember != null) && this.policy.IsDeferLoaded(this.receivingMember);
            Scope scope = this.scope;
            ParameterExpression fieldReader = Expression.Parameter(typeof(FieldReader), "r" + this.nReaders++);

            this.scope = new Scope(this.scope, fieldReader, projection.Select.Alias, projection.Select.Columns);
            LambdaExpression expression2 = Expression.Lambda(this.Visit(projection.Projector), new ParameterExpression[] { fieldReader });

            this.scope = scope;
            MappingEntity entity      = EntityFinder.Find(projection.Projector);
            string        methodName  = okayToDefer ? "ExecuteDeferred" : "Execute";
            Expression    replaceWith = Expression.Call(this.executor, methodName, new Type[] { expression2.Body.Type }, new Expression[] { Expression.Constant(command), expression2, Expression.Constant(entity, typeof(MappingEntity)), Expression.NewArrayInit(typeof(object), values) });

            if (projection.Aggregator != null)
            {
                replaceWith = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], replaceWith);
            }
            return(replaceWith);
        }