org.pentaho.di.core.database
Class SqlCommentScrubber

java.lang.Object
  extended by org.pentaho.di.core.database.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
static int EOF
          End-of-File (EOF) indicator
static int EOL
          End-of-Line (EOL) indicator
 
Method Summary
static String removeComments(String text)
          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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final int EOF
End-of-File (EOF) indicator

See Also:
Constant Field Values

EOL

public static final int EOL
End-of-Line (EOL) indicator

See Also:
Constant Field Values
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