From 3ab5b2a61aec2c13446f323f49b4ddf5df37d10a Mon Sep 17 00:00:00 2001 From: Markus Hall Date: Wed, 9 Oct 2024 11:03:04 +0200 Subject: [PATCH] Handle dxFilter array of length 1 --- Core.Arango.DevExtreme.Tests/TransformTest.cs | 19 +++ Core.Arango.DevExtreme/ArangoTransform.cs | 126 +++++++++--------- 2 files changed, 80 insertions(+), 65 deletions(-) diff --git a/Core.Arango.DevExtreme.Tests/TransformTest.cs b/Core.Arango.DevExtreme.Tests/TransformTest.cs index 5c19807..160ef7f 100644 --- a/Core.Arango.DevExtreme.Tests/TransformTest.cs +++ b/Core.Arango.DevExtreme.Tests/TransformTest.cs @@ -422,6 +422,25 @@ public void NewInTest() Assert.True(at.Transform(out _)); + _output.WriteLine(at.FilterExpression); + _output.WriteLine(JsonConvert.SerializeObject(at.Parameter.First())); + } + + [Fact] + public void OrAndTest() + { + var loadOptions = DxLoad(key => + { + if (key == "filter") + return WebUtility.UrlDecode( + @"%5B%5B%22searchstring%22%2C%22contains%22%2C%22asdads%22%5D%2C%22and%22%2C%5B%5B%5B%22key%22%2C%22%3D%22%2C%222f8a66f4-367a-4785-ae16-aa040157fc88%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22d3ed9c41-1732-4c6d-8f2d-a9f300dc4f42%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22b22fd5ac-7ec3-4003-a7f3-6ad1f35ad878%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22401871fa-22fd-48c0-b9f9-e06c9d0e3d7e%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22091cfd10-5eaf-4942-b7e0-84662e365749%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%226ce4c205-0f54-4ba6-8000-388ceac27e7a%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%2217f10194-680f-4b2e-ab8b-8912bbd68734%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22b70bda6f-aeae-411b-b4d2-f183143177d4%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22a5bd14bf-e618-45a0-9ec8-bea721e2f587%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22e2187c8c-e677-4aa9-9e00-d7117989af47%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%2215b01cbb-112c-49b9-bf14-c7b57d6a2a8b%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22fb98e956-bec2-4287-92cb-f72a162fccbe%22%5D%2C%22or%22%2C%5B%22key%22%2C%22%3D%22%2C%22985bd70f-cdec-4817-8f36-964af3c07f8e%22%5D%5D%5D%5D"); + return null; + }); + + var at = new ArangoTransform(loadOptions, new ArangoTransformSettings()); + + Assert.True(at.Transform(out _)); + _output.WriteLine(at.FilterExpression); _output.WriteLine(JsonConvert.SerializeObject(at.Parameter.First())); } diff --git a/Core.Arango.DevExtreme/ArangoTransform.cs b/Core.Arango.DevExtreme/ArangoTransform.cs index d8bee5a..24e0340 100644 --- a/Core.Arango.DevExtreme/ArangoTransform.cs +++ b/Core.Arango.DevExtreme/ArangoTransform.cs @@ -36,7 +36,7 @@ public ArangoTransform(DataSourceLoadOptionsBase loadOption, ArangoTransformSett loadOption.Take = settings.MaxTake; } - + public bool HasGrouping { get; } public SummaryInfo[] MergedGroupSummary { get; set; } public SummaryInfo[] MergedTotalSummary { get; set; } @@ -72,7 +72,7 @@ public async Task ExecuteAsync(IArangoContext arango, queryBuilder.AppendLine(_settings.Preamble); queryBuilder.AppendLine($"FOR {_settings.IteratorVar} IN {collection}"); - + if (!string.IsNullOrWhiteSpace(_settings.PreFilter)) queryBuilder.AppendLine(_settings.PreFilter); @@ -81,7 +81,7 @@ public async Task ExecuteAsync(IArangoContext arango, if (_settings.Filter != null) queryBuilder.AppendLine(" && " + _settings.Filter); - + if (!string.IsNullOrWhiteSpace(_settings.PostFilter)) queryBuilder.AppendLine(_settings.PostFilter); @@ -106,18 +106,18 @@ public async Task ExecuteAsync(IArangoContext arango, var query = queryBuilder.ToString(); - + if (HasGrouping) { var res = await arango.Query.ExecuteAsync(handle, query, Parameter, cancellationToken: cancellationToken); var groupData = BuildGrouping(this, res); - + var dxRes = new DxLoadResult { Data = groupData, - TotalCount = _loadOption.RequireTotalCount ? groupData.Sum(x=> x.Count ?? 0) : -1, + TotalCount = _loadOption.RequireTotalCount ? groupData.Sum(x => x.Count ?? 0) : -1, GroupCount = _loadOption.RequireGroupCount ? groupData.Count : -1, }; @@ -127,7 +127,6 @@ public async Task ExecuteAsync(IArangoContext arango, { try { - if (x.StartsWith("SUM") || x.StartsWith("COUNT")) return groupData.Sum(y => y.Summary[idx] ?? 0m); if (x.StartsWith("MAX")) @@ -138,17 +137,15 @@ public async Task ExecuteAsync(IArangoContext arango, { return groupData.Average(y => (y.Summary[idx] ?? 0m)); } - + return 0m; } catch (Exception) { - return (decimal?) 0m; + return (decimal?)0m; } }).ToArray(); - - } @@ -169,10 +166,10 @@ public async Task ExecuteAsync(IArangoContext arango, summaryQueryBuilder.AppendLine(_settings.Preamble); summaryQueryBuilder.AppendLine($"FOR {_settings.IteratorVar} IN {collection}"); - + if (!string.IsNullOrWhiteSpace(_settings.PreFilter)) summaryQueryBuilder.AppendLine(_settings.PreFilter); - + summaryQueryBuilder.AppendLine("FILTER " + FilterExpression); if (_settings.Filter != null) @@ -221,8 +218,6 @@ public bool Transform(out string error) }) .Select(x => x.First()) .ToArray(); - - } if (_loadOption.TotalSummary?.Any() == true) @@ -235,9 +230,8 @@ public bool Transform(out string error) }) .Select(x => x.First()) .ToArray(); - } - + #endregion // TODO: Recursive @@ -290,7 +284,7 @@ public bool Transform(out string error) if (_settings.RestrictGroups != null) if (_loadOption.Group.Any(x => !_settings.RestrictGroups - .Contains(x.Selector.FirstCharOfPropertiesToUpper()))) + .Contains(x.Selector.FirstCharOfPropertiesToUpper()))) { error = "restricted group selector"; return false; @@ -302,7 +296,6 @@ public bool Transform(out string error) FilterExpression = Filter(); - foreach (var a in _extractedFilters.GroupBy(x => x.Item1)) { var subfilters = a.GroupBy(y => y.Item2) @@ -334,7 +327,7 @@ public bool Transform(out string error) private static IList GetRootFilter(IList loadOptionsFilter) { if (loadOptionsFilter != null && loadOptionsFilter.Count == 1) - return GetRootFilter((JArray) loadOptionsFilter[0]); + return GetRootFilter((JArray)loadOptionsFilter[0]); return loadOptionsFilter; } @@ -353,13 +346,12 @@ private string PropertyName(string name, string iteratorVar = null) if (iteratorVar == string.Empty) return name; - + var nameLambda = _settings.PropertyTransform; if (nameLambda != null) return _settings.PropertyTransform(name, _settings); - return $"{iteratorVar ?? _settings.IteratorVar}.{name}"; } @@ -378,9 +370,13 @@ private string Aggregate() var groups = _loadOption.Group.Where(x => x.GroupInterval != "hour" && x.GroupInterval != "minute") .Select(g => { - var selector = PropertyName(_settings.ValidPropertyName(g.Selector).FirstCharOfPropertiesToUpper(), string.Empty); + var selector = + PropertyName(_settings.ValidPropertyName(g.Selector).FirstCharOfPropertiesToUpper(), + string.Empty); - var selectorRight = _settings?.PropertyTransform != null ? _settings.PropertyTransform(selector, _settings) : $"{_settings.IteratorVar}.{selector}"; + var selectorRight = _settings?.PropertyTransform != null + ? _settings.PropertyTransform(selector, _settings) + : $"{_settings.IteratorVar}.{selector}"; var selectorLeft = selector.Replace(".", ""); if (g.GroupInterval == "year") @@ -467,7 +463,7 @@ private string Aggregate() sb.AppendLine("AGGREGATE"); sb.AppendLine(string.Join(", ", aggregates.Distinct())); - var projection = new List {"TotalCount"}; + var projection = new List { "TotalCount" }; foreach (var group in Groups) projection.Add(group); @@ -521,7 +517,9 @@ private string Sort() if (_loadOption.Sort != null) sortingInfos.AddRange(_loadOption.Sort.Where(x => x.Selector != null).ToList()); else - return _settings.StableSort ? $"SORT {_settings.SortIteratorVar ?? _settings.IteratorVar}._key" : string.Empty; + return _settings.StableSort + ? $"SORT {_settings.SortIteratorVar ?? _settings.IteratorVar}._key" + : string.Empty; var sort = "SORT " + string.Join(", ", @@ -542,7 +540,7 @@ private string Sort() return (item[0] is IList); }*/ - + private string GetMatchingFilter(IList dxFilter, bool not = false) { if (dxFilter == null) @@ -551,45 +549,45 @@ private string GetMatchingFilter(IList dxFilter, bool not = false) if (dxFilter.Count == 0) return "true"; - if (dxFilter.Count >= 2) + if (dxFilter.Count == 1) + return GetMatchingFilter((JArray)dxFilter[0], not); + + if (dxFilter.Count == 2) { - if (dxFilter.Count == 2) + if ((dxFilter[0] is JValue v && v.Value is string s && s == "!" + || dxFilter[0] is string s2 && s2 == "!") && dxFilter[1] is JArray) { - if ((dxFilter[0] is JValue v && v.Value is string s && s == "!" - || dxFilter[0] is string s2 && s2 == "!") && dxFilter[1] is JArray) - { - var r = GetMatchingFilter((JArray) dxFilter[1], true); - return $"!({r})"; - } + var r = GetMatchingFilter((JArray)dxFilter[1], true); + return $"!({r})"; + } - dxFilter.Add(dxFilter[1]); + dxFilter.Add(dxFilter[1]); - if (dxFilter[0] is JArray) - dxFilter[1] = JToken.FromObject("and"); - else - dxFilter[1] = JToken.FromObject("="); - } + if (dxFilter[0] is JArray) + dxFilter[1] = JToken.FromObject("and"); else + dxFilter[1] = JToken.FromObject("="); + } + else + { + // [["X","=","A"],["X","=","B"],["X","=","C"]] + var newDxFilterList = new List(); + int i = 0; + foreach (var element in dxFilter) { - // [["X","=","A"],["X","=","B"],["X","=","C"]] - var newDxFilterList = new List(); - int i = 0; - foreach (var element in dxFilter) - { - newDxFilterList.Add(element); - - if (element is JArray && i != dxFilter.Count - 1) - { - if ((dxFilter[i+1] is JArray)) - newDxFilterList.Add(JToken.FromObject("and")); - } - + newDxFilterList.Add(element); - ++i; + if (element is JArray && i != dxFilter.Count - 1) + { + if ((dxFilter[i + 1] is JArray)) + newDxFilterList.Add(JToken.FromObject("and")); } - dxFilter = newDxFilterList; + + ++i; } + + dxFilter = newDxFilterList; } //if (IsCriteria(dxFilter)) @@ -616,7 +614,6 @@ private string GetMatchingFilter(IList dxFilter, bool not = false) //} - var op = dxFilter[1]; string opString; @@ -683,7 +680,7 @@ private string GetMatchingFilter(IList dxFilter, bool not = false) for (var i = 0; i < dxFilter.Count; i++) if (i % 2 == 0) { - logicalResult += GetMatchingFilter((JArray) dxFilter[i], not); + logicalResult += GetMatchingFilter((JArray)dxFilter[i], not); if (i + 1 != dxFilter.Count) logicalResult += $" {opString} "; } @@ -708,7 +705,6 @@ private string GetMatchingFilter(IList dxFilter, bool not = false) else property = PropertyName(realPropertyName); - string boundParam = null; @@ -820,10 +816,10 @@ private string GetMatchingFilter(IList dxFilter, bool not = false) break; } case JArray ja: - { - returnValue = $@"{property} IN {CreateParameter(ja)}"; - break; - } + { + returnValue = $@"{property} IN {CreateParameter(ja)}"; + break; + } default: { var type = rawValue.GetType(); @@ -898,7 +894,7 @@ public List BuildGrouping(ArangoTransform aq, List list, } catch (Exception) { - return (decimal?) 0m; + return (decimal?)0m; } }).ToArray(); } @@ -923,7 +919,7 @@ public List BuildGrouping(ArangoTransform aq, List list, } catch (Exception) { - return (decimal?) 0m; + return (decimal?)0m; } }).ToArray(); }