示例#1
0
        public static TreeExpression ComplieWithMemberExpression <T>(SQLInfo <T> sql, MemberExpression expr)
        {
            var field = sql.Fields.FirstOrDefault(p => p.ParamExpr == expr.Expression && p.Property == expr.Member);

            if (field == null)
            {
                field = sql.Fields.FirstOrDefault(p => p.ParamExpr.Type == expr.Expression.Type && p.Property.Name == expr.Member.Name);
            }
            if (field == null)
            {
                field = sql.Fields.FirstOrDefault(p => p.Property.Name == expr.Member.Name);
            }
            if (field != null)
            {
                return(new TreeExpression
                {
                    Field = new DataFieldInfo()
                    {
                        ParamExpr = Expression.Parameter(typeof(T), "p"),
                        Name = expr.Member.Name,
                        Property = expr.Member,
                        Schema = sql.SchemaName,
                        Table = sql.TableName
                    }
                });
            }
            else
            {
                throw new Exception(string.Format("'{0}' was not found", expr.Member.Name));
            }
            throw new NotImplementedException();
        }
示例#2
0
        public static List <DataFieldInfo> GetAllFieldsInMemberInitExpression <T>(SQLInfo <T> sql, ParameterExpression pExpr, MemberInitExpression expr)
        {
            var ret = new List <DataFieldInfo>();

            foreach (MemberAssignment x in expr.Bindings)
            {
                var fieldName = x.Member.Name;
                if (x.Expression is MethodCallExpression)
                {
                    var cx = x.Expression as MethodCallExpression;
                    if (cx.Method.DeclaringType == typeof(Factory))
                    {
                        if (cx.Method.Name == "Field")
                        {
                            fieldName = Utils.GetStrValue(cx.Arguments[0]);
                        }
                    }
                    ret.Add(new DataFieldInfo()
                    {
                        ParamExpr = pExpr,
                        Schema    = sql.SchemaName,
                        Table     = sql.TableName,
                        Name      = fieldName,
                        Property  = x.Member
                    });
                }
            }
            return(ret);
        }
示例#3
0
        public static SQLInfo <T> Create <T>(string Schema, string TableName)
        {
            var ret = new SQLInfo <T>()
            {
                SchemaName = Schema,
                TableName  = TableName,
                Fields     = new List <DataFieldInfo>()
            };
            var ParameterExpr = Expression.Parameter(typeof(T), "p");

            ret.SelectFields = new List <DataFieldInfo>();
            ret.SelectFields.Add(new DataFieldInfo()
            {
                Name      = "*",
                ParamExpr = ParameterExpr,
                Schema    = Schema,
                Table     = TableName,
            });
            foreach (var P in typeof(T).GetProperties())
            {
                ret.Fields.Add(new DataFieldInfo()
                {
                    Name      = P.Name,
                    ParamExpr = ParameterExpr,
                    Schema    = Schema,
                    Table     = TableName,
                    Property  = P
                });
            }
            return(ret);
        }
示例#4
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        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] + ".*");
                }
            }
        }
示例#5
0
        public static List <DataFieldInfo> GetAllFieldsInNewExpression <T>(SQLInfo <T> sql, ParameterExpression PExpr, NewExpression expr)
        {
            var ret = new List <DataFieldInfo>();

            foreach (var x in expr.Members)
            {
                var fieldName = x.Name;
                if (expr.Arguments[expr.Members.IndexOf(x)] is MethodCallExpression)
                {
                    var cx = expr.Arguments[expr.Members.IndexOf(x)] as MethodCallExpression;
                    if (cx.Method.DeclaringType == typeof(Factory))
                    {
                        if (cx.Method.Name == "Field")
                        {
                            fieldName = Utils.GetStrValue(cx.Arguments[0]);
                        }
                    }
                }
                ret.Add(new DataFieldInfo()
                {
                    ParamExpr = PExpr,
                    Schema    = sql.SchemaName,
                    Table     = sql.TableName,
                    Name      = fieldName,
                    Property  = x
                });
            }
            return(ret);
        }
示例#6
0
        public static TreeExpression Compile <T>(SQLInfo <T> sql, Expression Expr)
        {
            var ret = new TreeExpression();

            if (Expr is BinaryExpression)
            {
                ret.Op    = Utils.GetOp(Expr.NodeType);
                ret.Left  = Compile(sql, ((BinaryExpression)Expr).Left);
                ret.Right = Compile(sql, ((BinaryExpression)Expr).Right);
                return(ret);
            }
            if (Expr is MemberExpression)
            {
                return(ComplieWithMemberExpression(sql, (MemberExpression)Expr));
            }
            if (Expr is ConstantExpression)
            {
                return(CompileWitConstantEpxression(sql, (ConstantExpression)Expr));
            }
            if (Expr is UnaryExpression)
            {
                return(Compile(sql, ((UnaryExpression)Expr).Operand));
            }
            throw new NotImplementedException();
        }
