Package com.databricks.jdbc.common.util
Class SqlCommentParser
- java.lang.Object
-
- com.databricks.jdbc.common.util.SqlCommentParser
-
public class SqlCommentParser extends Object
Utility class for parsing out comments from SQL strings
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSqlCommentParser.IndexedSqlCharConsumerIndex-aware variant ofSqlCommentParser.SqlCharConsumer.static interfaceSqlCommentParser.SqlCharConsumerstatic classSqlCommentParser.State
-
Constructor Summary
Constructors Constructor Description SqlCommentParser()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]findPlaceholderPositions(String sql)Finds the source-string indices of placeholder ('?')static voidforEachNonCommentChar(String sql, SqlCommentParser.IndexedSqlCharConsumer consumer)Same asforEachNonCommentChar(String, SqlCharConsumer)but the consumer also receives the source-string index of each emitted character.static voidforEachNonCommentChar(String sql, SqlCommentParser.SqlCharConsumer consumer)Iterates over each character in the SQL string while keeping track of comment, string literal, and identifier state.static StringstripCommentsAndWhitespaces(String sql)Removes all comments and extra whitespace from the SQL string.
-
-
-
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 parseconsumer- called for each visible character with its parsing state
-
forEachNonCommentChar
public static void forEachNonCommentChar(String sql, SqlCommentParser.IndexedSqlCharConsumer consumer)
Same asforEachNonCommentChar(String, SqlCharConsumer)but the consumer also receives the source-string index of each emitted character. Useful when callers need to relate emitted characters back to positions in the original SQL (e.g. parameter placeholder discovery).
-
findPlaceholderPositions
public static int[] findPlaceholderPositions(String sql)
Finds the source-string indices of placeholder ('?') characters that appear inSqlCommentParser.State.NORMALstate — 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
-
-