Beispiel #1
0
        public override FilterNode <ClrValue> Visit(CompareFilter <IJsonValue> nodeIn)
        {
            CompareFilter <ClrValue> result = null;

            if (nodeIn.Path.TryGetProperty(schema, errors, out var property))
            {
                var isValidOperator = OperatorValidator.IsAllowedOperator(property, nodeIn.Operator);

                if (!isValidOperator)
                {
                    errors.Add($"{nodeIn.Operator} is not a valid operator for type {property.Type} at {nodeIn.Path}.");
                }

                var value = ValueConverter.Convert(property, nodeIn.Value, nodeIn.Path, errors);

                if (value != null && isValidOperator)
                {
                    if (value.IsList && nodeIn.Operator != CompareOperator.In)
                    {
                        errors.Add($"Array value is not allowed for '{nodeIn.Operator}' operator and path '{nodeIn.Path}'.");
                    }

                    result = new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, value);
                }
            }

            result = result ?? new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, ClrValue.Null);

            return(result);
        }
        public override FilterNode <ClrValue> Visit(CompareFilter <ClrValue> nodeIn)
        {
            CompareFilter <ClrValue> result;

            var path = pathConverter(nodeIn.Path);

            var value = nodeIn.Value.Value;

            if (value is Instant &&
                !string.Equals(path[0], "mt", StringComparison.OrdinalIgnoreCase) &&
                !string.Equals(path[0], "ct", StringComparison.OrdinalIgnoreCase))
            {
                result = new CompareFilter <ClrValue>(path, nodeIn.Operator, value.ToString());
            }
            else
            {
                result = new CompareFilter <ClrValue>(path, nodeIn.Operator, nodeIn.Value);
            }

            if (value is List <Guid> guidList)
            {
                result = new CompareFilter <ClrValue>(path, nodeIn.Operator, guidList.Select(x => x.ToString()).ToList());
            }
            else if (value is Guid guid)
            {
                result = new CompareFilter <ClrValue>(path, nodeIn.Operator, guid.ToString());
            }

            return(result);
        }
Beispiel #3
0
        public override FilterDefinition <T> Visit(CompareFilter <ClrValue> nodeIn, None args)
        {
            var propertyName = nodeIn.Path.ToString();

            var value = nodeIn.Value.Value;

            switch (nodeIn.Operator)
            {
            case CompareOperator.Empty:
                return(Filter.Or(
                           Filter.Exists(propertyName, false),
                           Filter.Eq <object?>(propertyName, null),
                           Filter.Eq <object?>(propertyName, string.Empty),
                           Filter.Size(propertyName, 0)));

            case CompareOperator.Exists:
                return(Filter.And(
                           Filter.Exists(propertyName, true),
                           Filter.Ne <object?>(propertyName, null)));

            case CompareOperator.Matchs:
                return(Filter.Regex(propertyName, BuildMatchRegex(nodeIn)));

            case CompareOperator.StartsWith:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => "^" + s)));

            case CompareOperator.Contains:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s)));

            case CompareOperator.EndsWith:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s + "$")));

            case CompareOperator.Equals:
                return(Filter.Eq(propertyName, value));

            case CompareOperator.GreaterThan:
                return(Filter.Gt(propertyName, value));

            case CompareOperator.GreaterThanOrEqual:
                return(Filter.Gte(propertyName, value));

            case CompareOperator.LessThan:
                return(Filter.Lt(propertyName, value));

            case CompareOperator.LessThanOrEqual:
                return(Filter.Lte(propertyName, value));

            case CompareOperator.NotEquals:
                return(Filter.Ne(propertyName, value));

            case CompareOperator.In:
                return(Filter.In(propertyName, ((IList)value !).OfType <object>()));
            }

            throw new NotSupportedException();
        }
