Skip to content

Commit

Permalink
Use function reference
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed May 23, 2024
1 parent 9fd3137 commit 8d4e106
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @see StandardReader
*/
public interface ValueReader<V, T> {
T parse(V value);
Function<@NotNull V,T> parser();

ThrowingBiFunction<Row, String, V, SQLException> namedReader();

Expand All @@ -32,8 +32,8 @@ static <V, T> ValueReader<V, T> create(Function<V, T> parser,
ThrowingBiFunction<Row, Integer, V, SQLException> indexReader) {
return new ValueReader<>() {
@Override
public T parse(@NotNull V value) {
return parser.apply(value);
public Function<@NotNull V,T> parser() {
return parser;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
public <V, T> T get(String columnLabel, ValueReader<V, T> reader) throws SQLException {
V value = reader.namedReader().apply(this, columnAlias(columnLabel));
if (value == null) return null;
return reader.parse(value);
return reader.parser().apply(value);
}

/**
Expand All @@ -2089,7 +2089,7 @@ public <V, T> T get(String columnLabel, ValueReader<V, T> reader) throws SQLExce
*/
public <V, T> T get(int index, ValueReader<V, T> reader) throws SQLException {
V value = reader.indexedReader().apply(this, index);
return reader.parse(value);
return reader.parser().apply(value);
}

private String columnAlias(String columnLabel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import de.chojo.sadu.PostgresDatabase;
import de.chojo.sadu.mapper.RowMapperRegistry;
import de.chojo.sadu.mapper.reader.StandardReader;
import de.chojo.sadu.mapper.rowmapper.RowMapper;
import de.chojo.sadu.postgresql.mapper.PostgresqlMapper;
import de.chojo.sadu.queries.api.call.Call;
Expand All @@ -24,9 +25,12 @@
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.UUID;

import static de.chojo.sadu.PostgresDatabase.createContainer;
import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_DATE_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_BYTES;

public class LocalDateTimeTest {
private QueryConfiguration query;
Expand Down Expand Up @@ -69,6 +73,25 @@ public void withTimezone() {
Assertions.assertEquals(now.truncatedTo(ChronoUnit.SECONDS), res.truncatedTo(ChronoUnit.SECONDS));
}

public void test(){
Optional<UUID> first = query.query("SELECT uuid FROM users")
.single()
.map(row -> row.get("uuid", UUID_FROM_BYTES))
.first();

Optional<UUID> first = query.query("SELECT uuid FROM users")
.single()
.map(row -> row.getUuidFromBytes("uuid"))
.first();

Optional<Status> first = query.query("SELECT status FROM users")
.single()
.map(row -> row.get("status", StandardReader.forEnum(Status.class)))
.first();


}

@AfterEach
void afterAll() {
db.close();
Expand Down

0 comments on commit 8d4e106

Please sign in to comment.