use of org.jdbi.v3.core.mapper.JoinRowMapperTest.User in project jdbi by jdbi.
the class TestRegisterJoinRowMapper method testSqlObjectJoinRow.
@Test
public void testSqlObjectJoinRow() {
Handle handle = dbRule.getSharedHandle();
// tag::joinrowusage[]
Multimap<User, Article> joined = HashMultimap.create();
handle.attach(UserArticleDao.class).getAuthorship().forEach(jr -> joined.put(jr.get(User.class), jr.get(Article.class)));
assertThat(joined).isEqualTo(JoinRowMapperTest.getExpected());
// end::joinrowusage[]
}
use of org.jdbi.v3.core.mapper.JoinRowMapperTest.User in project jdbi by jdbi.
the class StatementsTest method testBatch.
@Test
public void testBatch() throws Exception {
// tag::batch[]
PreparedBatch batch = handle.prepareBatch("INSERT INTO user(id, name) VALUES(:id, :name)");
for (int i = 100; i < 5000; i++) {
batch.bind("id", i).bind("name", "User:" + i).add();
}
int[] counts = batch.execute();
// end::batch[]
int[] expected = new int[4900];
Arrays.fill(expected, 1);
assertThat(counts).isEqualTo(expected);
}
use of org.jdbi.v3.core.mapper.JoinRowMapperTest.User in project jdbi by jdbi.
the class IntroductionTest method core.
@Test
public void core() {
// tag::core[]
// (H2 in-memory database)
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
List<User> users = jdbi.withHandle(handle -> {
handle.execute("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)");
// Inline positional parameters
handle.execute("INSERT INTO user(id, name) VALUES (?, ?)", 0, "Alice");
// Positional parameters
handle.createUpdate("INSERT INTO user(id, name) VALUES (?, ?)").bind(0, // 0-based parameter indexes
1).bind(1, "Bob").execute();
// Named parameters
handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bind("id", 2).bind("name", "Clarice").execute();
// Named parameters from bean properties
handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bindBean(new User(3, "David")).execute();
// Easy mapping to any type
return handle.createQuery("SELECT * FROM user ORDER BY name").mapToBean(User.class).list();
});
assertThat(users).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::core[]
}
use of org.jdbi.v3.core.mapper.JoinRowMapperTest.User in project jdbi by jdbi.
the class IntroductionTest method sqlObject.
// end::sqlobject-declaration[]
@Test
public void sqlObject() {
// tag::sqlobject-usage[]
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
jdbi.installPlugin(new SqlObjectPlugin());
// Jdbi implements your interface based on annotations
List<User> userNames = jdbi.withExtension(UserDao.class, dao -> {
dao.createTable();
dao.insertPositional(0, "Alice");
dao.insertPositional(1, "Bob");
dao.insertNamed(2, "Clarice");
dao.insertBean(new User(3, "David"));
return dao.listUsers();
});
assertThat(userNames).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::sqlobject-usage[]
}
use of org.jdbi.v3.core.mapper.JoinRowMapperTest.User in project jdbi by jdbi.
the class TestVavrMapCollectorWithDB method testNonUniqueIndex_withMultimap.
@Test
public void testNonUniqueIndex_withMultimap() {
Handle h = dbRule.getSharedHandle();
h.execute("create table user (id int, name varchar)");
h.prepareBatch("insert into user (id, name) values (?, ?)").add(1, "alice").add(2, "bob").add(3, "alice").execute();
Multimap<String, User> usersByName = h.createQuery("select * from user").setMapKeyColumn("name").registerRowMapper(ConstructorMapper.factory(User.class)).collectInto(new GenericType<Multimap<String, User>>() {
});
assertThat(usersByName.apply("alice")).hasSize(2).containsExactly(new User(1, "alice"), new User(3, "alice"));
assertThat(usersByName.apply("bob")).hasSize(1).containsExactly(new User(2, "bob"));
}
Aggregations