Alphashack.Graphdat.Agent.SqlQueryHelper.Outliner.TrySimplify C# (CSharp) Method

TrySimplify() public static method

Simplifies a query by removing the value of certain types of token. Basically the ones that might be parameterised
public static TrySimplify ( string query, string &value ) : bool
query string
value string
return bool
        public static bool TrySimplify(string query, out string value)
        {
            value = "";

            var parser = new TSql100Parser(true);
            IList<ParseError> errors;
            var result = parser.Parse(new StringReader(query), out errors);

            // cannot parse, cannot simplify
            if (errors.Count > 0)
            {
                LastError = "Cannot parse";
                return false;
            }

            // without at least one batch with at least one statement, cannot simplify
            // (should be 1 batch, 1 statement really as we are tracing StatementEnd)
            var script = result as TSqlScript;
            if (script == null || script.Batches.Count <= 0 && script.Batches[0].Statements.Count <= 0)
            {
                LastError = "Not 1 batch 1 statement";
                return false;
            }

            // only interested in certain types of statements (date manipulation ones)
            if (!HandledStatementType.Contains(script.Batches[0].Statements[0].GetType()))
            {
                LastError = "Not handled statement";
                return false;
            }

            // basically remove all comments, newlines and extra whitespace
            var options = new SqlScriptGeneratorOptions
                              {
                                  AlignClauseBodies = false,
                                  AlignColumnDefinitionFields = false,
                                  AlignSetClauseItem = false,
                                  AsKeywordOnOwnLine = false,
                                  IncludeSemicolons = false,
                                  IndentSetClause = false,
                                  IndentViewBody = false,
                                  NewLineBeforeCloseParenthesisInMultilineList = false,
                                  NewLineBeforeFromClause = false,
                                  NewLineBeforeGroupByClause = false,
                                  NewLineBeforeHavingClause = false,
                                  NewLineBeforeJoinClause = false,
                                  NewLineBeforeOpenParenthesisInMultilineList = false,
                                  NewLineBeforeOrderByClause = false,
                                  NewLineBeforeOutputClause = false,
                                  NewLineBeforeWhereClause = false,
                                  MultilineInsertSourcesList = false,
                                  MultilineInsertTargetsList = false,
                                  MultilineSelectElementsList = false,
                                  MultilineSetClauseItems = false,
                                  MultilineViewColumnsList = false,
                                  MultilineWherePredicatesList = false
                              };
            var generator = new Sql100ScriptGenerator(options);
            var tokens = generator.GenerateTokens(script);

            var summary = new StringBuilder();
            foreach (var token in tokens)
            {
                // replace values for parameterisable token types
                if(ReplaceTokens.Contains(token.TokenType))
                {
                    summary.Append("?");
                }
                else
                {
                    summary.Append(token.Text);
                }
            }
            // trim some junk
            value = summary.ToString().TrimEnd(RemoveChars);

            return true;
        }