Пример #1
0
 public WhereBool True()
 {
     if (!string.IsNullOrEmpty(_builder.ToString()))
     {
         _builder.Append(" ").Append(_operator.ToString().ToUpper()).Append(" ");
     }
     Value = true;
     _builder.Append("{0} = 1");
     return(this);
 }
Пример #2
0
 public WhereDate GreaterThan(DateTime value)
 {
     if (!string.IsNullOrEmpty(_builder.ToString()))
     {
         _builder.Append(" ").Append(_operator.ToString().ToUpper()).Append(" ");
     }
     Value = value;
     _builder.Append("{0} > '")
     .Append(NormalizeDate(value))
     .Append("'");
     return(this);
 }
Пример #3
0
        public QueryFilter <T> Where(Expression <Func <T, object> > expression, QueryOperator queryOperator, string value)
        {
            var type = new T();

            MemberExpression body = expression.Body as MemberExpression;

            if (body == null)
            {
                UnaryExpression ubody = (UnaryExpression)expression.Body;
                body = ubody.Operand as MemberExpression;
            }

            var parameterName = body.Member.Name;

            if (!type.FieldsToFilter.Contains(parameterName, StringComparer.InvariantCultureIgnoreCase))
            {
                throw new Exception($"Filtering for parameter {parameterName} is not allowed, see the EConomic Api documentation for further explanation");
            }

            var filterValue = new QueryFilterValue
            {
                Parameter       = parameterName,
                CompareOperator = queryOperator.ToString().ToLower(),
                Value           = value
            };

            Filters.Add(filterValue);

            return(this);
        }
Пример #4
0
        public static string ToString(QueryOperator op)
        {
            switch (op)
            {
            case QueryOperator.Add:
                return(" + ");

            case QueryOperator.BitwiseAND:
                return(" & ");

            case QueryOperator.BitwiseNOT:
                return(" ~ ");

            case QueryOperator.BitwiseOR:
                return(" | ");

            case QueryOperator.BitwiseXOR:
                return(" ^ ");

            case QueryOperator.Divide:
                return(" / ");

            case QueryOperator.Equal:
                return(" = ");

            case QueryOperator.Greater:
                return(" > ");

            case QueryOperator.GreaterOrEqual:
                return(" >= ");

            case QueryOperator.IsNULL:
                return(" IS NULL ");

            case QueryOperator.Less:
                return(" < ");

            case QueryOperator.LessOrEqual:
                return(" <= ");

            case QueryOperator.Like:
                return(" LIKE ");

            case QueryOperator.Modulo:
                return(" % ");

            case QueryOperator.Multiply:
                return(" * ");

            case QueryOperator.NotEqual:
                return(" <> ");

            case QueryOperator.Subtract:
                return(" - ");
            }

            throw new NotSupportedException("Unknown QueryOperator: " + op.ToString() + "!");
        }
Пример #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public static string ToString(QueryOperator op)
        {
            switch (op)
            {
                case QueryOperator.Add:
                    return " + ";
                case QueryOperator.BitwiseAND:
                    return " & ";
                case QueryOperator.BitwiseNOT:
                    return " ~ ";
                case QueryOperator.BitwiseOR:
                    return " | ";
                case QueryOperator.BitwiseXOR:
                    return " ^ ";
                case QueryOperator.Divide:
                    return " / ";
                case QueryOperator.Equal:
                    return " = ";
                case QueryOperator.Greater:
                    return " > ";
                case QueryOperator.GreaterOrEqual:
                    return " >= ";
                case QueryOperator.IsNULL:
                    return " IS NULL ";
                case QueryOperator.IsNotNULL:
                    return " IS NOT NULL ";
                case QueryOperator.Less:
                    return " < ";
                case QueryOperator.LessOrEqual:
                    return " <= ";
                case QueryOperator.Like:
                    return " LIKE ";
                case QueryOperator.Modulo:
                    return " % ";
                case QueryOperator.Multiply:
                    return " * ";
                case QueryOperator.NotEqual:
                    return " <> ";
                case QueryOperator.Subtract:
                    return " - ";
            }

            throw new NotSupportedException("Unknown QueryOperator: " + op.ToString() + "!");
        }
