예제 #1
0
        private void BuildSelect(Expression expression)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression;

            if (this.ConditionPars["select"].Length == 0)
            {
                if (methodCallExpression.Method.Name == "Count")
                {
                    this.ConditionPars["orderby"] = new StringBuilder();
                    this.ConditionPars["select"]  = new StringBuilder("count(*)");
                }
                else
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    Type          type          = methodCallExpression.Method.ReturnType;
                    if (type.Name == "IQueryable`1")
                    {
                        type = type.GetGenericArguments()[0];
                    }
                    PropertyInfo[] propertys = ConvertHelper.GetPropertys(type);
                    PropertyInfo[] array     = propertys;
                    for (int i = 0; i < array.Length; i++)
                    {
                        PropertyInfo propertyInfo = array[i];
                        stringBuilder.Append(propertyInfo.Name + ",");
                    }
                    this.ConditionPars["select"] = stringBuilder;
                }
            }
            if (this.ConditionPars["from"].Length == 0)
            {
                this.ConditionPars["from"] = new StringBuilder(this.t.Name);
            }
        }
예제 #2
0
        private string FieldsByNew <F>(Expression <Func <M, F> > fields)
        {
            MemberExpression memberExpression = null;

            if (fields.Body.NodeType == ExpressionType.Convert)
            {
                UnaryExpression body = (UnaryExpression)fields.Body;
                memberExpression = body.Operand as MemberExpression;

                return(memberExpression.Member.Name);
            }
            else if (fields.Body.NodeType == ExpressionType.MemberAccess)
            {
                memberExpression = fields.Body as MemberExpression;

                return(memberExpression.Member.Name);
            }
            else if (fields.Body.NodeType == ExpressionType.Parameter)
            {
                StringBuilder  stringBuilder = new StringBuilder();
                PropertyInfo[] propertys     = ConvertHelper.GetPropertys(this.type);
                PropertyInfo[] array         = propertys;
                for (int i = 0; i < array.Length; i++)
                {
                    PropertyInfo propertyInfo = array[i];
                    stringBuilder.Append(propertyInfo.Name + ",");
                }
                stringBuilder = stringBuilder.Remove(stringBuilder.Length - 1, 1);
                return(stringBuilder.ToString());
            }
            throw new ArgumentException("无法解析出字段");
        }
예제 #3
0
        public static List <T> GetList <T>(DataTable dt) where T : new()
        {
            List <T> list = new List <T>();

            if (dt == null)
            {
                return(list);
            }
            Type typeFromHandle = typeof(T);

            PropertyInfo[] propertys = ConvertHelper.GetPropertys(typeFromHandle);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object         obj   = Activator.CreateInstance(typeFromHandle);
                PropertyInfo[] array = propertys;
                for (int j = 0; j < array.Length; j++)
                {
                    PropertyInfo propertyInfo = array[j];
                    if (dt.Columns.Contains(propertyInfo.Name))
                    {
                        object obj2 = dt.Rows[i][propertyInfo.Name];
                        if (obj2 != null && obj2 != DBNull.Value)
                        {
                            if (!propertyInfo.PropertyType.IsGenericType)
                            {
                                propertyInfo.SetValue(obj, Convert.ChangeType(obj2, propertyInfo.PropertyType), null);
                            }
                            else
                            {
                                Type genericTypeDefinition = propertyInfo.PropertyType.GetGenericTypeDefinition();
                                if (genericTypeDefinition == typeof(Nullable <>))
                                {
                                    propertyInfo.SetValue(obj, Convert.ChangeType(obj2, Nullable.GetUnderlyingType(propertyInfo.PropertyType)), null);
                                }
                            }
                        }
                    }
                }
                list.Add((T)((object)obj));
            }
            return(list);
        }
