Skip to content

Commit

Permalink
Add test case illustrating failure to batch-invoke a PostgreSQL function
Browse files Browse the repository at this point in the history
  • Loading branch information
evnm committed Aug 27, 2016
1 parent 07ebb80 commit 407b377
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
package org.skife.jdbi.v2;

import org.junit.*;
import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.skife.jdbi.v2.util.IntegerColumnMapper;

import java.sql.BatchUpdateException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
Expand All @@ -41,11 +43,14 @@ public static void isPostgresInstalled() {
public void setUp() throws Exception {
h = new DBI("jdbc:postgresql:jdbi_test", "postgres", "").open();
h.execute("create table something (id serial, name varchar(50), create_time timestamp default now())");
h.execute("create function insert_func(varchar(50)) returns void as "
+ "'insert into something (name) values ($1);' LANGUAGE SQL");
}

@After
public void tearDown() throws Exception {
h.execute("drop table something");
h.execute("drop function insert_func(varchar)");
h.close();
}

Expand Down Expand Up @@ -84,6 +89,24 @@ public IdCreateTime map(int index, ResultSet r, StatementContext ctx) throws SQL
assertNotNull(ids.get(1).createTime);
}

@Test
public void testBatchVoidFunctionInvocation() throws SQLException {
PreparedBatch batch = h.prepareBatch("select insert_func(?)");
batch.add("Brian");
batch.add("Thom");

try {
batch.execute();
} catch (UnableToExecuteStatementException err) {
throw ((BatchUpdateException) err.getCause()).getNextException();
}

List<Something> somethings = h.createQuery("select id, name from something")
.map(Something.class)
.list();
assertEquals(Arrays.asList(new Something(1, "Brian"), new Something(2, "Thom")), somethings);
}

private static class IdCreateTime {

final Integer id;
Expand Down

0 comments on commit 407b377

Please sign in to comment.