コード例 #1
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        public static QueryToken Parse(string tokenString, QueryDescription qd, SubTokensOptions options)
        {
            if (string.IsNullOrEmpty(tokenString))
            {
                throw new ArgumentNullException(nameof(tokenString));
            }

            //https://stackoverflow.com/questions/35418597/split-string-on-the-dot-characters-that-are-not-inside-of-brackets
            string[] parts = Regex.Split(tokenString, @"\.(?!([^[]*\]|[^(]*\)))");

            string firstPart = parts.FirstEx();

            QueryToken?result = SubToken(null, qd, options, firstPart);

            if (result == null)
            {
                throw new FormatException("Column {0} not found on query {1}".FormatWith(firstPart, QueryUtils.GetKey(qd.QueryName)));
            }

            foreach (var part in parts.Skip(1))
            {
                var newResult = SubToken(result, qd, options, part);
                result = newResult ?? throw new FormatException("Token with key '{0}' not found on {1} of query {2}".FormatWith(part, result.FullKey(), QueryUtils.GetKey(qd.QueryName)));
            }

            return(result);
        }
コード例 #2
0
    public static QueryRequest ToQueryRequestValue(this UserQueryEntity userQuery, QueryToken?valueToken = null)
    {
        var qn = userQuery.Query.ToQueryName();

        if (valueToken == null)
        {
            var qd = QueryLogic.Queries.QueryDescription(qn);
            valueToken = QueryUtils.Parse("Count", qd, SubTokensOptions.CanAggregate);
        }

        var qr = new QueryRequest()
        {
            QueryName    = qn,
            GroupResults = userQuery.GroupResults || valueToken is AggregateToken,
        };

        qr.Filters = userQuery.Filters.ToFilterList();
        qr.Columns = new List <Column> {
            new Column(valueToken, null)
        };
        qr.Orders = valueToken is AggregateToken ? new List <Order>() : userQuery.Orders.Select(qo => new Order(qo.Token.Token, qo.OrderType)).ToList();

        qr.Pagination = userQuery.GetPagination() ?? new Pagination.All();

        return(qr);
    }
コード例 #3
0
        static QueryToken Append(this QueryToken baseToken, QueryToken suffix)
        {
            var        steps = suffix.Follow(a => a.Parent).Reverse();
            QueryToken token = baseToken;

            foreach (var step in steps)
            {
                QueryToken?newToken = null;
                if (step.Key == "Entity" && step is ColumnToken)
                {
                    if (token.Type.CleanType() == step.Type.CleanType())
                    {
                        continue;
                    }
                    else
                    {
                        newToken = token.SubTokenInternal("[" + TypeLogic.GetCleanName(baseToken.Type.CleanType()) + "]", SubTokensOptions.CanElement) !;
                    }
                }

                newToken = token.SubTokenInternal(step.Key, SubTokensOptions.CanElement);
                token    = newToken ?? throw new InvalidOperationException($"Token '{step}' not found in '{token.FullKey()}'");
            }

            return(token);
        }
コード例 #4
0
    public AggregateToken(AggregateFunction function, QueryToken parent, FilterOperation?filterOperation = null, object?value = null, bool distinct = false)
    {
        this.parent            = parent ?? throw new ArgumentNullException("parent");
        this.AggregateFunction = function;

        if (function == AggregateFunction.Count)
        {
            if (distinct == false && filterOperation == null)
            {
                throw new ArgumentException("Either distinct or filterOperation should be set");
            }

            else if (distinct == true && this.FilterOperation.HasValue)
            {
                throw new ArgumentException("distinct and filterOperation are incompatibles");
            }

            this.Value           = value;
            this.FilterOperation = filterOperation;
            this.Distinct        = distinct;
        }
        else
        {
            if (distinct == true || this.FilterOperation.HasValue)
            {
                throw new ArgumentException("distinct and filterOperation are incompatibles");
            }
        }
    }
コード例 #5
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        static List <QueryToken> SubTokensBasic(QueryToken?token, QueryDescription qd, SubTokensOptions options)
        {
            if (token == null)
            {
                if (MergeEntityColumns != null && !MergeEntityColumns())
                {
                    return(qd.Columns.Select(cd => (QueryToken) new ColumnToken(cd, qd.QueryName)).ToList());
                }

                var dictonary = qd.Columns.Where(a => !a.IsEntity).Select(cd => (QueryToken) new ColumnToken(cd, qd.QueryName)).ToDictionary(t => t.Key);

                var entity = new ColumnToken(qd.Columns.SingleEx(a => a.IsEntity), qd.QueryName);

                dictonary.Add(entity.Key, entity);

                foreach (var item in entity.SubTokensInternal(options).OrderByDescending(a => a.Priority).ThenBy(a => a.ToString()))
                {
                    if (!dictonary.ContainsKey(item.Key))
                    {
                        dictonary.Add(item.Key, item);
                    }
                }

                return(dictonary.Values.ToList());
            }
            else
            {
                return(token.SubTokensInternal(options));
            }
        }