Пример #6
0
        public IQuery Where(string field, QueryOperator queryOperator, object value)
        {
            switch (queryOperator)
            {
            case QueryOperator.Equal:
                queries.Add(collection.WhereEqualTo(field, value));
                break;

            case QueryOperator.LessThan:
                queries.Add(collection.WhereLessThan(field, value));
                break;

            case QueryOperator.GreaterThan:
                queries.Add(collection.WhereGreaterThan(field, value));
                break;

            case QueryOperator.LessThanOrEqualTo:
                queries.Add(collection.WhereLessThanOrEqualTo(field, value));
                break;

            case QueryOperator.GreaterThanOrEqualTo:
                queries.Add(collection.WhereGreaterThanOrEqualTo(field, value));
                break;

            case QueryOperator.Contains:
                // Because Firestore does not support this query operation
                // we need to include this sorting query to retrieve the list
                // of values to be filtered
                OrderBy(field, SortDirection.Asc);

                filterCommands.Add(new FilterCommand
                {
                    FilterKey   = field,
                    Operator    = queryOperator,
                    FilterValue = value
                });
                break;

            default:
                throw new System.ArgumentException($"Query operator not found", queryOperator.ToString());
            }

            return(this);
        }
        private static WhereClip LeftAndRight(BinaryExpression be, QueryOperator co, WhereType wtype = WhereType.Where)
        {
            ColumnFunction   leftFunction;
            ColumnFunction   rightFunction;
            MemberExpression leftMe = null;
            MemberExpression rightMe;

            System.Linq.Expressions.Expression expLeft  = be.Left;
            System.Linq.Expressions.Expression expRight = be.Right;
            if (be.Left.NodeType == ExpressionType.Convert)
            {
                expLeft = ((UnaryExpression)be.Left).Operand;
            }
            if (be.Right.NodeType == ExpressionType.Convert)
            {
                expRight = ((UnaryExpression)be.Right).Operand;
            }
            var isAgain = false;

Again:
            if (expLeft.NodeType == ExpressionType.Constant ||
                (expLeft.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expLeft).Expression == null) || isAgain)
            {
                if (expRight.NodeType == ExpressionType.Constant ||
                    (expRight.NodeType == ExpressionType.MemberAccess && ((MemberExpression)expRight).Expression == null))
                {
                    return(DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be))
                        ? new WhereClip(" 1=2 ")
                        : new WhereClip(" 1=1 "));
                }
                else
                {
                    var keyRightName = GetMemberName(expRight, out rightFunction, out rightMe);

                    if (expLeft.NodeType == ExpressionType.MemberAccess)
                    {
                        var left = (MemberExpression)expLeft;
                        if (left.Expression != null && (wtype == WhereType.JoinWhere || left.Expression.ToString() == rightMe.Expression.ToString()))
                        {
                            ColumnFunction functionLeft;
                            var            keyLeft = GetMemberName(expLeft, out functionLeft, out left);
                            if (keyRightName.Contains("$"))
                            {
                                return(new WhereClip(new Field(keyLeft, left.Expression.Type), GetValue(expRight), co));
                            }
                            else
                            {
                                return(new WhereClip(new Field(keyRightName, rightMe.Expression.Type), new Field(keyLeft, left.Expression.Type), co));
                            }
                        }
                    }
                    object value = GetValue(expLeft);
                    if (keyRightName.Contains("$"))
                    {
                        if (DataUtils.ConvertValue <bool>(fastEvaluator.Eval(be)))
                        {
                            return(new WhereClip(" 1=2 "));
                        }
                        return(new WhereClip(" 1=1 "));
                    }
                    if (value != null)
                    {
                        return(new WhereClip(new Field(keyRightName, rightMe.Expression.Type), value, co));
                    }
                    switch (co)
                    {
                    case QueryOperator.Equal:
                        return(new Field(keyRightName, rightMe.Expression.Type).IsNull());

                    case QueryOperator.NotEqual:
                        return(new Field(keyRightName, rightMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!出错比较符:" + co.ToString());
                }
            }
            else
            {
                var key = "";
                try
                {
                    key = GetMemberName(expLeft, out leftFunction, out leftMe);
                    if (key.Contains("$"))
                    {
                        isAgain = true;
                        goto Again;
                    }
                }
                catch (Exception)
                {
                    isAgain = true;
                    goto Again;
                }
                if (expRight.NodeType == ExpressionType.MemberAccess)
                {
                    var right = (MemberExpression)expRight;
                    if (right.Expression != null && (wtype == WhereType.JoinWhere || right.Expression == leftMe.Expression))
                    {
                        ColumnFunction functionRight;
                        var            keyRight = GetMemberName(expRight, out functionRight, out right);
                        return(new WhereClip(
                                   new Field(key, leftMe.Expression.Type),
                                   new Field(keyRight, right.Expression.Type)
                                   , co));
                    }
                }
                object value = GetValue(expRight);
                if (value == null)
                {
                    if (co == QueryOperator.Equal)
                    {
                        return(new Field(key, leftMe.Expression.Type).IsNull());
                    }
                    if (co == QueryOperator.NotEqual)
                    {
                        return(new Field(key, leftMe.Expression.Type).IsNotNull());
                    }
                    throw new Exception("null值只支持等于或不等于!");
                }
                return(new WhereClip(new Field(key, leftMe.Expression.Type), value, co));
            }
        }
Пример #8
0
        public static MvcHtmlString QueryDropdownList(this HtmlHelper html, string properyName, IEnumerable <SelectListItem> selectList,
                                                      QueryOperator opt = QueryOperator.EQUAL, string valuetype = null, String excludeValue = null)
        {
            var    select     = "<select id='{0}' name='{1}'>{2}</select>";
            var    option     = "<option {2} value='{0}'>{1}</option>";
            String optionList = String.Empty;

            foreach (SelectListItem item in selectList)
            {
                optionList += String.Format(option, item.Value, item.Text, item.Selected ? "selected=\"selected\"" : String.Empty);
            }
            var res = string.Format(select, properyName, "_query." + properyName, optionList);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "'/>";
            }
            if (!string.IsNullOrEmpty(valuetype))
            {
                res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='" + valuetype + "'/>";
            }
            if (!String.IsNullOrEmpty(excludeValue))
            {
                res += "<input type='hidden' name='_query." + properyName + ".exclude' value='" + excludeValue + "'/>";
            }
            return(MvcHtmlString.Create(res));
        }
