private static StringBuilder ParseFilter(ICollection <Filter> groups, GroupOp groupOp)
    {
        if (groups == null || groups.Count == 0)
        {
            return(null);
        }
        var  sb         = new StringBuilder();
        bool firstGroup = true;

        foreach (var group in groups)
        {
            var sbGroup = ParseFilter(group);
            if (sbGroup == null || sbGroup.Length == 0)
            {
                continue;
            }
            if (!firstGroup)
            {
                // skip groupOp before the first group
                sb.Append(groupOp);
            }
            else
            {
                firstGroup = false;
            }
            sb.EnsureCapacity(sb.Length + sbGroup.Length + 2);
            AppendWithBrackets(sb, sbGroup);
        }
        return(sb);
    }
    private static StringBuilder ParseRule(ICollection <Rule> rules, GroupOp groupOp)
    {
        if (rules == null || rules.Count == 0)
        {
            return(null);
        }
        var  sb        = new StringBuilder();
        bool firstRule = true;

        foreach (var rule in rules)
        {
            if (!firstRule)
            {
                // skip groupOp before the first rule
                sb.Append(groupOp);
            }
            else
            {
                firstRule = false;
            }
            sb.AppendFormat(FormatMapping[(int)rule.op], rule.field, rule.data);
        }
        return(sb.Length > 0 ? sb : null);
    }
Example #3
0
        private StringBuilder ParseFilter(ICollection<JqGridFilter> groups, GroupOp groupOp, Type targetSearchType)
        {
            if (groups == null || groups.Count == 0)
                return null;

            var sb = new StringBuilder();
            bool firstGroup = true;
            foreach (var group in groups)
            {
                var sbGroup = ParseFilter(group, targetSearchType);
                if (sbGroup == null || sbGroup.Length == 0)
                    continue;

                if (!firstGroup)
                    // skip groupOp before the first group
                    sb.Append(groupOp);
                else
                    firstGroup = false;

                sb.EnsureCapacity(sb.Length + sbGroup.Length + 2);
                AppendWithBrackets(sb, sbGroup);
            }
            return sb;
        }
Example #4
0
        private StringBuilder ParseRule(ICollection <JqGridRule> rules, GroupOp groupOp, Type targetSearchType)
        {
            if (rules == null || rules.Count == 0)
            {
                return(null);
            }

            var  sb        = new StringBuilder();
            bool firstRule = true;
            var  props     = targetSearchType.GetProperties().ToDictionary(p => p.Name, p => p.PropertyType);

            foreach (var rule in rules)
            {
                if (!firstRule)
                {
                    // skip groupOp before the first rule
                    sb.Append(groupOp);
                }
                else
                {
                    firstRule = false;
                }

                // get the object type of the rule
                Type ruleParseType;
                bool emptyNullable = false;
                try
                {
                    Type ruleType = ruleParseType = props[rule.Field];

                    if (ruleType.IsGenericType && ruleType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        if (rule.Data == "")
                        {
                            emptyNullable = true;
                        }
                        ruleParseType = Nullable.GetUnderlyingType(ruleType);
                    }
                }
                catch (KeyNotFoundException)
                {
                    throw new ArgumentOutOfRangeException(rule.Field + " is not a property of type "
                                                          + targetSearchType);
                }


                // parse it in as the correct object type
                var fmAdd = "";
                if (ruleParseType == typeof(string))
                {
                    _formatObjects.Add(rule.Data);
                    if (rule.Data == "")
                    {
                        fmAdd = " OR {0} = NULL";
                    }
                }
                else if (ruleParseType.IsEnum)
                {
                    if (!string.IsNullOrEmpty(rule.Data))
                    {
                        _formatObjects.Add(Enum.Parse(ruleParseType, rule.Data));
                    }
                }
                else
                {
                    if (emptyNullable)
                    {
                        _formatObjects.Add(null);
                    }
                    else
                    {
                        var parseMethod = ruleParseType.GetMethod("Parse", new[] { typeof(string) });
                        if (parseMethod != null)
                        {
                            _formatObjects.Add(parseMethod.Invoke(props[rule.Field], new object[] { rule.Data }));
                        }
                        else
                        {
                            throw new ArgumentOutOfRangeException(rule.Field +
                                                                  " is not a string and cannot be parsed either!!");
                        }
                    }
                }

                string fm = emptyNullable ? _nullValueFormatMapping[(int)rule.Op]
                                : _formatMapping[(int)rule.Op].Replace("{F}", fmAdd);

                sb.AppendFormat(fm, rule.Field, _formatObjects.Count - 1);
            }
            return(sb.Length > 0 ? sb : null);
        }
Example #5
0
        private StringBuilder ParseRule(ICollection<JqGridRule> rules, GroupOp groupOp, Type targetSearchType)
        {
            if (rules == null || rules.Count == 0)
                return null;

            var sb = new StringBuilder();
            bool firstRule = true;
            var props = targetSearchType.GetProperties().ToDictionary(p => p.Name, p => p.PropertyType);

            foreach (var rule in rules)
            {
                if (!firstRule)
                    // skip groupOp before the first rule
                    sb.Append(groupOp);
                else
                    firstRule = false;

                // get the object type of the rule
                Type ruleParseType;
                bool emptyNullable = false;
                try
                {
                    Type ruleType = ruleParseType = props[rule.Field];

                    if (ruleType.IsGenericType && ruleType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        if (rule.Data == "")
                            emptyNullable = true;
                        ruleParseType = Nullable.GetUnderlyingType(ruleType);
                    }

                }
                catch (KeyNotFoundException)
                {
                    throw new ArgumentOutOfRangeException(rule.Field + " is not a property of type "
                                                          + targetSearchType);
                }

                // parse it in as the correct object type
                var fmAdd = "";
                if (ruleParseType == typeof(string))
                {
                    _formatObjects.Add(rule.Data);
                    if (rule.Data == "")
                        fmAdd = " OR {0} = NULL";
                }
                else
                {
                    if (emptyNullable)
                        _formatObjects.Add(null);
                    else
                    {
                        var parseMethod = ruleParseType.GetMethod("Parse", new[] { typeof(string) });
                        if (parseMethod != null)
                            _formatObjects.Add(parseMethod.Invoke(props[rule.Field], new object[] { rule.Data }));
                        else
                            throw new ArgumentOutOfRangeException(rule.Field +
                                                                  " is not a string and cannot be parsed either!!");
                    }
                }

                string fm = emptyNullable ? _nullValueFormatMapping[(int)rule.Op]
                                : _formatMapping[(int)rule.Op].Replace("{F}", fmAdd);

                sb.AppendFormat(fm, rule.Field, _formatObjects.Count - 1);

            }
            return sb.Length > 0 ? sb : null;
        }