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 VisitExists(ExistsExpression exists) { // how did we get here? Translate exists into count query var colType = this.linguist.Language.TypeSystem.GetColumnType(typeof(int)); var newSelect = exists.Select.SetColumns( new[] { new ColumnDeclaration("value", new AggregateExpression(typeof(int), "Count", null, false), colType) } ); var projection = new ProjectionExpression( newSelect, new ColumnExpression(typeof(int), colType, newSelect.Alias, "value"), Aggregator.GetAggregator(typeof(int), typeof(IEnumerable <int>)) ); var expression = projection.GreaterThan(Expression.Constant(0)); return(this.Visit(expression)); }
public override Expression GetMemberExpression(Expression root, MappingEntity entity, MemberInfo member) { if (this.mapping.IsAssociationRelationship(entity, member)) { MappingEntity relatedEntity = this.mapping.GetRelatedEntity(entity, member); ProjectionExpression projection = this.GetQueryExpression(relatedEntity); // make where clause for joining back to 'root' var declaredTypeMembers = this.mapping.GetAssociationKeyMembers(entity, member).ToList(); var associatedMembers = this.mapping.GetAssociationRelatedKeyMembers(entity, member).ToList(); Expression where = null; for (int i = 0, n = associatedMembers.Count; i < n; i++) { Expression equal = this.GetMemberExpression(projection.Projector, relatedEntity, associatedMembers[i]).Equal( this.GetMemberExpression(root, entity, declaredTypeMembers[i]) ); where = (where != null) ? where.And(equal) : equal; } TableAlias newAlias = new TableAlias(); var pc = ColumnProjector.ProjectColumns(this.translator.Linguist.Language, projection.Projector, null, newAlias, projection.Select.Alias); LambdaExpression aggregator = Aggregator.GetAggregator(TypeHelper.GetMemberType(member), typeof(IEnumerable <>).MakeGenericType(pc.Projector.Type)); var result = new ProjectionExpression( new SelectExpression(newAlias, pc.Columns, projection.Select, where), pc.Projector, aggregator ); return(this.translator.Police.ApplyPolicy(result, member)); } else { AliasedExpression aliasedRoot = root as AliasedExpression; if (aliasedRoot != null && this.mapping.IsColumn(entity, member)) { return(new ColumnExpression(TypeHelper.GetMemberType(member), this.GetColumnType(entity, member), aliasedRoot.Alias, this.mapping.GetColumnName(entity, member))); } return(QueryBinder.BindMember(root, member)); } }
protected override Expression VisitDeclaration(DeclarationCommand decl) { if (decl.Source != null) { // make query that returns all these declared values as an object[] var projection = new ProjectionExpression( decl.Source, Expression.NewArrayInit( typeof(object), decl.Variables.Select(v => v.Expression.Type.IsValueType ? Expression.Convert(v.Expression, typeof(object)) : v.Expression).ToArray() ), Aggregator.GetAggregator(typeof(object[]), typeof(IEnumerable <object[]>)) ); // create execution variable to hold the array of declared variables var vars = Expression.Parameter(typeof(object[]), "vars"); this.variables.Add(vars); this.initializers.Add(Expression.Constant(null, typeof(object[]))); // create subsitution for each variable (so it will find the variable value in the new vars array) for (int i = 0, n = decl.Variables.Count; i < n; i++) { var v = decl.Variables[i]; NamedValueExpression nv = new NamedValueExpression( v.Name, v.QueryType, Expression.Convert(Expression.ArrayIndex(vars, Expression.Constant(i)), v.Expression.Type) ); this.variableMap.Add(v.Name, nv); } // make sure the execution of the select stuffs the results into the new vars array return(MakeAssign(vars, this.Visit(projection))); } // probably bad if we get here since we must not allow mulitple commands throw new InvalidOperationException("Declaration query not allowed for this langauge"); }
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); }