Skip to content

Commit 3ab5b2a

Browse files
author
Markus Hall
committed
Handle dxFilter array of length 1
1 parent 38c045f commit 3ab5b2a

File tree

2 files changed

+80
-65
lines changed

2 files changed

+80
-65
lines changed

Core.Arango.DevExtreme.Tests/TransformTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,25 @@ public void NewInTest()
422422
Assert.True(at.Transform(out _));
423423

424424

425+
_output.WriteLine(at.FilterExpression);
426+
_output.WriteLine(JsonConvert.SerializeObject(at.Parameter.First()));
427+
}
428+
429+
[Fact]
430+
public void OrAndTest()
431+
{
432+
var loadOptions = DxLoad(key =>
433+
{
434+
if (key == "filter")
435+
return WebUtility.UrlDecode(
436+
@"%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");
437+
return null;
438+
});
439+
440+
var at = new ArangoTransform(loadOptions, new ArangoTransformSettings());
441+
442+
Assert.True(at.Transform(out _));
443+
425444
_output.WriteLine(at.FilterExpression);
426445
_output.WriteLine(JsonConvert.SerializeObject(at.Parameter.First()));
427446
}

Core.Arango.DevExtreme/ArangoTransform.cs

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ArangoTransform(DataSourceLoadOptionsBase loadOption, ArangoTransformSett
3636
loadOption.Take = settings.MaxTake;
3737
}
3838

39-
39+
4040
public bool HasGrouping { get; }
4141
public SummaryInfo[] MergedGroupSummary { get; set; }
4242
public SummaryInfo[] MergedTotalSummary { get; set; }
@@ -72,7 +72,7 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
7272
queryBuilder.AppendLine(_settings.Preamble);
7373

7474
queryBuilder.AppendLine($"FOR {_settings.IteratorVar} IN {collection}");
75-
75+
7676
if (!string.IsNullOrWhiteSpace(_settings.PreFilter))
7777
queryBuilder.AppendLine(_settings.PreFilter);
7878

@@ -81,7 +81,7 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
8181
if (_settings.Filter != null)
8282
queryBuilder.AppendLine(" && " + _settings.Filter);
8383

84-
84+
8585
if (!string.IsNullOrWhiteSpace(_settings.PostFilter))
8686
queryBuilder.AppendLine(_settings.PostFilter);
8787

@@ -106,18 +106,18 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
106106

107107
var query = queryBuilder.ToString();
108108

109-
109+
110110
if (HasGrouping)
111111
{
112112
var res = await arango.Query.ExecuteAsync<JObject>(handle, query, Parameter,
113113
cancellationToken: cancellationToken);
114114

115115
var groupData = BuildGrouping(this, res);
116-
116+
117117
var dxRes = new DxLoadResult
118118
{
119119
Data = groupData,
120-
TotalCount = _loadOption.RequireTotalCount ? groupData.Sum(x=> x.Count ?? 0) : -1,
120+
TotalCount = _loadOption.RequireTotalCount ? groupData.Sum(x => x.Count ?? 0) : -1,
121121
GroupCount = _loadOption.RequireGroupCount ? groupData.Count : -1,
122122
};
123123

@@ -127,7 +127,6 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
127127
{
128128
try
129129
{
130-
131130
if (x.StartsWith("SUM") || x.StartsWith("COUNT"))
132131
return groupData.Sum(y => y.Summary[idx] ?? 0m);
133132
if (x.StartsWith("MAX"))
@@ -138,17 +137,15 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
138137
{
139138
return groupData.Average(y => (y.Summary[idx] ?? 0m));
140139
}
141-
140+
142141

143142
return 0m;
144143
}
145144
catch (Exception)
146145
{
147-
return (decimal?) 0m;
146+
return (decimal?)0m;
148147
}
149148
}).ToArray();
150-
151-
152149
}
153150

154151

@@ -169,10 +166,10 @@ public async Task<DxLoadResult> ExecuteAsync<T>(IArangoContext arango,
169166
summaryQueryBuilder.AppendLine(_settings.Preamble);
170167

171168
summaryQueryBuilder.AppendLine($"FOR {_settings.IteratorVar} IN {collection}");
172-
169+
173170
if (!string.IsNullOrWhiteSpace(_settings.PreFilter))
174171
summaryQueryBuilder.AppendLine(_settings.PreFilter);
175-
172+
176173
summaryQueryBuilder.AppendLine("FILTER " + FilterExpression);
177174

178175
if (_settings.Filter != null)
@@ -221,8 +218,6 @@ public bool Transform(out string error)
221218
})
222219
.Select(x => x.First())
223220
.ToArray();
224-
225-
226221
}
227222