Beispiel #4
0
        private static BsonRegularExpression BuildMatchRegex(CompareFilter <ClrValue> node)
        {
            var value = node.Value.Value?.ToString();

            if (value == null)
            {
                return(new BsonRegularExpression("null", "i"));
            }

            if (value.Length > 3 && ((value[0] == '/' && value[^ 1] == '/') || value[^ 2] == '/'))
Beispiel #5
0
        public override FilterNode <ClrValue> Visit(CompareFilter <ClrValue> nodeIn, Args args)
        {
            var result = nodeIn;

            var(path, op, value) = nodeIn;

            var clrValue = value.Value;

            if (string.Equals(path[0], "id", StringComparison.OrdinalIgnoreCase))
            {
                path = "_id";

                if (clrValue is List <string> idList)
                {
                    value = idList.Select(x => DomainId.Combine(args.AppId, DomainId.Create(x)).ToString()).ToList();
                }
                else if (clrValue is string id)
                {
                    value = DomainId.Combine(args.AppId, DomainId.Create(id)).ToString();
                }
                else if (clrValue is List <Guid> guidIdList)
                {
                    value = guidIdList.Select(x => DomainId.Combine(args.AppId, DomainId.Create(x)).ToString()).ToList();
                }
                else if (clrValue is Guid guidId)
                {
                    value = DomainId.Combine(args.AppId, DomainId.Create(guidId)).ToString();
                }
            }
            else
            {
                path = Adapt.MapPath(path);

                if (clrValue is List <Guid> guidList)
                {
                    value = guidList.Select(x => x.ToString()).ToList();
                }
                else if (clrValue is Guid guid)
                {
                    value = guid.ToString();
                }
                else if (clrValue is Instant &&
                         !string.Equals(path[0], "mt", StringComparison.OrdinalIgnoreCase) &&
                         !string.Equals(path[0], "ct", StringComparison.OrdinalIgnoreCase))
                {
                    value = clrValue.ToString();
                }
            }

            return(result with {
                Path = path, Value = value
            });
        }
Beispiel #6
0
        public override FilterNode <ClrValue> Visit(CompareFilter <IJsonValue> nodeIn, Args args)
        {
            CompareFilter <ClrValue>?result = null;

            if (nodeIn.Path.TryGetProperty(args.Schema, args.Errors, out var property))
            {
                var isValidOperator = OperatorValidator.IsAllowedOperator(property, nodeIn.Operator);

                if (!isValidOperator)
                {
                    var name = property.Type.ToString();

                    if (!string.IsNullOrWhiteSpace(property.Format))
                    {
                        name = $"{name}({property.Format})";
                    }

                    args.Errors.Add($"'{nodeIn.Operator}' is not a valid operator for type {name} at '{nodeIn.Path}'.");
                }

                var value = ValueConverter.Convert(property, nodeIn.Value, nodeIn.Path, args.Errors);

                if (value != null && isValidOperator)
                {
                    if (nodeIn.Operator == CompareOperator.In)
                    {
                        if (!value.IsList)
                        {
                            value = value.ToList();
                        }
                    }
                    else
                    {
                        if (value.IsList)
                        {
                            args.Errors.Add($"Array value is not allowed for '{nodeIn.Operator}' operator and path '{nodeIn.Path}'.");
                        }
                    }

                    if (nodeIn.Operator == CompareOperator.Matchs && value.Value?.ToString()?.IsValidRegex() != true)
                    {
                        args.Errors.Add($"{value} is not a valid regular expression.");
                    }

                    result = new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, value);
                }
            }

            result ??= new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, ClrValue.Null);

            return(result);
        }
Beispiel #7
0
        public override FilterNode <ClrValue> Visit(CompareFilter <ClrValue> nodeIn, None args)
        {
            if (string.Equals(nodeIn.Path[0], "id", StringComparison.OrdinalIgnoreCase))
            {
                return(nodeIn);
            }

            var path = Adapt.MapPath(nodeIn.Path);

            return(nodeIn with {
                Path = path
            });
        }