Пример #9
0
        public static MvcHtmlString QueryTextbox(this HtmlHelper html, string properyName, QueryOperator opt = QueryOperator.EQUAL, string valuetype = null, object htmlAttributes = null)
        {
            string attrstring = string.Empty;

            if (htmlAttributes != null)
            {
                Type tt = htmlAttributes.GetType();
                foreach (var info in tt.GetProperties())
                {
                    attrstring += info.Name + "=\"" + info.GetValue(htmlAttributes, null) + "\"   ";
                }
            }
            var input = "<input  type='text' name='{0}' {1}/>";
            var res   = string.Format(input, "_query." + properyName, attrstring);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "' />";
            }

            if (!string.IsNullOrEmpty(valuetype))
            {
                res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='" + valuetype + "'  />";
            }
            return(MvcHtmlString.Create(res));
        }
Пример #10
0
        public static MvcHtmlString QueryTextbox(this HtmlHelper html, string properyName, QueryOperator opt = QueryOperator.EQUAL, string valuetype = null)
        {
            var input = "<input class='input-medium search-query' type='text' name='{0}'/>";
            var res   = string.Format(input, "_query." + properyName);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "'/>";
            }

            if (!string.IsNullOrEmpty(valuetype))
            {
                res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='" + valuetype + "'/>";
            }
            return(MvcHtmlString.Create(res));
        }
