Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for Multimap and generics #2636

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-guava</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
Expand Down Expand Up @@ -135,6 +141,12 @@
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-guava</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
120 changes: 120 additions & 0 deletions e2e/src/test/java/org/jdbi/v3/e2e/TestIssue2623.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.e2e;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import org.jdbi.v3.core.generic.GenericType;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.guava.GuavaPlugin;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.jdbi.v3.sqlobject.config.KeyColumn;
import org.jdbi.v3.sqlobject.config.RegisterConstructorMapper;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.testing.junit5.JdbiExtension;
import org.jdbi.v3.testing.junit5.internal.TestingInitializers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.guava.api.Assertions.assertThat;

public class TestIssue2623 {

@RegisterExtension
public JdbiExtension h2Extension = JdbiExtension.h2()
.withInitializer(TestingInitializers.usersWithData())
.withPlugins(new SqlObjectPlugin(), new GuavaPlugin());

@BeforeEach
public void setUp() {
h2Extension.getJdbi().registerRowMapper(new GenericType<>() {}, new UserIntegerMapper());
}

@Test
public void testParameterizeTypedMapper() {
var jdbi = h2Extension.getJdbi();

Multimap<Integer, User<Integer>> result = jdbi.withExtension(Dao.class, dao -> dao.getUsers(1));
assertThat(result).isNotNull().containsAllEntriesOf(ImmutableMultimap.of(1, new User<>("Alice", 1)));
}

@Test
public void testConcreteTypedMapper() {
var jdbi = h2Extension.getJdbi();

Multimap<Integer, IntUser> result = jdbi.withExtension(Dao.class, dao -> dao.getIntUsers(1));
assertThat(result).isNotNull().containsAllEntriesOf(ImmutableMultimap.of(1, new IntUser("Alice", 1)));
}

public interface Dao {
@SqlQuery("select name, id from users where id = :id")
@KeyColumn("id")
Multimap<Integer, User<Integer>> getUsers(int id);

@SqlQuery("select name, id from users where id = :id")
@KeyColumn("id")
@RegisterConstructorMapper(IntUser.class)
Multimap<Integer, IntUser> getIntUsers(int id);
}

public record User<T>(String name, T id) {}

public static class UserIntegerMapper implements RowMapper<User<Integer>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might really neatly be replaced with a ConstructorMapper, unless showing this mapper is part of the point of the example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showing the mapper is the point of the example.

@Override
public User<Integer> map(ResultSet rs, StatementContext ctx) throws SQLException {
return new User<>(rs.getString("name"), rs.getInt("id"));
}
}

public static class IntUser {
private final String name;
private final Integer id;

public IntUser(String name, Integer id) {
this.name = name;
this.id = id;
}

public String getName() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a record too to save some boilerplate hashCode / equals?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, same goal. Show the actual implementation. Correct, it could be a record, too.

return name;
}

public Integer getId() {
return id;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IntUser intUser = (IntUser) o;
return Objects.equals(name, intUser.name) && Objects.equals(id, intUser.id);
}

@Override
public int hashCode() {
return Objects.hash(name, id);
}
}
}