Exemplo n.º 1
0
        protected virtual void ApplyContainsText(SqlQuery query, string containsText)
        {
            query.ApplyContainsText(containsText, (text, id) =>
            {
                var fields = GetQuickSearchFields(Request.ContainsField.TrimToNull());
                if (fields == null || !fields.Any())
                {
                    throw new ArgumentOutOfRangeException("containsField");
                }

                var criteria = Criteria.Empty;

                bool orFalse = false;

                foreach (var field in fields)
                {
                    ApplyFieldContainsText(field, containsText, id, ref criteria, ref orFalse);
                }

                if (orFalse && criteria.IsEmpty)
                {
                    criteria |= new Criteria("1 = 0");
                }

                if (!criteria.IsEmpty)
                {
                    query.Where(~(criteria));
                }
            });
        }
Exemplo n.º 2
0
        protected virtual void ApplyContainsText(SqlQuery query, string containsText)
        {
            query.ApplyContainsText(containsText, (text, id) =>
            {
                var fields = GetQuickSearchFields(Request.ContainsField.TrimToNull());

                if (fields == null || !fields.Any())
                {
                    throw new ArgumentOutOfRangeException("containsField");
                }

                var criteria = Criteria.Empty;

                bool orFalse = false;

                foreach (var field in fields)
                {
                    var attr        = field.CustomAttributes == null ? null : field.CustomAttributes.OfType <QuickSearchAttribute>().FirstOrDefault();
                    var searchType  = attr == null ? SearchType.Auto : attr.SearchType;
                    var numericOnly = attr == null ? null : attr.NumericOnly;

                    if (numericOnly == null)
                    {
                        numericOnly = field is Int32Field || field is Int16Field || field is Int64Field;
                    }

                    if (searchType == SearchType.Auto)
                    {
                        if (field is Int32Field || field is Int16Field || field is Int64Field)
                        {
                            searchType = SearchType.Equals;
                        }
                        else
                        {
                            searchType = SearchType.Contains;
                        }
                    }

                    if (numericOnly == true && (id == null))
                    {
                        orFalse = true;
                        continue;
                    }

                    switch (searchType)
                    {
                    case SearchType.Contains:
                        criteria |= new Criteria(field).Contains(containsText);
                        break;

                    case SearchType.StartsWith:
                        criteria |= new Criteria(field).StartsWith(containsText);
                        break;

                    case SearchType.Equals:
                        if (field is Int32Field)
                        {
                            if (id == null || id < Int32.MinValue || id > Int32.MaxValue)
                            {
                                orFalse = true;
                                continue;
                            }

                            criteria |= new Criteria(field) == (int)id;
                        }
                        else if (field is Int16Field)
                        {
                            if (id == null || id < Int16.MinValue || id > Int16.MaxValue)
                            {
                                orFalse = true;
                                continue;
                            }

                            criteria |= new Criteria(field) == (Int16)id;
                        }
                        else if (field is Int64Field)
                        {
                            if (id == null || id < Int64.MinValue || id > Int64.MaxValue)
                            {
                                orFalse = true;
                                continue;
                            }

                            criteria |= new Criteria(field) == id.Value;
                        }
                        else
                        {
                            criteria |= new Criteria(field) == containsText;
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("searchType");
                    }
                }

                if (orFalse && criteria.IsEmpty)
                {
                    criteria |= new Criteria("1 = 0");
                }

                if (!criteria.IsEmpty)
                {
                    query.Where(~(criteria));
                }
            });
        }