wt.dataservice
Class SQLScriptExecutor

java.lang.Object
  extended bywt.dataservice.SQLScriptExecutor
Direct Known Subclasses:
SQLScriptExecutor

public class SQLScriptExecutor
extends Object

Used to execute general SQL strings or to SQL Script files, but the result is a long string equal to "as seen on a console".

This class is thread-safe but can only execute scripts on one thread at a time. However, it can be used multiple times in succession.


Nested Class Summary
static class SQLScriptExecutor.ExecutionException
           
 
Field Summary
private  boolean configureFromPropertyFiles_
           
private  String connectString_
           
private static String DB_DIR
           
private  long inactivityTimeout_
           
private  long overallTimeout_
           
private  String password_
           
private static String SQL_DIR
           
private static File sqlPathFile_
           
private  String userName_
           
private static WTProperties wtProperties_
           
 
Constructor Summary
SQLScriptExecutor()
           
 
Method Summary
private  File createScriptToCall(String sql_to_execute)
           
private  String execute(String sql_to_execute)
          Buffers the constructor's argument in a wt.temp file and executes the file using SQLPlus.
protected  String execute(String sql_to_execute, PrintStream print_stream)
          Buffers the constructor's argument in a wt.temp file and executes the file using SQLPlus.
 String executeScript(File script_file)
          Execute the script in the specified file.
 void executeScript(File script_file, PrintStream print_stream)
          Execute the script in the specified file.
 String executeScript(String sql_script_path)
          Specify the full path to a script file or a relative path to the script based on $(wt.home)/db/sql, and it will be executed it in a SQLPlus session.
 String executeSQL(String sql_string)
          Execute the supplied SQL String in a SQLPlus session.
 long getInactivityTimeOut()
           
private  String getLoginStringFromFields()
           
private  String getLoginStringFromProperties()
          Parse the DSProperties for the USER/PASSWORD/SERVICE information, and maybe a connectString.
 long getOverallTimeOut()
           
private  File getSQLPathFile()
          The path to the SQL script file is held in a File object.
private static WTProperties getWTProperties()
          The constructor performed a one-time call to obtain these wt.properties
