/// <summary> /// Visits the given query. /// </summary> /// <param name="query">Query to visit.</param> /// <returns>Either the same or a different query.</returns> protected virtual Query Visit(Query query) { Query ret = query; QueryNode visited = null; bool nodesChanged = false; List<QueryNode> nodes = new List<QueryNode>(); if (query != null) { if (query.Nodes.Any()) { foreach (QueryNode n in query.Nodes) { if (n != null) { visited = VisitNode(n); if (visited != null) nodes.Add(visited); else nodesChanged = true; } else nodesChanged = true; } if (!nodesChanged) { nodesChanged = nodes.Count != query.Nodes.Count; if (!nodesChanged) { for (int i = 0; i < nodes.Count; i++) { nodesChanged = nodes[i] != query.Nodes[i]; if (nodesChanged) break; } } } if (nodesChanged) ret = new Query(nodes); } } return ret; }
/// <summary> /// Parses the query from the higher level representation. /// </summary> /// <returns>Parsed query.</returns> public static Query Parse(SearchQuery query) { int currentCount = 0; Queries.Query ret = null; string[] valueComponents = null; object v = null, lb = null, ub = null; List<object> values = new List<object>(); List<Queries.QueryNode> nodes = new List<Queries.QueryNode>(); if (query != null && query.Criteria != null) { foreach (Criterion c in query.Criteria.Where(cr => cr != null && !string.IsNullOrWhiteSpace(cr.Value))) { currentCount = nodes.Count; if (string.IsNullOrWhiteSpace(c.Name)) nodes.Add(new Queries.LiteralQueryNode(c.Value)); else { if (c.Value.Trim().IndexOf(' ') > 0) { v = GetValue(c.Value); if (v != null) nodes.Add(new Queries.SingleValueParameterQueryNode(c.Name, v)); } else { valueComponents = c.Value.Split(new char[] { '-' }, System.StringSplitOptions.RemoveEmptyEntries); if (valueComponents != null && valueComponents.Length == 2 && c.Value.IndexOf(',') < 0 && c.Value.Where(s => s == '-').Count() == 1 && string.Compare(c.Name ?? string.Empty, "tags", System.StringComparison.InvariantCultureIgnoreCase) != 0) { lb = null; ub = null; if (string.Compare((valueComponents[0] ?? string.Empty).Trim(), "*") != 0) lb = GetValue(valueComponents[0]); if (string.Compare((valueComponents[1] ?? string.Empty).Trim(), "*") != 0) ub = GetValue(valueComponents[1]); if (lb != null || ub != null) nodes.Add(new Queries.RangeParameterQueryNode(c.Name, lb, ub)); } if (currentCount == nodes.Count) { valueComponents = c.Value.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries); if (valueComponents != null && valueComponents.Length > 1 && string.Compare(c.Name ?? string.Empty, "location", System.StringComparison.InvariantCultureIgnoreCase) != 0) { values.Clear(); foreach (string vc in valueComponents) { v = GetValue(vc); if (v != null) values.Add(v); } if (values.Any()) nodes.Add(new Queries.ListParameterQueryNode(c.Name, values)); } if (currentCount == nodes.Count) { v = GetValue(c.Value); if (v != null) nodes.Add(new Queries.SingleValueParameterQueryNode(c.Name, v)); } } } } } } if (nodes.Any()) ret = new Query(nodes); return ret; }