public class SQLScriptParser extends Object
Description: This class is capable to parse 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
| Constructor and Description |
|---|
SQLScriptParser(org.slf4j.Logger aLog) |
SQLScriptParser(String aScriptRoot,
org.slf4j.Logger aLog) |
| Modifier and Type | Method and Description |
|---|---|
void |
execSQLScript(ScriptVisitor visitor,
String scriptName)
execute the content of a file as a single SQL statement.
|
void |
execSQLScript(ScriptVisitor visitor,
URL url) |
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.slf4j.Logger |
getLog() |
protected String |
getScriptDir() |
static BufferedReader |
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 |
iterateSQLLines(ScriptVisitor visitor,
String scriptName)
read and visit the statements in the given sql-script file.
|
void |
iterateSQLLines(ScriptVisitor visitor,
URL url)
parse an visit the statements in the given sql-script file.
|
void |
iterateSQLScript(ScriptVisitor visitor,
String scriptName)
parse and 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.slf4j.Logger aLogger) |
public SQLScriptParser(String aScriptRoot, org.slf4j.Logger aLog)
public SQLScriptParser(org.slf4j.Logger aLog)
public void useLogger(org.slf4j.Logger aLogger)
public void setFailOnError(boolean aFailOnError)
aFailOnError - - true/falseprotected void handleAffectedRow(int affectedRows,
String command)
protected void handleError(SQLException ex, String command) throws SQLException
SQLExceptionprotected void handleError(JdbcException ex, String command) throws JdbcException
JdbcExceptionprotected org.slf4j.Logger getLog()
public void setEnvironment(Map aEnv)
protected String getScriptDir()
protected String fixLF(String aStatement)
aStatement - - file content as Stringpublic void iterateSQLScript(ScriptVisitor visitor, String scriptName) throws SQLException, IOException
SQLException - - errors during execution of the SQL statements in the scriptIOException - - error accessing the script file (e.g. FileNotFound)public void iterateSQLLines(ScriptVisitor visitor, String scriptName) throws SQLException, IOException
SQLException - - errors during execution of the SQL statements in the scriptIOException - - error accessing the script file (e.g. FileNotFound)public void iterateSQLLines(ScriptVisitor visitor, URL url) throws SQLException, IOException
url - - a complete URL (absolute URL) where the script isSQLException - - errors during execution of the SQL statements in the scriptIOException - - error accessing the script file (e.g. FileNotFound)public void execSQLScript(ScriptVisitor visitor, String scriptName) throws IOException, SQLException
IOExceptionSQLExceptionpublic void execSQLScript(ScriptVisitor visitor, URL url) throws IOException, SQLException
IOExceptionSQLExceptionprotected Object[] openReaderPath(String scriptName) throws IOException
scriptName - IOException - - file not foundpublic void iterateSQLScript(ScriptVisitor visitor, URL url) throws SQLException, IOException
url - - a complete URL (absolute URL) where the script isSQLException - - errors during execution of the SQL statements in the scriptIOException - - error accessing the script file (e.g. FileNotFound)public static BufferedReader getURLReader(URL url) throws IOException
IOExceptionpublic void iterateSQL(ScriptVisitor visitor, String aSqls) throws SQLException, IOException
aSqls - - a SQLScript as a Stringvisitor - - the visitor to do something with the parsed statementsSQLExceptionIOExceptionpublic void iterateSQL(ScriptVisitor visitor, Reader input) throws SQLException, IOException
input - - a Reader on sql statementsvisitor - - the visitor to do something with the parsed statementsSQLExceptionIOExceptionCopyright © 2008–2018. All rights reserved.