예제 #4
0
        public long Insert(M entity)
        {
            PropertyInfo[]      propertys = ConvertHelper.GetPropertys(this.type);
            string              text      = "insert into " + this.type.Name + "({0}) values ({1});";
            List <SqlParameter> list      = new List <SqlParameter>();

            if (this.autoIncrement != string.Empty)
            {
                text += "select  @@IDENTITY;";
            }
            StringBuilder stringBuilder  = new StringBuilder();
            StringBuilder stringBuilder2 = new StringBuilder();

            PropertyInfo[] array = propertys;
            for (int i = 0; i < array.Length; i++)
            {
                PropertyInfo propertyInfo = array[i];
                if (propertyInfo.Name.ToLower() != this.autoIncrement.ToLower())
                {
                    list.Add(new SqlParameter("@" + propertyInfo.Name, TypeConvert.ConvertDbType(propertyInfo.PropertyType.Name))
                    {
                        Value = propertyInfo.GetValue(entity, null)
                    });
                    stringBuilder.Append(propertyInfo.Name + ",");
                    stringBuilder2.Append("@" + propertyInfo.Name + ",");
                }
            }
            text = string.Format(text, stringBuilder.ToString().TrimEnd(new char[]
            {
                ','
            }), stringBuilder2.ToString().TrimEnd(new char[]
            {
                ','
            }));
            object single = this.DBbase.GetSingle(text, list.ToArray());

            if (single != null)
            {
                return(Convert.ToInt64(single));
            }
            return(0L);
        }
예제 #5
0
        public static IList GetList(DataTable dt, Type t)
        {
            Type typeFromHandle = typeof(List <>);
            Type type           = typeFromHandle.MakeGenericType(new Type[]
            {
                t
            });
            IList list = Activator.CreateInstance(type) as IList;

            PropertyInfo[] propertys = ConvertHelper.GetPropertys(t);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object         obj   = Activator.CreateInstance(t);
                PropertyInfo[] array = propertys;
                for (int j = 0; j < array.Length; j++)
                {
                    PropertyInfo propertyInfo = array[j];
                    object       obj2         = dt.Rows[i][propertyInfo.Name];
                    if (obj2 != null && obj2 != DBNull.Value)
                    {
                        if (!propertyInfo.PropertyType.IsGenericType)
                        {
                            propertyInfo.SetValue(obj, Convert.ChangeType(obj2, propertyInfo.PropertyType), null);
                        }
                        else
                        {
                            Type genericTypeDefinition = propertyInfo.PropertyType.GetGenericTypeDefinition();
                            if (genericTypeDefinition == typeof(Nullable <>))
                            {
                                propertyInfo.SetValue(obj, Convert.ChangeType(obj2, Nullable.GetUnderlyingType(propertyInfo.PropertyType)), null);
                            }
                        }
                    }
                }
                list.Add(obj);
            }
            return(list);
        }
예제 #6
0
        public bool Update(M entity)
        {
            PropertyInfo[]      propertys = ConvertHelper.GetPropertys(this.type);
            string              text      = "update " + this.type.Name + "{0} {1};";
            List <SqlParameter> list      = new List <SqlParameter>();

            if (this.unique == string.Empty)
            {
                throw new ArgumentException("没有找到主键列");
            }
            StringBuilder stringBuilder = new StringBuilder(" set ");
            string        arg           = string.Empty;

            PropertyInfo[] array = propertys;
            for (int i = 0; i < array.Length; i++)
            {
                PropertyInfo propertyInfo = array[i];
                list.Add(new SqlParameter("@" + propertyInfo.Name, TypeConvert.ConvertDbType(propertyInfo.PropertyType.Name))
                {
                    Value = propertyInfo.GetValue(entity, null)
                });
                if (propertyInfo.Name.ToLower() != this.unique.ToLower())
                {
                    stringBuilder.Append(propertyInfo.Name + "=@" + propertyInfo.Name + ",");
                }
                else
                {
                    arg = " where " + propertyInfo.Name + "=@" + propertyInfo.Name;
                }
            }
            text = string.Format(text, stringBuilder.ToString().TrimEnd(new char[]
            {
                ','
            }), arg);
            return(this.DBbase.ExecuteSql(text, list.ToArray()) != 0);
        }
