Skip to content

Commit 96600bd

Browse files
craiggwilsonrstam
authored andcommitted
minor refactoring.
1 parent f3bd74b commit 96600bd

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

Driver/Linq/Translators/PredicateTranslator.cs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,51 +1050,52 @@ private IMongoQuery BuildStringLengthQuery(Expression variableExpression, Expres
10501050
private IMongoQuery BuildStringCaseComparisonQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
10511051
{
10521052
var methodExpression = variableExpression as MethodCallExpression;
1053-
if (methodExpression != null)
1053+
if (methodExpression == null)
10541054
{
1055-
var sourceExpression = methodExpression.Object as MemberExpression;
1055+
return null;
1056+
}
1057+
var sourceExpression = methodExpression.Object as MemberExpression;
10561058

1057-
if (methodExpression.Type == typeof(string) && sourceExpression != null)
1058-
{
1059-
var serializationInfo = _serializationInfoHelper.GetSerializationInfo(methodExpression.Object);
1060-
var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, constantExpression.Value);
1061-
var coalescedStringValue = constantExpression.Value == null ? string.Empty : serializedValue.AsString;
1059+
if (methodExpression.Type != typeof(string) || sourceExpression == null)
1060+
{
1061+
return null;
1062+
}
10621063

1063-
string regexPattern = "/^" + Regex.Escape(coalescedStringValue) + "$/i";
1064-
var regex = new BsonRegularExpression(regexPattern);
1064+
var serializationInfo = _serializationInfoHelper.GetSerializationInfo(methodExpression.Object);
1065+
var serializedValue = _serializationInfoHelper.SerializeValue(serializationInfo, constantExpression.Value);
1066+
var coalescedStringValue = constantExpression.Value == null ? string.Empty : serializedValue.AsString;
10651067

1066-
bool caseMismatch = false;
1068+
string regexPattern = "/^" + Regex.Escape(coalescedStringValue) + "$/i";
1069+
var regex = new BsonRegularExpression(regexPattern);
10671070

1068-
if (methodExpression.Method.Name == "ToLower" && (coalescedStringValue != coalescedStringValue.ToLower()))
1069-
caseMismatch = true;
1070-
else if (methodExpression.Method.Name == "ToUpper" && (coalescedStringValue != coalescedStringValue.ToUpper()))
1071-
caseMismatch = true;
1072-
else if (constantExpression.Value == null)
1073-
caseMismatch = true;
1071+
bool caseMismatch = false;
10741072

1075-
if (operatorType == ExpressionType.Equal)
1076-
{
1077-
// if comparing Foo.ToLower() == "Some Non Lower Case String"
1078-
// then that is always false for all documents
1079-
if (caseMismatch)
1080-
return Query.Exists("_id", false);
1073+
if (methodExpression.Method.Name == "ToLower" && (coalescedStringValue != coalescedStringValue.ToLower()))
1074+
caseMismatch = true;
1075+
else if (methodExpression.Method.Name == "ToUpper" && (coalescedStringValue != coalescedStringValue.ToUpper()))
1076+
caseMismatch = true;
1077+
else if (constantExpression.Value == null)
1078+
caseMismatch = true;
10811079

1082-
return Query.And(Query.Exists(serializationInfo.ElementName, true),
1083-
Query.Matches(serializationInfo.ElementName, regex));
1084-
}
1085-
else if (operatorType == ExpressionType.NotEqual)
1086-
{
1087-
// if comparing Foo.ToLower() != "Some Non Lower Case String"
1088-
// then that is always true as long as Foo is set/exists
1089-
if (caseMismatch)
1090-
return Query.Exists(serializationInfo.ElementName, true);
1080+
if (operatorType == ExpressionType.Equal)
1081+
{
1082+
// if comparing Foo.ToLower() == "Some Non Lower Case String"
1083+
// then that is always false for all documents
1084+
if (caseMismatch)
1085+
return Query.Exists("_id", false);
10911086

1092-
return Query.And(Query.Exists(serializationInfo.ElementName, true),
1093-
Query.Not(serializationInfo.ElementName).Matches(regex));
1094-
}
1095-
else
1096-
return null;
1097-
}
1087+
return Query.And(Query.Exists(serializationInfo.ElementName, true),
1088+
Query.Matches(serializationInfo.ElementName, regex));
1089+
}
1090+
else if (operatorType == ExpressionType.NotEqual)
1091+
{
1092+
// if comparing Foo.ToLower() != "Some Non Lower Case String"
1093+
// then that is always true as long as Foo is set/exists
1094+
if (caseMismatch)
1095+
return Query.Exists(serializationInfo.ElementName, true);
1096+
1097+
return Query.And(Query.Exists(serializationInfo.ElementName, true),
1098+
Query.Not(serializationInfo.ElementName).Matches(regex));
10981099
}
10991100

11001101
return null;

0 commit comments

Comments
 (0)