Ejemplo n.º 1
0
        public object GetSum <F>(Expression <Func <M, bool> > expression, Expression <Func <M, F> > fields)
        {
            ConditionBuilder builder = this.GetBuilder(expression);
            string           text    = this.FieldsByNew <F>(fields);
            string           text2   = string.Concat(new string[]
            {
                "select sum(",
                text,
                ") from ",
                this.type.Name,
                " {0}"
            });

            text2 = string.Format(text2, builder.Condition);
            object obj = this.DBbase.GetSingle(text2, builder.Arguments.ToArray());

            if (obj != null)
            {
                obj = Convert.ToDecimal(obj).ToString("G0");
            }
            else
            {
                obj = 0;
            }
            return(obj);
        }
Ejemplo n.º 2
0
        public int Delete(Expression <Func <M, bool> > expression)
        {
            string           text    = "delete from " + this.type.Name + " {0}";
            ConditionBuilder builder = this.GetBuilder(expression);

            text = string.Format(text, builder.Condition);
            return(this.DBbase.ExecuteSql(text, builder.Arguments.ToArray()));
        }
Ejemplo n.º 3
0
        public int GetCount(Expression <Func <M, bool> > expression)
        {
            ConditionBuilder builder = this.GetBuilder(expression);
            string           text    = "select count(*) from " + this.type.Name + " {0}";

            text = string.Format(text, builder.Condition);
            object single = this.DBbase.GetSingle(text, builder.Arguments.ToArray());

            return(Convert.ToInt32(single));
        }
Ejemplo n.º 4
0
        private ConditionBuilder GetBuilder(Expression <Func <M, bool> > expression)
        {
            ConditionBuilder conditionBuilder = new ConditionBuilder();

            if (expression == null)
            {
                conditionBuilder.Condition = string.Empty;
            }
            else
            {
                conditionBuilder.Build(expression.Body, this.type, false);
            }
            return(conditionBuilder);
        }
Ejemplo n.º 5
0
        public M Select(Expression <Func <M, bool> > expression)
        {
            ConditionBuilder builder = this.GetBuilder(expression);
            string           text    = "select top 1 * from " + this.type.Name + " {0} ";

            text = string.Format(text, builder.Condition);
            DataTable dataTable = this.DBbase.GetDataTable(text, builder.Arguments.ToArray());
            IList <M> list      = ConvertHelper.GetList <M>(dataTable);
            M         result    = default(M);

            if (list != null && list.Count > 0)
            {
                result = list[0];
            }
            return(result);
        }
Ejemplo n.º 6
0
        public object GetSingle <F>(Expression <Func <M, bool> > expression, Expression <Func <M, F> > fields)
        {
            ConditionBuilder builder = this.GetBuilder(expression);
            string           text    = this.FieldsByNew <F>(fields);
            string           text2   = string.Concat(new string[]
            {
                "select ",
                text,
                " from ",
                this.type.Name,
                " {0}"
            });

            text2 = string.Format(text2, builder.Condition);
            return(this.DBbase.GetSingle(text2, builder.Arguments.ToArray()));
        }
Ejemplo n.º 7
0
        public List <M> GetListRow <TKey, F>(Expression <Func <M, bool> > expression, Expression <Func <M, TKey> > orderby, bool IsDesc, int pStart, int pSize, Expression <Func <M, F> > fields)
        {
            ConditionBuilder builder = this.GetBuilder(expression);
            string           str     = string.Empty;

            if (orderby != null)
            {
                str = " order by " + ((MemberExpression)orderby.Body).Member.Name + (IsDesc ? " desc " : " asc ");
            }
            string str2 = this.FieldsByNew <F>(fields);

            string sql = string.Format("select " + str2 + ", ROW_NUMBER() OVER({1}) AS RowNumber  from " + this.type.Name + " {0} ", builder.Condition, str);

            if (pSize > 0)
            {
                sql = string.Concat("select * from (", sql, ") tttx where RowNumber Between ", (pStart - 1) * pSize + 1, " and ", pStart * pSize);
            }
            DataTable dataTable = this.DBbase.GetDataTable(sql, builder.Arguments.ToArray());

            builder.Arguments = null;
            return(ConvertHelper.GetList <M>(dataTable));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        private object ProviderCallBack(Expression expression, Type returnType)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression;
            ConditionBuilder     conditionBuilder     = new ConditionBuilder();

            conditionBuilder.Build(expression, this.type, true);
            object obj = null;
            string name;

            if ((name = methodCallExpression.Method.Name) != null)
            {
                if (_sqlKeywords == null)
                {
                    _sqlKeywords = new Dictionary <string, int>(8)
                    {
                        {
                            "FirstOrDefault",
                            0
                        },

                        {
                            "First",
                            1
                        },

                        {
                            "Select",
                            2
                        },

                        {
                            "Skip",
                            3
                        },

                        {
                            "Take",
                            4
                        },

                        {
                            "OrderByDescending",
                            5
                        },

                        {
                            "OrderBy",
                            6
                        },

                        {
                            "Count",
                            7
                        }
                    };
                }
                int num;
                if (_sqlKeywords.TryGetValue(name, out num))
                {
                    DataTable dataTable;
                    switch (num)
                    {
                    case 0:
                    case 1:
                    {
                        dataTable = this.DBbase.GetDataTable(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                        object obj2;
                        if (AnonymouseHelper.IsAnonyousType(returnType))
                        {
                            obj2 = ConvertHelper.GetAnonymous(dataTable, returnType);
                        }
                        else
                        {
                            obj2 = ConvertHelper.GetList(dataTable, returnType);
                        }
                        IEnumerable enumerable = (IEnumerable)obj2;
                        IEnumerator enumerator = enumerable.GetEnumerator();
                        try
                        {
                            if (enumerator.MoveNext())
                            {
                                object current = enumerator.Current;
                                obj = current;
                            }
                            return(obj);
                        }
                        finally
                        {
                            IDisposable disposable = enumerator as IDisposable;
                            if (disposable != null)
                            {
                                disposable.Dispose();
                            }
                        }
                        break;
                    }

                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        break;

                    case 7:
                        obj = this.DBbase.GetSingle(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                        obj = Convert.ToInt32(obj);
                        return(obj);

                    default:
                        return(obj);
                    }
                    Type[] genericArguments = returnType.GetGenericArguments();
                    returnType = ((genericArguments.Length == 0) ? this.type : genericArguments[0]);
                    dataTable  = this.DBbase.GetDataTable(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                    if (AnonymouseHelper.IsAnonyousType(returnType))
                    {
                        obj = ConvertHelper.GetAnonymous(dataTable, returnType);
                    }
                    else
                    {
                        obj = ConvertHelper.GetList(dataTable, returnType);
                    }
                    obj = (IEnumerable)obj;
                }
            }
            return(obj);
        }