Skip to content
Browse files

Minor refactoring. Only check for constant on the left of comparison …

…operator in one place.
  • Loading branch information...
1 parent a6c995c commit 018d7da9327eb928adad939115ef553bb346fbd6 Robert Stam committed Apr 4, 2012
Showing with 30 additions and 41 deletions.
  1. +30 −41 Driver/Linq/Translators/SelectQuery.cs
View
71 Driver/Linq/Translators/SelectQuery.cs
@@ -238,18 +238,14 @@ private IMongoQuery BuildAnyQuery(MethodCallExpression methodCallExpression)
return null;
}
- private IMongoQuery BuildArrayLengthQuery(BinaryExpression binaryExpression)
+ private IMongoQuery BuildArrayLengthQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
{
- // the constant could be on either side (but only == and != are supported and they don't need to be flipped)
- var variableExpression = binaryExpression.Left;
- var constantExpression = binaryExpression.Right as ConstantExpression;
- if (constantExpression == null)
+ if (operatorType != ExpressionType.Equal && operatorType != ExpressionType.NotEqual)
{
- constantExpression = binaryExpression.Left as ConstantExpression;
- variableExpression = binaryExpression.Right;
+ return null;
}
-
- if (constantExpression == null || constantExpression.Type != typeof(int))
+
+ if (constantExpression.Type != typeof(int))
{
return null;
}
@@ -302,7 +298,7 @@ private IMongoQuery BuildArrayLengthQuery(BinaryExpression binaryExpression)
if (serializationInfo != null)
{
- if (binaryExpression.NodeType == ExpressionType.Equal)
+ if (operatorType == ExpressionType.Equal)
{
return Query.Size(serializationInfo.ElementName, value);
}
@@ -330,36 +326,21 @@ private IMongoQuery BuildBooleanQuery(Expression expression)
private IMongoQuery BuildComparisonQuery(BinaryExpression binaryExpression)
{
- var operatorNodeType = binaryExpression.NodeType;
- if (operatorNodeType == ExpressionType.Equal || operatorNodeType == ExpressionType.NotEqual)
- {
- var query = BuildArrayLengthQuery(binaryExpression);
- if (query != null)
- {
- return query;
- }
-
- query = BuildModQuery(binaryExpression);
- if (query != null)
- {
- return query;
- }
- }
-
// the constant could be on either side
var variableExpression = binaryExpression.Left;
var constantExpression = binaryExpression.Right as ConstantExpression;
+ var operatorType = binaryExpression.NodeType;
if (constantExpression == null)
{
constantExpression = binaryExpression.Left as ConstantExpression;
variableExpression = binaryExpression.Right;
// if the constant was on the left some operators need to be flipped
- switch (operatorNodeType)
+ switch (operatorType)
{
- case ExpressionType.LessThan: operatorNodeType = ExpressionType.GreaterThan; break;
- case ExpressionType.LessThanOrEqual: operatorNodeType = ExpressionType.GreaterThanOrEqual; break;
- case ExpressionType.GreaterThan: operatorNodeType = ExpressionType.LessThan; break;
- case ExpressionType.GreaterThanOrEqual: operatorNodeType = ExpressionType.LessThanOrEqual; break;
+ case ExpressionType.LessThan: operatorType = ExpressionType.GreaterThan; break;
+ case ExpressionType.LessThanOrEqual: operatorType = ExpressionType.GreaterThanOrEqual; break;
+ case ExpressionType.GreaterThan: operatorType = ExpressionType.LessThan; break;
+ case ExpressionType.GreaterThanOrEqual: operatorType = ExpressionType.LessThanOrEqual; break;
}
}
@@ -368,6 +349,18 @@ private IMongoQuery BuildComparisonQuery(BinaryExpression binaryExpression)
return null;
}
+ var query = BuildArrayLengthQuery(variableExpression, operatorType, constantExpression);
+ if (query != null)
+ {
+ return query;
+ }
+
+ query = BuildModQuery(variableExpression, operatorType, constantExpression);
+ if (query != null)
+ {
+ return query;
+ }
+
BsonSerializationInfo serializationInfo = null;
var value = constantExpression.Value;
@@ -389,7 +382,7 @@ private IMongoQuery BuildComparisonQuery(BinaryExpression binaryExpression)
if (serializationInfo != null)
{
var serializedValue = SerializeValue(serializationInfo, value);
- switch (operatorNodeType)
+ switch (operatorType)
{
case ExpressionType.Equal: return Query.EQ(serializationInfo.ElementName, serializedValue);
case ExpressionType.GreaterThan: return Query.GT(serializationInfo.ElementName, serializedValue);
@@ -596,18 +589,14 @@ private IMongoQuery BuildMethodCallQuery(MethodCallExpression methodCallExpressi
return null;
}
- private IMongoQuery BuildModQuery(BinaryExpression binaryExpression)
+ private IMongoQuery BuildModQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)
{
- // the constant could be on either side
- var variableExpression = binaryExpression.Left;
- var constantExpression = binaryExpression.Right as ConstantExpression;
- if (constantExpression == null)
+ if (operatorType != ExpressionType.Equal && operatorType != ExpressionType.NotEqual)
{
- constantExpression = binaryExpression.Left as ConstantExpression;
- variableExpression = binaryExpression.Right;
+ return null;
}
- if (constantExpression == null || constantExpression.Type != typeof(int))
+ if (constantExpression.Type != typeof(int))
{
return null;
}
@@ -621,7 +610,7 @@ private IMongoQuery BuildModQuery(BinaryExpression binaryExpression)
if (serializationInfo != null && modulusExpression != null)
{
var modulus = ToInt32(modulusExpression);
- if (binaryExpression.NodeType == ExpressionType.Equal)
+ if (operatorType == ExpressionType.Equal)
{
return Query.Mod(serializationInfo.ElementName, modulus, value);
}

0 comments on commit 018d7da

Please sign in to comment.
Something went wrong with that request. Please try again.