Beispiel #1
0
        public static (TimeSeriesRangeType Type, int Count) ParseCount(MethodExpression expression, string queryText, BlittableJsonReaderObject parameters = null)
        {
            TimeSeriesRangeType type;
            int count;

            switch (QueryMethod.GetMethodType(expression.Name.ToString()))
            {
            case MethodType.Last:
                type  = TimeSeriesRangeType.Last;
                count = QueryMethod.TimeSeries.LastWithCount(expression, queryText, parameters);
                break;

            default:
                throw new InvalidQueryException($"Got invalid type {expression.Name}.", queryText, parameters);
            }

            return(type, count);
        }
Beispiel #2
0
 public static void ThrowIfInvalidMethodInvocationInWhere(this QueryExpression where, BlittableJsonReaderObject parameters, string queryText, string whereCollectionName = null)
 {
     if (where is MethodExpression me)
     {
         var methodType = QueryMethod.GetMethodType(me.Name.Value);
         switch (methodType)
         {
         case MethodType.Id:
         case MethodType.CompareExchange:
         case MethodType.Count:
         case MethodType.Sum:
         case MethodType.Spatial_Point:
         case MethodType.Spatial_Wkt:
         case MethodType.Spatial_Circle:
             ThrowInvalidMethod(parameters, me, queryText, whereCollectionName);
             break;
         }
     }
 }
Beispiel #3
0
        public void ToJavaScript(string query, string alias, TextWriter writer)
        {
            switch (Type)
            {
            case OperatorType.Field:
                if (alias != null)
                {
                    writer.Write(alias);
                    writer.Write(".");
                }
                writer.Write(Extract(query, Field.TokenStart, Field.TokenLength, Field.EscapeChars));
                break;

            case OperatorType.Equal:
            case OperatorType.NotEqual:
            case OperatorType.LessThan:
            case OperatorType.GreaterThan:
            case OperatorType.LessThanEqual:
            case OperatorType.GreaterThanEqual:
                var fieldName = Extract(query, Field.TokenStart, Field.TokenLength, Field.EscapeChars);
                WriteSimpleOperatorJavaScript(query, writer, fieldName, alias, Type, Value);
                break;

            case OperatorType.Between:
                throw new InvalidOperationException("Cannot translate between operation to JavaScript");

            case OperatorType.In:
            case OperatorType.AllIn:
                throw new InvalidOperationException("Cannot translate in operation to JavaScript");

            case OperatorType.And:
            case OperatorType.AndNot:
            case OperatorType.Or:
            case OperatorType.OrNot:
                writer.Write("(");
                Left.ToJavaScript(query, alias, writer);
                switch (Type)
                {
                case OperatorType.And:
                    writer.Write(" && ");
                    break;

                case OperatorType.AndNot:
                    writer.Write(" && !(");
                    break;

                case OperatorType.Or:
                    writer.Write(" || ");
                    break;

                case OperatorType.OrNot:
                    writer.Write(" || !(");
                    break;
                }
                Right.ToJavaScript(query, alias, writer);
                if (Type == OperatorType.OrNot || Type == OperatorType.AndNot)
                {
                    writer.Write(")");
                }
                writer.Write(")");
                break;

            case OperatorType.Method:
                var method     = Extract(query, Field.TokenStart, Field.TokenLength, Field.EscapeChars);
                var methodType = QueryMethod.GetMethodType(method, throwIfNoMatch: false);
                if (methodType == MethodType.Id && Arguments.Count == 1 && Arguments[0] is QueryExpression idExpression)
                {
                    WriteSimpleOperatorJavaScript(query, writer, $"id({alias})", null, idExpression.Type, idExpression.Value);
                    break;
                }

                writer.Write(method);
                writer.Write("(");

                for (int i = 0; i < Arguments.Count; i++)
                {
                    var arg = Arguments[i];
                    if (i != 0)
                    {
                        writer.Write(", ");
                    }
                    if (arg is QueryExpression qe)
                    {
                        qe.ToJavaScript(query, alias, writer);
                    }
                    else if (arg is FieldToken field)
                    {
                        writer.Write(Extract(query, field.TokenStart, field.TokenLength, field.EscapeChars));
                    }
                    else
                    {
                        var val = (ValueToken)arg;
                        writer.Write(Extract(query, val));
                    }
                }
                writer.Write(")");
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #4
0
        public static bool IsMatchedBy(this QueryExpression where, BlittableJsonReaderObject value, BlittableJsonReaderObject queryParameters, bool shouldCaseSensitiveStringCompare = false)
        {
            switch (where)
            {
            case BetweenExpression between:
                var src    = Evaluate(between.Source, value);
                var hValue = GetFieldValue(between.Max.Token.Value, between.Max.Value, queryParameters);
                var lValue = GetFieldValue(between.Min.Token.Value, between.Min.Value, queryParameters);

                if (Compare(lValue, src, shouldCaseSensitiveStringCompare) < 0)
                {
                    return(false);
                }

                if (Compare(src, hValue, shouldCaseSensitiveStringCompare) > 0)
                {
                    return(false);
                }

                return(true);

            case TrueExpression _:
                return(true);

            case BinaryExpression be:
                return(IsMatchedBy(be, value, queryParameters, shouldCaseSensitiveStringCompare));

            case NegatedExpression not:
                var result = IsMatchedBy(not.Expression, value, queryParameters, shouldCaseSensitiveStringCompare);
                return(!result);

            case InExpression ine:
                var inSrc   = Evaluate(ine.Source, value);
                int matches = 0;
                foreach (var item in ine.Values)
                {
                    var val = GetValue(item, value, queryParameters);
                    if (val is BlittableJsonReaderArray array)
                    {
                        foreach (var el in array)
                        {
                            if (AreEqual(inSrc, el, shouldCaseSensitiveStringCompare))
                            {
                                if (ine.All == false)
                                {
                                    return(true);
                                }
                                matches++;
                            }
                        }
                    }
                    else
                    {
                        if (AreEqual(inSrc, val, shouldCaseSensitiveStringCompare))
                        {
                            if (ine.All == false)
                            {
                                return(true);
                            }
                            matches++;
                        }
                    }
                }
                return(matches == ine.Values.Count);

            case MethodExpression me:
                var methodType = QueryMethod.GetMethodType(me.Name.Value);
                switch (methodType)
                {
                case MethodType.Exact:
                    if (me.Arguments.Count == 1)
                    {
                        return(me.Arguments[0].IsMatchedBy(value, queryParameters, true));
                    }

                    break;
                }
                throw new InvalidOperationException("Unsupported edge method invocation in where clause: " + where);

            default:
                throw new InvalidOperationException("Unsupported edge where expression: " + where);
            }
        }