コード例 #6
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        private static IEnumerable <QueryToken> AggregateTokens(QueryToken?token, QueryDescription qd)
        {
            if (token == null)
            {
                yield return(new AggregateToken(AggregateFunction.Count, qd.QueryName));
            }
            else if (!(token is AggregateToken))
            {
                FilterType?ft = QueryUtils.TryGetFilterType(token.Type);

                if (ft == FilterType.Integer || ft == FilterType.Decimal || ft == FilterType.Boolean)
                {
                    yield return(new AggregateToken(AggregateFunction.Average, token));

                    yield return(new AggregateToken(AggregateFunction.Sum, token));

                    yield return(new AggregateToken(AggregateFunction.Min, token));

                    yield return(new AggregateToken(AggregateFunction.Max, token));
                }
                else if (ft == FilterType.DateTime) /*ft == FilterType.String || */
                {
                    yield return(new AggregateToken(AggregateFunction.Min, token));

                    yield return(new AggregateToken(AggregateFunction.Max, token));
                }

                if (ft != null)
                {
                    yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.DistinctTo, null));

                    yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.EqualTo, null));
                }

                if (token.IsGroupable)
                {
                    yield return(new AggregateToken(AggregateFunction.Count, token, distinct: true));
                }

                if (ft == FilterType.Enum)
                {
                    foreach (var v in Enum.GetValues(token.Type.UnNullify()))
                    {
                        yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.EqualTo, v));

                        yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.DistinctTo, v));
                    }
                }

                if (ft == FilterType.Boolean)
                {
                    yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.EqualTo, true));

                    yield return(new AggregateToken(AggregateFunction.Count, token, FilterOperation.EqualTo, false));
                }
            }
        }
コード例 #7
0
 public TranslateInstanceValueProvider(ParsedToken token, bool isExplicit, Action <bool, string> addError)
 {
     this.ParsedToken = token;
     this.IsExplicit  = isExplicit;
     if (token.QueryToken != null)
     {
         this.Route       = token.QueryToken.GetPropertyRoute();
         this.EntityToken = DeterminEntityToken(token.QueryToken, addError);
     }
 }
コード例 #8
0
    public AggregateToken(AggregateFunction function, object queryName)
    {
        if (function != AggregateFunction.Count)
        {
            throw new ArgumentException("function should be Count for this overload");
        }

        this.parent            = null;
        this.queryName         = queryName ?? throw new ArgumentNullException("queryName");
        this.AggregateFunction = function;
    }
コード例 #9
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        public static List <QueryToken> SubTokens(this QueryToken?token, QueryDescription qd, SubTokensOptions options)
        {
            var result = SubTokensBasic(token, qd, options);

            if ((options & SubTokensOptions.CanAggregate) != 0)
            {
                result.InsertRange(0, AggregateTokens(token, qd));
            }

            return(result);
        }
コード例 #10
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        public static QueryToken?SubToken(QueryToken?token, QueryDescription qd, SubTokensOptions options, string key)
        {
            var result = SubTokenBasic(token, qd, options, key);

            if (result != null)
            {
                return(result);
            }

            if ((options & SubTokensOptions.CanAggregate) != 0)
            {
                return(AggregateTokens(token, qd).SingleOrDefaultEx(a => a.Key == key));
            }

            return(null);
        }
コード例 #11
0
ファイル: QueryUtils.cs プロジェクト: doganc/framework
        static QueryToken?SubTokenBasic(QueryToken?token, QueryDescription qd, SubTokensOptions options, string key)
        {
            if (token == null)
            {
                var column = qd.Columns.SingleOrDefaultEx(a => a.Name == key);

                if (column == null)
                {
                    return(null);
                }

                return(new ColumnToken(column, qd.QueryName));
            }
            else
            {
                return(token.SubTokenInternal(key, options));
            }
        }