예제 #7
0
        public bool Update <V>(V value, Expression <Func <M, bool> > expression)
        {
            int                 num           = 0;
            string              text          = "update " + this.type.Name + "{0} {1};";
            ConditionBuilder    builder       = this.GetBuilder(expression);
            StringBuilder       stringBuilder = new StringBuilder(" set ");
            List <SqlParameter> arguments     = builder.Arguments;

            PropertyInfo[] propertys = ConvertHelper.GetPropertys(value.GetType());
            PropertyInfo[] array     = propertys;
            for (int i = 0; i < array.Length; i++)
            {
                PropertyInfo propertyInfo = array[i];
                num++;
                string text2 = "@U" + propertyInfo.Name + num;
                stringBuilder.Append(propertyInfo.Name + "=" + text2 + ",");
                arguments.Add(new SqlParameter(text2, propertyInfo.GetValue(value, null)));
            }
            text = string.Format(text, stringBuilder.ToString().TrimEnd(new char[]
            {
                ','
            }), builder.Condition);
            return(this.DBbase.ExecuteSql(text, arguments.ToArray()) != 0);
        }
예제 #8
0
        protected virtual Expression VisitMethodCall(MethodCallExpression m)
        {
            if (this.ConditionPars == null)
            {
                Expression expression = this.Visit(m.Object);
                IEnumerable <Expression> enumerable = this.VisitExpressionList(m.Arguments);
                if (expression != m.Object || enumerable != m.Arguments)
                {
                    return(Expression.Call(expression, m.Method, enumerable));
                }
            }
            else
            {
                if (m == null)
                {
                    return(m);
                }
                string arg_4F_0 = string.Empty;
                string value    = string.Empty;
                string name;
                if ((name = m.Method.Name) != null)
                {
                    if (_sqlKeywords == null)
                    {
                        _sqlKeywords = new Dictionary <string, int>(12)
                        {
                            {
                                "Contains",
                                0
                            },

                            {
                                "FirstOrDefault",
                                1
                            },

                            {
                                "First",
                                2
                            },

                            {
                                "Select",
                                3
                            },

                            {
                                "Count",
                                4
                            },

                            {
                                "Skip",
                                5
                            },

                            {
                                "Take",
                                6
                            },

                            {
                                "OrderBy",
                                7
                            },

                            {
                                "OrderByDescending",
                                8
                            },

                            {
                                "Where",
                                9
                            },

                            {
                                "WhereAnd",
                                10
                            },

                            {
                                "WhereOr",
                                11
                            }
                        };
                    }
                    int num;
                    if (_sqlKeywords.TryGetValue(name, out num))
                    {
                        switch (num)
                        {
                        case 0:
                        {
                            string name2;
                            if ((name2 = m.Arguments[0].Type.Name) != null && (name2 == "String[]" || name2 == "Int64[]" || name2 == "Int32[]" || name2 == "List`1"))
                            {
                                this.Visit(m.Arguments[1]);
                                this.ConditionPars["where"].Append(" in (");
                                this.Visit(m.Arguments[0]);
                                this.ConditionPars["where"].Append(") ");
                                return(m);
                            }
                            this.Visit(m.Object);
                            this.ConditionPars["where"].Append(" like ");
                            this.Visit(m.Arguments[0]);
                            return(m);
                        }

                        case 1:
                        case 2:
                            foreach (Expression current in m.Arguments)
                            {
                                if (current.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current);
                                }
                            }
                            this.ConditionPars["top"] = new StringBuilder("1");
                            return(m);

                        case 3:
                        {
                            foreach (Expression current2 in m.Arguments)
                            {
                                if (current2.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current2);
                                }
                            }
                            UnaryExpression  unaryExpression  = (UnaryExpression)m.Arguments[1];
                            LambdaExpression lambdaExpression = (LambdaExpression)unaryExpression.Operand;
                            StringBuilder    stringBuilder    = new StringBuilder();
                            if (lambdaExpression.Body.Type.Equals(ExpressionType.New))
                            {
                                NewExpression newExpression = (NewExpression)lambdaExpression.Body;
                                using (IEnumerator <MemberInfo> enumerator3 = newExpression.Members.GetEnumerator())
                                {
                                    while (enumerator3.MoveNext())
                                    {
                                        MemberInfo current3 = enumerator3.Current;
                                        stringBuilder.Append(current3.Name + ",");
                                    }
                                    goto IL_3FF;
                                }
                            }
                            PropertyInfo[] propertys = ConvertHelper.GetPropertys(lambdaExpression.Body.Type);
                            PropertyInfo[] array     = propertys;
                            for (int i = 0; i < array.Length; i++)
                            {
                                PropertyInfo propertyInfo = array[i];
                                stringBuilder.Append(propertyInfo.Name + ",");
                            }
IL_3FF:
                            stringBuilder.Remove(stringBuilder.Length - 1, 1);
                            this.ConditionPars["select"] = stringBuilder;
                            this.ConditionPars["from"].Append(lambdaExpression.Type.GetGenericArguments()[0].Name);
                            return(m);
                        }

                        case 4:
                            foreach (Expression current4 in m.Arguments)
                            {
                                if (current4.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current4);
                                }
                            }
                            this.ConditionPars["select"]  = new StringBuilder("count(*)");
                            this.ConditionPars["orderby"] = new StringBuilder();
                            return(m);

                        case 5:
                            foreach (Expression current5 in m.Arguments)
                            {
                                if (current5.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current5);
                                }
                            }
                            this.ConditionPars["top"].Append(m.Arguments[1].ToString());
                            return(m);

                        case 6:
                            foreach (Expression current6 in m.Arguments)
                            {
                                if (current6.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current6);
                                }
                            }
                            this.ConditionPars["top"].Append(m.Arguments[1].ToString());
                            return(m);

                        case 7:
                            foreach (Expression current7 in m.Arguments)
                            {
                                if (current7.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current7);
                                }
                            }
                            value = this.GetMemName(m) + ",";
                            this.ConditionPars["orderby"].Append(value);
                            return(m);

                        case 8:
                            foreach (Expression current8 in m.Arguments)
                            {
                                if (current8.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current8);
                                }
                            }
                            value = this.GetMemName(m) + " desc ,";
                            this.ConditionPars["orderby"].Append(value);
                            return(m);

                        case 9:
                            using (IEnumerator <Expression> enumerator9 = m.Arguments.GetEnumerator())
                            {
                                while (enumerator9.MoveNext())
                                {
                                    Expression current9 = enumerator9.Current;
                                    if (current9.NodeType.Equals(ExpressionType.Quote) || current9.NodeType.Equals(ExpressionType.Call))
                                    {
                                        this.Visit(current9);
                                    }
                                }
                                return(m);
                            }
                            break;

                        case 10:
                            break;

                        case 11:
                            goto IL_7D8;

                        default:
                            goto IL_867;
                        }
                        using (IEnumerator <Expression> enumerator10 = m.Arguments.GetEnumerator())
                        {
                            while (enumerator10.MoveNext())
                            {
                                Expression current10 = enumerator10.Current;
                                if (current10.NodeType.Equals(ExpressionType.Quote))
                                {
                                    this.Visit(current10);
                                }
                                if (current10.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current10);
                                    this.ConditionPars["where"].Append(" and ");
                                }
                            }
                            return(m);
                        }
IL_7D8:
                        using (IEnumerator <Expression> enumerator11 = m.Arguments.GetEnumerator())
                        {
                            while (enumerator11.MoveNext())
                            {
                                Expression current11 = enumerator11.Current;
                                if (current11.NodeType.Equals(ExpressionType.Quote))
                                {
                                    this.Visit(current11);
                                }
                                if (current11.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current11);
                                    this.ConditionPars["where"].Append(" or ");
                                }
                            }
                            return(m);
                        }
                    }
                }
IL_867:
                throw new NotSupportedException(m.NodeType + " is not supported!");
            }
            return(m);
        }