public static bool TryTranslate(TranslationContext context, Expression expression, out AstFilter filter) { try { filter = Translate(context, expression); return(true); } catch (ExpressionNotSupportedException) { filter = null; return(false); } }
// caller is responsible for ensuring constant is on the right public static bool TryTranslateComparisonExpression(TranslationContext context, Expression expression, Expression leftExpression, AstComparisonFilterOperator comparisonOperator, Expression rightExpression, out AstFilter filter) { try { filter = TranslateComparisonExpression(context, expression, leftExpression, comparisonOperator, rightExpression); return(true); } catch (ExpressionNotSupportedException) { filter = null; return(false); } }
public static AstFilter Translate(TranslationContext context, BinaryExpression expression, BinaryExpression moduloExpression, Expression remainderExpression) { var fieldExpression = moduloExpression.Left; var field = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression); var divisorExpression = moduloExpression.Right; BsonValue divisor; BsonValue remainder; if (divisorExpression.Type == typeof(decimal) && remainderExpression.Type == typeof(decimal)) { divisor = divisorExpression.GetConstantValue <decimal>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <decimal>(containingExpression: expression); } else if (divisorExpression.Type == typeof(double) && remainderExpression.Type == typeof(double)) { divisor = divisorExpression.GetConstantValue <double>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <double>(containingExpression: expression); } else if (divisorExpression.Type == typeof(float) && remainderExpression.Type == typeof(float)) { divisor = divisorExpression.GetConstantValue <float>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <float>(containingExpression: expression); } else if (divisorExpression.Type == typeof(int) && remainderExpression.Type == typeof(int)) { divisor = divisorExpression.GetConstantValue <int>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <int>(containingExpression: expression); } else if (divisorExpression.Type == typeof(long) && remainderExpression.Type == typeof(long)) { divisor = divisorExpression.GetConstantValue <long>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <long>(containingExpression: expression); } else if (divisorExpression.Type == typeof(uint) && remainderExpression.Type == typeof(uint)) { divisor = divisorExpression.GetConstantValue <uint>(containingExpression: moduloExpression); remainder = remainderExpression.GetConstantValue <uint>(containingExpression: expression); } else if (divisorExpression.Type == typeof(ulong) && remainderExpression.Type == typeof(ulong)) { divisor = (long)divisorExpression.GetConstantValue <ulong>(containingExpression: moduloExpression); remainder = (long)remainderExpression.GetConstantValue <ulong>(containingExpression: expression); } else { throw new ExpressionNotSupportedException(expression); } var moduloComparisonAst = AstFilter.Mod(field, divisor, remainder); switch (expression.NodeType) { case ExpressionType.Equal: return(moduloComparisonAst); case ExpressionType.NotEqual: return(AstFilter.Not(moduloComparisonAst)); } throw new ExpressionNotSupportedException(expression); }