Skip to content

Commit 4623dfb

Browse files
author
zzzprojects
committed
Added support to ToLower, ToUpper
Added support to ToLower, ToUpper
1 parent c83fd79 commit 4623dfb

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

src/EntityFramework.DynamicFilters/LambdaToDbExpressionVisitor.cs

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private LambdaToDbExpressionVisitor(DynamicFilterDefinition filter, DbExpression
7272
_Filter = filter;
7373
_Binding = binding;
7474
_DbContext = dbContext;
75-
_ObjectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
75+
_ObjectContext = ((IObjectContextAdapter) dbContext).ObjectContext;
7676
_DataSpace = dataSpace;
7777
}
7878

@@ -148,8 +148,8 @@ protected override Expression VisitBinary(BinaryExpression node)
148148

149149
case ExpressionType.Coalesce:
150150
// EF does not expose the "coalesce" function. So best we can do is a case statement. Issue #77.
151-
var whenExpressions = new List<DbExpression>() { DbExpressionBuilder.IsNull(leftExpression) };
152-
var thenExpressions = new List<DbExpression>() { rightExpression };
151+
var whenExpressions = new List<DbExpression>() {DbExpressionBuilder.IsNull(leftExpression)};
152+
var thenExpressions = new List<DbExpression>() {rightExpression};
153153
dbExpression = DbExpressionBuilder.Case(whenExpressions, thenExpressions, leftExpression);
154154
break;
155155

@@ -217,25 +217,25 @@ protected override Expression VisitConstant(ConstantExpression node)
217217
}
218218

219219
if (type == typeof(byte[]))
220-
MapExpressionToDbExpression(expression, DbConstantExpression.FromBinary((byte[])node.Value));
220+
MapExpressionToDbExpression(expression, DbConstantExpression.FromBinary((byte[]) node.Value));
221221
else if (type == typeof(bool))
222-
MapExpressionToDbExpression(expression, DbConstantExpression.FromBoolean((bool?)node.Value));
222+
MapExpressionToDbExpression(expression, DbConstantExpression.FromBoolean((bool?) node.Value));
223223
else if (type == typeof(byte))
224-
MapExpressionToDbExpression(expression, DbConstantExpression.FromByte((byte?)node.Value));
224+
MapExpressionToDbExpression(expression, DbConstantExpression.FromByte((byte?) node.Value));
225225
else if (type == typeof(DateTime))
226-
MapExpressionToDbExpression(expression, DbConstantExpression.FromDateTime((DateTime?)node.Value));
226+
MapExpressionToDbExpression(expression, DbConstantExpression.FromDateTime((DateTime?) node.Value));
227227
else if (type == typeof(DateTimeOffset))
228-
MapExpressionToDbExpression(expression, DbConstantExpression.FromDateTimeOffset((DateTimeOffset?)node.Value));
228+
MapExpressionToDbExpression(expression, DbConstantExpression.FromDateTimeOffset((DateTimeOffset?) node.Value));
229229
else if (type == typeof(decimal))
230-
MapExpressionToDbExpression(expression, DbConstantExpression.FromDecimal((decimal?)node.Value));
230+
MapExpressionToDbExpression(expression, DbConstantExpression.FromDecimal((decimal?) node.Value));
231231
else if (type == typeof(double))
232-
MapExpressionToDbExpression(expression, DbConstantExpression.FromDouble((double?)node.Value));
232+
MapExpressionToDbExpression(expression, DbConstantExpression.FromDouble((double?) node.Value));
233233
else if (type == typeof(Guid))
234-
MapExpressionToDbExpression(expression, DbConstantExpression.FromGuid((Guid?)node.Value));
234+
MapExpressionToDbExpression(expression, DbConstantExpression.FromGuid((Guid?) node.Value));
235235
else if (type == typeof(Int16))
236-
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt16((Int16?)node.Value));
236+
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt16((Int16?) node.Value));
237237
else if (type == typeof(Int32))
238-
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt32((Int32?)node.Value));
238+
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt32((Int32?) node.Value));
239239
else if (type.IsEnum)
240240
{
241241
if (_DataSpace == DataSpace.CSpace)
@@ -244,14 +244,14 @@ protected override Expression VisitConstant(ConstantExpression node)
244244
MapExpressionToDbExpression(expression, DbExpressionBuilder.Constant(typeUsage, node.Value));
245245
}
246246
else
247-
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt32((Int32)node.Value));
247+
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt32((Int32) node.Value));
248248
}
249249
else if (type == typeof(Int64))
250-
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt64((Int64?)node.Value));
250+
MapExpressionToDbExpression(expression, DbConstantExpression.FromInt64((Int64?) node.Value));
251251
else if (type == typeof(float))
252-
MapExpressionToDbExpression(expression, DbConstantExpression.FromSingle((float?)node.Value));
252+
MapExpressionToDbExpression(expression, DbConstantExpression.FromSingle((float?) node.Value));
253253
else if (type == typeof(string))
254-
MapExpressionToDbExpression(expression, DbConstantExpression.FromString((string)node.Value));
254+
MapExpressionToDbExpression(expression, DbConstantExpression.FromString((string) node.Value));
255255
else
256256
throw new NotImplementedException(string.Format("Unhandled Type of {0} for Constant value {1} in LambdaToDbExpressionVisitor.VisitConstant", node.Type.Name, node.Value ?? "null"));
257257

