public void Analyze(IEnumerable <Token> query, IList <IDictionary <QueryPart, IEnumerable <Token> > > result, IDictionary <QueryPart, IEnumerable <Token> > currParts = null) { currParts = currParts ?? new Dictionary <QueryPart, IEnumerable <Token> > { { QueryPart.SELECT, GetSelect(query) }, { QueryPart.FROM, GetFrom(query) }, { QueryPart.WHERE, GetWhere(query) } }; var analyzer = new BaseAnalyzer(); var keys = new QueryPart[currParts.Count]; currParts.Keys.CopyTo(keys, 0); foreach (var key in keys) { analyzer.StrategyType = key; analyzer.Analyze(currParts[key], result, currParts); } result.Add(currParts); }
public static IEnumerable <Token> SplitOnUnion(QueryPart part, IEnumerable <Token> tokens, IList <IDictionary <QueryPart, IEnumerable <Token> > > result, IDictionary <QueryPart, IEnumerable <Token> > currParts) { var tokenArray = tokens.ToArray(); var resultTokens = new List <Token>(); var analyzer = new BaseAnalyzer(); for (var i = 0; i < tokenArray.Length; i++) { var token = tokenArray[i]; if (token.Type.GetType() == SQLTokenTypeEnum.KEYWORD && token.Text.Equals("or", StringComparison.OrdinalIgnoreCase)) { var additionalArray = new Token[resultTokens.Count]; resultTokens.CopyTo(additionalArray); var newTokens = additionalArray .Reverse() .SkipWhile(t => !t.Text.Equals("or", StringComparison.OrdinalIgnoreCase) && !t.Text.Equals("and", StringComparison.OrdinalIgnoreCase) && !t.Text.Equals("on", StringComparison.OrdinalIgnoreCase)) .Reverse() .ToList(); newTokens.AddRange(tokenArray.Skip(i + 1)); var newParts = new Dictionary <QueryPart, IEnumerable <Token> >(currParts); newParts[part] = newTokens; analyzer.StrategyType = QueryPart.QUERY; analyzer.Analyze(newTokens, result, newParts); i = tokenArray.Skip(i + 1) .SkipWhile(t => !t.Text.Equals("or", StringComparison.OrdinalIgnoreCase) || !t.Text.Equals("and", StringComparison.OrdinalIgnoreCase) || !t.Text.Equals("on", StringComparison.OrdinalIgnoreCase)) .Select((t, index) => index).FirstOrDefault(); if (i == 0) { break; } } else { resultTokens.Add(token); } } return(resultTokens); }
public virtual string GetOptimizedQuery(string select) { var tokenizer = new RegexTokenizer(select); var analyzer = new BaseAnalyzer(); var res = new List <IDictionary <QueryPart, IEnumerable <Token> > >(); analyzer.StrategyType = QueryPart.QUERY; analyzer.Analyze(tokenizer, res); var resultSelect = new SelectBuilder(); foreach (var tokenQuery in res) { if (resultSelect.IsEmpty) { BuidSelect(tokenQuery, resultSelect); } else { resultSelect.Union(BuidSelect(tokenQuery)); } } return(resultSelect.ToString()); }