java.lang.Object com.ibm.broker.plugin.MbSQLStatement
public class MbSQLStatement
extends Object
MbSQLStatement provides support for accessing an external ODBC Database. An object of this class is created using one of the createSQLStatement methods of MbNode.
Example usage:
public void evaluate(MbMessageAssembly assembly, MbInputTerminal inTerm) throws MbException { MbMessage newMsg = new MbMessage(assembly.getMessage()); MbMessageAssembly newAssembly = new MbMessageAssembly(assembly, newMsg); String table = assembly.getMessage().getRootElement().getLastChild().getFirstChild().getName(); MbSQLStatement state = createSQLStatement( "dbName", "SET OutputRoot.XML.integer[] = PASSTHRU('SELECT * FROM " + table + "');" ); state.setThrowExceptionOnDatabaseError(false); state.setTreatWarningsAsErrors(true); state.select( assembly, newAssembly ); int sqlCode = state.getSQLCode(); if(sqlCode != 0) { // Do error handling here System.out.println("sqlCode = " + sqlCode); System.out.println("sqlNativeError = " + state.getSQLNativeError()); System.out.println("sqlState = " + state.getSQLState()); System.out.println("sqlErrorText = " + state.getSQLErrorText()); } getOutputTerminal("out").propagate(assembly); }Database access using JDBC is not supported.
Field | Description |
---|---|
SQL_TRANSACTION_AUTO | Transaction specifier. |
SQL_TRANSACTION_COMMIT | Transaction specifier. |
Method | Description |
---|---|
void clearStatement() | Clears the statement. |
void execute(MbMessageAssembly) | Executes the SQL expression associated with this object. |
String getDataSourceName() | Gets the ODBC data source name associated with this SQL statement object. |
int getSQLCode() | Returns the SQLCode state variable associated with the last select or execute call. |
String getSQLErrorText() | Returns the SQLErrorText state variable associated with the last select or execute call. |
int getSQLNativeError() | Returns the SQLNativeError state variable associated with the last select or execute call. |
String getSQLState() | Returns the SQLState state variable associated with the last select or execute call. |
String getStatement() | Gets the SQL expression associated with this SQL statement object. |
boolean getThrowExceptionOnDatabaseError() | |
int getTransactionType() | Gets the the transaction type of this SQL statement object. |
boolean getTreatWarningsAsErrors() | |
void select(MbMessageAssembly, MbMessageAssembly) | Executes the SQL expression associated with this object. |
void setThrowExceptionOnDatabaseError(boolean) | Sets the value of the 'throwExceptionOnDatabaseError' attribute. |
void setTreatWarningsAsErrors(boolean) | Sets the value of the 'treatWarningAsErrors' attribute. |
String toString() |
public static final int SQL_TRANSACTION_AUTOTransaction specifier. Specifies that a database commit or rollback will be performed at the completion of the message flow.
public static final int SQL_TRANSACTION_COMMITTransaction specifier. Specifies that a commit will be performed after execution of the statement.
public void clearStatement() throws MbExceptionClears the statement. The removes all of the resources associated with this SQL statement object. It should be called when the object is no longer required, prior to garbage collection. Any attempt to use this object after it has been cleared will result in a NullPointerException being thrown.
- Throws
public void execute(MbMessageAssembly assembly) throws MbExceptionExecutes the SQL expression associated with this object. This method is to be used when the expression does not return data, for example when a PASSTHRU function is used.
If a database error occurs during the execution of this statement, the behaviour is determined by the the value of the 'throwExceptionOnDatabaseError' attribute. If set to true (the default value), an exception gets thrown which can be caught if run within a try/catch block. If set to true using setThrowExceptionOnDatabaseError then no exception is thrown, but the SQLCode must be examined determine status. The four database state values are available using getSQLCode, getSQLState, getSQLNativeError, and getSQLErrorText
- Parameters
- assembly - The MbMessageAssembly in which any data referenced by the SQL expression is held.
- Throws
public String getDataSourceName() throws MbExceptionGets the ODBC data source name associated with this SQL statement object.
- Returns
- The data source name
public int getSQLCode()Returns the SQLCode state variable associated with the last select or execute call. This method only returns a valid value if 'throwExceptionOnDatabaseError' was set to false prior to the select() or execute() call. This must be called to determine the success/failure status of the database call.
- Returns
- The SQLCode integer value.
public String getSQLErrorText()Returns the SQLErrorText state variable associated with the last select or execute call. This method only returns a valid value if 'throwExceptionOnDatabaseError' was set to false prior to the select() or execute() call.
- Returns
- The SQLErrorText string.
public int getSQLNativeError()Returns the SQLNativeError state variable associated with the last select or execute call. This method only returns a valid value if 'throwExceptionOnDatabaseError' was set to false prior to the select() or execute() call.
- Returns
- The SQLNativeError integer value.
public String getSQLState()Returns the SQLState state variable associated with the last select or execute call. This method only returns a valid value if 'throwExceptionOnDatabaseError' was set to false prior to the select() or execute() call.
- Returns
- The SQLState string.
public String getStatement() throws MbExceptionGets the SQL expression associated with this SQL statement object.
- Returns
- The SQL expression as a String.
public boolean getThrowExceptionOnDatabaseError()
- Returns
- The current value of the 'throwExceptionOnDatabaseError' attribute.
public int getTransactionType() throws MbExceptionGets the the transaction type of this SQL statement object. Will be one of:
The former value specifies that a database commit will be performed at the completion of the message flow (that is, as a fully globally coordinated or partially globally coordinated transaction). The latter value specifies that a commit will be performed after execution of the statement, and within the execute or select method (that is, the message flow is partially broker coordinated).
- SQL_TRANSACTION_AUTO
- SQL_TRANSACTION_COMMIT
- Returns
- The transaction type
public boolean getTreatWarningsAsErrors()
- Returns
- The current value of the 'treatWarningAsErrors' attribute.
public void select(MbMessageAssembly assemblyIn, MbMessageAssembly assemblyOut) throws MbExceptionExecutes the SQL expression associated with this object. If the expression returns data, then it is written into the message assembly object specified by the assemblyOut parameter.
If a database error occurs during the execution of this statement, the behaviour is determined by the the value of the 'throwExceptionOnDatabaseError' attribute. If set to true (the default value), an exception gets thrown which can be caught if run within a try/catch block. If set to true using setThrowExceptionOnDatabaseError then no exception is thrown, but the SQLCode must be examined determine status. The four database state values are available using getSQLCode, getSQLState, getSQLNativeError, and getSQLErrorText
- Parameters
- assemblyIn - The MbMessageAssembly in which any data referenced by the SQL expression is held.
- assemblyOut - The MbMessageAssembly into which any data returned by the SQL expression will be written.
- Throws
public void setThrowExceptionOnDatabaseError(boolean throwExceptions)Sets the value of the 'throwExceptionOnDatabaseError' attribute. This attribute determines the behaviour of the broker when it encounters a database error. If set to true, an exception is thrown which can be caught and handled by the plugin if desired. If set to false, no exception is thrown, but the SQLCode must be examined determine status. The four database state values are available using getSQLCode, getSQLState, getSQLNativeError, and getSQLErrorText.
By default, this attribute is set to 'true'.
- Parameters
- throwExceptions - The boolean value to be set.
public void setTreatWarningsAsErrors(boolean warningsAsErrors)Sets the value of the 'treatWarningAsErrors' attribute. If set to true, warnings generated by the database call will be handled in the same manner as errors. By default, this attribute is set to 'false'.
- Parameters
- warningsAsErrors - The boolean value to be set.
public String toString()