org.apache.derby.impl.tools.ij
Class StatementFinder

java.lang.Object
  extended byorg.apache.derby.impl.tools.ij.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.

The only comment form currently recognized is the SQL comment, which begins with "--" and ends at the next EOL.

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'.

Author:
ames

Field Summary
private  boolean atEOF
           
private static char DOUBLEQUOTE
           
private static int END_OF_INPUT
           
private static int END_OF_STATEMENT
           
private static char FORMFEED
           
private static int IN_SQLCOMMENT
           
private static int IN_STATEMENT
           
private static int IN_STRING
           
private static char MINUS
           
private static char NEWLINE
           
private  char peekChar
           
private  boolean peeked
           
private  boolean peekEOF
           
private static char RETURN
           
private static char SEMICOLON
           
private static char SINGLEQUOTE
           
private  java.io.Reader source
           
private static char SPACE
           
private  int state
           
private  java.lang.StringBuffer statement
           
private static char TAB
           
 
Constructor Summary
StatementFinder(java.io.Reader s)
          The constructor does not assume the stream is data input or buffered, so it will wrap it appropriately.
 
Method Summary
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  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(java.io.Reader 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
 

Field Detail

source

private java.io.Reader source

statement

private java.lang.StringBuffer statement

state

private int state

atEOF

private boolean atEOF

peekEOF

private boolean peekEOF

peekChar

private char peekChar

peeked

private boolean peeked

IN_STATEMENT

private static final int IN_STATEMENT
See Also:
Constant Field Values

IN_STRING

private static final int IN_STRING
See Also:
Constant Field Values

IN_SQLCOMMENT

private static final int IN_SQLCOMMENT
See Also:
Constant Field Values

END_OF_STATEMENT

private static final int END_OF_STATEMENT
See Also:
Constant Field Values

END_OF_INPUT

private static final int END_OF_INPUT
See Also:
Constant Field Values

MINUS

private static final char MINUS
See Also:
Constant Field Values

SINGLEQUOTE

private static final char SINGLEQUOTE
See Also:
Constant Field Values

DOUBLEQUOTE

private static final char DOUBLEQUOTE
See Also:
Constant Field Values

SEMICOLON

private static final char SEMICOLON
See Also:
Constant Field Values

NEWLINE

private static final char NEWLINE
See Also:
Constant Field Values

RETURN

private static final char RETURN
See Also:
Constant Field Values

SPACE

private static final char SPACE
See Also:
Constant Field Values

TAB

private static final char TAB
See Also:
Constant Field Values

FORMFEED

private static final char FORMFEED
See Also:
Constant Field Values
Constructor Detail

StatementFinder

public StatementFinder(java.io.Reader s)
The constructor does not assume the stream is data input or buffered, so it will wrap it appropriately.

Parameters:
s - the input stream for reading statements from.
Method Detail

ReInit

public void ReInit(java.io.Reader s)
Reinit is used to redirect the finder to another stream. The previous stream should not have been in a PEEK state.

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

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.


Apache Derby V10.0 Engine Documentation - Copyright © 1997,2004 The Apache Software Foundation or its licensors, as applicable.