Beispiel #8
0
        public override FilterNode <ClrValue> Visit(CompareFilter <ClrValue> nodeIn)
        {
            if (string.Equals(nodeIn.Path[0], nameof(IAssetEntity.Tags), StringComparison.OrdinalIgnoreCase) && nodeIn.Value.Value is string stringValue)
            {
                var tagNames = Task.Run(() => tagService.GetTagIdsAsync(appId, TagGroups.Assets, HashSet.Of(stringValue))).Result;

                if (tagNames.TryGetValue(stringValue, out var normalized))
                {
                    return(new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, normalized));
                }
            }

            return(nodeIn);
        }
        public override FilterNode <ClrValue>?Visit(CompareFilter <ClrValue> nodeIn)
        {
            if (nodeIn.Value.Value is string stringValue && IsDataPath(nodeIn.Path) && IsTagField(nodeIn.Path))
            {
                var tagNames = Task.Run(() => tagService.GetTagIdsAsync(appId, TagGroups.Schemas(schema.Id), HashSet.Of(stringValue))).Result;

                if (tagNames.TryGetValue(stringValue, out var normalized))
                {
                    return(new CompareFilter <ClrValue>(nodeIn.Path, nodeIn.Operator, normalized));
                }
            }

            return(nodeIn);
        }
        private static Expression BuildCompare <T>(CompareFilter filter, Expression parameter)
        {
            var prp       = filter.PropertyName.Split(".");
            var prpAccess = Expression.Property(parameter, prp[0]);

            if (prp.Length > 1)
            {
                for (int a = 1; a < prp.Length; a++)
                {
                    prpAccess = Expression.Property(prpAccess, prp[a]);
                }
            }

            var body = CompareExpression(filter.Operator, prpAccess, filter.Value, filter.Value2);

            return(body);
        }
Beispiel #11
0
 public static List<SMBLevelCompare> CompareLevels(List<SMBLevel> levels1, List<SMBLevel> levels2, CompareFilter compareFilter)
 {
     return (from levels in levels1.Zip(levels2, (x1, x2) => new { x1, x2 })
             where levels.x1.IsCompleted && levels.x2.IsCompleted
             let difference = levels.x1.Time - levels.x2.Time
             where compareFilter == CompareFilter.All || (compareFilter == CompareFilter.Bigger && difference > 0)
             || (compareFilter == CompareFilter.Smaller && difference < 0)
             orderby difference descending
             select new SMBLevelCompare
             {
                 LevelName = levels.x1.LevelNameWithNumber,
                 Time1 = levels.x1.Time,
                 Time2 = levels.x2.Time,
                 Difference = difference,
                 ParTime = levels.x1.ParTime
             }).ToList();
 }
Beispiel #12
0
        public override FilterDefinition <T> Visit(CompareFilter <ClrValue> nodeIn)
        {
            var propertyName = nodeIn.Path.ToString();

            switch (nodeIn.Operator)
            {
            case CompareOperator.Empty:
                return(Filter.Or(Filter.Exists(propertyName, false), Filter.Eq(propertyName, default(T)), Filter.Eq(propertyName, string.Empty), Filter.Eq(propertyName, new T[0])));

            case CompareOperator.StartsWith:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => "^" + s)));

            case CompareOperator.Contains:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s)));

            case CompareOperator.EndsWith:
                return(Filter.Regex(propertyName, BuildRegex(nodeIn, s => s + "$")));

            case CompareOperator.Equals:
                return(Filter.Eq(propertyName, nodeIn.Value.Value));

            case CompareOperator.GreaterThan:
                return(Filter.Gt(propertyName, nodeIn.Value.Value));

            case CompareOperator.GreaterThanOrEqual:
                return(Filter.Gte(propertyName, nodeIn.Value.Value));

            case CompareOperator.LessThan:
                return(Filter.Lt(propertyName, nodeIn.Value.Value));

            case CompareOperator.LessThanOrEqual:
                return(Filter.Lte(propertyName, nodeIn.Value.Value));

            case CompareOperator.NotEquals:
                return(Filter.Ne(propertyName, nodeIn.Value.Value));

            case CompareOperator.In:
                return(Filter.In(propertyName, ((IList)nodeIn.Value.Value).OfType <object>()));
            }

            throw new NotSupportedException();
        }
