private static IEnumerable <SearchItem> SpreadExpression(string query, SearchExpressionContext c) { var spreaded = new List <SpreadContext>(); var toSpread = new List <SpreadContext>() { new SpreadContext(query.ToString(), c.ResolveAlias()) }; foreach (var e in c.args) { spreaded = new List <SpreadContext>(); foreach (var r in e.Execute(c)) { if (r != null) { foreach (var q in toSpread) { if (r.value == null) { continue; } var replacement = r.value.ToString(); if (replacement.LastIndexOf(' ') != -1) { replacement = '"' + replacement + '"'; } var pattern = @"[\[\{]?" + Regex.Escape(e.outerText.ToString()) + @"[\}\]]?"; spreaded.Add(new SpreadContext(Regex.Replace(q.query, pattern, replacement), alias: c.ResolveAlias(e, replacement))); } } else { yield return(null); } } toSpread = spreaded; } foreach (var s in spreaded) { if (c.flags.HasFlag(SearchExpressionExecutionFlags.Expand)) { yield return(SearchExpression.CreateSearchExpressionItem(s.query, s.alias)); } else { foreach (var r in RunQuery(c, s.query)) { yield return(r); } } } }
public static IEnumerable <SearchItem> GroupBy(SearchExpressionContext c) { string selector = null; if (c.args.Length > 1) { selector = c.args[1].innerText.ToString(); } var outputValueFieldName = System.Guid.NewGuid().ToString("N"); var dataSet = SelectorManager.SelectValues(c.search, c.args[0].Execute(c), selector, outputValueFieldName); foreach (var _group in dataSet.GroupBy(item => item.GetValue(outputValueFieldName))) { var group = _group; var groupId = group.Key?.ToString() ?? $"group{++s_NextGroupId}"; if (c.HasFlag(SearchExpressionExecutionFlags.Expand)) { var evaluator = new SearchExpressionEvaluator(groupId, _ => group, SearchExpressionEvaluationHints.Default); var genExpr = new SearchExpression(SearchExpressionType.Group, groupId.GetStringView(), groupId.GetStringView(), (group.Key?.ToString() ?? groupId).GetStringView(), evaluator); yield return(SearchExpression.CreateSearchExpressionItem(genExpr)); } else { SearchProvider groupProvider = null; foreach (var item in group) { if (groupProvider == null) { groupProvider = SearchUtils.CreateGroupProvider(item.provider, groupId, s_NextGroupId); } item.provider = groupProvider; yield return(item); } } } }
public static IEnumerable <SearchItem> Alias(SearchExpressionContext c) { var aliasSelector = c.args.Last(); if (c.HasFlag(SearchExpressionExecutionFlags.Expand)) { yield return(SearchExpression.CreateSearchExpressionItem(new SearchExpression(c.args[0], newAlias: aliasSelector.innerText))); } else { foreach (var r in c.args[0].Execute(c)) { if (r == null) { yield return(null); continue; } var hasFormatString = SearchExpression.GetFormatString(aliasSelector, out var formatStr); if (hasFormatString && aliasSelector.types.HasAny(SearchExpressionType.Text)) { r.label = SearchExpression.FormatItem(c.search, r, formatStr); } else if (aliasSelector.types.HasAny(SearchExpressionType.Selector)) { r.label = SelectorManager.SelectValue(r, c.search, aliasSelector.innerText.ToString()).ToString(); yield return(r); } else if (aliasSelector.types.HasAny(SearchExpressionType.Iterable)) { bool valueSelected = false; using (c.runtime.Push(r)) { foreach (var s in aliasSelector.Execute(c)) { if (s != null) { r.label = s.value.ToString(); valueSelected = true; break; } else { yield return(null); } } } if (!valueSelected) { r.label = r.label = SearchExpression.FormatItem(c.search, r, aliasSelector.innerText.ToString()); } } else { c.ThrowError($"Alias selector `{aliasSelector.outerText}` not supported", aliasSelector.outerText); } yield return(r); } } }