Пример #11
0
        public static MvcHtmlString QueryEnumDropdownList(this HtmlHelper html, string properyName, Type enumType,
                                                          QueryOperator opt = QueryOperator.EQUAL, String excludeValue = null)
        {
            // Looks for a [Display(Name="Some Name")] or a [Display(Name="Some Name", ResourceType=typeof(ResourceFile)] Attribute on your enum
            Func <Enum, string> getDescription = en =>
            {
                Type type = en.GetType();
                System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);

                    if (attrs != null && attrs.Length > 0)
                    {
                        return(((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetName());
                    }
                }

                return(en.ToString());
            };

            var    select     = "<select id='{0}' name='{1}'>{2}</select>";
            var    option     = "<option {2} value='{0}'>{1}</option>";
            String optionList = String.Empty;

            var listItems = Enum.GetValues(enumType).OfType <Enum>();

            int count = 0;

            foreach (Enum obj in listItems)
            {
                optionList += String.Format(option, obj, getDescription(obj), (count == 0) ? "selected=\"selected\"" : String.Empty);
                count++;
            }

            var res = string.Format(select, properyName, "_query." + properyName, optionList);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "'/>";
            }
            if (!String.IsNullOrEmpty(excludeValue))
            {
                res += "<input type='hidden' name='_query." + properyName + ".exclude' value='" + excludeValue + "'/>";
            }
            res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='String'/>";
            return(MvcHtmlString.Create(res));
        }
Пример #12
0
        public static MvcHtmlString QueryDropdownList(this HtmlHelper html, string properyName, IEnumerable <SelectListItem> selectList,
                                                      QueryOperator opt = QueryOperator.EQUAL, string valuetype = null, String excludeValue = null, object htmlAttributes = null)
        {
            string attrstring = string.Empty;

            if (htmlAttributes != null)
            {
                Type tt = htmlAttributes.GetType();
                foreach (var info in tt.GetProperties())
                {
                    attrstring += info.Name + "=\"" + info.GetValue(htmlAttributes, null) + "\"     ";
                }
            }
            var    select     = "<select id='{0}' name='{1}' {3}>{2}</select>";
            var    option     = "<option {2} value='{0}'>{1}</option>";
            String optionList = String.Empty;

            if (selectList != null)
            {
                foreach (SelectListItem item in selectList)
                {
                    optionList += String.Format(option, item.Value, item.Text, item.Selected ? "selected=\"selected\"" : String.Empty);
                }
            }
            var res = string.Format(select, properyName, "_query." + properyName, optionList, attrstring);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "'/>";
            }
            if (!string.IsNullOrEmpty(valuetype))
            {
                res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='" + valuetype + "'/>";
            }
            if (!String.IsNullOrEmpty(excludeValue))
            {
                res += "<input type='hidden' name='_query." + properyName + ".exclude' value='" + excludeValue + "'/>";
            }

            return(MvcHtmlString.Create(res));
        }
Пример #13
0
        public static string ToString(QueryOperator op)
        {
            switch (op)
            {
                case QueryOperator.Add:
                    return " + ";
                case QueryOperator.BitwiseAND:
                    return " & ";
                case QueryOperator.BitwiseNOT:
                    return " ~ ";
                case QueryOperator.BitwiseOR:
                    return " | ";
                case QueryOperator.BitwiseXOR:
                    return " ^ ";
                case QueryOperator.Divide:
                    return " / ";
                case QueryOperator.Equal:
                    return " = ";
                case QueryOperator.Greater:
                    return " > ";
                case QueryOperator.GreaterOrEqual:
                    return " >= ";
                case QueryOperator.IsNULL:
                    return " IS NULL ";
                case QueryOperator.Less:
                    return " < ";
                case QueryOperator.LessOrEqual:
                    return " <= ";
                case QueryOperator.Like:
                    return " LIKE ";
                case QueryOperator.Modulo:
                    return " % ";
                case QueryOperator.Multiply:
                    return " * ";
                case QueryOperator.NotEqual:
                    return " <> ";
                case QueryOperator.Subtract:
                    return " - ";
            }

            throw new NotSupportedException("Unknown QueryOperator: " + op.ToString() + "!");
        }
