Skip to content

Commit

Permalink
Merge pull request #19 from dynamicweb/mss/Conditionals
Browse files Browse the repository at this point in the history
Added better logic for conditionals, and added .ToLower for booleans
  • Loading branch information
MatthiasSort authored Oct 10, 2023
2 parents 2dd3297 + 91b74bf commit c4ab95f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.0.7</Version>
<Version>10.0.8</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>OData Provider</Title>
<Description>The Odata Provider lets you fetch and map data from or to any OData endpoint.</Description>
Expand Down
180 changes: 118 additions & 62 deletions src/ODataSourceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,79 +317,135 @@ private List<string> GetFilterAsParameters()
{
foreach (var item in mappingConditionals)
{
string condition = item.Condition;
string operatorInOData = "";
switch (item.ConditionalOperator)
if (item.Condition != null)
{
case ConditionalOperator.EqualTo:
operatorInOData = "eq";
break;
case ConditionalOperator.LessThan:
operatorInOData = "lt";
break;
case ConditionalOperator.GreaterThan:
operatorInOData = "gt";
break;
case ConditionalOperator.DifferentFrom:
operatorInOData = "ne";
break;
case ConditionalOperator.Contains:
result.Add($"contains({item.SourceColumn.Name},'{item.Condition}')");
continue;
case ConditionalOperator.NotContains:
result.Add($"contains({item.SourceColumn.Name},'{item.Condition}') ne true");
continue;
case ConditionalOperator.In:
operatorInOData = "eq";
List<string> equalConditions = item.Condition.Split(',').Select(val => val.Trim()).ToList();
if (item.SourceColumn.Type == typeof(string))
{
condition = $"{string.Join($"' or {item.SourceColumn.Name} eq '", equalConditions)}";
}
else
{
condition = $"{string.Join($" or {item.SourceColumn.Name} eq ", equalConditions)}";
}
break;
case ConditionalOperator.NotIn:
operatorInOData = "ne";
List<string> notEqualConditions = item.Condition.Split(',').Select(val => val.Trim()).ToList();
if (item.SourceColumn.Type == typeof(string))
{
condition = $"{string.Join($"' and {item.SourceColumn.Name} ne '", notEqualConditions)}";
}
else
{
condition = $"{string.Join($" and {item.SourceColumn.Name} ne ", notEqualConditions)}";
}
break;
case ConditionalOperator.StartsWith:
result.Add($"startswith({item.SourceColumn.Name},'{item.Condition}')");
continue;
case ConditionalOperator.NotStartsWith:
result.Add($"startswith({item.SourceColumn.Name},'{item.Condition}') ne true");
continue;
case ConditionalOperator.EndsWith:
result.Add($"endswith({item.SourceColumn.Name},'{item.Condition}')");
continue;
case ConditionalOperator.NotEndsWith:
result.Add($"endswith({item.SourceColumn.Name},'{item.Condition}') ne true");
continue;
string condition = item.Condition;
string operatorInOData = "";
switch (item.ConditionalOperator)
{
case ConditionalOperator.EqualTo:
operatorInOData = "eq";
break;
case ConditionalOperator.LessThan:
operatorInOData = "lt";
break;
case ConditionalOperator.GreaterThan:
operatorInOData = "gt";
break;
case ConditionalOperator.DifferentFrom:
operatorInOData = "ne";
break;
case ConditionalOperator.Contains:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"contains({item.SourceColumn.Name},'{item.Condition}')");
}
else
{
LogWarningForConditional(item);
}
continue;
case ConditionalOperator.NotContains:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"contains({item.SourceColumn.Name},'{item.Condition}') ne true");
}
else
{
LogWarningForConditional(item);
}
continue;
case ConditionalOperator.In:
operatorInOData = "eq";
List<string> equalConditions = item.Condition.Split(',').Select(val => val.Trim()).ToList();
if (item.SourceColumn.Type == typeof(string))
{
condition = $"{string.Join($"' or {item.SourceColumn.Name} eq '", equalConditions)}";
}
else
{
condition = $"{string.Join($" or {item.SourceColumn.Name} eq ", equalConditions)}";
}
break;
case ConditionalOperator.NotIn:
operatorInOData = "ne";
List<string> notEqualConditions = item.Condition.Split(',').Select(val => val.Trim()).ToList();
if (item.SourceColumn.Type == typeof(string))
{
condition = $"{string.Join($"' and {item.SourceColumn.Name} ne '", notEqualConditions)}";
}
else
{
condition = $"{string.Join($" and {item.SourceColumn.Name} ne ", notEqualConditions)}";
}
break;
case ConditionalOperator.StartsWith:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"startswith({item.SourceColumn.Name},'{item.Condition}')");
}
else
{
LogWarningForConditional(item);
}
continue;
case ConditionalOperator.NotStartsWith:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"startswith({item.SourceColumn.Name},'{item.Condition}') ne true");
}
else
{
LogWarningForConditional(item);
}
continue;
case ConditionalOperator.EndsWith:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"endswith({item.SourceColumn.Name},'{item.Condition}')");
}
else
{
LogWarningForConditional(item);
}
continue;
case ConditionalOperator.NotEndsWith:
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"endswith({item.SourceColumn.Name},'{item.Condition}') ne true");
}
else
{
LogWarningForConditional(item);
}
continue;

}
if (item.SourceColumn.Type == typeof(string))
{
result.Add($"({item.SourceColumn.Name} {operatorInOData} '{condition}')");
}
var conditionToAdd = $"{condition}";
if (item.SourceColumn.Type == typeof(string))
{
conditionToAdd = $"'{condition}'";
}
else if (item.SourceColumn.Type == typeof(bool))
{
conditionToAdd = $"{condition.ToLower()}";
}
result.Add($"({item.SourceColumn.Name} {operatorInOData} {conditionToAdd})");
}
else
{
result.Add($"({item.SourceColumn.Name} {operatorInOData} {condition})");
_logger?.Warn($"The condition for the table mapping {_mapping.SourceTable.Name} to {_mapping.DestinationTable.Name} on source column {item.SourceColumn.Name} is null, so this have been removed from the $filter.");
}
}
}
return result;
}

private void LogWarningForConditional(MappingConditional item)
{
_logger?.Warn($"Can only add {item.ConditionalOperator} on Edm.String and the {item.SourceColumn.Name} is a type of {item.SourceColumn.Type.Name} for the table mapping {_mapping.SourceTable.Name} to {_mapping.DestinationTable.Name}, so this have been removed from the $filter.");
}

/// <summary>
/// Handles a specified response stream, by creating an IEnumerable with yield return, so we can later enumerate the result one object at a time without loading them all into memory
/// </summary>
Expand Down

0 comments on commit c4ab95f

Please sign in to comment.