Beispiel #13
0
        public override async ValueTask <FilterNode <ClrValue>?> Visit(CompareFilter <ClrValue> nodeIn, Args args)
        {
            if (nodeIn.Value.Value is FilterSphere sphere)
            {
                var field = string.Join(".", nodeIn.Path.Skip(1));

                var searchQuery = new GeoQuery(args.Schema.Id, field, sphere.Latitude, sphere.Longitude, sphere.Radius, 1000);
                var searchScope = args.Context.Scope();

                var ids = await args.TextIndex.SearchAsync(args.Context.App, searchQuery, searchScope);

                if (ids == null || ids.Count == 0)
                {
                    return(ClrFilter.Eq("id", "__notfound__"));
                }

                return(ClrFilter.In("id", ids.Select(x => x.ToString()).ToList()));
            }

            return(nodeIn);
        }
 public override FilterNode <TValue>?Visit(CompareFilter <TValue> nodeIn)
 {
     return(new CompareFilter <TValue>(nodeIn.Path.ToFirstPascalCase(), nodeIn.Operator, nodeIn.Value));
 }
Beispiel #15
0
 public override FilterNode <TValue>?Visit(CompareFilter <TValue> nodeIn, TArgs args)
 {
     return(nodeIn);
 }
Beispiel #16
0
 public virtual T Visit(CompareFilter <TValue> nodeIn, TArgs args)
 {
     throw new NotImplementedException();
 }
Beispiel #17
0
 private static BsonRegularExpression BuildRegex(CompareFilter <ClrValue> node, Func <string, string> formatter)
 {
     return(new BsonRegularExpression(formatter(node.Value.Value.ToString()), "i"));
 }
 public override FilterNode <TValue>?Visit(CompareFilter <TValue> nodeIn, None args)
 {
     return(nodeIn with {
         Path = nodeIn.Path.ToFirstPascalCase()
     });
 }
Beispiel #19
0
        public void And(string Column, CompareFilter comp, object value)
        {
            string cc = "";

            if (comp == CompareFilter.Like)
            {
                cc = " LIKE ";
            }
            else if (comp == CompareFilter.NotLike)
            {
                cc = " NOT LIKE ";
            }
            else if (comp == CompareFilter.Equal)
            {
                cc = " = ";
            }
            else if (comp == CompareFilter.NotEqual)
            {
                cc = " <> ";
            }
            else if (comp == CompareFilter.In)
            {
                cc = " IN ";
            }
            else if (comp == CompareFilter.NotIn)
            {
                cc = " NOT IN ";
            }
            else if (comp == CompareFilter.Null)
            {
                cc = " NULL ";
            }
            else if (comp == CompareFilter.NotNull)
            {
                cc = " NOT NULL ";
            }
            else if (comp == CompareFilter.GreaterThan)
            {
                cc = " > ";
            }
            else if (comp == CompareFilter.GreaterThanOrEqual)
            {
                cc = " >= ";
            }
            else if (comp == CompareFilter.LessThan)
            {
                cc = " < ";
            }
            else if (comp == CompareFilter.LessThanOrEqual)
            {
                cc = " <= ";
            }

            if (IsNew)
            {
                IsNew = false;

                if (value != null)
                {
                    if (comp == CompareFilter.Like || comp == CompareFilter.NotLike)
                    {
                        q.Where(Column + " " + cc + " N'%" + value.ToString() + "%'", null);
                    }
                    else if (comp == CompareFilter.In || comp == CompareFilter.NotIn)
                    {
                        q.Where(Column + " " + cc + " (@0)", value);
                    }
                    else
                    {
                        q.Where(Column + " " + cc + " @0", value);
                    }
                }
                else
                {
                    q.Where(Column + " " + cc);
                }
            }
            else
            {
                q.Append(" AND ");

                if (value != null)
                {
                    if (comp == CompareFilter.Like || comp == CompareFilter.NotLike)
                    {
                        q.Append(Column + " " + cc + " N'%" + value.ToString() + "%'", null);
                    }
                    else if (comp == CompareFilter.In || comp == CompareFilter.NotIn)
                    {
                        q.Append(Column + " " + cc + " (@0)", value);
                    }
                    else
                    {
                        q.Append(Column + " " + cc + " @0", value);
                    }
                }
                else
                {
                    q.Append(Column + " " + cc);
                }
            }
        }
Beispiel #20
0
 public override ValueTask <FilterNode <TValue>?> Visit(CompareFilter <TValue> nodeIn, TArgs args)
 {
     return(new ValueTask <FilterNode <TValue>?>(nodeIn));
 }