Пример #14
0
        /// <summary>
        /// 绑定下拉列表 枚举
        /// </summary>
        /// <param name="html"></param>
        /// <param name="properyName">字段名称</param>
        /// <param name="enumType">枚举类型 例如typeof(Enum)</param>
        /// <param name="defalutV">扩展项值</param>
        /// <param name="defaultName">扩展项名称 空不扩展</param>
        /// <param name="val">默认选择项</param>
        /// <param name="opt"></param>
        /// <param name="ignore">忽略选择项 如 new string[]{"2"}</param>
        /// <param name="excludeValue"></param>
        /// <returns></returns>
        public static MvcHtmlString QueryEnumForIntDropdownListLYM(this HtmlHelper html, string properyName, Type enumType, string defalutV, string defaultName, object val,
                                                                   QueryOperator opt = QueryOperator.EQUAL, string[] ignore = null, string valuetype = null, String excludeValue = null, object htmlAttributes = null)
        {
            string attrstring = string.Empty;

            if (htmlAttributes != null)
            {
                Type tt = htmlAttributes.GetType();
                foreach (var info in tt.GetProperties())
                {
                    attrstring += info.Name + "=\"" + info.GetValue(htmlAttributes, null) + "\"     ";
                }
            }

            // Looks for a [Display(Name="Some Name")] or a [Display(Name="Some Name", ResourceType=typeof(ResourceFile)] Attribute on your enum
            Func <Enum, string> getDescription = en =>
            {
                Type type = en.GetType();
                System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);

                    if (attrs != null && attrs.Length > 0)
                    {
                        return(((System.ComponentModel.DescriptionAttribute)attrs[0]).Description);
                    }
                }

                return(en.ToString());
            };

            Func <Enum, int?> GetEnumValue = en =>
            {
                Type type = en.GetType();
                System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());

                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);

                    if (attrs != null && attrs.Length > 0)
                    {
                        return(((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetOrder());
                    }
                }

                return(null);
            };

            var    select     = "<select id='{0}' name='{1}'  " + attrstring + ">{2}</select>";
            var    option     = "<option {2} value='{0}'>{1}</option>";
            String optionList = String.Empty;

            var listItems = Enum.GetValues(enumType).OfType <Enum>();



            if (!string.IsNullOrEmpty(defaultName))
            {
                optionList += string.Format(option, defalutV, defaultName, (val == (object)defalutV) ? "selected=\"selected\"" : String.Empty);
            }
            int count = 0;

            Dictionary <string, int?> lst = new Dictionary <string, int?>();

            foreach (Enum obj in listItems)
            {
                int?   value = GetEnumValue(obj);
                string key   = getDescription(obj);
                if (ignore != null)
                {
                    if (!ignore.ToList().Contains(value + ""))
                    {
                        lst.Add(key, value);
                    }
                }
                else
                {
                    lst.Add(key, value);
                }


                count++;
            }
            lst = lst.OrderBy(x => x.Value).ToDictionary(c => c.Key, c => c.Value);

            foreach (var item in lst)
            {
                bool isSelect = (((object)item.Value).ToString() == val.ToString());
                optionList += String.Format(option, item.Value, item.Key, isSelect ? "selected=\"selected\"" : String.Empty);
            }
            var res = string.Format(select, properyName, "_query." + properyName, optionList);

            if (QueryOperator.EQUAL != opt)
            {
                res += "<input type='hidden' name='_query." + properyName + ".operator' value='" + opt.ToString() + "'/>";
            }
            if (!string.IsNullOrEmpty(valuetype))
            {
                res += "<input type='hidden' name='_query." + properyName + ".valuetype' value='" + valuetype + "'/>";
            }
            if (!String.IsNullOrEmpty(excludeValue))
            {
                res += "<input type='hidden' name='_query." + properyName + ".exclude' value='" + excludeValue + "'/>";
            }

            return(MvcHtmlString.Create(res));
        }