static void main(String[] args)
          When invoked will execute args[0] as a sql command in SQL*Plus using db.properties for connect credentials.
 void setConfigureConnectionFromPropertyFiles(boolean config_from_props)
          If true, tells the executor that when it executes, it should obtain connection information from db.properties (in particular it should use the properties wt.pom.dbUser, wt.pom.dbPassword, wt.pom.serviceName, and optionally wt.pom.connectString Otherwise, the methods setUserName(java.lang.String), setPassword(java.lang.String), and setConnectString(java.lang.String) must be used.
 void setConnectString(String connect_string)
          Sets the SQL*Plus connect string (i.e.
 void setInactivityTimeout(long inactivity_time_out)
          Sets the maximum amount of inactivity (time in milliseconds) that will be allowed before the SQL*Plus script is considered hung.
 void setOverallTimeout(long overall_time_out)
          Sets the maximum time (in milliseconds) that will be allowed for each script to run in its entirety.
 void setPassword(String password)
          Sets the SQL*Plus connect password.
 void setUserName(String user_name)
          Sets the SQL*Plus connect user name.
private  String systemIndependentPath(String relative_path_file_name)
          Corrects relative paths for the UNIX versus Windows standard for the separatorChar.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DB_DIR

private static final String DB_DIR
See Also:
Constant Field Values

SQL_DIR

private static final String SQL_DIR
See Also:
Constant Field Values

wtProperties_

private static WTProperties wtProperties_

sqlPathFile_

private static File sqlPathFile_

inactivityTimeout_

private long inactivityTimeout_

overallTimeout_

private long overallTimeout_

configureFromPropertyFiles_

private boolean configureFromPropertyFiles_

userName_

private String userName_

password_

private String password_

connectString_

private String connectString_
Constructor Detail

SQLScriptExecutor

public SQLScriptExecutor()
Method Detail

setConfigureConnectionFromPropertyFiles

public void setConfigureConnectionFromPropertyFiles(boolean config_from_props)
If true, tells the executor that when it executes, it should obtain connection information from db.properties (in particular it should use the properties wt.pom.dbUser, wt.pom.dbPassword, wt.pom.serviceName, and optionally wt.pom.connectString Otherwise, the methods setUserName(java.lang.String), setPassword(java.lang.String), and setConnectString(java.lang.String) must be used.

The default value for this is "false"


setUserName

public void setUserName(String user_name)
Sets the SQL*Plus connect user name. This method only has an effect if (false) is called.


setPassword

public void setPassword(String password)
Sets the SQL*Plus connect password. This method only has an effect if (false) is called.


setConnectString

public void setConnectString(String connect_string)
Sets the SQL*Plus connect string (i.e. the part after the @ in the login string). This method only has an effect if (false) is called.


getInactivityTimeOut

public long getInactivityTimeOut()

getOverallTimeOut

public long getOverallTimeOut()

setOverallTimeout

public void setOverallTimeout(long overall_time_out)
Sets the maximum time (in milliseconds) that will be allowed for each script to run in its entirety. A value of zero means allow the script to run indefinitely. This value, if specified, should be greater than or equal to the value for the inactivity timeout. Validation of the value will be validated when scripts are executed.

See Also:
setInactivityTimeout(long)

setInactivityTimeout

public void setInactivityTimeout(long inactivity_time_out)
Sets the maximum amount of inactivity (time in milliseconds) that will be allowed before the SQL*Plus script is considered hung. A value of zero means allow SQL*Plus to run indefinitely without producing change in the subprocess's output streams. This value, if specified, should be less than or equal to the value for the overall timeout unless it is specified to be zero. Validation of the value will be validated when scripts are executed.

See Also:
setOverallTimeout(long)

executeSQL

public String executeSQL(String sql_string)
                  throws SQLScriptExecutor.ExecutionException
Execute the supplied SQL String in a SQLPlus session.

Parameters:
sql_string - Approximately used for: " sqlplus user/password&064;system sql_string; ".
Returns:
The script's output which should always be non-null.
Throws:
SQLScriptExecutor.ExecutionException - If the SQL could not be executed. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

executeScript

public String executeScript(String sql_script_path)
                     throws SQLScriptExecutor.ExecutionException
Specify the full path to a script file or a relative path to the script based on $(wt.home)/db/sql, and it will be executed it in a SQLPlus session.

If you provide a relative path, then the wt.properties and db.properties files will be consulted to identify the system's db/sql path. For this to work, you must be able to load the proper version of wt.properties off of the classpath.

Parameters:
sql_script_path - the script to be executed as an absolute path to a file or a path relative to $(wt.home)/db/sql
Returns:
The script's output which should always be non-null.
Throws:
SQLScriptExecutor.ExecutionException - If the file cannot be found relative to the $(wt.home)/db/sql directory or if there is an error invoking SQLPlus or executing the SQL contained in the script file. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

executeScript

public String executeScript(File script_file)
                     throws SQLScriptExecutor.ExecutionException
Execute the script in the specified file. It is the caller's responsibility to ensure the proper parent path of the file.

Parameters:
script_file - sql script that will be executed
Returns:
The script's output which should always be non-null.
Throws:
SQLScriptExecutor.ExecutionException - If the file cannot be found if there is an error invoking SQLPlus or executing the SQL contained in the script file. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

executeScript

public void executeScript(File script_file,
                          PrintStream print_stream)
                   throws SQLScriptExecutor.ExecutionException
Execute the script in the specified file. The output goes to the PrintStream. It is the caller's responsibility to ensure the proper parent path of the file.

Parameters:
script_file - Approximately used for: " sqlplus user/password&064;system &064;script_file ".
print_stream - a stream to write the script's output to
Throws:
SQLScriptExecutor.ExecutionException - If the file cannot be found if there is an error invoking SQLPlus or executing the SQL contained in the script file. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

systemIndependentPath

private String systemIndependentPath(String relative_path_file_name)
Corrects relative paths for the UNIX versus Windows standard for the separatorChar.

Parameters:
relative_path_file_name - This may have back-slash characters.
Returns:
All the back-slash characters replaced with forward-slash characters.

getSQLPathFile

private File getSQLPathFile()
                     throws SQLScriptExecutor.ExecutionException
The path to the SQL script file is held in a File object. The SQL script file itself is not part of this File object.

Returns:
File The object used only to ask its path, not its contents.
Throws:
SQLScriptExecutor.ExecutionException - If processing is unable to reade or use the properties.

getWTProperties

private static WTProperties getWTProperties()
                                     throws SQLScriptExecutor.ExecutionException
The constructor performed a one-time call to obtain these wt.properties

Returns:
WTProperties The LocalProperties (mostly in wt.properties).
Throws:
SQLScriptExecutor.ExecutionException - A converted IOException.

getLoginStringFromProperties

private String getLoginStringFromProperties()
                                     throws IOException,
                                            SQLScriptExecutor.ExecutionException
Parse the DSProperties for the USER/PASSWORD/SERVICE information, and maybe a connectString. Also, obtain OVERALL and INACTIVITY timeouts.

Returns:
The access credentials found in the db.properties file.
Throws:
IOException - If processing can not interpret or parse the strings in the db.properties file.
SQLScriptExecutor.ExecutionException

getLoginStringFromFields

private String getLoginStringFromFields()
                                 throws SQLScriptExecutor.ExecutionException
Throws:
SQLScriptExecutor.ExecutionException

createScriptToCall

private File createScriptToCall(String sql_to_execute)
                         throws SQLScriptExecutor.ExecutionException
Throws:
SQLScriptExecutor.ExecutionException

execute

private String execute(String sql_to_execute)
                throws SQLScriptExecutor.ExecutionException
Buffers the constructor's argument in a wt.temp file and executes the file using SQLPlus.

Parameters:
sql_to_execute - Typical of the SQL command(s) one would type, where some previous lines performed the login.
Returns:
The script's output which should always be non-null.
Throws:
SQLScriptExecutor.ExecutionException - If the SQL could not be executed. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

execute

protected String execute(String sql_to_execute,
                         PrintStream print_stream)
                  throws SQLScriptExecutor.ExecutionException
Buffers the constructor's argument in a wt.temp file and executes the file using SQLPlus.

This is the single point where all script execution is performed. Subtypes may override this to extend execution behavior.

Parameters:
sql_to_execute - This is the SQL that is buffered into a file.
print_stream - Used to start reading large outputs, even before execution has completed.
Returns:
The script's output, which will always be non-null, but will be blank if print_stream is null.
Throws:
SQLScriptExecutor.ExecutionException - If the SQL could not be executed. If there's a timeout, then getNestedThrowable() will be an instanceof wt.util.ProcessLauncher.TimeoutException

main

public static void main(String[] args)
When invoked will execute args[0] as a sql command in SQL*Plus using db.properties for connect credentials.