Class SqlParser

java.lang.Object
com.randomnoun.common.db.SqlParser

public class SqlParser extends Object
A class to read an InputStream containing SQL statements (e.g. a MySQL input file) and split it into individual SQL statements.

Some MySQL command-line directives (e.g. 'DELIMITER') are processed whilst parsing.

  • Constructor Details

  • Method Details

    • parseStatements

      public List<String> parseStatements(InputStream is, boolean includeComments) throws IOException, ParseException
      Convert an InputStream of SQL statements into a List of individual statements. Statements are delimited by ";" strings that occur outside of strings or comments. The delimiter string is not included in the returned list of statements.

      The delimiter string may be changed using the 'delimiter str' command. This command may end with a newline instead of a delimiter.

      Comments may also be returned in the result.

      Comments are defined by '-- to-end-of-line' or '/* within slash-star star-slash **/' syntax. Comments that are created with '--' and that occur within a statement are returned before that statement has finished parsing.

      NB: Does not handle escape sequences found within double or single quotes (i.e. escape sequences are handled by the underlying database, not by this Parser)

      Parameters:
      is - inputStream to parse
      includeComments - include comment strings in result
      Throws:
      IOException
      ParseException - unclosed /*-style comment or single/double-quoted string
    • parseStatements

      public List<String> parseStatements(Reader r, boolean includeComments) throws IOException, ParseException
      Convert an InputStream of SQL statements into a List of individual statements. Statements are delimited by ";" strings that occur outside of strings or comments. The delimiter string is not included in the returned list of statements.

      The delimiter string may be changed using the 'delimiter str' command. This command may end with a newline instead of a delimiter.

      Comments may also be returned in the result.

      Comments are defined by '-- to-end-of-line' or '/* within slash-star star-slash **/' syntax. Comments that are created with '--' and that occur within a statement are returned before that statement has finished parsing.

      NB: Does not handle escape sequences found within double or single quotes (i.e. escape sequences are handled by the underlying database, not by this Parser)

      Parameters:
      includeComments - include comment strings in result
      reader - Reader to parse
      Throws:
      IOException
      ParseException - unclosed /*-style comment or single/double-quoted string
    • consumeStatements

      public void consumeStatements(InputStream is, boolean includeComments, SqlParser.Consumer<String> consumer) throws IOException, ParseException
      convert a text file of SQL statements into a List of individual statements. Comments may also be returned in the result.

      NB: Does not handle escape sequences found within double or single quotes

      Parameters:
      includeComments - include comment strings in result
      Throws:
      IOException
      ParseException - unclosed /*-style comment or single/double-quoted string
    • consumeStatements

      public void consumeStatements(Reader is, boolean includeComments, SqlParser.Consumer<String> consumer) throws IOException, ParseException
      Throws:
      IOException
      ParseException