示例#7
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        public static string GetSql <T>(SQLInfo <T> sql, string Quote)
        {
            var fromStr = string.Format("{0}{{0}}{1}.{0}{{1}}{1}", Quote[0], Quote[1]);
            var ret     = "select " + GetSqlSelectFields(sql, Quote);

            ret += " from " + string.Format(fromStr, sql.SchemaName, sql.TableName);
            return(ret);
        }
示例#8
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        public static string GetSqlSelectFields <T>(SQLInfo <T> sql, string Quote)
        {
            var strFormat  = string.Format("{0}{{0}}{1}.{0}{{1}}{1}", Quote[0], Quote[1]);
            var strFormat2 = string.Format("{0}{{0}}{1}.{0}{{1}}{1}.{0}{{2}}.{1}", Quote[0], Quote[1]);
            var r          = new List <string>();

            return(string.Join(",",
                               sql.SelectFields.Select(p => GetTextField(sql, p, Quote))));
        }
示例#9
0
 public static TreeExpression CompileWitConstantEpxression <T>(SQLInfo <T> sql, ConstantExpression expr)
 {
     return(new TreeExpression
     {
         Value = new FieldValue {
             Val = Expression.Lambda(expr).Compile().DynamicInvoke(),
             DataType = expr.Type
         }
     });
 }
示例#10
0
        public static SQLInfo <T> Create <T>(string Schema, string TableName, Expression <Func <object, T> > Expr)
        {
            var ret = new SQLInfo <T>()
            {
                SchemaName = Schema,
                TableName  = TableName
            };

            return(SqlBuilder.Create(ret, Expr.Parameters[0], Expr.Body));
        }
示例#11
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        public static SQLInfo <T1> Select <T, T1>(SQLInfo <T> sql, Expression <Func <T, T1> > Expr)
        {
            var retSQl = new SQLInfo <T1>();

            if (retSQl.Fields == null)
            {
                retSQl.Fields = new List <DataFieldInfo>();
            }
            if (Expr.Body is NewExpression)
            {
                retSQl = SelectByNewExpression <T, T1>(sql, Expr.Parameters[0], (NewExpression)Expr.Body);
            }
            var Field = sql.Fields.FirstOrDefault(
                p => p.ParamExpr == Expr.Parameters[0] &&
                p.Property == ((MemberExpression)Expr.Body).Member
                );

            if (Field != null)
            {
                retSQl.Fields.Add(new DataFieldInfo()
                {
                    Name      = Field.Name,
                    ParamExpr = Expr.Parameters[0],
                    Property  = typeof(T1).GetProperty(Field.Property.Name),
                    Schema    = sql.SchemaName,
                    Table     = sql.TableName,
                    Expr      = Compiler.Compile(sql, Expr.Body)
                });
            }

            if (sql.AliasName != null)
            {
                retSQl.AliasCount = 0;
                retSQl.AliasName  = "qr" + retSQl.AliasCount;
            }
            else
            {
                retSQl.AliasCount = sql.AliasCount + 1;
                retSQl.AliasName  = "qr" + retSQl.AliasCount;
            }
            retSQl.SelectFields = retSQl.SelectFields.Select(p => new DataFieldInfo()
            {
                Expr      = p.Expr,
                Name      = p.Name,
                ParamExpr = p.ParamExpr,
                Property  = p.Property,
                Table     = retSQl.AliasName
            }).ToList();
            retSQl.Fields = retSQl.SelectFields.ToList();
            return(retSQl);
        }
示例#12
0
 public static List <DataFieldInfo> GetAllFields <T>(SQLInfo <T> sql, ParameterExpression PExpr, Expression Expr)
 {
     if (Expr is NewExpression)
     {
         return(GetAllFieldsInNewExpression(sql, PExpr, (NewExpression)Expr));
     }
     else if (Expr is MemberInitExpression)
     {
         return(GetAllFieldsInMemberInitExpression(sql, PExpr, (MemberInitExpression)Expr));
     }
     else
     {
         throw new NotImplementedException();
     }
 }
