Class StatementFinder


  • public class StatementFinder
    extends java.lang.Object
    StatementGrabber looks through an input stream for the next JSQL statement. A statement is considered to be any tokens up to the next semicolon or EOF.

    Semicolons inside comments, strings, and delimited identifiers are not considered to be statement terminators but to be part of those tokens.

    Comments currently recognized include the SQL comment, which begins with "--" and ends at the next EOL, and nested bracketed comments.

    Strings and delimited identifiers are permitted to contain newlines; the actual IJ or JSQL parsers will report errors when those cases occur.

    There are no escaped characters, i.e. "\n" is considered to be two characters, '\' and 'n'.

    • Constructor Summary

      Constructors 
      Constructor Description
      StatementFinder​(LocalizedInput s, LocalizedOutput promptDest)
      The constructor does not assume the stream is data input or buffered, so it will wrap it appropriately.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean atEOF()  
      void close()  
      java.lang.String nextStatement()
      get the next statement in the input stream.
      private char peekChar()
      return the next character in the source stream, without advancing.
      private boolean peekEOF()  
      private void readBracketedComment()
      Advance the source stream to the end of a comment if it is on one, assuming the first character of a potential bracketed comment has been found.
      private char readChar()
      return the next character in the source stream and append it to the statement buffer.
      private void readSingleLineComment​(char commentChar)
      Advance the source stream to the end of a comment if it is on one, assuming the first character of a potential single line comment has been found.
      private void readString​(char stringDelimiter)
      Advance the stream to the end of the string.
      void ReInit​(LocalizedInput s)
      Reinit is used to redirect the finder to another stream.
      private boolean whiteSpace​(char c)
      Determine if the given character is considered whitespace
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StatementFinder

        public StatementFinder​(LocalizedInput s,
                               LocalizedOutput promptDest)
        The constructor does not assume the stream is data input or buffered, so it will wrap it appropriately. If the StatementFinder's input stream is connected to System.in, a LocalizedOutput stream may be given to print line continuation prompts when StatementFinder reads a newline.
        Parameters:
        s - the input stream for reading statements from.
        promptDest - LocalizedOutput stream to write line continuation prompts ("> ") to. If null, no such prompts will be written.
    • Method Detail

      • ReInit

        public void ReInit​(LocalizedInput s)
        Reinit is used to redirect the finder to another stream. The previous stream should not have been in a PEEK state. If an output stream was given when constructing this StatementFinder and the input is standard input, continuation prompting will be enabled.
        Parameters:
        s - the input stream for reading statements from.
      • close

        public void close()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • nextStatement

        public java.lang.String nextStatement()
        get the next statement in the input stream. Returns it, dropping its closing semicolon if it has one. If there is no next statement, return a null.
        Returns:
        the next statement in the input stream.
      • whiteSpace

        private boolean whiteSpace​(char c)
        Determine if the given character is considered whitespace
        Parameters:
        c - the character to consider
        Returns:
        true if the character is whitespace
      • readBracketedComment

        private void readBracketedComment()
        Advance the source stream to the end of a comment if it is on one, assuming the first character of a potential bracketed comment has been found. If it is not a comment, do not advance the stream.
      • readSingleLineComment

        private void readSingleLineComment​(char commentChar)
        Advance the source stream to the end of a comment if it is on one, assuming the first character of a potential single line comment has been found. If it is not a comment, do not advance the stream.

        The form of a single line comment is, in regexp, XX.*$, where XX is two instances of commentChar.

        Parameters:
        commentChar - the character whose duplication signifies the start of the comment.
      • readString

        private void readString​(char stringDelimiter)
        Advance the stream to the end of the string. Assumes the opening delimiter of the string has been read. This handles the SQL ability to put the delimiter within the string by doubling it, by reading those as two strings sitting next to one another. I.e, 'Mary''s lamb' is read by this class as two strings, 'Mary' and 's lamb'.

        The delimiter of the string is expected to be repeated at its other end. If the other flavor of delimiter occurs within the string, it is just a normal character within it.

        All characters except the delimiter are permitted within the string. If EOF is hit before the closing delimiter is found, the end of the string is assumed. Parsers using this parser will detect the error in that case and return appropriate messages.

        Parameters:
        stringDelimiter - the starting and ending character for the string being read.
      • atEOF

        private boolean atEOF()
      • peekEOF

        private boolean peekEOF()
      • readChar

        private char readChar()
        return the next character in the source stream and append it to the statement buffer.
        Returns:
        the next character in the source stream.
      • peekChar

        private char peekChar()
        return the next character in the source stream, without advancing.
        Returns:
        the next character in the source stream.