public static string GetTextField <T>(SQLInfo <T> sql, DataFieldInfo p, string Quote) { var strFormat = string.Format("{0}{{0}}{1}.{0}{{1}}{1}", Quote[0], Quote[1]); var strAliasFormat = string.Format("{0}{{0}}{1}", Quote[0], Quote[1]); if (p.Expr != null) { return(GetTextField(sql, p.Expr, Quote) + " " + string.Format(strAliasFormat, p.Name)); } if (p.Schema != null) { if (p.Name != "*") { strFormat = string.Format("{0}{{0}}{1}.{0}{{1}}{1}.{0}{{2}}{1}", Quote[0], Quote[1]); return(string.Format(strFormat, p.Schema, p.Table, p.Name)); } else { strFormat = string.Format("{0}{{0}}{1}.{0}{{1}}{1}.*", Quote[0], Quote[1]); return(string.Format(strFormat, p.Schema, p.Table)); } } else { if (p.Name != "*") { return(string.Format(strFormat, p.Table, p.Name)); } else { return(Quote[0] + p.Table + Quote[1] + ".*"); } } }
//[System.Diagnostics.DebuggerStepThrough] private static DataFieldInfo GetExpressionInBinaryExpression <T>( MemberInfo memberInfo, SQLInfo <T> sql, List <DataFieldInfo> fields1, List <DataFieldInfo> fields2, ParameterExpression pExpr1, ParameterExpression pExpr2, string alias1, string alias2, BinaryExpression Expr) { var ret = new DataFieldInfo(); ret.Expr = new TreeExpression(); ret.Expr.Op = Utils.GetOp(Expr.NodeType); ret.Expr.Left = GetExpression(sql, fields1, fields2, pExpr1, pExpr2, alias1, alias2, Expr.Left); ret.Expr.Right = GetExpression(sql, fields1, fields2, pExpr1, pExpr2, alias1, alias2, Expr.Right); ret.Property = memberInfo; ret.ParamExpr = Expression.Parameter(typeof(T), "p"); ret.Name = memberInfo.Name; return(ret); }
private static SQLInfo <T1> SelectByNewExpression <T, T1>(SQLInfo <T> sql, ParameterExpression pExpr, NewExpression Expr) { var retSQl = new SQLInfo <T1>(); DataFieldInfo field = null; if (retSQl.SelectFields == null) { retSQl.SelectFields = new List <DataFieldInfo>(); } foreach (var x in Expr.Arguments) { var mb = Expr.Members[Expr.Arguments.IndexOf(x)]; if (x is BinaryExpression) { var treeExpr = Compiler.Compile(sql, x); field = new DataFieldInfo { Expr = treeExpr, Name = mb.Name, ParamExpr = pExpr, Property = mb }; retSQl.SelectFields.Add(field); } else { field = sql.Fields.FirstOrDefault(p => p.ParamExpr == pExpr && p.Property == mb); if (field == null) { field = sql.Fields.FirstOrDefault(p => p.ParamExpr.Type == pExpr.Type && p.Property.Name == mb.Name); } if (field == null) { field = sql.Fields.FirstOrDefault(p => p.Property.Name == mb.Name); } if (field != null) { if (!(x is MemberExpression)) { retSQl.SelectFields.Add(new DataFieldInfo() { Expr = Compiler.Compile(sql, x), Name = field.Name, ParamExpr = pExpr, Property = pExpr.Type.GetProperty(field.Property.Name), Schema = sql.SchemaName, Table = sql.TableName }); } else { retSQl.SelectFields.Add(new DataFieldInfo() { Name = field.Name, ParamExpr = pExpr, Property = pExpr.Type.GetProperty(field.Property.Name), Schema = sql.SchemaName, Table = sql.TableName }); } } else { throw new Exception(string.Format("'{0}' was not found", mb.Name)); } } } return(retSQl); }
private static List <DataFieldInfo> SelectFieldsInNewExpression <T>( SQLInfo <T> sql, List <DataFieldInfo> fields1, List <DataFieldInfo> fields2, ParameterExpression pExpr1, ParameterExpression pExpr2, string Alias1, string Alias2, NewExpression expr) { var ret = new List <DataFieldInfo>(); foreach (var x in expr.Arguments) { if (x == pExpr1) { ret.Add(new DataFieldInfo() { Name = "*", ParamExpr = pExpr1, Table = Alias1 }); } else if (x == pExpr2) { ret.Add(new DataFieldInfo() { Name = "*", ParamExpr = pExpr2, Table = Alias2 }); } else if (x is MemberExpression) { var mb = x as MemberExpression; if (mb.Expression == pExpr1) { var fMb = fields2.FirstOrDefault(p => p.Property == mb.Member); //if (fMb == null) //{ // fMb = fields2.FirstOrDefault(p => p.Property.Name == mb.Member.Name); //} //if (fMb == null) //{ // fMb = fields2.FirstOrDefault(p => p.Name == mb.Member.Name); //} if (fMb == null) { throw new Exception(string.Format("{0} was not found", mb.Member)); } ret.Add(new DataFieldInfo() { Name = mb.Member.Name, ParamExpr = pExpr1, Table = Alias1 }); } else if (mb.Expression == pExpr2) { var fMb = fields2.FirstOrDefault(p => p.Property == mb.Member); if (fMb == null) { throw new Exception(string.Format("{0} was not found", mb.Member)); } ret.Add(new DataFieldInfo() { Name = fMb.Name, ParamExpr = pExpr1, Table = Alias2 }); } } else if (x is BinaryExpression) { DataFieldInfo field = GetExpressionInBinaryExpression( expr.Members[expr.Arguments.IndexOf(x)], sql, fields1, fields2, pExpr1, pExpr2, Alias1, Alias2, (BinaryExpression)x); ret.Add(field); } else { throw new NotImplementedException(); } } return(ret); }