示例#13
0
 internal static List <DataFieldInfo> SelectFields <T>(
     SQLInfo <T> sql,
     List <DataFieldInfo> fields1,
     List <DataFieldInfo> fields2,
     ParameterExpression pExpr1,
     ParameterExpression pExpr2,
     string Alias1,
     string Alias2,
     Expression expr)
 {
     if (expr is NewExpression)
     {
         return(SelectFieldsInNewExpression(sql, fields1, fields2, pExpr1, pExpr2, Alias1, Alias2, (NewExpression)expr));
     }
     throw new NotImplementedException();
 }
示例#14
0
 //[System.Diagnostics.DebuggerStepThrough]
 private static TreeExpression GetExpressionInMemberExpression <T>(
     SQLInfo <T> sql,
     List <DataFieldInfo> fields1,
     List <DataFieldInfo> fields2,
     ParameterExpression pExpr1,
     ParameterExpression pExpr2,
     string alias1,
     string alias2,
     MemberExpression expr)
 {
     if (expr.Expression == pExpr1)
     {
         var mb = fields1.FirstOrDefault(p => p.Property == expr.Member);
         return(new TreeExpression
         {
             Field = new DataFieldInfo
             {
                 Name = mb.Name,
                 ParamExpr = pExpr1,
                 Property = mb.Property,
                 Table = alias1
             }
         });
     }
     else if (expr.Expression == pExpr2)
     {
         var mb = fields2.FirstOrDefault(p => p.Property == expr.Member);
         return(new TreeExpression
         {
             Field = new DataFieldInfo
             {
                 Name = mb.Name,
                 ParamExpr = pExpr2,
                 Property = mb.Property,
                 Table = alias2
             }
         });
     }
     else
     {
         throw new NotImplementedException();
     }
 }
示例#15
0
文件: SQL.cs 项目: nttlong/SqlBuilder
 private static string GetTextField <T>(SQLInfo <T> sql, TreeExpression expr, string quote)
 {
     if (expr.Op != null)
     {
         return("(" + GetTextField(sql, expr.Left, quote) + ")" + expr.Op + "(" + GetTextField(sql, expr.Right, quote) + ")");
     }
     if (expr.Field != null)
     {
         return(GetTextField(sql, expr.Field, quote));
     }
     if (expr.Value != null)
     {
         if (sql.Params == null)
         {
             sql.Params = new List <FieldValue>();
         }
         sql.Params.Add(expr.Value);
         return("{" + (sql.Params.Count - 1) + "}");
     }
     throw new NotImplementedException();
 }
示例#16
0
        //[System.Diagnostics.DebuggerStepThrough]
        private static TreeExpression GetExpression <T>(
            SQLInfo <T> sql,
            List <DataFieldInfo> fields1,
            List <DataFieldInfo> fields2,
            ParameterExpression pExpr1,
            ParameterExpression pExpr2,
            string alias1,
            string alias2,
            Expression Expr)
        {
            var ret = new TreeExpression();

            if (Expr is UnaryExpression)
            {
                return(GetExpression(sql, fields1, fields2, pExpr1, pExpr2, alias1, alias2, ((UnaryExpression)Expr).Operand));
            }
            if (Expr is MemberExpression)
            {
                return(GetExpressionInMemberExpression(sql, fields1, fields2, pExpr1, pExpr2, alias1, alias2, (MemberExpression)Expr));
            }
            return(ret);
        }
示例#17
0
        //[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);
        }
示例#18
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        //[System.Diagnostics.DebuggerStepThrough]
        public static SQLInfo <T> Combine <T1, T2, T>(SQLInfo <T1> sql1, SQLInfo <T2> sql2, Expression <Func <T1, T2, T> > Selector)
        {
            var ret = new SQLInfo <T>();

            if (sql1.Params != null)
            {
                ret.Params.AddRange(sql1.Params);
            }
            if (sql1.Params != null)
            {
                ret.Params.AddRange(sql2.Params);
            }
            ret.SelectFields = Combinator.SelectFields(
                ret,
                sql1.Fields,
                sql2.Fields,
                Selector.Parameters[0],
                Selector.Parameters[1],
                sql1.AliasName,
                sql2.AliasName,
                Selector.Body);
            ret.Fields = ret.SelectFields.ToList();
            return(ret);
        }
示例#19
0
 public static SQLInfo <T> Create <T>(SQLInfo <T> Sql, ParameterExpression PExpr, Expression Expr)
 {
     Sql.Fields = GetAllFields(Sql, PExpr, Expr);
     return(Sql);
 }
示例#20
0
文件: SQL.cs 项目: nttlong/SqlBuilder
        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);
        }
示例#21
0
        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);
        }