Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004 - 2011 Brian McCallister
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.skife.jdbi.v2.unstable.oracle;
 
 
 import java.util.List;

Provides access to Oracle's "DML Returning" features introduced in 10.2. To use, add the statement customizer to a DML statement and bind (positionally) the return params. Sadly, I think they (and the mapper) have to be positional. Usage is like this:

 public void testFoo() throws Exception
 {
     Handle h = dbi.open();
 

OracleReturning<Integer> or = new OracleReturning<Integer>(new ResultSetMapper<Integer>() { public Integer map(int index, ResultSet r) throws SQLException { return r.getInt(1); } });

or.registerReturnParam(1, OracleTypes.INTEGER);

h.createStatement("insert into something (id, name) values (17, 'Brian') returning id into ?") .addStatementCustomizer(or) .execute(); List<Integer> ids = or.getReturnedResults();

assertEquals(1, ids.size()); assertEquals(Integer.valueOf(17), ids.getAttribute(0)); h.close(); }

Though you can bind multiple params, and whatnot
 
 public class OracleReturning<ResultType> extends BaseStatementCustomizer implements StatementCustomizer
 {
     private ResultSetMapper<ResultType> mapper;
     private final List<int[]> binds = new ArrayList<int[]>();
     private StatementContext context;
     private List<ResultType> results;
 	private Class<?> oraclePS ;
 	private Object stmt ;

    
Provide a mapper which knows how to do positional access, sadly the org.skife.jdbi.v2.BeanMapper uses the names in the result set

Parameters:
mapper Must use only positional access to the result set
 
     public OracleReturning(ResultSetMapper<ResultType> mapper)
     {
         this. = mapper;
 	    try {
 		    this. = Class.forName("oracle.jdbc.OraclePreparedStatement");
 		    this. = .getMethod("registerReturnParameter"new Class[]{int.classint.class});
 		    this. = .getMethod("getReturnResultSet");
 	    }
 	    catch (Exception e) {
 		    throw new RuntimeException(e);
 	    }
     }

    
 
     @Override
     public void beforeExecution(PreparedStatement stmtStatementContext ctxthrows SQLException
     {
        this. = ctx;
        if (!.isAssignableFrom(stmt.getClass())) {
            try {
                final Method get_delegate = stmt.getClass().getMethod("getDelegate");
                final Object candidate = get_delegate.invoke(stmt);
                if (!.isAssignableFrom(candidate.getClass())) {
                    throw new Exception("Obtained delegate, but it still wasn't an OraclePreparedStatement");
                }
	            else {
	                this. = candidate ;
                }
            }
            catch (Exception e) {
                throw new IllegalStateException("Statement is not an OraclePreparedStatement, nor" +
                                                "one which we know how to find it from"e);
            }
        }
	    else {
	        this. = stmt ;
        }
        for (int[] bind : ) {
	        try {
		        .invoke(this.bind[0], bind[1]);
	        }
	        catch (Exception e) {
		        throw new RuntimeException(e);
	        }
        }
    }
    @Override
    public void afterExecution(PreparedStatement stmtStatementContext ctxthrows SQLException
    {
        ResultSet rs;
		try {
		}
		catch (Exception e) {
			throw new ResultSetException("Unable to retrieve return result set"ectx);
		}
        this. = new ArrayList<ResultType>();
        try {
            int i = 0;
            while (rs.next()) {
                .add(.map(i++, rs));
            }
        }
        catch (SQLException e) {
            throw new ResultSetException("Unable to retrieve results from returned result set"ectx);
        }
    }


    
Callable after the statement has been executed to obtain the mapped results.

Returns:
the mapped results
    public List<ResultType> getReturnedResults()
    {
        return ;
    }

    
Used to specify the types (required) of the out parameters. You must register each of them.

Parameters:
position 1 based position of the out parameter
oracleType one of the values from oracle.jdbc.driver..OracleTypes
Returns:
The same instance, in case you want to chain things
    public OracleReturning<ResultType> registerReturnParam(int positionint oracleType)
    {
        .add(new int[]{positionoracleType});
        return this;
    }
New to GrepCode? Check out our FAQ X