@@ -276,20 +276,20 @@ protected override Expression VisitMember(MemberExpression node)
276276
// If the value should change or be re-evaluated each time the query is executed, it should
277277
// be made a parameter of the filter!
278278
// See https://github.com/jcachat/EntityFramework.DynamicFilters/issues/109
279-
if ((node.Expression is ConstantExpression) || // class field/property
280-
((node.Expression == null) && (node.NodeType == ExpressionType.MemberAccess))) // static field/property
279+
if ((node.Expression is ConstantExpression) || // class field/property
280+
((node.Expression == null) && (node.NodeType == ExpressionType.MemberAccess))) // static field/property
281281
{
282282
// Class fields & properties must reference the container (the class instance that contains the field/property)
283-
object container = (node.Expression != null) ? ((ConstantExpression)node.Expression).Value : null;
283+
object container = (node.Expression != null) ? ((ConstantExpression) node.Expression).Value : null;
284284

285-
if (node.Member is FieldInfo) // regular field property (not a get accessor)
285+
if (node.Member is FieldInfo) // regular field property (not a get accessor)
286286
{
287-
var value = ((FieldInfo)node.Member).GetValue(container);
287+
var value = ((FieldInfo) node.Member).GetValue(container);
288288
return VisitConstant(Expression.Constant(value));
289289
}
290-
else if (node.Member is PropertyInfo) // get accessor
290+
else if (node.Member is PropertyInfo) // get accessor
291291
{
292-
object value = ((PropertyInfo)node.Member).GetValue(container, null);
292+
object value = ((PropertyInfo) node.Member).GetValue(container, null);
293293
return VisitConstant(Expression.Constant(value));
294294
}
295295
else
@@ -400,10 +400,10 @@ protected override Expression VisitParameter(ParameterExpression node)
400400
var expression = base.VisitParameter(node);
401401

402402
if (node.Type.IsClass || node.Type.IsInterface)
403-
return expression; // Ignore class or interface param
403+
return expression; // Ignore class or interface param
404404

405405
if (_Parameters.ContainsKey(node.Name))
406-
return expression; // Already created sql parameter for this node.Name
406+
return expression; // Already created sql parameter for this node.Name
407407

408408
// Create a new DbParameterReferenceExpression for this parameter.
409409
var param = CreateParameter(node.Name, node.Type);
@@ -482,7 +482,6 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
482482
#if (DEBUG_VISITS)
483483
System.Diagnostics.Debug.Print("VisitMethodCall: {0}", node);
484484
#endif
485-
486485
// Do not call base.VisitMethodCall(node) here because of the method that is being
487486
// called has a lambdas as an argument, we need to handle it differently. If we call the base,
488487
// the visits that we do will be against the current binding - not the source of the method call.
@@ -506,6 +505,12 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
506505
case "All":
507506
expression = MapAnyOrAllExpression(node);
508507
break;
508+
case "ToLower":
509+
expression = MapSimpleExpression(node, EdmFunctions.ToLower);
510+
break;
511+
case "ToUpper":
512+
expression = MapSimpleExpression(node, EdmFunctions.ToUpper);
513+
break;
509514
default:
510515
// Anything else is invoked and handled as a constant. This allows us to handle user-defined methods.
511516
// If this evaluates to something that is not a constant, it will throw an exception...which is what we used to do anyway.
@@ -520,7 +525,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
520525

521526
return expression;
522527
}
523-
528+
524529
private Expression MapEnumerableContainsExpression(MethodCallExpression node)
525530
{
526531
var expression = base.VisitMethodCall(node) as MethodCallExpression;
@@ -536,10 +541,10 @@ private Expression MapEnumerableContainsExpression(MethodCallExpression node)
536541
if ((expression.Arguments.Count > 1) && (expression.Object == null))
537542
collectionObjExp = expression.Arguments[0] as ParameterExpression;
538543
if (collectionObjExp != null)
539-
argExpression = GetDbExpressionForExpression(expression.Arguments[1]); // IEnumerable
544+
argExpression = GetDbExpressionForExpression(expression.Arguments[1]); // IEnumerable
540545
else
541546
{
542-
argExpression = GetDbExpressionForExpression(expression.Arguments[0]); // List, IList, ICollection
547+
argExpression = GetDbExpressionForExpression(expression.Arguments[0]); // List, IList, ICollection
543548
collectionObjExp = expression.Object as ParameterExpression;
544549
}
545550

@@ -572,13 +577,13 @@ private Expression MapEnumerableContainsExpression(MethodCallExpression node)
572577
// Find all of the constant & parameter expressions.
573578
var constantExpressionList = listExpression.Initializers
574579
.Select(i => i.Arguments.FirstOrDefault() as ConstantExpression)
575-
.Where(c => (c != null) && (c.Value != null)) // null not supported - can only use DbConstant in "In" expression
580+
.Where(c => (c != null) && (c.Value != null)) // null not supported - can only use DbConstant in "In" expression
576581
.Select(c => CreateConstantExpression(c.Value))
577582
.ToList();
578583
constantExpressionList.AddRange(listExpression.Initializers
579584
.Select(i => i.Arguments.FirstOrDefault() as UnaryExpression)
580585
.Where(c => (c != null) && (c.Operand is ConstantExpression))
581-
.Select(c => CreateConstantExpression(((ConstantExpression)c.Operand).Value)));
586+
.Select(c => CreateConstantExpression(((ConstantExpression) c.Operand).Value)));
582587
var parameterExpressionList = listExpression.Initializers
583588
.Select(i => i.Arguments.FirstOrDefault() as ParameterExpression)
584589
.Where(c => c != null)
@@ -631,6 +636,16 @@ private bool SupportsIn()
631636
return !entityConnection.StoreConnection.GetType().FullName.Contains("Oracle");
632637
}
633638

639+
private Expression MapSimpleExpression(MethodCallExpression node, Func<DbExpression, DbFunctionExpression> dbExpressionFactory)
640+
{
641+
var expression = base.VisitMethodCall(node) as MethodCallExpression;
642+
643+
DbExpression srcExpression = GetDbExpressionForExpression(expression.Object);
644+
var dbExpression = dbExpressionFactory(srcExpression);
645+
MapExpressionToDbExpression(expression, dbExpression);
646+
return expression;
647+
}
648+
634649
private Expression MapStringLikeExpression(MethodCallExpression node, bool matchStart, bool matchEnd)
635650
{
636651
var expression = base.VisitMethodCall(node) as MethodCallExpression;

src/EntityFramework.DynamicFilters/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.11.0")]
36-
[assembly: AssemblyFileVersion("2.11.0")]
37-
[assembly: AssemblyInformationalVersion("2.11.0")]
35+
[assembly: AssemblyVersion("3.0.0")]
36+
[assembly: AssemblyFileVersion("3.0.0")]
37+
[assembly: AssemblyInformationalVersion("3.0.0")]

0 commit comments

Comments
 (0)