Exemplo n.º 1
0
        /// inheritedDoc
        public void AppendQuickSearchCriteria(Type rowType, List <DataTableColumn> columns, QuickSearchMode searchMode, string sSearch, FilterCriteria filterCriteria, Action <FilterCriteria, string> onRequestToQuickSearch, string cacheKey)
        {
            if (string.IsNullOrWhiteSpace(sSearch))
            {
                return;
            }
            if (!rowType.IsEntityType())
            {
                return;
            }

            var subQueries = new List <string>();

            if (searchMode == QuickSearchMode.Sql || searchMode == QuickSearchMode.Combined)
            {
                // get list of properties existing in the table configuration
                var props = GetPropertiesForSqlQuickSearch(rowType, columns, cacheKey);

                if (!props.Any())
                {
                    subQueries.Add("0=1");
                }

                var addSubQuery = new Action <string, object>((q, v) =>
                {
                    var queryParamName = "p" + filterCriteria.FilterParameters.Count.ToString();
                    var criteria       = string.Format((string)q, ":" + queryParamName);
                    subQueries.Add(criteria);

                    filterCriteria.FilterParameters.Add(queryParamName, v);
                });

                foreach (var prop in props)
                {
                    switch (prop.Value)
                    {
                    case GeneralDataType.Text:
                    {
                        if (!prop.Key.Contains('.'))
                        {
                            addSubQuery($"ent.{prop.Key} like {{0}}", "%" + sSearch + "%");
                        }
                        else
                        {
                            // use `exists` for nested entities because NH uses inner joins
                            var nestedEntity = prop.Key.LeftPart('.', ProcessDirection.RightToLeft);
                            var nestedProp   = prop.Key.RightPart('.', ProcessDirection.RightToLeft);

                            addSubQuery($@"exists (from ent.{nestedEntity} where {nestedProp} like {{0}})", "%" + sSearch + "%");
                        }
                        break;
                    }
                    }
                }
            }

            // add custom quick search logic
            if (onRequestToQuickSearch != null)
            {
                var quickSearchCriteria = new FilterCriteria(FilterCriteria.FilterMethod.Hql);

                // copy parameters to fix numbering todo: review and make parameters unique
                foreach (var paramName in filterCriteria.FilterParameters.Keys)
                {
                    quickSearchCriteria.FilterParameters.Add(paramName, filterCriteria.FilterParameters[paramName]);
                }

                onRequestToQuickSearch.Invoke(quickSearchCriteria, sSearch);
                if (quickSearchCriteria.FilterClauses.Any())
                {
                    subQueries.AddRange(quickSearchCriteria.FilterClauses);
                    var missingParameters = quickSearchCriteria.FilterParameters.Keys
                                            .Where(p => !filterCriteria.FilterParameters.Keys.Contains(p)).ToList();
                    foreach (var missingParameter in missingParameters)
                    {
                        filterCriteria.FilterParameters.Add(missingParameter, quickSearchCriteria.FilterParameters[missingParameter]);
                    }
                }
            }

            if (subQueries.Any())
            {
                filterCriteria.FilterClauses.Add(subQueries.Delimited(" or "));
            }
        }
Exemplo n.º 2
0
 public void AppendQuickSearchCriteria(DataTableConfig tableConfig, QuickSearchMode searchMode, string sSearch, FilterCriteria filterCriteria)
 {
     AppendQuickSearchCriteria(tableConfig.RowType, tableConfig.Columns, searchMode, sSearch, filterCriteria, tableConfig.OnRequestToQuickSearch, tableConfig.Id);
 }