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); }
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); }
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); }
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"); } } }
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)); } }
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)); } } }
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); } }
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; }
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); }
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); }
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)); } }
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); }
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); }
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); } } } }
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); }
internal string DefaultValue(QueryToken?token) { return(this.ValueDefinition.DefaultValue(token)); }
string IChartParameterValueDefinition.DefaultValue(QueryToken?token) { return(DefaultValue.ToString(CultureInfo.InvariantCulture)); }
public FilterGroup(FilterGroupOperation groupOperation, QueryToken?token, List <Filter> filters) { this.GroupOperation = groupOperation; this.Token = token; this.Filters = filters; }
public string DefaultValue(QueryToken?token) { return(this.Where(a => a.CompatibleWith(token)).FirstEx(() => "No default parameter value for {0} found".FormatWith(token?.NiceName())).Name); }
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); } } }
public bool CompatibleWith(QueryToken?token) { return(TypeFilter == null || token != null && ChartUtils.IsChartColumnType(token, TypeFilter.Value)); }
public string?Validate(string?parameter, QueryToken?token) { return(null); }
string IChartParameterValueDefinition.DefaultValue(QueryToken?token) { return(DefaultValue); }
public string?Validate(string?value, QueryToken?token) { return(ValueDefinition?.Validate(value, token)); }