Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1.7.0)

E13403-08

oracle.javatools.db.execute
Class QueryWrapper

java.lang.Object
  extended by oracle.javatools.db.execute.StatementWrapper
      extended by oracle.javatools.db.execute.QueryWrapper
Direct Known Subclasses:
PlSqlWrapper

public class QueryWrapper
extends StatementWrapper

Execution wrapper for querying the database and processing ResultSets (rather than just executing statements). On top of what StatementWrapper provides, it provides:

  1. Automatic parameter support, use ? and the parameters will be substituted using a PreparedStatement, use {0}, {1} etc and java.text.MessageFormat will be used instead.
  2. DBObjects as parameters - convert them to strings properly
  3. Auto-closing of the java.sql.ResultSet and java.sql.Statement after execution.
Example Usage:
 final Database db = ...;
 final Schema schema = ...;
 final String query = ...;
 final QueryWrapper wrap = new QueryWrapper( db, query, new Object[]{ schema });
 final QueryWrapper.QueryRunnable r = new QueryWrapper.QueryRunnable()
 {
   public void processResultSet( ResultSet rs ) throws DBException
   {
     try
     {
       while( rs.next() )
       {
         // process the result set as appropriate
       }
     }
     catch( SQLException sqe )
     {
       wrap.throwDBException( sqe );
     }
   }
 };

 try
 {
   wrap.executeQuery( r );
 }
 catch( DBException dbe )
 {
   // process exception
 }
 

Since:
11.0

Nested Class Summary
static interface QueryWrapper.QueryRunnable
          A QueryRunnable implementation is passed to the QueryWrapper on execute of the query to process the resulting ResultSet.
 
Nested classes/interfaces inherited from class oracle.javatools.db.execute.StatementWrapper
StatementWrapper.ExecutionProxy, StatementWrapper.ExecutionRunnable<T>, StatementWrapper.Listener
 
Constructor Summary
QueryWrapper(Database db, java.lang.String query)
          Creates a wrapper for a query with no parameters.
QueryWrapper(Database db, java.lang.String query, java.util.List params)
          Creates a wrapper for the given query text.
QueryWrapper(Database db, java.lang.String query, java.lang.Object... params)
          Creates a wrapper for the given query text.
QueryWrapper(java.lang.String name, java.sql.Connection conn, java.lang.String query, java.lang.Object... params)
          Only use in the absence of a Database (e.g.
 
Method Summary
 void close()
          Closes the statement and result set if we got that far.
protected  java.sql.Statement createStatment()
          Creates the Statement object to use.
 boolean execute()
          Executes the statment against the database.
 void executeQuery(int fetchSize, QueryWrapper.QueryRunnable run)
          Execute the query against the builder's connection using the appropriate parameter substitutions.
 void executeQuery(QueryWrapper.QueryRunnable run)
          Execute the query against the builder's connection using the appropriate parameter substitutions.
protected  java.util.List<java.lang.Object> getParameters()
          Returns the parameters to substitute on the statement.
protected  java.lang.String getQuery()
          Returns the query string.
protected  java.lang.String getStatementTextForLog()
          For logging and exception handling.
static QueryWrapper union(QueryWrapper... wrappers)
          Unions together the queries in the given wrapper and bundles up all the parameters (if any) to be used with the various queries.
 
Methods inherited from class oracle.javatools.db.execute.StatementWrapper
addListener, doExecute, executeImpl, getConnection, getConnectionName, getDatabase, getExecutionProxy, getIdentifier, getStatement, getStatementStrings, hasCancelled, isIgnoreErrors, queryFinished, queryStarted, removeListener, setBypassExecutionProxy, setDatabase, setExecuting, setExecutionProxy, setIdentifier, setIgnoreErrors, setStatement, sqlTrace, throwDBException, throwDBException, throwDBException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryWrapper

public QueryWrapper(Database db,
                    java.lang.String query)
Creates a wrapper for a query with no parameters.

See Also:
#QueryWrapper(java.sql.Connection, java.lang.String, java.lang.Object...)

QueryWrapper

public QueryWrapper(Database db,
                    java.lang.String query,
                    java.lang.Object... params)
Creates a wrapper for the given query text. The params are set on the statement as strings. If any of the params is a DBObject its correct string representation is used.


QueryWrapper

public QueryWrapper(java.lang.String name,
                    java.sql.Connection conn,
                    java.lang.String query,
                    java.lang.Object... params)
Only use in the absence of a Database (e.g. in unit tests).


QueryWrapper

public QueryWrapper(Database db,
                    java.lang.String query,
                    java.util.List params)
Creates a wrapper for the given query text. The params are set on the statement as strings. If any of the params is a DBObject its correct string representation is used.

Method Detail

createStatment

protected java.sql.Statement createStatment()
                                     throws java.sql.SQLException
Description copied from class: StatementWrapper
Creates the Statement object to use. By default this creates a simple Statement, subclasses can override to create a Prepared or Callable statement.

Overrides:
createStatment in class StatementWrapper
Throws:
java.sql.SQLException

getStatementTextForLog

protected java.lang.String getStatementTextForLog()
Description copied from class: StatementWrapper
For logging and exception handling. Returns the statement text by default, sublcasses should override as appropriate.

Overrides:
getStatementTextForLog in class StatementWrapper

getQuery

protected java.lang.String getQuery()
Returns the query string. If using message format for the parameters this will substitute the parameters into the query.

See Also:
setMessageFormat()

getParameters

protected java.util.List<java.lang.Object> getParameters()
Returns the parameters to substitute on the statement. If using message format for the parameters this will return an empty list.

See Also:
getQuery()

execute

public final boolean execute()
                      throws DBException
Description copied from class: StatementWrapper
Executes the statment against the database. Returns true if the execution succeeded (i.e. java.sql.Statement.execute() returned true). If multiple statements are included, true is returned if any succeed. If an error occurs (SQLException) execution halts at that statement.

Overrides:
execute in class StatementWrapper
Throws:
DBException
See Also:
Statement.execute(java.lang.String)

executeQuery

public void executeQuery(int fetchSize,
                         QueryWrapper.QueryRunnable run)
                  throws DBException
Execute the query against the builder's connection using the appropriate parameter substitutions.

Parameters:
fetchSize - Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed. If the value specified is zero, then the hint is ignored.
Throws:
DBException

executeQuery

public void executeQuery(QueryWrapper.QueryRunnable run)
                  throws DBException
Execute the query against the builder's connection using the appropriate parameter substitutions.

Throws:
DBException

close

public void close()
Closes the statement and result set if we got that far. Call in a finally{ ... } block.

Overrides:
close in class StatementWrapper

union

public static QueryWrapper union(QueryWrapper... wrappers)
Unions together the queries in the given wrapper and bundles up all the parameters (if any) to be used with the various queries.

No checking is done on the number of columns - the database will complain if they don't match.


Oracle Fusion Middleware Java API Reference for Oracle Extension SDK Reference
11g Release 1 (11.1.1.7.0)

E13403-08

Copyright © 1997, 2013, Oracle. All rights reserved.