This post originated from an RSS feed registered with Java Buzz
by Brian McCallister.
Original Post: Oracle's DML Returning and jDBI 2.0pre7
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
So I broke down and have started adding some database-specific functionality to jDBI. The first bit is a statement customizer which does the downcasts and whatnot to make it convenient to use oracle's dml returning stuff (like "insert into something (id, name) values (something_id_seq.nextval, ?) returning id into ?").
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.get(0));
h.close();
}
This allows for a nice API, without client code downcasts as well, to make use of results returned from DML against Oracle. When I get a chance I'll add one for Postgres as well :-) The code to do it is pretty straightforward. The nice part was that it required no changes to the core of jDBI 2.0 to do this :-)