Created
August 28, 2017 14:23
-
-
Save electrum/5b08813442be8b2d348293ce0ddd2423 to your computer and use it in GitHub Desktop.
Jdbi Batchable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jdbi.v3.sqlobject; | |
/** | |
* A mixin interface to expose batching methods on the SQL object. | |
*/ | |
public interface Batchable | |
{ | |
/** | |
* Begin a SQL batch that will send multiple statements to the database | |
* at once. After this is called, a single | |
* {@link org.jdbi.v3.sqlobject.statement.SqlUpdate @SqlUpdate} | |
* method may be invoked one or more times to add statements to the batch. | |
* The batch should then be completed with {@link #endBatch()}. | |
*/ | |
void beginBatch(); | |
/** | |
* End a batch that was started with {@link #beginBatch()}. | |
*/ | |
void endBatch(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jdbi.v3.sqlobject; | |
import org.jdbi.v3.core.Handle; | |
import org.jdbi.v3.core.mapper.SomethingMapper; | |
import org.jdbi.v3.core.rule.H2DatabaseRule; | |
import org.jdbi.v3.sqlobject.config.RegisterRowMapper; | |
import org.jdbi.v3.sqlobject.customizer.Bind; | |
import org.jdbi.v3.sqlobject.statement.SqlQuery; | |
import org.jdbi.v3.sqlobject.statement.SqlUpdate; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class TestImplicitBatching | |
{ | |
@Rule | |
public H2DatabaseRule dbRule = new H2DatabaseRule().withPlugin(new SqlObjectPlugin()); | |
private Handle handle; | |
@Before | |
public void setUp() throws Exception | |
{ | |
handle = dbRule.getSharedHandle(); | |
} | |
@Test | |
public void testInsert() throws Exception | |
{ | |
UsesBatching b = handle.attach(UsesBatching.class); | |
b.beginBatch(); | |
b.insert(1, "Brian"); | |
b.insert(2, "Dain"); | |
b.endBatch(); | |
assertThat(b.size()).isEqualTo(2); | |
} | |
@RegisterRowMapper(SomethingMapper.class) | |
public interface UsesBatching extends Batchable | |
{ | |
@SqlUpdate("insert into something (id, name) values (:id, :name)") | |
void insert(@Bind("id") int id, @Bind("name") String name); | |
@SqlQuery("select count(*) from something") | |
int size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment