henplus
Class SQLStatementSeparator

java.lang.Object
  extended by henplus.SQLStatementSeparator

public class SQLStatementSeparator
extends Object

Simple parser that separates SQLStatements. Example.

-----------------------
 statementSeparator.append("select * from foo; echo $foobar \n");
 while (statementSeparator.hasNext()) {
      String stmt = statementSeparator.next();
      if (stmt.startsWith("echo")) {
          // is ok, this command works always without ';'
          statementSeparator.consumed();
          System.err.println(stmt.substring("echo ".length());
      }
      else {  // SQL-command. we require a semicolon at the end.
         if (!stmt.charAt(stmt.length()-1) == ';') {
            statementSeparator.cont(); // continue.
         }
         else {
            statementSeparator.consumed();
            SQLExecute(stmt);
         }
      }
 }
----------------------- 
FIXME: this is a bit rough and accummulated some ideas. Should be cleaned up.

Author:
Henner Zeller

Constructor Summary
SQLStatementSeparator()
           
 
Method Summary
 void append(String s)
          add a new line including the '\n' to the input buffer.
 void consumed()
          after having called next() and you were pleased with the result call this method to state, that you consumed it.
 void cont()
          after having called next(), call cont(), if you are not yet pleased with the result; the parser should read to the next possible end.
 void discard()
          discard any input.
 PropertyHolder getRemoveCommentsProperty()
          returns a property holder for the remove comments property.
 boolean hasNext()
          returns true, if the parser can find a complete command that either ends with newline or with ';'
 String next()
          returns the next command; requires to call hasNext() before.
 void pop()
           
 void push()
          push the current state and start with a clean one.
 void removeComments(boolean b)
          set, whether comments should be removed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLStatementSeparator

public SQLStatementSeparator()
Method Detail

push

public void push()
push the current state and start with a clean one. Use to parse other files (like includes), and continue then with the old state. like load foobar.sql ; select * from foobar


pop

public void pop()

append

public void append(String s)
add a new line including the '\n' to the input buffer.


discard

public void discard()
discard any input.


cont

public void cont()
after having called next(), call cont(), if you are not yet pleased with the result; the parser should read to the next possible end.


consumed

public void consumed()
after having called next() and you were pleased with the result call this method to state, that you consumed it.


hasNext

public boolean hasNext()
                throws IllegalStateException
returns true, if the parser can find a complete command that either ends with newline or with ';'

Throws:
IllegalStateException

next

public String next()
            throws IllegalStateException
returns the next command; requires to call hasNext() before.

Throws:
IllegalStateException

getRemoveCommentsProperty

public PropertyHolder getRemoveCommentsProperty()
returns a property holder for the remove comments property.


removeComments

public void removeComments(boolean b)
set, whether comments should be removed.



? 1997..2006 Henner Zeller