public static TreeExpr CompileMember(BaseSql sql, MemberExpression expr, MemberInfo mB) { ParameterExpression P = GetParamFromMember(expr); var mb = sql.MapFields.Where(p => p.ParamExpr != null).FirstOrDefault(p => p.ParamExpr == P && p.Member == expr.Member); if (mb == null) { mb = sql.MapFields.FirstOrDefault(p => p.Member == expr.Member); } if (mb == null) { mb = sql.MapFields.FirstOrDefault(p => p.Member.Name == expr.Member.Name); } if (mb != null) { if (mb.ExprField != null) { return(mb.ExprField); } return(new TreeExpr { Field = new FieldExpr { AliasName = (mB != null && mB.Name != expr.Member.Name) ? mB.Name : (mb.AliasName ?? null), Name = mb.Name, Schema = mb.Schema, TableName = mb.TableName } }); } throw new NotImplementedException(); }
public static Sql <T2> Clone <T2>(BaseSql sql) { var ret = new Sql <T2>(true); CopyTo(sql, ret); return(ret); }
public static IQueryable <T> DoSelectByNewExpression <T>(BaseSql qr, NewExpression Expr) { var ret = BaseSql.Clone <T>(qr); ret.SelectedFields = Expr.Arguments.Select(p => ExprCompiler.Compile(qr, p, Expr.Members[Expr.Arguments.IndexOf(p)])).ToList(); return(ret as IQueryable <T>); }
public static TreeExpr Compile(BaseSql sql, Expression Expr, MemberInfo MB) { if (Expr is BinaryExpression) { return(CompileBin(sql, (BinaryExpression)Expr, MB)); } if (Expr is MemberExpression) { return(CompileMember(sql, (MemberExpression)Expr, MB)); } if (Expr is ConstantExpression) { return(CompileConst(sql, (ConstantExpression)Expr, MB)); } if (Expr is ParameterExpression) { return(CompileParaExpr(sql, (ParameterExpression)Expr)); } if (Expr is UnaryExpression) { return(Compile(sql, ((UnaryExpression)Expr).Operand, MB)); } if (Expr is LambdaExpression) { return(Compile(sql, ((LambdaExpression)Expr).Body, MB)); } if (Expr is MethodCallExpression) { return(CompileMethodCallExpression(sql, (MethodCallExpression)Expr, MB)); } throw new NotImplementedException(); }
public static IQueryable <T> DoSelectByMemberExpression <T>(BaseSql qr, MemberExpression selector) { var ret = BaseSql.Clone <T>(qr); ret.SelectedFields = GetSelectedFieldsFromMemberExpression(ret, selector); return(ret as IQueryable <T>); }
public static IQueryable <T> DoSelectByMethodCallExpression <T>(BaseSql sql, MethodCallExpression expr) { var ret = BaseSql.Clone <T>(sql); if (expr.Method.DeclaringType == typeof(SqlFn)) { if (expr.Arguments.Count == 1) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } ret.SelectedFields.Add(new TreeExpr { Callee = new FuncExpr { Name = expr.Method.Name, Arguments = expr.Arguments.Select(p => ExprCompiler.Compile(ret, p, null)).ToList() } }); return(ret as IQueryable <T>); } } else if (expr.Method.DeclaringType == typeof(Enumerable)) { if (expr.Method.Name == "First") { } } throw new NotImplementedException(); }
public static IQueryable <T> DoSelectByExpression <T>(BaseSql qr, Expression Expr) { if (Expr is MemberExpression) { return(DoSelectByMemberExpression <T>(qr, (MemberExpression)Expr)); } throw new NotImplementedException(); }
public Sql <T> Where(Expression <Func <T, bool> > Expr) { Sql <T> ret = BaseSql.Clone <T, T>(this); if (!this.IsSubQuery) { if (this.SelectedFields != null && this.SelectedFields.Count > 0) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } ret.SelectedFields.AddRange(this.SelectedFields.Select(p => p.Clone())); } if (((BaseSql)ret).filter == null) { ((BaseSql)ret).filter = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, null); } else { ((BaseSql)ret).filter = new TreeExpr { Op = ExprCompiler.GetOp(ExpressionType.And), Left = ((BaseSql)ret).filter, Right = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, ((MemberExpression)Expr.Body).Member) }; } return(ret); } else { ret.AliasCount = this.AliasCount + 1; ret.Alias = "sql" + ret.AliasCount; ret.MapFields.ForEach(p => { if (p.TableName == null) { p.TableName = ret.Alias; } }); ret.source = new ExprDataSource { Alias = ret.Alias, Fields = this.SelectedFields.Select(p => p.Clone()).ToList(), Schema = this.schema, Table = this.table, Source = this.source, ParamExpr = Expression.Parameter(ret.ElementType, "p") }; if (this.filter != null) { ret.source.filter = this.filter.Clone(); } ret.filter = ExprCompiler.Compile(((BaseSql)ret), Expr.Body, null); return(ret); } }
public static IQueryable <T> DoSelectByParameterExpression <T>(BaseSql qr, ParameterExpression selector) { var ret = BaseSql.Clone <T>(qr); if (selector.Type == typeof(T)) { return(ret); } throw new NotImplementedException(); }
public static TreeExpr CompileBin(BaseSql sql, BinaryExpression expr, MemberInfo mb) { return(new TreeExpr { AliasName = (mb != null) ? mb.Name : null, Op = GetOp(expr.NodeType), Left = Compile(sql, expr.Left, mb), Right = Compile(sql, expr.Right, mb) }); }
public static IQueryable <T> DoSelectByMMemberInitExpression <T>(BaseSql qr, MemberInitExpression Expr) { var ret = BaseSql.Clone <T>(qr); ret.SelectedFields = new List <TreeExpr>(); foreach (MemberAssignment mba in Expr.Bindings) { ret.SelectedFields.Add(ExprCompiler.Compile(ret, mba.Expression, mba.Member)); } return(ret); }
private static XSqlCommand GetCommand(ISqlCompiler Compiler, BaseSql Sql, string Bracket, string ParamPrefix) { var Params = new List <XSqlCommandParam>(); var ret = new XSqlCommand { CommandText = Sql.ToSQLString(Bracket, ParamPrefix, Params, Compiler) }; ret.Params = Params; var selectFields = new List <string>(); return(ret); }
public static IQueryable <T> DoSelectMany <T>(MethodCallExpression Expr) { if (Expr.Arguments[0].Type.BaseType == typeof(BaseSql)) { var qr = Expression.Lambda(Expr.Arguments[0]).Compile().DynamicInvoke() as BaseSql; var secondParam = ((LambdaExpression)((UnaryExpression)Expr.Arguments[1]).Operand).Body; if (secondParam is MemberExpression) { var fx = (MemberExpression)secondParam; var qr2 = (BaseSql)Expression.Lambda(fx).Compile().DynamicInvoke(); return(CrossJoin <T>(qr, qr2, (LambdaExpression)((UnaryExpression)Expr.Arguments[2]).Operand)); } else if (secondParam is MethodCallExpression) { var cx = secondParam as MethodCallExpression; if (cx.Method.Name == "Where") { var qr2 = Expression.Lambda(cx.Arguments[0]).Compile().DynamicInvoke() as BaseSql; var conditional = ((LambdaExpression)((UnaryExpression)cx.Arguments[1]).Operand); var selector = ((LambdaExpression)((UnaryExpression)Expr.Arguments[2]).Operand); return(DoInnerJoinByLambdaExpression <T>(qr, qr2, conditional, selector)); } if (cx.Method.Name == "DefaultIfEmpty") { if (((MethodCallExpression)cx.Arguments[0]).Method.Name == "Where") { BaseSql qr2 = null; LambdaExpression conditional = null; var cxChild = (MethodCallExpression)cx.Arguments[0]; if (cxChild.Object == null) { qr2 = Expression.Lambda(cxChild.Arguments[0]).Compile().DynamicInvoke() as BaseSql; conditional = ((LambdaExpression)((UnaryExpression)cxChild.Arguments[1]).Operand); } else { qr2 = Expression.Lambda(cxChild.Object).Compile().DynamicInvoke() as BaseSql; conditional = ((LambdaExpression)((UnaryExpression)cxChild.Arguments[0]).Operand); } var selector = ((LambdaExpression)((UnaryExpression)Expr.Arguments[2]).Operand); var ret = DoInnerJoinByLambdaExpression <T>(qr, qr2, conditional, selector); return(ret); } } } } throw new NotImplementedException(); }
public static TreeExpr CompileConst(BaseSql sql, ConstantExpression expr, MemberInfo mB) { var val = new ValueExpr { Type = expr.Type, Val = Expression.Lambda(expr).Compile().DynamicInvoke(), AliasName = (mB != null)?mB.Name:null }; var ret = new TreeExpr { Value = val }; return(ret); }
public static TreeExpr CompileParaExpr(BaseSql sql, ParameterExpression expr) { if (sql.source != null) { if (sql.source.LeftSource != null && sql.source.LeftSource.ParamExpr.Type == expr.Type) { return(new TreeExpr { Field = new FieldExpr { TableName = sql.source.LeftSource.Table } }); } if (sql.source.LeftSource != null && sql.source.RightSource.ParamExpr.Type == expr.Type) { return(new TreeExpr { Field = new FieldExpr { TableName = sql.source.RightSource.Table } }); } } else { var mp = sql.MapFields.FirstOrDefault(p => p.ParamExpr == expr); if (mp != null) { return(new TreeExpr { Field = new FieldExpr { TableName = mp.TableName, Schema = mp.Schema } }); } if (expr.Type.Name.IndexOf("IGrouping") > -1) { } } throw new NotImplementedException(); }
public Sql <T> Select(params string[] fields) { var ret = BaseSql.Clone <T, T>(this); ret.SelectedFields = new List <TreeExpr>(); foreach (var F in fields) { var mp = this.MapFields.FirstOrDefault(p => p.Name.Equals(F, StringComparison.InvariantCultureIgnoreCase)); if (mp == null) { throw new Exception(string.Format("{0} was not found", F)); } ret.SelectedFields.Add(new TreeExpr { }); } return(ret); }
public static TreeExpr CompileMethodCallExpression(BaseSql sql, MethodCallExpression expr, MemberInfo mB) { if (expr.Method.DeclaringType == typeof(SqlFn)) { if (expr.Arguments.Count == 1 && expr.Arguments[0] is NewArrayExpression) { var args = expr.Arguments[0] as NewArrayExpression; var tmpArgs = args.Expressions.Select(p => Compile(sql, p, mB)).ToList(); tmpArgs.ForEach(p => { p.ClearAliasName(); }); return(new TreeExpr { Callee = new FuncExpr { Name = expr.Method.Name, Arguments = tmpArgs, AliasName = (mB != null) ? mB.Name:null }, AliasName = (mB != null)?mB.Name:null }); } else { var tmpArgs = expr.Arguments.Select(p => Compile(sql, p, mB)).ToList(); tmpArgs.ForEach(p => { p.ClearAliasName(); }); return(new TreeExpr { Callee = new FuncExpr { Name = expr.Method.Name, Arguments = tmpArgs, AliasName = (mB != null) ? mB.Name : null }, AliasName = mB.Name }); } } throw new NotImplementedException(); }
internal static Sql <T> DoGroupBy <T>(MethodCallExpression expr) { var sql = Expression.Lambda(expr.Arguments[0]).Compile().DynamicInvoke() as BaseSql; var ret = BaseSql.Clone <T>(sql); ret.MapFields.AddRange(sql.MapFields.Select(p => p.Clone())); var ExprGroupBy = expr.Arguments[1]; ret.GroupByFields = ExprCompiler.GetSelectedFieldsFromExpression(ret, ExprGroupBy); ret.IsSubQuery = true; var mbxs = ExprCompiler.GetAllMemberExpression(expr); typeof(T).GetProperties().ToList().ForEach(p => { ret.MapFields.Add(new MapFieldInfo { Member = p, ParamExpr = Expression.Parameter(p.PropertyType, "p"), Name = p.Name }); }); return(ret); }
public static void CopyTo(BaseSql sql, BaseSql ret) { ret.MapFields = sql.MapFields.ToList(); ret.ParamExpr = sql.ParamExpr; ret.schema = sql.schema; ret.table = sql.table; ret.AliasCount = sql.AliasCount; ret.Alias = sql.Alias; if (sql.source != null) { ret.source = sql.source.Clone(); } if (ret.SelectedFields != null) { ret.SelectedFields = sql.SelectedFields.Select(p => p.Clone()).ToList(); ret.SelectedFields.ForEach(p => { if (p.Field.AliasName != null) { } }); } if (sql.filter != null) { ret.filter = sql.filter.Clone(); } if (sql.GroupByFields != null) { ret.GroupByFields = sql.GroupByFields.Select(p => p.Clone()).ToList(); } if (sql.HavingFields != null) { ret.HavingFields = sql.HavingFields.Select(p => p.Clone()).ToList(); } ret.IsSubQuery = sql.IsSubQuery || sql.filter != null || sql.source != null; }
public static IQueryable <T> DoInnerJoin <T>(BaseSql qr1, BaseSql qr2, MemberExpression leftKey, MemberExpression rightKey, Expression selector) { var ret = new Sql <T>(true); var leftName = "l" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; var rightName = "r" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; if (!(qr1.IsSubQuery) && (!qr2.IsSubQuery)) { var leftMp = qr1.MapFields.FirstOrDefault(p => p.Member == leftKey.Member); var rightMp = qr2.MapFields.FirstOrDefault(p => p.Member == rightKey.Member); ret.source = new ExprDataSource { JoinType = "inner", LeftSource = new ExprDataSource { Alias = leftName, Schema = qr1.schema, Table = qr1.table }, RightSource = new ExprDataSource { Alias = rightName, Schema = qr2.schema, Table = qr2.table }, JoinExpr = new TreeExpr { Op = ExprCompiler.GetOp(ExpressionType.Equal), Left = new TreeExpr { Field = new FieldExpr { TableName = leftName, Name = leftMp.Name } }, Right = new TreeExpr { Field = new FieldExpr { TableName = rightName, Name = rightMp.Name } } } }; return(ret as IQueryable <T>); } if ((qr1.IsSubQuery) && (!qr2.IsSubQuery)) { var sql1 = new BaseSql(); var leftMp = qr1.MapFields.FirstOrDefault(p => p.Member == leftKey.Member); var rightMp = qr2.MapFields.FirstOrDefault(p => p.Member == rightKey.Member); ret.source = new ExprDataSource { JoinType = "inner", LeftSource = new ExprDataSource { Source = new ExprDataSource { Alias = qr1.Alias, Fields = (qr1.SelectedFields != null)?qr1.SelectedFields.Select(p => p.Clone()).ToList():null, filter = (qr1.filter != null)?qr1.filter.Clone():null, GroupFields = (qr1.GroupByFields != null)?qr1.GroupByFields.Select(p => p.Clone()).ToList():null, HavingFields = (qr1.HavingFields != null)?qr1.HavingFields.Select(p => p.Clone()).ToList():null, Schema = qr1.schema, Table = qr1.table, JoinExpr = (qr1.source != null)?qr1.source.JoinExpr:null, JoinType = (qr1.source != null) ? qr1.source.JoinType : null, LeftSource = (qr1.source != null) ? qr1.source.LeftSource : null, RightSource = (qr1.source != null) ? qr1.source.RightSource : null, ParamExpr = qr1.ParamExpr } }, RightSource = new ExprDataSource { Alias = rightName, Schema = qr2.schema, Table = qr2.table }, JoinExpr = new TreeExpr { Op = ExprCompiler.GetOp(ExpressionType.Equal), Left = new TreeExpr { Field = new FieldExpr { TableName = leftName, Name = leftMp.Name } }, Right = new TreeExpr { Field = new FieldExpr { TableName = rightName, Name = rightMp.Name } } } }; if (ret.source.LeftSource.Fields == null || ret.source.Fields.Count == 0) { ret.source.Fields = new List <TreeExpr>(); ret.source.Fields.Add(new TreeExpr { Field = new FieldExpr { TableName = qr1.table } }); } return(ret as IQueryable <T>); } throw new NotSupportedException(); }
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 static IQueryable <T> CrossJoin <T>(BaseSql qr1, BaseSql qr2, LambdaExpression Expr) { var ret = new Sql <T>(); ret.MapFields = new List <MapFieldInfo>(); ret.MapFields.AddRange(qr1.MapFields.Select(p => p.Clone())); ret.MapFields.AddRange(qr2.MapFields.Select(p => p.Clone())); ret.AliasCount++; if (Expr.Body is NewExpression) { ret.source = new ExprDataSource { LeftSource = (qr1.source != null) ? qr1.source.Clone() : null, RightSource = (qr2.source != null) ? qr2.source.Clone() : null, ParamExpr = Expression.Parameter(ret.ElementType, "p") }; if (ret.source.LeftSource == null) { ret.source.LeftSource = new ExprDataSource { Table = qr1.table, Schema = qr1.schema, Alias = "l" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount, ParamExpr = Expression.Parameter(qr1.ElementType, "p") }; if (qr1.filter != null) { ret.source.LeftSource.filter = qr1.filter.Clone(); } if (qr1.SelectedFields != null) { ret.source.LeftSource.Fields = qr1.SelectedFields.Select(p => p.Clone()).ToList(); } } else { ret.source.LeftSource.Alias = "l" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; } if (ret.source.RightSource == null) { ret.source.RightSource = new ExprDataSource { Table = qr2.table, Schema = qr2.schema, Alias = "r" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount, ParamExpr = Expression.Parameter(qr2.ElementType, "p") }; if (qr2.filter != null) { ret.source.LeftSource.filter = qr2.filter.Clone(); } if (qr2.SelectedFields != null) { ret.source.LeftSource.Fields = qr2.SelectedFields.Select(p => p.Clone()).ToList(); } } else { ret.source.RightSource.Alias = "r" + ret.AliasCount + "" + qr1.AliasCount + "" + qr2.AliasCount; } var nx = Expr.Body as NewExpression; foreach (var x in nx.Arguments) { var isInLeft = true; if (x is MemberExpression) { var mx = x as MemberExpression; var mp = qr1.MapFields.FirstOrDefault(p => p.Member == mx.Member); if (mp == null) { isInLeft = false; mp = qr2.MapFields.FirstOrDefault(p => p.Member == mx.Member); } if (mp != null) { ret.MapFields.Add(new MapFieldInfo() { TableName = (isInLeft) ? ret.source.LeftSource.Alias : ret.source.RightSource.Alias, AliasName = mp.AliasName, Member = mp.Member, Name = mp.Name }); } if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { TableName = (isInLeft) ? ret.source.LeftSource.Alias : ret.source.RightSource.Alias, Name = mp.Name } }); } else if (x is ParameterExpression) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } if (x == Expr.Parameters[0]) { ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { TableName = ret.source.LeftSource.Alias } }); x.Type.GetProperties().ToList().ForEach(p => { var mp = qr1.MapFields.FirstOrDefault(fx => fx.Member == p); ret.MapFields.Add(new MapFieldInfo() { Member = mp.Member, Name = mp.Name, TableName = ret.source.LeftSource.Alias }); }); ret.MapFields.AddRange(x.Type.GetProperties().Select(p => new MapFieldInfo { TableName = ret.source.LeftSource.Alias, Member = p as PropertyInfo }).ToList()); } else { x.Type.GetProperties().ToList().ForEach(p => { var mp = qr2.MapFields.FirstOrDefault(fx => fx.Member == p); ret.MapFields.Add(new MapFieldInfo() { Member = mp.Member, Name = mp.Name, TableName = ret.source.RightSource.Alias }); }); ret.SelectedFields.Add(new TreeExpr { Field = new FieldExpr { TableName = ret.source.RightSource.Alias } }); } } else if (x is BinaryExpression) { if (ret.SelectedFields == null) { ret.SelectedFields = new List <TreeExpr>(); } var F = Compile(ret, x, nx.Members[nx.Arguments.IndexOf(x)]); ret.SelectedFields.Add(F); ret.MapFields.Add(new MapFieldInfo { Alias = ret.Alias, AliasName = ret.Alias, Member = nx.Members[nx.Arguments.IndexOf(x)] as PropertyInfo, Name = nx.Members[nx.Arguments.IndexOf(x)].Name, Schema = ret.schema, TableName = ret.table, ExprField = F }); } else { throw new NotImplementedException(); } } return(ret); } else { 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); }