protected DeclarationCommand UpdateDeclaration(DeclarationCommand decl, IEnumerable <VariableDeclaration> variables, SelectExpression source) { if ((variables != decl.Variables) || (source != decl.Source)) { return(new DeclarationCommand(variables, source)); } return(decl); }
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); }
private CommandExpression GetDependentGeneratedVariableDeclaration(MappingEntity entity, MappingTable table, List <MemberInfo> members, Expression instance, Dictionary <MemberInfo, Expression> map) { Func <MemberInfo, int, Expression> selector = null; DeclarationCommand command = null; List <MemberInfo> second = (from m in this.mapping.GetMappedMembers(entity) where this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m) select m).ToList <MemberInfo>(); if (second.Count > 0) { command = this.GetGeneratedIdCommand(entity, members, map); if (members.Count == second.Count) { return(command); } } members = members.Except <MemberInfo>(second).ToList <MemberInfo>(); TableAlias alias = new TableAlias(); TableExpression tex = new TableExpression(alias, entity, this.mapping.GetTableName(table)); Expression where = null; if (second.Count > 0) { if (selector == null) { selector = (m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m]); } where = second.Select <MemberInfo, Expression>(selector).Aggregate <Expression>((x, y) => x.And(y)); } else { where = this.GetIdentityCheck(tex, entity, instance); } TableAlias alias2 = new TableAlias(); List <ColumnDeclaration> columns = new List <ColumnDeclaration>(); List <VariableDeclaration> variables = new List <VariableDeclaration>(); foreach (MemberInfo info in members) { ColumnExpression expression = (ColumnExpression)this.GetMemberExpression(tex, entity, info); columns.Add(new ColumnDeclaration(this.mapping.GetColumnName(entity, info), expression, expression.QueryType)); ColumnExpression expression3 = new ColumnExpression(expression.Type, expression.QueryType, alias2, expression.Name); variables.Add(new VariableDeclaration(info.Name, expression.QueryType, expression3)); map.Add(info, new VariableExpression(info.Name, expression.Type, expression.QueryType)); } DeclarationCommand command2 = new DeclarationCommand(variables, new SelectExpression(alias2, columns, tex, where)); if (command != null) { return(new BlockCommand(new Expression[] { command, command2 })); } return(command2); }
protected override Expression VisitDeclaration(DeclarationCommand decl) { ParameterExpression expression2; if (decl.Source == null) { throw new InvalidOperationException("Declaration query not allowed for this langauge"); } ProjectionExpression exp = new ProjectionExpression(decl.Source, Expression.NewArrayInit(typeof(object), (from v in decl.Variables select v.Expression).ToArray <Expression>()), Aggregator.GetAggregator(typeof(object[]), typeof(IEnumerable <object[]>))); this.variables.Add(expression2 = Expression.Parameter(typeof(object[]), "vars")); this.initializers.Add(Expression.Constant(null, typeof(object[]))); int num = 0; int count = decl.Variables.Count; while (num < count) { VariableDeclaration declaration = decl.Variables[num]; NamedValueExpression expression3 = new NamedValueExpression(declaration.Name, declaration.QueryType, Expression.Convert(Expression.ArrayIndex(expression2, Expression.Constant(num)), declaration.Expression.Type)); this.variableMap.Add(declaration.Name, expression3); num++; } return(MakeAssign(expression2, this.Visit(exp))); }
protected override Expression VisitDeclaration(DeclarationCommand decl) { throw new NotSupportedException(); }