com.agimatec.sql.script
Class SQLScriptParser

java.lang.Object
  extended by com.agimatec.sql.script.SQLScriptParser

public class SQLScriptParser
extends Object

Title: Agimatec GmbH

Description: This class is capable to parse Oracle-SQL scripts. It is not a fully SQL and PL/SQL parser and somehow heuristic.
Able to parse:
- SQL-statements (insert, update, select, delete, merge, synonyms, grants, create table, alter table, ...). Statements terminated by ; are correctly handled. Statements terminated by / are correctly handled in most situations.
- SQL-comments (single- and multi-line comments) - commit/rollback statements are detected - Triggers
- PL/SQL blocks
Known limitations/bugs:
- parsing PL/SQL package files is currently not supported!
- parsing PL/SQL (triggers, blocks) is not always correct, when statement termination is not detected by this parser correctly. (Just try it.)

Copyright: Copyright (c) 2007

Company: Agimatec GmbH

Author:
Roman Stumm

Constructor Summary
SQLScriptParser(org.apache.commons.logging.Log aLog)
           
SQLScriptParser(String aScriptRoot, org.apache.commons.logging.Log aLog)
           
 
Method Summary
 void execSQLScript(ScriptVisitor visitor, String scriptName)
          execute the content of a file as a single SQL statement.
protected  String fixLF(String aStatement)
          Tested with: Oracle10.2 fix \r\n --> \n (always, otherwise the package will be invalid) remove last / but keep last ; (optional, only if a / was found after a ;)
protected  org.apache.commons.logging.Log getLog()
           
protected  String getScriptDir()
           
static Reader getURLReader(URL url)
           
protected  void handleAffectedRow(int affectedRows, String command)
           
protected  void handleError(JdbcException ex, String command)
           
protected  void handleError(SQLException ex, String command)
           
 void iterateSQL(ScriptVisitor visitor, Reader input)
          parse and visit the statements in the given sql string.
 void iterateSQL(ScriptVisitor visitor, String aSqls)
          parse and visit the statements in the given sql string.
 void iterateSQLScript(ScriptVisitor visitor, String scriptName)
          parse an visit the statements in the given sql-script file.
 void iterateSQLScript(ScriptVisitor visitor, URL url)
          parse an visit the statements in the given sql-script file.
protected  Object[] openReaderPath(String scriptName)
          when scriptName starts with cp:// read the scriptName as a resource from the classpath, otherwise access the script as a file or cp:// resource by scriptdir + scriptname.
 void setEnvironment(Map aEnv)
          set a Map of environment entries that are replaced during parse inside the SQL Statements/comments
 void setFailOnError(boolean aFailOnError)
          default = false true: stop executing the SQLScript when the first error occurs and throw an exception.
 void useLogger(org.apache.commons.logging.Log aLogger)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SQLScriptParser

public SQLScriptParser(String aScriptRoot,
                       org.apache.commons.logging.Log aLog)

SQLScriptParser

public SQLScriptParser(org.apache.commons.logging.Log aLog)
Method Detail

useLogger

public void useLogger(org.apache.commons.logging.Log aLogger)

setFailOnError

public void setFailOnError(boolean aFailOnError)
default = false true: stop executing the SQLScript when the first error occurs and throw an exception. false: log errors to log and continue without throwing an exception.

Parameters:
aFailOnError - - true/false

handleAffectedRow

protected void handleAffectedRow(int affectedRows,
                                 String command)

handleError

protected void handleError(SQLException ex,
                           String command)
                    throws SQLException
Throws:
SQLException

handleError

protected void handleError(JdbcException ex,
                           String command)
                    throws JdbcException
Throws:
JdbcException

getLog

protected org.apache.commons.logging.Log getLog()

setEnvironment

public void setEnvironment(Map aEnv)
set a Map of environment entries that are replaced during parse inside the SQL Statements/comments


getScriptDir

protected String getScriptDir()
Returns:
the root dir for sql scripts or null

fixLF

protected String fixLF(String aStatement)
Tested with: Oracle10.2 fix \r\n --> \n (always, otherwise the package will be invalid) remove last / but keep last ; (optional, only if a / was found after a ;)

Parameters:
aStatement - - file content as String
Returns:
file content as String the can be executed as a single SQL statement via Oracle-JDBC

iterateSQLScript

public void iterateSQLScript(ScriptVisitor visitor,
                             String scriptName)
                      throws SQLException,
                             IOException
parse an visit the statements in the given sql-script file.

Throws:
SQLException - - errors during execution of the SQL statements in the script
IOException - - error accessing the script file (e.g. FileNotFound)

execSQLScript

public void execSQLScript(ScriptVisitor visitor,
                          String scriptName)
                   throws IOException,
                          SQLException
execute the content of a file as a single SQL statement. You can use this, when you need not parse the file or when the file cannot be parsed. Example: use this to execute a PL/SQL package, that is stored in a single file (1 file for the spec, 1 file for the body).

Throws:
IOException
SQLException

openReaderPath

protected Object[] openReaderPath(String scriptName)
                           throws IOException
when scriptName starts with cp:// read the scriptName as a resource from the classpath, otherwise access the script as a file or cp:// resource by scriptdir + scriptname.

Parameters:
scriptName -
Returns:
an array with 2 elements. array[0] = Reader, array[1] = String (Path)
Throws:
IOException - - file not found

iterateSQLScript

public void iterateSQLScript(ScriptVisitor visitor,
                             URL url)
                      throws SQLException,
                             IOException
parse an visit the statements in the given sql-script file.

Parameters:
url - - a complete URL (absolute URL) where the script is
Throws:
SQLException - - errors during execution of the SQL statements in the script
IOException - - error accessing the script file (e.g. FileNotFound)

getURLReader

public static Reader getURLReader(URL url)
                           throws IOException
Throws:
IOException

iterateSQL

public void iterateSQL(ScriptVisitor visitor,
                       String aSqls)
                throws SQLException,
                       IOException
parse and visit the statements in the given sql string.

Parameters:
aSqls - - a SQLScript as a String
visitor - - the visitor to do something with the parsed statements
Throws:
SQLException
IOException

iterateSQL

public void iterateSQL(ScriptVisitor visitor,
                       Reader input)
                throws SQLException,
                       IOException
parse and visit the statements in the given sql string.

Parameters:
input - - a Reader on sql statements
visitor - - the visitor to do something with the parsed statements
Throws:
SQLException
IOException


Copyright © 2008-2012. All Rights Reserved.