コード例 #12
0
        public string?Validate(string?parameter, QueryToken?token)
        {
            if (!decimal.TryParse(parameter, NumberStyles.Float, CultureInfo.InvariantCulture, out decimal value))
            {
                return("{0} is not a valid number".FormatWith(parameter));
            }

            if (MinValue.HasValue && value < MinValue)
            {
                return("{0} is lesser than the minimum {1}".FormatWith(value, MinValue));
            }

            if (MaxValue.HasValue && MaxValue < value)
            {
                return("{0} is grater than the maximum {1}".FormatWith(value, MinValue));
            }

            return(null);
        }
コード例 #13
0
        public string?Validate(string?parameter, QueryToken?token)
        {
            if (token == null)
            {
                return(null); //?
            }
            var enumValue = this.SingleOrDefault(a => a.Name == parameter);

            if (enumValue == null)
            {
                return("{0} is not in the list".FormatWith(parameter));
            }

            if (!enumValue.CompatibleWith(token))
            {
                return("{0} is not compatible with {1}".FormatWith(parameter, token?.NiceName()));
            }

            return(null);
        }
コード例 #14
0
        protected virtual IEnumerable <(QueryToken token, ImmutableStack <OmniboxMatch> stack)> GetAmbiguousTokens(QueryToken?queryToken, ImmutableStack <OmniboxMatch> distancePack,
                                                                                                                   QueryDescription queryDescription, List <OmniboxToken> omniboxTokens, int index, int operatorIndex)
        {
            OmniboxToken omniboxToken = omniboxTokens[index];

            bool isPascal = OmniboxUtils.IsPascalCasePattern(omniboxToken.Value);

            var dic     = QueryUtils.SubTokens(queryToken, queryDescription, SubTokensOptions.CanAnyAll | SubTokensOptions.CanElement).ToOmniboxPascalDictionary(qt => qt.ToString(), qt => qt);
            var matches = OmniboxUtils.Matches(dic, qt => qt.IsAllowed() == null, omniboxToken.Value, isPascal);

            if (index == operatorIndex - 1)
            {
                foreach (var m in matches)
                {
                    var token = (QueryToken)m.Value;
                    yield return(token : token, stack : distancePack.Push(m));
                }
            }
            else
            {
                foreach (var m in matches)
                {
                    foreach (var newPair in GetAmbiguousTokens((QueryToken)m.Value, distancePack.Push(m), queryDescription, omniboxTokens, index + 2, operatorIndex))
                    {
                        yield return(newPair);
                    }
                }
            }
        }
コード例 #15
0
        static bool TryParseRemember(Replacements replacements, string tokenString, QueryDescription qd, SubTokensOptions options, out QueryToken?result)
        {
            string[] parts = tokenString.Split('.');

            result = null;
            for (int i = 0; i < parts.Length; i++)
            {
                string part = parts[i];

                QueryToken?newResult = QueryUtils.SubToken(result, qd, options, part);
                if (newResult != null)
                {
                    result = newResult;
                }
                else
                {
                    if (i == 0)
                    {
                        var        entity       = QueryUtils.SubToken(result, qd, options, "Entity");
                        QueryToken?newSubResult = QueryUtils.SubToken(entity, qd, options, part);
                        if (newSubResult != null)
                        {
                            result = newSubResult;
                            continue;
                        }
                    }


                    if (Replacements.AutoReplacement != null)
                    {
                        Replacements.Selection?sel = Replacements.AutoReplacement(new Replacements.AutoReplacementContext(
                                                                                      replacementKey: "QueryToken",
                                                                                      oldValue: part,
                                                                                      newValues: result.SubTokens(qd, options).Select(a => a.Key).ToList()));

                        if (sel != null && sel.Value.NewValue != null)
                        {
                            newResult = QueryUtils.SubToken(result, qd, options, sel.Value.NewValue);

                            if (newResult != null)
                            {
                                result = newResult;
                                continue;
                            }
                        }
                    }

                    string key = result == null?QueryKey(qd.QueryName) : TypeKey(result.Type);

                    Dictionary <string, string>?dic = replacements.TryGetC(key);

                    if (dic == null)
                    {
                        return(false);
                    }

                    string remainging = parts.Skip(i).ToString(".");

                    string old = dic.Keys.OrderByDescending(a => a.Length).FirstOrDefault(s => remainging.StartsWith(s));

                    if (old == null)
                    {
                        return(false);
                    }

                    var subParts = dic[old].Let(s => s.HasText() ? s.Split('.') : new string[0]);

                    for (int j = 0; j < subParts.Length; j++)
                    {
                        string subPart = subParts[j];

                        QueryToken?subNewResult = QueryUtils.SubToken(result, qd, options, subPart);

                        if (subNewResult == null)
                        {
                            return(false);
                        }

                        result = subNewResult;
                    }

                    i += (old == "" ? 0 : old.Split('.').Length) - 1;
                }
            }

            return(true);
        }
