internal override SqlExpression VisitNew(SqlNew sox) { if (sox.Constructor != null) { System.Reflection.ParameterInfo[] pis = sox.Constructor.GetParameters(); for (int i = 0, n = sox.Args.Count; i < n; i++) { sox.Args[i] = this.VisitNamedExpression(sox.Args[i], pis[i].Name); } } else { for (int i = 0, n = sox.Args.Count; i < n; i++) { sox.Args[i] = this.VisitExpression(sox.Args[i]); } } foreach (SqlMemberAssign ma in sox.Members) { string n = ma.Member.Name; if (this.useMappedNames) { n = sox.MetaType.GetDataMember(ma.Member).MappedName; } ma.Expression = this.VisitNamedExpression(ma.Expression, n); } return(sox); }
internal virtual SqlExpression VisitNew(SqlNew sox) { for (int i = 0, n = sox.Args.Count; i < n; i++) { sox.Args[i] = this.VisitExpression(sox.Args[i]); } for (int i = 0, n = sox.Members.Count; i < n; i++) { sox.Members[i].Expression = this.VisitExpression(sox.Members[i].Expression); } return(sox); }
internal override SqlExpression VisitNew(SqlNew sox) { SqlExpression[] args = new SqlExpression[sox.Args.Count]; SqlMemberAssign[] bindings = new SqlMemberAssign[sox.Members.Count]; for (int i = 0, n = args.Length; i < n; i++) { args[i] = this.VisitExpression(sox.Args[i]); } for (int i = 0, n = bindings.Length; i < n; i++) { bindings[i] = this.VisitMemberAssign(sox.Members[i]); } return(new SqlNew(sox.MetaType, sox.SqlType, sox.Constructor, args, sox.ArgMembers, bindings, sox.SourceExpression)); }
internal override SqlExpression VisitNew(SqlNew sox) { var array = new SqlExpression[sox.Args.Count]; var array2 = new SqlMemberAssign[sox.Members.Count]; var i = 0; for (var num = array.Length; i < num; i++) { array[i] = VisitExpression(sox.Args[i]); } var j = 0; for (var num2 = array2.Length; j < num2; j++) { array2[j] = VisitMemberAssign(sox.Members[j]); } return(new SqlNew(sox.MetaType, sox.SqlType, sox.Constructor, array, sox.ArgMembers, array2, sox.SourceExpression)); }
internal SqlExpression BuildProjection(SqlExpression item, MetaType rowType, bool allowDeferred, SqlLink link, Expression source) { if (!rowType.HasInheritance) { return(this.BuildProjectionInternal(item, rowType, (rowType.Table != null) ? rowType.PersistentDataMembers : rowType.DataMembers, allowDeferred, link, source)); } else { // Build a type case that represents a switch between the various type. List <MetaType> mappedTypes = new List <MetaType>(rowType.InheritanceTypes); List <SqlTypeCaseWhen> whens = new List <SqlTypeCaseWhen>(); SqlTypeCaseWhen @else = null; MetaType root = rowType.InheritanceRoot; MetaDataMember discriminator = root.Discriminator; Type dt = discriminator.Type; SqlMember dm = sql.Member(item, discriminator.Member); foreach (MetaType type in mappedTypes) { if (type.HasInheritanceCode) { SqlNew defaultProjection = this.BuildProjectionInternal(item, type, type.PersistentDataMembers, allowDeferred, link, source); if (type.IsInheritanceDefault) { @else = new SqlTypeCaseWhen(null, defaultProjection); } // Add an explicit case even for the default. // Redundant results will be optimized out later. object code = InheritanceRules.InheritanceCodeForClientCompare(type.InheritanceCode, dm.SqlType); SqlExpression match = sql.Value(dt, sql.Default(discriminator), code, true, source); whens.Add(new SqlTypeCaseWhen(match, defaultProjection)); } } if (@else == null) { throw Error.EmptyCaseNotSupported(); } whens.Add(@else); // Add the else at the end. return(sql.TypeCase(root.Type, root, dm, whens.ToArray(), source)); } }
private void FlattenGroupByExpression(List <SqlExpression> exprs, SqlExpression expr) { SqlNew sn = expr as SqlNew; if (sn != null) { foreach (SqlMemberAssign ma in sn.Members) { this.FlattenGroupByExpression(exprs, ma.Expression); } foreach (SqlExpression arg in sn.Args) { this.FlattenGroupByExpression(exprs, arg); } } else if (expr.NodeType == SqlNodeType.TypeCase) { SqlTypeCase tc = (SqlTypeCase)expr; this.FlattenGroupByExpression(exprs, tc.Discriminator); foreach (SqlTypeCaseWhen when in tc.Whens) { this.FlattenGroupByExpression(exprs, when.TypeBinding); } } else if (expr.NodeType == SqlNodeType.Link) { SqlLink link = (SqlLink)expr; if (link.Expansion != null) { this.FlattenGroupByExpression(exprs, link.Expansion); } else { foreach (SqlExpression key in link.KeyExpressions) { this.FlattenGroupByExpression(exprs, key); } } } else if (expr.NodeType == SqlNodeType.OptionalValue) { SqlOptionalValue sop = (SqlOptionalValue)expr; this.FlattenGroupByExpression(exprs, sop.HasValue); this.FlattenGroupByExpression(exprs, sop.Value); } else if (expr.NodeType == SqlNodeType.OuterJoinedValue) { this.FlattenGroupByExpression(exprs, ((SqlUnary)expr).Operand); } else if (expr.NodeType == SqlNodeType.DiscriminatedType) { SqlDiscriminatedType dt = (SqlDiscriminatedType)expr; this.FlattenGroupByExpression(exprs, dt.Discriminator); } else { // this expression should have been 'pushed-down' in SqlBinder, so we // should only find column-references & expr-sets unless the expression could not // be columnized (in which case it was a bad group-by expression.) if (expr.NodeType != SqlNodeType.ColumnRef && expr.NodeType != SqlNodeType.ExprSet) { if (!expr.SqlType.CanBeColumn) { throw Error.InvalidGroupByExpressionType(expr.SqlType.ToQueryString()); } throw Error.InvalidGroupByExpression(); } exprs.Add(expr); } }
internal override SqlExpression VisitNew(SqlNew sox) { this.isNew = true; return(base.VisitNew(sox)); }
internal SqlNew New(MetaType type, ConstructorInfo cons, IEnumerable <SqlExpression> args, IEnumerable <MemberInfo> argMembers, IEnumerable <SqlMemberAssign> bindings, Expression sourceExpression) { SqlNew tb = new SqlNew(type, typeProvider.From(type.Type), cons, args, argMembers, bindings, sourceExpression); return(tb); }
private SqlExpression ExpandTogether(List <SqlExpression> exprs) { switch (exprs[0].NodeType) { case SqlNodeType.MethodCall: { SqlMethodCall[] mcs = new SqlMethodCall[exprs.Count]; for (int i = 0; i < mcs.Length; ++i) { mcs[i] = (SqlMethodCall)exprs[i]; } List <SqlExpression> expandedArgs = new List <SqlExpression>(); for (int i = 0; i < mcs[0].Arguments.Count; ++i) { List <SqlExpression> args = new List <SqlExpression>(); for (int j = 0; j < mcs.Length; ++j) { args.Add(mcs[j].Arguments[i]); } SqlExpression expanded = this.ExpandTogether(args); expandedArgs.Add(expanded); } return(factory.MethodCall(mcs[0].Method, mcs[0].Object, expandedArgs.ToArray(), mcs[0].SourceExpression)); } case SqlNodeType.ClientCase: { // Are they all the same? SqlClientCase[] scs = new SqlClientCase[exprs.Count]; scs[0] = (SqlClientCase)exprs[0]; for (int i = 1; i < scs.Length; ++i) { scs[i] = (SqlClientCase)exprs[i]; } // Expand expressions together. List <SqlExpression> expressions = new List <SqlExpression>(); for (int i = 0; i < scs.Length; ++i) { expressions.Add(scs[i].Expression); } SqlExpression expression = this.ExpandTogether(expressions); // Expand individual expressions together. List <SqlClientWhen> whens = new List <SqlClientWhen>(); for (int i = 0; i < scs[0].Whens.Count; ++i) { List <SqlExpression> scos = new List <SqlExpression>(); for (int j = 0; j < scs.Length; ++j) { SqlClientWhen when = scs[j].Whens[i]; scos.Add(when.Value); } whens.Add(new SqlClientWhen(scs[0].Whens[i].Match, this.ExpandTogether(scos))); } return(new SqlClientCase(scs[0].ClrType, expression, whens, scs[0].SourceExpression)); } case SqlNodeType.TypeCase: { // Are they all the same? SqlTypeCase[] tcs = new SqlTypeCase[exprs.Count]; tcs[0] = (SqlTypeCase)exprs[0]; for (int i = 1; i < tcs.Length; ++i) { tcs[i] = (SqlTypeCase)exprs[i]; } // Expand discriminators together. List <SqlExpression> discriminators = new List <SqlExpression>(); for (int i = 0; i < tcs.Length; ++i) { discriminators.Add(tcs[i].Discriminator); } SqlExpression discriminator = this.ExpandTogether(discriminators); // Write expanded discriminators back in. for (int i = 0; i < tcs.Length; ++i) { tcs[i].Discriminator = discriminators[i]; } // Expand individual type bindings together. List <SqlTypeCaseWhen> whens = new List <SqlTypeCaseWhen>(); for (int i = 0; i < tcs[0].Whens.Count; ++i) { List <SqlExpression> scos = new List <SqlExpression>(); for (int j = 0; j < tcs.Length; ++j) { SqlTypeCaseWhen when = tcs[j].Whens[i]; scos.Add(when.TypeBinding); } SqlExpression expanded = this.ExpandTogether(scos); whens.Add(new SqlTypeCaseWhen(tcs[0].Whens[i].Match, expanded)); } return(factory.TypeCase(tcs[0].ClrType, tcs[0].RowType, discriminator, whens, tcs[0].SourceExpression)); } case SqlNodeType.New: { // first verify all are similar client objects... SqlNew[] cobs = new SqlNew[exprs.Count]; cobs[0] = (SqlNew)exprs[0]; for (int i = 1, n = exprs.Count; i < n; i++) { if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.New) { throw Error.UnionIncompatibleConstruction(); } cobs[i] = (SqlNew)exprs[1]; if (cobs[i].Members.Count != cobs[0].Members.Count) { throw Error.UnionDifferentMembers(); } for (int m = 0, mn = cobs[0].Members.Count; m < mn; m++) { if (cobs[i].Members[m].Member != cobs[0].Members[m].Member) { throw Error.UnionDifferentMemberOrder(); } } } SqlMemberAssign[] bindings = new SqlMemberAssign[cobs[0].Members.Count]; for (int m = 0, mn = bindings.Length; m < mn; m++) { List <SqlExpression> mexprs = new List <SqlExpression>(); for (int i = 0, n = exprs.Count; i < n; i++) { mexprs.Add(cobs[i].Members[m].Expression); } bindings[m] = new SqlMemberAssign(cobs[0].Members[m].Member, this.ExpandTogether(mexprs)); for (int i = 0, n = exprs.Count; i < n; i++) { cobs[i].Members[m].Expression = mexprs[i]; } } SqlExpression[] arguments = new SqlExpression[cobs[0].Args.Count]; for (int m = 0, mn = arguments.Length; m < mn; ++m) { List <SqlExpression> mexprs = new List <SqlExpression>(); for (int i = 0, n = exprs.Count; i < n; i++) { mexprs.Add(cobs[i].Args[m]); } arguments[m] = ExpandTogether(mexprs); } return(factory.New(cobs[0].MetaType, cobs[0].Constructor, arguments, cobs[0].ArgMembers, bindings, exprs[0].SourceExpression)); } case SqlNodeType.Link: { SqlLink[] links = new SqlLink[exprs.Count]; links[0] = (SqlLink)exprs[0]; for (int i = 1, n = exprs.Count; i < n; i++) { if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.Link) { throw Error.UnionIncompatibleConstruction(); } links[i] = (SqlLink)exprs[i]; if (links[i].KeyExpressions.Count != links[0].KeyExpressions.Count || links[i].Member != links[0].Member || (links[i].Expansion != null) != (links[0].Expansion != null)) { throw Error.UnionIncompatibleConstruction(); } } SqlExpression[] kexprs = new SqlExpression[links[0].KeyExpressions.Count]; List <SqlExpression> lexprs = new List <SqlExpression>(); for (int k = 0, nk = links[0].KeyExpressions.Count; k < nk; k++) { lexprs.Clear(); for (int i = 0, n = exprs.Count; i < n; i++) { lexprs.Add(links[i].KeyExpressions[k]); } kexprs[k] = this.ExpandTogether(lexprs); for (int i = 0, n = exprs.Count; i < n; i++) { links[i].KeyExpressions[k] = lexprs[i]; } } SqlExpression expansion = null; if (links[0].Expansion != null) { lexprs.Clear(); for (int i = 0, n = exprs.Count; i < n; i++) { lexprs.Add(links[i].Expansion); } expansion = this.ExpandTogether(lexprs); for (int i = 0, n = exprs.Count; i < n; i++) { links[i].Expansion = lexprs[i]; } } return(new SqlLink(links[0].Id, links[0].RowType, links[0].ClrType, links[0].SqlType, links[0].Expression, links[0].Member, kexprs, expansion, links[0].SourceExpression)); } case SqlNodeType.Value: { /* * ExprSet of all literals of the same value reduce to just a single literal. */ SqlValue val0 = (SqlValue)exprs[0]; for (int i = 1; i < exprs.Count; ++i) { SqlValue val = (SqlValue)exprs[i]; if (!object.Equals(val.Value, val0.Value)) { return(this.ExpandIntoExprSet(exprs)); } } return(val0); } case SqlNodeType.OptionalValue: { if (exprs[0].SqlType.CanBeColumn) { goto default; } List <SqlExpression> hvals = new List <SqlExpression>(exprs.Count); List <SqlExpression> vals = new List <SqlExpression>(exprs.Count); for (int i = 0, n = exprs.Count; i < n; i++) { if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.OptionalValue) { throw Error.UnionIncompatibleConstruction(); } SqlOptionalValue sov = (SqlOptionalValue)exprs[i]; hvals.Add(sov.HasValue); vals.Add(sov.Value); } return(new SqlOptionalValue(this.ExpandTogether(hvals), this.ExpandTogether(vals))); } case SqlNodeType.OuterJoinedValue: { if (exprs[0].SqlType.CanBeColumn) { goto default; } List <SqlExpression> values = new List <SqlExpression>(exprs.Count); for (int i = 0, n = exprs.Count; i < n; i++) { if (exprs[i] == null || exprs[i].NodeType != SqlNodeType.OuterJoinedValue) { throw Error.UnionIncompatibleConstruction(); } SqlUnary su = (SqlUnary)exprs[i]; values.Add(su.Operand); } return(factory.Unary(SqlNodeType.OuterJoinedValue, this.ExpandTogether(values))); } case SqlNodeType.DiscriminatedType: { SqlDiscriminatedType sdt0 = (SqlDiscriminatedType)exprs[0]; List <SqlExpression> foos = new List <SqlExpression>(exprs.Count); foos.Add(sdt0.Discriminator); for (int i = 1, n = exprs.Count; i < n; i++) { SqlDiscriminatedType sdtN = (SqlDiscriminatedType)exprs[i]; if (sdtN.TargetType != sdt0.TargetType) { throw Error.UnionIncompatibleConstruction(); } foos.Add(sdtN.Discriminator); } return(factory.DiscriminatedType(this.ExpandTogether(foos), ((SqlDiscriminatedType)exprs[0]).TargetType)); } case SqlNodeType.ClientQuery: case SqlNodeType.Multiset: case SqlNodeType.Element: case SqlNodeType.Grouping: throw Error.UnionWithHierarchy(); default: return(this.ExpandIntoExprSet(exprs)); } }