Search in sources :

Example 1 with User

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[]
}
Also used : User(org.jdbi.v3.core.mapper.JoinRowMapperTest.User) Article(org.jdbi.v3.core.mapper.JoinRowMapperTest.Article) Handle(org.jdbi.v3.core.Handle) JoinRowMapperTest(org.jdbi.v3.core.mapper.JoinRowMapperTest) Test(org.junit.Test)

Example 2 with User

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);
}
Also used : PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Test(org.junit.Test)

Example 3 with User

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[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 4 with User

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[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Test(org.junit.Test)

Example 5 with User

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"));
}
Also used : Multimap(io.vavr.collection.Multimap) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 Handle (org.jdbi.v3.core.Handle)3 Jdbi (org.jdbi.v3.core.Jdbi)2 HashMap (io.vavr.collection.HashMap)1 Map (io.vavr.collection.Map)1 Multimap (io.vavr.collection.Multimap)1 JoinRowMapperTest (org.jdbi.v3.core.mapper.JoinRowMapperTest)1 Article (org.jdbi.v3.core.mapper.JoinRowMapperTest.Article)1 User (org.jdbi.v3.core.mapper.JoinRowMapperTest.User)1 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)1 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)1