Class SqlCommentScrubber

java.lang.Object
org.pentaho.reporting.libraries.base.util.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
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    End-of-File (EOF) indicator *
    static final int
    End-of-Line (EOL) indicator *
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    This method will remove SQL comments (both multi-line and single-line) from a string representing a SQL query.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • 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