Completing the where translater

This commit is contained in:
Zoe Roux 2020-06-30 13:00:06 +02:00
parent ea625fa45b
commit 1da26b449b

View File

@ -245,29 +245,31 @@ namespace Kyoo
ParameterExpression param = Expression.Parameter(typeof(T)); ParameterExpression param = Expression.Parameter(typeof(T));
Expression expression = null; Expression expression = null;
foreach (KeyValuePair<string, string> cond in where) foreach ((string key, string desired) in where)
{ {
string value = cond.Value; string value = desired;
string operand = "eq"; string operand = "eq";
if (value.Contains(':')) if (desired.Contains(':'))
{ {
operand = value.Substring(0, value.IndexOf(':')); operand = desired.Substring(0, desired.IndexOf(':'));
value = value.Substring(value.IndexOf(':') + 1); value = desired.Substring(desired.IndexOf(':') + 1);
} }
PropertyInfo valueParam = typeof(T).GetProperty(cond.Key); // TODO get this property with case insensitive. PropertyInfo property = typeof(T).GetProperty(key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
// TODO throw if valueParam is null. if (property == null)
MemberExpression property = Expression.Property(param, valueParam); throw new ArgumentException($"No filterable parameter with the name {key}.");
ConstantExpression condValue = Expression.Constant(value); // TODO Cast this to the right type (take nullable into account). MemberExpression propertyExpr = Expression.Property(param, property);
ConstantExpression valueExpr = Expression.Constant(Convert.ChangeType(value, property.PropertyType));
Expression condition = operand switch Expression condition = operand switch
{ {
"eq" => Expression.Equal(property, condValue), "eq" => Expression.Equal(propertyExpr, valueExpr),
"not" => Expression.NotEqual(property, condValue), "not" => Expression.NotEqual(propertyExpr, valueExpr),
"lt" => Expression.LessThan(property, condValue), "lt" => Expression.LessThan(propertyExpr, valueExpr),
"lte" => Expression.LessThanOrEqual(property, condValue), "lte" => Expression.LessThanOrEqual(propertyExpr, valueExpr),
"gt" => Expression.GreaterThan(property, condValue), "gt" => Expression.GreaterThan(propertyExpr, valueExpr),
"gte" => Expression.GreaterThanOrEqual(property, condValue), "gte" => Expression.GreaterThanOrEqual(propertyExpr, valueExpr),
// TODO Implement the Like expression
"like" => throw new NotImplementedException("Like not implemented yet"), "like" => throw new NotImplementedException("Like not implemented yet"),
_ => throw new ArgumentException($"Invalid operand: {operand}") _ => throw new ArgumentException($"Invalid operand: {operand}")
}; };