private Expression ExecuteProjection(DbProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values, bool isTopLevel) { okayToDefer &= this.receivingMember != null && this.policy.IsDeferLoaded(this.receivingMember); var saveScope = this.scope; var reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++); this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns); var projector = Expression.Lambda(this.Visit(projection.Projector), reader); this.scope = saveScope; var entity = EntityFinder.Find(projection.Projector); var methExecute = okayToDefer ? nameof(QueryExecutor.ExecuteDeferred) : nameof(QueryExecutor.Execute); var result = Expression.Call(this.executor, methExecute, new Type[] { projector.Body.Type }, Expression.Constant(command), projector, Expression.Constant(entity, typeof(MappingEntity)), Expression.NewArrayInit(typeof(object), values)) as Expression; if (projection.Aggregator != null) { result = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], result); } return(result); }
private Expression ExecuteProjection(ProjectionExpression projection, bool okayToDefer, QueryCommand command, Expression[] values) { okayToDefer &= (this.receivingMember != null && this.policy.IsDeferLoaded(this.receivingMember)); var saveScope = this.scope; ParameterExpression reader = Expression.Parameter(typeof(FieldReader), "r" + nReaders++); this.scope = new Scope(this.scope, reader, projection.Select.Alias, projection.Select.Columns); LambdaExpression projector = Expression.Lambda(this.Visit(projection.Projector), reader); this.scope = saveScope; var entity = EntityFinder.Find(projection.Projector); string methExecute = okayToDefer ? "ExecuteDeferred" : "Execute"; // call low-level execute directly on supplied DbQueryProvider Expression result = Expression.Call(this.executor, methExecute, new Type[] { projector.Body.Type }, Expression.Constant(command), projector, Expression.Constant(entity, typeof(MappingEntity)), Expression.NewArrayInit(typeof(object), values) ); if (projection.Aggregator != null) { // apply aggregator result = DbExpressionReplacer.Replace(projection.Aggregator.Body, projection.Aggregator.Parameters[0], result); } return(result); }
protected virtual Expression GetEntityStateTest(MappingEntity entity, Expression instance, LambdaExpression updateCheck) { ProjectionExpression tq = this.GetQueryExpression(entity); Expression where = this.GetIdentityCheck(tq.Select, entity, instance); Expression check = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], tq.Projector); where = where.And(check); return(new ExistsExpression(new SelectExpression(new TableAlias(), null, tq.Select, where))); }
public override Expression GetUpdateExpression(MappingEntity entity, Expression instance, LambdaExpression updateCheck, LambdaExpression selector, Expression exp) { var tableAlias = new TableAlias(); var table = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(entity)); var where = this.GetIdentityCheck(table, entity, instance); if (updateCheck != null) { var typeProjector = this.GetEntityExpression(table, entity); var pred = DbExpressionReplacer.Replace(updateCheck.Body, updateCheck.Parameters[0], typeProjector); if (pred != null) { where = where.And(pred); } } var assignments = this.GetColumnAssignments(table, instance, entity, (e, m) => this.mapping.IsUpdatable(e, m)); var update = new DbUpdateCommand(table, where, assignments); if (selector != null) { return(new DbBlockCommand ( update, new DbIFCommand ( this.translator.Linguist.Language.GetRowsAffectedExpression(update).GreaterThan(Expression.Constant(0)), this.GetUpdateResult(entity, instance, selector), exp ) )); } else if (exp != null) { return(new DbBlockCommand ( update, new DbIFCommand ( this.translator.Linguist.Language.GetRowsAffectedExpression(update).LessThanOrEqual(Expression.Constant(0)), exp, null ) )); } else { return(update); } }
protected virtual Expression GetUpdateResult(MappingEntity entity, Expression instance, LambdaExpression selector) { var tq = this.GetQueryExpression(entity); Expression where = this.GetIdentityCheck(tq.Select, entity, instance); Expression selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], tq.Projector); TableAlias newAlias = new TableAlias(); var pc = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, selection, null, newAlias, tq.Select.Alias); return(new ProjectionExpression( new SelectExpression(newAlias, pc.Columns, tq.Select, where), pc.Projector, Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type)) )); }
protected override Expression VisitClientJoin(ClientJoinExpression join) { // convert client join into a up-front lookup table builder & replace client-join in tree with lookup accessor // 1) lookup = query.Select(e => new KVP(key: inner, value: e)).ToLookup(kvp => kvp.Key, kvp => kvp.Value) Expression innerKey = MakeJoinKey(join.InnerKey); Expression outerKey = MakeJoinKey(join.OuterKey); ConstructorInfo kvpConstructor = typeof(KeyValuePair <,>).MakeGenericType(innerKey.Type, join.Projection.Projector.Type).GetConstructor(new Type[] { innerKey.Type, join.Projection.Projector.Type }); Expression constructKVPair = Expression.New(kvpConstructor, innerKey, join.Projection.Projector); ProjectionExpression newProjection = new ProjectionExpression(join.Projection.Select, constructKVPair); int iLookup = ++nLookup; Expression execution = this.ExecuteProjection(newProjection, false); ParameterExpression kvp = Expression.Parameter(constructKVPair.Type, "kvp"); // filter out nulls if (join.Projection.Projector.NodeType == (ExpressionType)DbExpressionType.OuterJoined) { LambdaExpression pred = Expression.Lambda( Expression.PropertyOrField(kvp, "Value").NotEqual(TypeHelper.GetNullConstant(join.Projection.Projector.Type)), kvp ); execution = Expression.Call(typeof(Enumerable), "Where", new Type[] { kvp.Type }, execution, pred); } // make lookup LambdaExpression keySelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Key"), kvp); LambdaExpression elementSelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Value"), kvp); Expression toLookup = Expression.Call(typeof(Enumerable), "ToLookup", new Type[] { kvp.Type, outerKey.Type, join.Projection.Projector.Type }, execution, keySelector, elementSelector); // 2) agg(lookup[outer]) ParameterExpression lookup = Expression.Parameter(toLookup.Type, "lookup" + iLookup); PropertyInfo property = lookup.Type.GetProperty("Item"); Expression access = Expression.Call(lookup, property.GetGetMethod(), this.Visit(outerKey)); if (join.Projection.Aggregator != null) { // apply aggregator access = DbExpressionReplacer.Replace(join.Projection.Aggregator.Body, join.Projection.Aggregator.Parameters[0], access); } this.variables.Add(lookup); this.initializers.Add(toLookup); return(access); }
protected override Expression VisitClientJoin(DbClientJoinExpression join) { var innerKey = MakeJoinKey(join.InnerKey); var outerKey = MakeJoinKey(join.OuterKey); var kvpConstructor = typeof(KeyValuePair <,>).MakeGenericType(innerKey.Type, join.Projection.Projector.Type).GetConstructor(new Type[] { innerKey.Type, join.Projection.Projector.Type }); var constructKVPair = Expression.New(kvpConstructor, innerKey, join.Projection.Projector); var newProjection = new DbProjectionExpression(join.Projection.Select, constructKVPair); var iLookup = ++nLookup; var execution = this.ExecuteProjection(newProjection, okayToDefer: false, isTopLevel: false); var kvp = Expression.Parameter(constructKVPair.Type, "kvp"); if (join.Projection.Projector.NodeType == (ExpressionType)DbExpressionType.OuterJoined) { var pred = Expression.Lambda ( Expression.PropertyOrField(kvp, "Value").NotEqual(TypeHelper.GetNullConstant(join.Projection.Projector.Type)), kvp ); execution = Expression.Call(typeof(Enumerable), nameof(Enumerable.Where), new Type[] { kvp.Type }, execution, pred); } var keySelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Key"), kvp); var elementSelector = Expression.Lambda(Expression.PropertyOrField(kvp, "Value"), kvp); var toLookup = Expression.Call(typeof(Enumerable), nameof(Enumerable.ToLookup), new Type[] { kvp.Type, outerKey.Type, join.Projection.Projector.Type }, execution, keySelector, elementSelector); var lookup = Expression.Parameter(toLookup.Type, "lookup" + iLookup); var property = lookup.Type.GetProperty("Item"); var access = Expression.Call(lookup, property.GetGetMethod(), this.Visit(outerKey)) as Expression; if (join.Projection.Aggregator != null) { access = DbExpressionReplacer.Replace(join.Projection.Aggregator.Body, join.Projection.Aggregator.Parameters[0], access); } this.variables.Add(lookup); this.initializers.Add(toLookup); return(access); }
public override Expression GetDeleteExpression(MappingEntity entity, Expression instance, LambdaExpression deleteCheck) { TableExpression table = new TableExpression(new TableAlias(), entity, this.mapping.GetTableName(entity)); Expression where = null; if (instance != null) { where = this.GetIdentityCheck(table, entity, instance); } if (deleteCheck != null) { Expression row = this.GetEntityExpression(table, entity); Expression pred = DbExpressionReplacer.Replace(deleteCheck.Body, deleteCheck.Parameters[0], row); where = (where != null) ? where.And(pred) : pred; } return(new DeleteCommand(table, where)); }
protected virtual Expression GetInsertResult(MappingEntity entity, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map) { var tableAlias = new TableAlias(); var tex = new TableExpression(tableAlias, entity, this.mapping.GetTableName(entity)); var aggregator = Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type)); Expression where; DeclarationCommand genIdCommand = null; var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList(); if (generatedIds.Count > 0) { if (map == null || !generatedIds.Any(m => map.ContainsKey(m))) { var localMap = new Dictionary <MemberInfo, Expression>(); genIdCommand = this.GetGeneratedIdCommand(entity, generatedIds.ToList(), localMap); map = localMap; } // is this just a retrieval of one generated id member? var mex = selector.Body as MemberExpression; if (mex != null && this.mapping.IsPrimaryKey(entity, mex.Member) && this.mapping.IsGenerated(entity, mex.Member)) { if (genIdCommand != null) { // just use the select from the genIdCommand return(new ProjectionExpression( genIdCommand.Source, new ColumnExpression(mex.Type, genIdCommand.Variables[0].QueryType, genIdCommand.Source.Alias, genIdCommand.Source.Columns[0].Name), aggregator )); } else { TableAlias alias = new TableAlias(); var colType = this.GetColumnType(entity, mex.Member); return(new ProjectionExpression( new SelectExpression(alias, new[] { new ColumnDeclaration("", map[mex.Member], colType) }, null, null), new ColumnExpression(TypeHelper.GetMemberType(mex.Member), colType, alias, ""), aggregator )); } } where = generatedIds.Select((m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m]) ).Aggregate((x, y) => x.And(y)); } else { where = this.GetIdentityCheck(tex, entity, instance); } Expression typeProjector = this.GetEntityExpression(tex, entity); Expression selection = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector); TableAlias newAlias = new TableAlias(); var pc = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, selection, null, newAlias, tableAlias); var pe = new ProjectionExpression( new SelectExpression(newAlias, pc.Columns, tex, where), pc.Projector, aggregator ); if (genIdCommand != null) { return(new BlockCommand(genIdCommand, pe)); } return(pe); }