Class SqlCommentScrubber


  • public class SqlCommentScrubber
    extends Object
    This class represents a parser that will remove SQL comments (both multi-line and single-line) from a string representing a SQL query. It respects the notion of a string literal, such that if a comment appears in a string literal, it is treated as part of the string instead of a comment. Both single-quoted and double-quoted string literals are supported, including nested quotes (whether the SQL dialect supports them or not).
    Author:
    Matt Burgess
    • Method Detail

      • removeComments

        public static String removeComments​(String text)
        This method will remove SQL comments (both multi-line and single-line) from a string representing a SQL query. It respects the notion of a string literal, such that if a comment appears in a string literal, it is treated as part of the string instead of a comment. A simple state machine is implemented, keeping track of whether the current character is starting, ending, or inside a comment construct. The state machine also checks to see if the current character is starting, ending, or inside a single-quoted string literal, as this takes precedence over comment constructs. In other words, comments inside strings are not actually comments, they are part of the string literal.
        Parameters:
        text - a string representing the SQL query to parse and from which to remove comments
        Returns:
        the input string with SQL comments removed, or null if the input string is null