コード例 #16
0
 internal string DefaultValue(QueryToken?token)
 {
     return(this.ValueDefinition.DefaultValue(token));
 }
コード例 #17
0
 string IChartParameterValueDefinition.DefaultValue(QueryToken?token)
 {
     return(DefaultValue.ToString(CultureInfo.InvariantCulture));
 }
コード例 #18
0
ファイル: Filter.cs プロジェクト: zeevir/framework
 public FilterGroup(FilterGroupOperation groupOperation, QueryToken?token, List <Filter> filters)
 {
     this.GroupOperation = groupOperation;
     this.Token          = token;
     this.Filters        = filters;
 }
コード例 #19
0
 public string DefaultValue(QueryToken?token)
 {
     return(this.Where(a => a.CompatibleWith(token)).FirstEx(() => "No default parameter value for {0} found".FormatWith(token?.NiceName())).Name);
 }
コード例 #20
0
        public static FixTokenResult FixToken(Replacements replacements, string original, out QueryToken?token, QueryDescription qd, SubTokensOptions options, string?remainingText, bool allowRemoveToken, bool allowReGenerate)
        {
            string[] parts = original.Split('.');

            if (TryParseRemember(replacements, original, qd, options, out QueryToken? current))
            {
                if (current !.FullKey() != original)
                {
                    SafeConsole.WriteColor(ConsoleColor.DarkRed, "  " + original);
                    Console.Write(" -> ");
                    SafeConsole.WriteColor(ConsoleColor.DarkGreen, current.FullKey());
                }
                Console.WriteLine(remainingText);
                token = current;
                return(FixTokenResult.Fix);
            }

            while (true)
            {
                var tempToken = current !;
                var result    = SelectInteractive(ref tempToken, qd, options, remainingText, allowRemoveToken, allowReGenerate);
                current = tempToken;
                switch (result)
                {
                case UserAssetTokenAction.DeleteEntity:
                    SafeConsole.WriteLineColor(ConsoleColor.Red, "Entity deleted");
                    token = null;
                    return(FixTokenResult.DeleteEntity);

                case UserAssetTokenAction.ReGenerateEntity:
                    if (!allowReGenerate)
                    {
                        throw new InvalidOperationException("Unexpected ReGenerate");
                    }

                    SafeConsole.WriteLineColor(ConsoleColor.Magenta, "Entity Re-Generated");
                    token = null;
                    return(FixTokenResult.ReGenerateEntity);

                case UserAssetTokenAction.RemoveToken:
                    if (!allowRemoveToken)
                    {
                        throw new InvalidOperationException("Unexpected RemoveToken");
                    }

                    Console.SetCursorPosition(0, Console.CursorTop - 1);
                    SafeConsole.WriteColor(ConsoleColor.DarkRed, "  " + original);
                    SafeConsole.WriteColor(ConsoleColor.DarkRed, " (token removed)");
                    Console.WriteLine(remainingText);
                    token = null;
                    return(FixTokenResult.RemoveToken);

                case UserAssetTokenAction.SkipEntity:
                    SafeConsole.WriteLineColor(ConsoleColor.DarkYellow, "Entity skipped");
                    token = null;
                    return(FixTokenResult.SkipEntity);

                case UserAssetTokenAction.Confirm:
                    Remember(replacements, original, current);
                    Console.SetCursorPosition(0, Console.CursorTop - 1);
                    SafeConsole.WriteColor(ConsoleColor.DarkRed, "  " + original);
                    Console.Write(" -> ");
                    SafeConsole.WriteColor(ConsoleColor.DarkGreen, current.FullKey());
                    Console.WriteLine(remainingText);
                    token = current;
                    return(FixTokenResult.Fix);
                }
            }
        }
コード例 #21
0
 public bool CompatibleWith(QueryToken?token)
 {
     return(TypeFilter == null || token != null && ChartUtils.IsChartColumnType(token, TypeFilter.Value));
 }
コード例 #22
0
 public string?Validate(string?parameter, QueryToken?token)
 {
     return(null);
 }
コード例 #23
0
 string IChartParameterValueDefinition.DefaultValue(QueryToken?token)
 {
     return(DefaultValue);
 }
コード例 #24
0
 public string?Validate(string?value, QueryToken?token)
 {
     return(ValueDefinition?.Validate(value, token));
 }