com.cafesoft.core.jdbc
Class SqlCommandIterator

java.lang.Object
  |
  +--com.cafesoft.core.jdbc.SqlCommandIterator
All Implemented Interfaces:
Debuggable

public class SqlCommandIterator
extends Object
implements Debuggable

SqlCommandIterator parses semicolon-terminated SQL commands from a file and returns them one by one via an iterator API. The parser does not check SQL commands contained in the file for syntactic correctness, but behaves as follows:

Here's some example file content:
 #
 #---	DROP the USERS table if it exists, then create it
 #
 DROP TABLE USERS IF EXISTS;
 CREATE TABLE USERS (
         USERNAME VARCHAR(20) DEFAULT '' NOT NULL,
         BALANCE INTEGER DEFAULT 0 NOT NULL,
         PRIMARY KEY (USERNAME),
         UNIQUE (USERNAME)
 );

 #
 #---	INSERT sample data
 #
 INSERT INTO USERS VALUES ('User1',101);
 INSERT INTO USERS VALUES ('User2',102);
 INSERT INTO USERS VALUES ('User3',103);
 INSERT INTO USERS VALUES ('User4',104);
 


Constructor Summary
SqlCommandIterator(String filePath)
          Create a new SqlCommandIterator.
SqlCommandIterator(String filePath, Logger logger)
          Create a new SqlCommandIterator with the specified Logger.
 
Method Summary
 int getLineNumber()
          Get the line number of the current SQL command.
 boolean hasNext()
          Check for a next SQL command.
 String next()
          Get the next SQL command.
 boolean setDebug(boolean enable)
          Enable or disable debugging.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlCommandIterator

public SqlCommandIterator(String filePath)
                   throws FileNotFoundException,
                          IOException
Create a new SqlCommandIterator.

Parameters:
filePath - the path to the SQL command file.
Throws:
FileNotFoundException - if the file does not exist.
IOException - if unable to read from the file.

SqlCommandIterator

public SqlCommandIterator(String filePath,
                          Logger logger)
                   throws FileNotFoundException,
                          IOException
Create a new SqlCommandIterator with the specified Logger.

Parameters:
filePath - the path to the SQL command file.
Throws:
FileNotFoundException - if the file does not exist.
IOException - if unable to read from the file.
Method Detail

setDebug

public boolean setDebug(boolean enable)
Enable or disable debugging.

Specified by:
setDebug in interface Debuggable
Parameters:
enable - if true, enable debugging, else disable debugging.
Returns:
the previous value of the debug flag, which is useful for restoring the debug state if temporarily changed.

hasNext

public boolean hasNext()
Check for a next SQL command.

Returns:
true if another SQL command is available, else false.

next

public String next()
Get the next SQL command.

Returns:
the next SQL command as a String or null if another SQL command is not available.

getLineNumber

public int getLineNumber()
Get the line number of the current SQL command.

Returns:
the line number within the command file at which the SQL command last retrieved with next() starts.


Generated on 5:16:42 PM May 12, 2008, © 1996-2005 Cafésoft LLC. All rights reserved.