228223
if (_loadOption.TotalSummary?.Any() == true)
@@ -235,9 +230,8 @@ public bool Transform(out string error)
235230
})
236231
.Select(x => x.First())
237232
.ToArray();
238-
239233
}
240-
234+
241235
#endregion
242236

243237
// TODO: Recursive
@@ -290,7 +284,7 @@ public bool Transform(out string error)
290284

291285
if (_settings.RestrictGroups != null)
292286
if (_loadOption.Group.Any(x => !_settings.RestrictGroups
293-
.Contains(x.Selector.FirstCharOfPropertiesToUpper())))
287+
.Contains(x.Selector.FirstCharOfPropertiesToUpper())))
294288
{
295289
error = "restricted group selector";
296290
return false;
@@ -302,7 +296,6 @@ public bool Transform(out string error)
302296
FilterExpression = Filter();
303297

304298

305-
306299
foreach (var a in _extractedFilters.GroupBy(x => x.Item1))
307300
{
308301
var subfilters = a.GroupBy(y => y.Item2)
@@ -334,7 +327,7 @@ public bool Transform(out string error)
334327
private static IList GetRootFilter(IList loadOptionsFilter)
335328
{
336329
if (loadOptionsFilter != null && loadOptionsFilter.Count == 1)
337-
return GetRootFilter((JArray) loadOptionsFilter[0]);
330+
return GetRootFilter((JArray)loadOptionsFilter[0]);
338331

339332
return loadOptionsFilter;
340333
}
@@ -353,13 +346,12 @@ private string PropertyName(string name, string iteratorVar = null)
353346

354347
if (iteratorVar == string.Empty)
355348
return name;
356-
349+
357350
var nameLambda = _settings.PropertyTransform;
358351

359352
if (nameLambda != null)
360353
return _settings.PropertyTransform(name, _settings);
361354

362-
363355

364356
return $"{iteratorVar ?? _settings.IteratorVar}.{name}";
365357
}
@@ -378,9 +370,13 @@ private string Aggregate()
378370
var groups = _loadOption.Group.Where(x => x.GroupInterval != "hour" && x.GroupInterval != "minute")
379371
.Select(g =>
380372
{
381-
var selector = PropertyName(_settings.ValidPropertyName(g.Selector).FirstCharOfPropertiesToUpper(), string.Empty);
373+
var selector =
374+
PropertyName(_settings.ValidPropertyName(g.Selector).FirstCharOfPropertiesToUpper(),
375+
string.Empty);
382376

383-
var selectorRight = _settings?.PropertyTransform != null ? _settings.PropertyTransform(selector, _settings) : $"{_settings.IteratorVar}.{selector}";
377+
var selectorRight = _settings?.PropertyTransform != null
378+
? _settings.PropertyTransform(selector, _settings)
379+
: $"{_settings.IteratorVar}.{selector}";
384380
var selectorLeft = selector.Replace(".", "");
385381

386382
if (g.GroupInterval == "year")
@@ -467,7 +463,7 @@ private string Aggregate()
467463
sb.AppendLine("AGGREGATE");
468464
sb.AppendLine(string.Join(", ", aggregates.Distinct()));
469465

470-
var projection = new List<string> {"TotalCount"};
466+
var projection = new List<string> { "TotalCount" };
471467

472468
foreach (var group in Groups)
473469
projection.Add(group);
@@ -521,7 +517,9 @@ private string Sort()
521517
if (_loadOption.Sort != null)
522518
sortingInfos.AddRange(_loadOption.Sort.Where(x => x.Selector != null).ToList());
523519
else
524-
return _settings.StableSort ? $"SORT {_settings.SortIteratorVar ?? _settings.IteratorVar}._key" : string.Empty;
520+
return _settings.StableSort
521+
? $"SORT {_settings.SortIteratorVar ?? _settings.IteratorVar}._key"
522+
: string.Empty;
525523

526524

527525
var sort = "SORT " + string.Join(", ",
@@ -542,7 +540,7 @@ private string Sort()
542540
return (item[0] is IList);
543541
}*/
544542

545-
543+
546544
private string GetMatchingFilter(IList dxFilter, bool not = false)
547545
{
548546
if (dxFilter == null)
@@ -551,45 +549,45 @@ private string GetMatchingFilter(IList dxFilter, bool not = false)
551549
if (dxFilter.Count == 0)
552550
return "true";
553551

554-
if (dxFilter.Count >= 2)
552+
if (dxFilter.Count == 1)
553+
return GetMatchingFilter((JArray)dxFilter[0], not);
554+
555+
if (dxFilter.Count == 2)
555556
{
556-
if (dxFilter.Count == 2)
557+
if ((dxFilter[0] is JValue v && v.Value is string s && s == "!"
558+
|| dxFilter[0] is string s2 && s2 == "!") && dxFilter[1] is JArray)
557559
{
558-
if ((dxFilter[0] is JValue v && v.Value is string s && s == "!"
559-
|| dxFilter[0] is string s2 && s2 == "!") && dxFilter[1] is JArray)
560-
{
561-
var r = GetMatchingFilter((JArray) dxFilter[1], true);
562-
return $"!({r})";
563-
}
560+
var r = GetMatchingFilter((JArray)dxFilter[1], true);
561+
return $"!({r})";
562+
}
564563

565-
dxFilter.Add(dxFilter[1]);
564+
dxFilter.Add(dxFilter[1]);
566565

567-
if (dxFilter[0] is JArray)
568-
dxFilter[1] = JToken.FromObject("and");
569-
else
570-
dxFilter[1] = JToken.FromObject("=");
571-
}
566+
if (dxFilter[0] is JArray)
567+
dxFilter[1] = JToken.FromObject("and");
572568
else
569+
dxFilter[1] = JToken.FromObject("=");
570+
}
571+
else
572+
{
573+
// [["X","=","A"],["X","=","B"],["X","=","C"]]
574+
var newDxFilterList = new List<object>();
575+
int i = 0;
576+
foreach (var element in dxFilter)
573577
{
574-
// [["X","=","A"],["X","=","B"],["X","=","C"]]
575-
var newDxFilterList = new List<object>();
576-
int i = 0;
577-
foreach (var element in dxFilter)
578-
{
579-
newDxFilterList.Add(element);
580-
581-
if (element is JArray && i != dxFilter.Count - 1)
582-
{
583-
if ((dxFilter[i+1] is JArray))
584-
newDxFilterList.Add(JToken.FromObject("and"));
585-
}
586-
578+
newDxFilterList.Add(element);
587579

588-
++i;
580+
if (element is JArray && i != dxFilter.Count - 1)
581+
{
582+
if ((dxFilter[i + 1] is JArray))
583+
newDxFilterList.Add(JToken.FromObject("and"));
589584
}
590585

591-
dxFilter = newDxFilterList;
586+
587+
++i;
592588
}
589+
590+
dxFilter = newDxFilterList;
593591
}
594592

595593
//if (IsCriteria(dxFilter))
@@ -616,7 +614,6 @@ private string GetMatchingFilter(IList dxFilter, bool not = false)
616614
//}
617615

618616

619-
620617
var op = dxFilter[1];
621618

622619
string opString;
@@ -683,7 +680,7 @@ private string GetMatchingFilter(IList dxFilter, bool not = false)
683680
for (var i = 0; i < dxFilter.Count; i++)
684681
if (i % 2 == 0)
685682
{
686-
logicalResult += GetMatchingFilter((JArray) dxFilter[i], not);
683+
logicalResult += GetMatchingFilter((JArray)dxFilter[i], not);
687684
if (i + 1 != dxFilter.Count) logicalResult += $" {opString} ";
688685
}
689686

@@ -708,7 +705,6 @@ private string GetMatchingFilter(IList dxFilter, bool not = false)
708705
else
709706
property = PropertyName(realPropertyName);
710707

711-
712708

713709
string boundParam = null;
714710

@@ -820,10 +816,10 @@ private string GetMatchingFilter(IList dxFilter, bool not = false)
820816
break;
821817
}
822818
case JArray ja:
823-
{
824-
returnValue = $@"{property} IN {CreateParameter(ja)}";
825-
break;
826-
}
819+
{
820+
returnValue = $@"{property} IN {CreateParameter(ja)}";
821+
break;
822+
}
827823
default:
828824
{
829825
var type = rawValue.GetType();
@@ -898,7 +894,7 @@ public List<DxGroupResult> BuildGrouping(ArangoTransform aq, List<JObject> list,
898894
}
899895
catch (Exception)
900896
{
901-
return (decimal?) 0m;
897+
return (decimal?)0m;
902898
}
903899
}).ToArray();
904900
}
@@ -923,7 +919,7 @@ public List<DxGroupResult> BuildGrouping(ArangoTransform aq, List<JObject> list,
923919
}
924920
catch (Exception)
925921
{
926-
return (decimal?) 0m;
922+
return (decimal?)0m;
927923
}
928924
}).ToArray();
929925
}

0 commit comments

Comments
 (0)