Class SqlCommentParser


  • public class SqlCommentParser
    extends Object
    Utility class for parsing out comments from SQL strings
    • Constructor Detail

      • SqlCommentParser

        public SqlCommentParser()
    • Method Detail

      • forEachNonCommentChar

        public static void forEachNonCommentChar​(String sql,
                                                 SqlCommentParser.SqlCharConsumer consumer)
        Iterates over each character in the SQL string while keeping track of comment, string literal, and identifier state. Each character that is not part of a comment calls the consumer with the current state and character. Emits a ' ' character after each comment to avoid token fusion.
        Parameters:
        sql - the SQL string to parse
        consumer - called for each visible character with its parsing state
      • findPlaceholderPositions

        public static int[] findPlaceholderPositions​(String sql)
        Finds the source-string indices of placeholder ('?') characters that appear in SqlCommentParser.State.NORMAL state — i.e. not inside comments, string literals, or quoted identifiers. Used by parameter binding to locate the real placeholders in a SQL statement.
        Parameters:
        sql - the SQL string to scan
        Returns:
        source-string indices of placeholder '?' characters, in order
      • stripCommentsAndWhitespaces

        public static String stripCommentsAndWhitespaces​(String sql)
        Removes all comments and extra whitespace from the SQL string.
        Parameters:
        sql - the SQL string to remove comments and extra whitespace from
        Returns:
        the SQL string with all comments and extra whitespace removed