public Sql <T2> Select <T2>(Expression <Func <T, T2> > Expr) { if (Expr.Body is NewExpression) { var ret = BaseSql.Clone <T, T2>(this); if (ret.MapFields == null) { ret.MapFields = new List <MapFieldInfo>(); } ret.MapFields.AddRange(this.MapFields.Select(p => p.Clone())); var nx = Expr.Body as NewExpression; foreach (var mbx in nx.Members) { var arg = nx.Arguments[nx.Members.IndexOf(mbx)]; MapFieldInfo mp = null; if (arg is MemberExpression) { mp = ret.MapFields.FirstOrDefault(p => p.Member == mbx && p.ParamExpr == Expr.Parameters[0]); if (mp == null) { mp = ret.MapFields.FirstOrDefault(p => p.Member == mbx && p.ParamExpr.Type == Expr.Parameters[0].Type); } if (mp == null) { mp = ret.MapFields.FirstOrDefault(p => p.Name == mbx.Name && p.ParamExpr == Expr.Parameters[0]); } if (mp == null) { mp = ret.MapFields.FirstOrDefault(p => p.Name == mbx.Name && p.ParamExpr.Type == Expr.Parameters[0].Type); } if (mp != null) { mp.Member = mbx as PropertyInfo; mp.ParamExpr = Expr.Parameters[0]; } else { ret.MapFields.Add(new MapFieldInfo { AliasName = mbx.Name, Member = mbx as PropertyInfo, ParamExpr = Expr.Parameters[0], Name = ((MemberExpression)arg).Member.Name }); } } else { ret.MapFields.Add(new MapFieldInfo { AliasName = mbx.Name, Member = mbx as PropertyInfo, ParamExpr = Expr.Parameters[0], Name = mbx.Name }); } } ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromNewExpression(ret, (NewExpression)Expr.Body); var tempMapping = new List <MapFieldInfo>(); nx.Members.ToList().ForEach(p => { var mp = ret.MapFields.FirstOrDefault(x => x.Member == p); tempMapping.Add(new MapFieldInfo() { Alias = (mp != null)?mp.Alias:null, AliasName = (mp != null) ? mp.AliasName : null, ExprField = (mp != null && mp.ExprField != null) ? mp.ExprField.Clone() : null, Member = p as PropertyInfo, Name = p.Name, ParamExpr = Expr.Parameters[0], Schema = this.schema, TableName = this.table }); }); ret.MapFields = tempMapping; ret.SelectedFields.ForEach(p => { if (p.Field != null) { if (p.Field.Name == p.Field.AliasName) { p.Field.AliasName = null; } else { //ret.IsSubQuery = true; } } else if (p.Op != null) { //ret.IsSubQuery = true; } }); ret.SelectedFields.Join(ret.MapFields, p => p.AliasName, q => q.Name, (p, q) => new { p, q }) .ToList().ForEach(F => { F.q.ExprField = F.p.Clone(); }); return(ret); } if (Expr.Body is MemberExpression) { var ret = BaseSql.Clone <T, T2>(this); var mp = ret.MapFields.FirstOrDefault(p => p.Member == ((MemberExpression)Expr.Body).Member); if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { AliasName = mp.AliasName, Name = mp.Name, Schema = mp.Schema, TableName = mp.TableName } }); ret.MapFields.Clear(); ret.MapFields.Add(mp); return(ret); } if (Expr.Body is MemberInitExpression) { var mx = Expr.Body as MemberInitExpression; var ret = BaseSql.Clone <T, T2>(this); ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromMemberInitExpression(ret, mx); ret.SelectedFields.ForEach(p => { if (p.Field != null) { if (p.Field.Name == p.Field.AliasName) { p.Field.AliasName = null; } else { ret.IsSubQuery = true; } } //else if (p.Op != null) //{ // ret.IsSubQuery = true; //} }); return(ret); } if (Expr.Body is ParameterExpression) { var px = Expr.Body as ParameterExpression; var ret = BaseSql.Clone <T, T2>(this); var mp = ret.MapFields.FirstOrDefault(p => (p.ParamExpr != null) && p.ParamExpr.Type == px.Type); if (mp != null) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } ret.SelectedFields.Add(new TreeExpr() { Field = new FieldExpr { TableName = mp.TableName } }); ret.MapFields.AddRange(px.Type.GetProperties().Select(p => new MapFieldInfo() { Member = p, Name = p.Name, Schema = mp.Schema, TableName = mp.TableName, ParamExpr = px })); } return(ret); } throw new NotImplementedException(); }
public Sql <T2> Join <T1, T2>(Sql <T1> sql, Expression <Func <T, T1, bool> > ExprJoin, Expression <Func <T, T1, T2> > Selector) { if ((!this.IsSubQuery) && (!sql.IsSubQuery)) { var ret = new Sql <T2>(); ret.MapFields = new List <MapFieldInfo>(); this.AliasCount++; sql.AliasCount++; ret.AliasCount++; var leftAlias = "l" + ret.AliasCount + "" + (this.AliasCount); var rightAlias = "r" + ret.AliasCount + "" + (sql.AliasCount); this.MapFields.ForEach(p => { var P = p.Clone(); if (!ExprCompiler.IsPrimitiveType(((PropertyInfo)p.Member).PropertyType)) { P.Name = null; } P.SetTableName(leftAlias); P.ParamExpr = ExprJoin.Parameters[0]; ret.MapFields.Add(P); }); sql.MapFields.ForEach(p => { var P = p.Clone(); if (!ExprCompiler.IsPrimitiveType(((PropertyInfo)p.Member).PropertyType)) { P.Name = null; } P.SetTableName(rightAlias); P.ParamExpr = ExprJoin.Parameters[1]; ret.MapFields.Add(P); }); if (Selector.Body is NewExpression) { var nx = Selector.Body as NewExpression; if (nx.Members != null) { nx.Members.ToList().ForEach(p => { var fx = nx.Arguments[nx.Members.IndexOf(p)]; var mp = ret.MapFields.FirstOrDefault(x => x.Member == p && x.ParamExpr == fx); if (mp == null) { mp = ret.MapFields.FirstOrDefault(x => x.Member == p); } ret.MapFields.Add(new MapFieldInfo() { Alias = (mp != null) ? mp.Alias : null, AliasName = (mp != null) ? mp.AliasName : null, ExprField = (mp != null) ? mp.ExprField.Clone() : null, Member = p as PropertyInfo, Name = (mp != null) ? mp.Name : p.Name }); }); } } var JoinExpr = ExprCompiler.Compile(ret, ExprJoin.Body, null); JoinExpr.ClearAliasName(); ret.source = new ExprDataSource { JoinType = "inner", JoinExpr = JoinExpr, LeftSource = new ExprDataSource { Alias = leftAlias, Schema = this.schema, Table = this.table, ParamExpr = Expression.Parameter(this.ElementType, "p"), Source = (this.source != null)?this.source.Clone():null }, RightSource = new ExprDataSource { Alias = rightAlias, Schema = sql.schema, Table = sql.table, ParamExpr = Expression.Parameter(sql.ElementType, "p"), Source = (sql.source != null) ? sql.source.Clone() : null } }; if (Selector.Body is NewExpression) { ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromNewExpression(ret, (NewExpression)Selector.Body); } else if (Selector.Body is MemberInitExpression) { ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromMemberInitExpression(ret, (MemberInitExpression)Selector.Body); } else if (Selector.Body is ParameterExpression) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } if (Selector.Parameters[0] == Selector.Body) { ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { TableName = leftAlias } }); } else if (Selector.Body == Selector.Parameters[1]) { ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { TableName = rightAlias } }); } } else { throw new NotSupportedException(); } if (this.filter != null) { ret.filter = this.filter.Clone(); ret.filter.SetTableName(leftAlias); } if (sql.filter != null) { var rFilter = sql.filter.Clone(); rFilter.SetTableName(rightAlias); sql.filter = new TreeExpr { Op = ExprCompiler.GetOp(ExpressionType.And), Left = sql.filter, Right = rFilter }; } ret.SelectedFields.Join(ret.MapFields, p => p.AliasName, q => q.Name, (p, q) => new { p, q }) .ToList().ForEach(F => { F.q.ExprField = F.p.Clone(); }); return(ret); } throw new NotImplementedException(); }
public static Sql <T> DoInnerJoinByLambdaExpression <T>(BaseSql qr1, BaseSql qr2, LambdaExpression conditional, LambdaExpression selector) { var x = GetAllParamsExpr(conditional); //var y = GetAllParamsExpr(selector); var ret = new Sql <T>(true); ret.AliasCount++; var leftAlias = "l" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; var righttAlias = "r" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; ret.MapFields = new List <MapFieldInfo>(); if (qr1.MapFields != null) { qr1.MapFields.ForEach(p => { var P = p.Clone(); P.SetTableName(leftAlias); ret.MapFields.Add(P); P.ParamExpr = selector.Parameters[0]; var P1 = P.Clone(); P1.ParamExpr = x[0]; ret.MapFields.Add(P1); }); } if (qr2.MapFields != null) { qr2.MapFields.ForEach(p => { var P = p.Clone(); P.SetTableName(righttAlias); ret.MapFields.Add(P); P.ParamExpr = selector.Parameters[1]; var P1 = P.Clone(); P1.ParamExpr = x[1]; ret.MapFields.Add(P1); }); } var joinExor = ExprCompiler.Compile(ret, conditional, null); if (selector.Body is MemberInitExpression) { ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromMemberInitExpression(ret, (MemberInitExpression)selector.Body); } if (selector.Body is NewExpression) { ret.SelectedFields = ExprCompiler.GetSelectedFieldsFromNewExpression(ret, (NewExpression)selector.Body); } ret.source = new ExprDataSource { JoinExpr = joinExor, JoinType = "left" }; ret.source.LeftSource = new ExprDataSource { Schema = qr1.schema, Table = qr1.table, Alias = leftAlias, Source = (qr1.source != null) ? qr1.source.Clone() : null }; ret.source.RightSource = new ExprDataSource { Schema = qr2.schema, Table = qr2.table, Alias = righttAlias, Source = (qr2.source != null) ? qr2.source.Clone() : null }; if (selector.Body is NewExpression) { var nx = selector.Body as NewExpression; foreach (var mb in nx.Members) { if (!(nx.Arguments[nx.Members.IndexOf(mb)] is ParameterExpression)) { var fm = ret.SelectedFields.FirstOrDefault(p => (p.Field != null) && (p.Field.Name == mb.Name)); if (fm == null) { fm = ret.SelectedFields.FirstOrDefault(p => (p.Value != null) && (p.Value.AliasName == mb.Name)); } ret.MapFields.Add(new MapFieldInfo() { Member = mb as PropertyInfo, ParamExpr = Expression.Parameter(typeof(T), "p"), Name = mb.Name, Schema = ret.schema, TableName = ret.table, ExprField = fm }); } } } else if (selector.Body is MemberInitExpression) { var mbi = selector.Body as MemberInitExpression; foreach (MemberAssignment mba in mbi.Bindings) { if (!(mba.Expression is ParameterExpression)) { var fm = ret.SelectedFields.FirstOrDefault(p => (p.Field != null) && (p.Field.Name == mba.Member.Name)); if (fm == null) { fm = ret.SelectedFields.FirstOrDefault(p => (p.Value != null) && (p.Value.AliasName == mba.Member.Name)); } ret.MapFields.Add(new MapFieldInfo() { Member = mba.Member as PropertyInfo, ParamExpr = Expression.Parameter(typeof(T), "p"), Name = mba.Member.Name, Schema = ret.schema, TableName = ret.table, ExprField = fm }); } } } else { throw new NotImplementedException(); } return(ret); }