public final class SQLScriptHelper extends Object
Default separator for isolating statements is the semi colon
character: ;
.
It can be changed by using a comment like the following snippet
for using the string ---
:
-- _separator_=---
begin
execute immediate 'DROP TABLE MYTABLE';
Exception when others then null;
end;
---
-- _separator_=;
CREATE TABLE MYTABLE;
...
Note that the command must be placed at the end of the comment.
The runner can be configured to ignore SQLExceptions. This can be useful
to execute DROP statements when it's unknown if the tables exist:
--_ignore_exceptions_=on
DROP TABLE MYTABLE;
--_ignore_exceptions_=off
Modifier and Type | Class and Description |
---|---|
protected static class |
SQLScriptHelper.ScriptContext
Script execution context.
|
Modifier and Type | Field and Description |
---|---|
protected static Logger |
__LOGGER
Logger available to subclasses.
|
static String |
CHANGE_SEPARATOR_COMMAND
Command to change the separator.
|
static String |
DEFAULT_SEPARATOR
Default separator used for isolating statements.
|
static String |
IGNORE_EXCEPTIONS_COMMAND
Command to ignore sql exceptions.
|
Modifier | Constructor and Description |
---|---|
private |
SQLScriptHelper() |
Modifier and Type | Method and Description |
---|---|
private static void |
_execute(Connection connection,
String command,
int lineNumber,
SQLScriptHelper.ScriptContext scriptContext) |
private static void |
_processCommand(Connection connection,
StringBuilder command,
int lineNumber,
SQLScriptHelper.ScriptContext scriptContext) |
static boolean |
createTableIfNotExists(Connection connection,
String tableNameToCheck,
String location,
SourceResolver sourceResolver)
This method will test if a table exists, and if not will execute a script to create it
|
static boolean |
createTableIfNotExists(Connection connection,
String tableNameToCheck,
String location,
SourceResolver sourceResolver,
Map<String,String> replace)
This method will test if a table exists, and if not will execute a script to create it
|
static boolean |
createTableIfNotExists(String datasourceId,
String tableNameToCheck,
String location,
SourceResolver sourceResolver)
This method will test if a table exists, and if not will execute a script to create it
|
static boolean |
createTableIfNotExists(String datasourceId,
String tableNameToCheck,
String location,
SourceResolver sourceResolver,
Map<String,String> replace)
This method will test if a table exists, and if not will execute a script to create it
|
protected static boolean |
processScriptLine(String line,
StringBuilder commandBuffer,
SQLScriptHelper.ScriptContext scriptContext)
Process a script line.
|
static void |
runScript(Connection connection,
InputStream is)
Run a SQL script using the connection passed in.
|
static void |
runScript(Connection connection,
String script)
Run a SQL script using the connection passed in.
|
static boolean |
tableExists(Connection connection,
String tableName)
Checks whether the given table exists in the database.
|
public static final String DEFAULT_SEPARATOR
public static final String IGNORE_EXCEPTIONS_COMMAND
public static final String CHANGE_SEPARATOR_COMMAND
private SQLScriptHelper()
public static boolean createTableIfNotExists(String datasourceId, String tableNameToCheck, String location, SourceResolver sourceResolver) throws SQLException, IOException
datasourceId
- The data source id to open a connection to the databasetableNameToCheck
- The name of the table that will be checkedlocation
- The source location where to find the script to execute to create the table. This string will be format with String.format with the dbType as argument.sourceResolver
- The source resolverSQLException
- If an error occurred while executing SQL script, or while testing table existenceIOException
- If an error occurred while getting the script file, or if the url is malformedpublic static boolean createTableIfNotExists(String datasourceId, String tableNameToCheck, String location, SourceResolver sourceResolver, Map<String,String> replace) throws SQLException, IOException
datasourceId
- The data source id to open a connection to the databasetableNameToCheck
- The name of the table that will be checkedlocation
- The source location where to find the script to execute to create the table. This string will be format with String.format with the dbType as argument.sourceResolver
- The source resolverreplace
- The map of string to replace. Key is the regexp to seek, value is the replacing string.SQLException
- If an error occurred while executing SQL script, or while testing table existenceIOException
- If an error occurred while getting the script file, or if the url is malformedpublic static boolean createTableIfNotExists(Connection connection, String tableNameToCheck, String location, SourceResolver sourceResolver) throws SQLException, IOException
connection
- The database connection to usetableNameToCheck
- The name of the table that will be checkedlocation
- The source location where to find the script to execute to create the table. This string will be format with String.format with the dbType as argument.sourceResolver
- The source resolverSQLException
- If an error occurred while executing SQL script, or while testing table existenceIOException
- If an error occurred while getting the script file, or if the url is malformedpublic static boolean createTableIfNotExists(Connection connection, String tableNameToCheck, String location, SourceResolver sourceResolver, Map<String,String> replace) throws SQLException, IOException
connection
- The database connection to usetableNameToCheck
- The name of the table that will be checkedlocation
- The source location where to find the script to execute to create the table. This string will be format with String.format with the dbType as argument.sourceResolver
- The source resolverreplace
- The map of string to replace. Key is the regexp to seek, value is the replacing string.SQLException
- If an error occurred while executing SQL script, or while testing table existenceIOException
- If an error occurred while getting the script file, or if the url is malformedpublic static boolean tableExists(Connection connection, String tableName) throws SQLException
connection
- The database connectiontableName
- the name of the tableSQLException
- In an SQL exception occurspublic static void runScript(Connection connection, String script) throws IOException, SQLException
connection
- the connection to use for the scriptscript
- the script data.IOException
- if an error occurs while reading the script.SQLException
- if an error occurs while executing the script.public static void runScript(Connection connection, InputStream is) throws IOException, SQLException
connection
- the connection to use for the scriptis
- the input stream containing the script data.IOException
- if an error occurs while reading the script.SQLException
- if an error occurs while executing the script.protected static boolean processScriptLine(String line, StringBuilder commandBuffer, SQLScriptHelper.ScriptContext scriptContext)
line
- the line to process.commandBuffer
- the command buffer.scriptContext
- the script execution context.private static void _processCommand(Connection connection, StringBuilder command, int lineNumber, SQLScriptHelper.ScriptContext scriptContext) throws SQLException
SQLException
private static void _execute(Connection connection, String command, int lineNumber, SQLScriptHelper.ScriptContext scriptContext) throws SQLException
SQLException