com.sibvisions.util.db
Class DBImporter

java.lang.Object
  extended by com.sibvisions.util.db.DBImporter

public class DBImporter
extends Object

The DBImporter executes DML or DDL statements. The statemens are read from simple text files. Use an empty line or a semicolon to separate statements. Use //, # or -- to comment single lines. A comment line is completly ignored.

It allows simple scripting like:

 ${VAR1} = select max(id) from table
 
 insert into detail(id, name, table_id) values (1, 'JVx', ${VAR1})
 
Use custom commands to extend the standard calls:
 insert into table(name, creation) values ('JVx', executeTime())
 
The above command executeTime is mapped to ExecuteTimeCommand.


Nested Class Summary
protected static class DBImporter.DBStatement
          The DBStatement is a simple POJO that stores the statement (with or without wildcards) and the parameters, if available.
 
Constructor Summary
DBImporter()
           
 
Method Summary
protected  DBImporter.DBStatement createDBStatement(String pStatement)
          Creates a statement and parses the parameters.
 boolean execute(Connection pConnection, InputStream pInput)
          Reads the statements from a stream and executes each statement.
 Exception[] getExceptions()
          Gets the last occured exception(s).
 Object getParameter(String pParameter)
          Gets the value of a user-defined parameter.
 boolean isCancelOnFailure()
          Gets whether the script execution should be stopped if an error occurs.
 boolean isParameterSet(String pParameter)
          Gets whether a parameter is set.
 List<DBImporter.DBStatement> list(InputStream pInput)
          Gets all statements from the given stream.
 void setCancelOnFailure(boolean pCancel)
          Sets whether the complete script should be executed independent of errors.
 void setParameter(String pParameter, Object pValue)
          Sets a user-defined parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBImporter

public DBImporter()
Method Detail

list

public List<DBImporter.DBStatement> list(InputStream pInput)
                                  throws Exception
Gets all statements from the given stream.

Parameters:
pInput - the stream with statements
Returns:
the list of found statements
Throws:
Exception - if statement parsing fails

execute

public boolean execute(Connection pConnection,
                       InputStream pInput)
                throws Exception
Reads the statements from a stream and executes each statement.

Parameters:
pConnection - the database connection
pInput - the statement stream
Returns:
true if executions was successful, false if an error occured and setCancelOnFailure(boolean) is enabled
Throws:
Exception - if execution of a statement failed and setCancelOnFailure(boolean) is disabled

createDBStatement

protected DBImporter.DBStatement createDBStatement(String pStatement)
                                            throws Exception
Creates a statement and parses the parameters.

Parameters:
pStatement - the complete statement
Returns:
the statement with parameter syntax
Throws:
Exception - if parameter detection fails

setCancelOnFailure

public void setCancelOnFailure(boolean pCancel)
Sets whether the complete script should be executed independent of errors.

Parameters:
pCancel - true to ignore exceptions during execution, false to throw exceptions immediate and stop execution
See Also:
getExceptions()

isCancelOnFailure

public boolean isCancelOnFailure()
Gets whether the script execution should be stopped if an error occurs.

Returns:
true if execution is stopped after the first exception, false to collect exceptions
See Also:
getExceptions()

getExceptions

public Exception[] getExceptions()
Gets the last occured exception(s).

Returns:
the execution exception(s)
See Also:
setCancelOnFailure(boolean)

setParameter

public void setParameter(String pParameter,
                         Object pValue)
Sets a user-defined parameter.

Parameters:
pParameter - the parameter name
pValue - the value

getParameter

public Object getParameter(String pParameter)
Gets the value of a user-defined parameter.

Parameters:
pParameter - the parameter name
Returns:
the value. null is possible if the value is not set or the value itself is null. isParameterSet(String)

isParameterSet

public boolean isParameterSet(String pParameter)
Gets whether a parameter is set. This methods helps to find out if the value of a parameter is null or the parameter is not set.

Parameters:
pParameter - the parameter name
Returns:
true if the parameter is set, false if the parameter is not available


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.