Skip to content

Commit

Permalink
Replace internal calls with reader implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed May 23, 2024
1 parent 8573a12 commit fe59e66
Showing 1 changed file with 45 additions and 69 deletions.
114 changes: 45 additions & 69 deletions sadu-mapper/src/main/java/de/chojo/sadu/mapper/wrapper/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package de.chojo.sadu.mapper.wrapper;

import de.chojo.sadu.core.conversion.ArrayConverter;
import de.chojo.sadu.core.conversion.UUIDConverter;
import de.chojo.sadu.mapper.MapperConfig;
import de.chojo.sadu.mapper.reader.StandardReader;
import de.chojo.sadu.mapper.reader.ValueReader;
Expand Down Expand Up @@ -36,13 +35,27 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_DATE;
import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_DATE_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.LOCAL_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.OFFSET_DATE_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.OFFSET_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_BYTES;
import static de.chojo.sadu.mapper.reader.StandardReader.UUID_FROM_STRING;
import static de.chojo.sadu.mapper.reader.StandardReader.ZONED_DATE_TIME;
import static de.chojo.sadu.mapper.reader.StandardReader.localDate;
import static de.chojo.sadu.mapper.reader.StandardReader.localDateTime;
import static de.chojo.sadu.mapper.reader.StandardReader.localTime;
import static de.chojo.sadu.mapper.reader.StandardReader.offsetDateTime;
import static de.chojo.sadu.mapper.reader.StandardReader.offsetTime;
import static de.chojo.sadu.mapper.reader.StandardReader.zonedDateTime;

/**
* Represents the row of a result set. Made to restrict actions on valid calls.
*/
Expand Down Expand Up @@ -132,11 +145,7 @@ public <T extends Enum<T>> T getEnum(int columnIndex, Class<T> clazz) throws SQL
*/
@Deprecated
public UUID getUuidFromString(int columnIndex) throws SQLException {
var value = resultSet.getString(columnIndex);
if (value == null) {
return null;
}
return UUID.fromString(value);
return get(columnIndex, StandardReader.UUID_FROM_STRING);
}

/**
Expand Down Expand Up @@ -290,7 +299,7 @@ public byte[] getBytes(int columnIndex) throws SQLException {
*/
@Deprecated
public UUID getUuidFromBytes(int columnIndex) throws SQLException {
return UUIDConverter.convert(resultSet.getBytes(columnIndex));
return get(columnIndex, UUID_FROM_BYTES);
}

/**
Expand Down Expand Up @@ -324,8 +333,7 @@ public Date getDate(int columnIndex) throws SQLException {
*/
@Deprecated
public LocalDate getLocalDate(int columnIndex) throws SQLException {
var date = getDate(columnIndex);
return date == null ? null : date.toLocalDate();
return get(columnIndex, LOCAL_DATE);
}

/**
Expand All @@ -339,9 +347,7 @@ public LocalDate getLocalDate(int columnIndex) throws SQLException {
* @throws SQLException if the columnIndex is not valid;
* if a database access error occurs or this method is
* called on a closed result set
* @deprecated Use {@link #get(String, ValueReader)} with one of the {@link StandardReader}
*/
@Deprecated
public Time getTime(int columnIndex) throws SQLException {
return resultSet.getTime(columnIndex);
}
Expand All @@ -361,8 +367,7 @@ public Time getTime(int columnIndex) throws SQLException {
*/
@Deprecated
public LocalTime getLocalTime(int columnIndex) throws SQLException {
var time = getTime(columnIndex);
return time != null ? time.toLocalTime() : null;
return get(columnIndex, LOCAL_TIME);
}

/**
Expand Down Expand Up @@ -396,8 +401,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException {
*/
@Deprecated
public LocalDateTime getLocalDateTime(int columnIndex) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : timestamp.toLocalDateTime();
return get(columnIndex, LOCAL_DATE_TIME);
}

/**
Expand All @@ -415,8 +419,7 @@ public LocalDateTime getLocalDateTime(int columnIndex) throws SQLException {
*/
@Deprecated
public ZonedDateTime getZonedDateTime(int columnIndex) throws SQLException {
var timestamp = getTimestamp(columnIndex);
return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault());
return get(columnIndex, ZONED_DATE_TIME);
}

/**
Expand All @@ -434,8 +437,7 @@ public ZonedDateTime getZonedDateTime(int columnIndex) throws SQLException {
*/
@Deprecated
public OffsetDateTime getOffsetDateTime(int columnIndex) throws SQLException {
LocalDateTime localDateTime = getLocalDateTime(columnIndex);
return localDateTime == null ? null : OffsetDateTime.from(localDateTime);
return get(columnIndex, OFFSET_DATE_TIME);
}

/**
Expand All @@ -453,8 +455,7 @@ public OffsetDateTime getOffsetDateTime(int columnIndex) throws SQLException {
*/
@Deprecated
public OffsetTime getOffsetTime(int columnIndex) throws SQLException {
LocalTime localDateTime = getLocalTime(columnIndex);
return localDateTime == null ? null : OffsetTime.from(localDateTime);
return get(columnIndex, OFFSET_TIME);
}

/**
Expand Down Expand Up @@ -547,11 +548,7 @@ public String getString(String columnLabel) throws SQLException {
* @since 1.2
*/
public <T extends Enum<T>> T getEnum(String columnLabel, Class<T> clazz) throws SQLException {
var value = getString(columnLabel);
if (value == null) {
return null;
}
return Enum.valueOf(clazz, value);
return get(columnLabel, StandardReader.forEnum(clazz));
}

/**
Expand All @@ -569,11 +566,7 @@ public <T extends Enum<T>> T getEnum(String columnLabel, Class<T> clazz) throws
*/
@Deprecated
public UUID getUuidFromString(String columnLabel) throws SQLException {
var value = resultSet.getString(columnAlias(columnLabel));
if (value == null) {
return null;
}
return UUID.fromString(value);
return get(columnLabel, UUID_FROM_STRING);
}

/**
Expand Down Expand Up @@ -727,7 +720,7 @@ public byte[] getBytes(String columnLabel) throws SQLException {
*/
@Deprecated
public UUID getUuidFromBytes(String columnLabel) throws SQLException {
return UUIDConverter.convert(resultSet.getBytes(columnAlias(columnLabel)));
return get(columnLabel, UUID_FROM_BYTES);
}

/**
Expand Down Expand Up @@ -794,8 +787,7 @@ public Date getDate(String columnLabel) throws SQLException {
*/
@Deprecated
public LocalDate getLocalDate(String columnLabel) throws SQLException {
var date = getDate(columnLabel);
return date == null ? null : date.toLocalDate();
return get(columnLabel, LOCAL_DATE);
}

/**
Expand Down Expand Up @@ -831,8 +823,7 @@ public Time getTime(String columnLabel) throws SQLException {
*/
@Deprecated
public LocalTime getLocalTime(String columnLabel) throws SQLException {
var time = getTime(columnLabel);
return time != null ? time.toLocalTime() : null;
return get(columnLabel, LOCAL_TIME);
}

/**
Expand Down Expand Up @@ -866,8 +857,7 @@ public Timestamp getTimestamp(String columnLabel) throws SQLException {
*/
@Deprecated
public LocalDateTime getLocalDateTime(String columnLabel) throws SQLException {
Timestamp timestamp = getTimestamp(columnLabel);
return timestamp == null ? null : timestamp.toLocalDateTime();
return get(columnLabel, LOCAL_DATE_TIME);
}

/**
Expand All @@ -885,8 +875,7 @@ public LocalDateTime getLocalDateTime(String columnLabel) throws SQLException {
*/
@Deprecated
public ZonedDateTime getZonedDateTime(String columnLabel) throws SQLException {
var timestamp = getTimestamp(columnLabel);
return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault());
return get(columnLabel, ZONED_DATE_TIME);
}

/**
Expand All @@ -904,8 +893,7 @@ public ZonedDateTime getZonedDateTime(String columnLabel) throws SQLException {
*/
@Deprecated
public OffsetDateTime getOffsetDateTime(String columnLabel) throws SQLException {
var localDateTime = getTimestamp(columnLabel);
return localDateTime == null ? null : OffsetDateTime.from(localDateTime.toInstant());
return get(columnLabel, OFFSET_DATE_TIME);
}

/**
Expand All @@ -923,8 +911,7 @@ public OffsetDateTime getOffsetDateTime(String columnLabel) throws SQLException
*/
@Deprecated
public OffsetTime getOffsetTime(String columnLabel) throws SQLException {
LocalTime localDateTime = getLocalTime(columnLabel);
return localDateTime == null ? null : OffsetTime.from(localDateTime);
return get(columnLabel, OFFSET_TIME);
}

/**
Expand Down Expand Up @@ -1372,8 +1359,7 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
*/
@Deprecated
public LocalDate getLocalDate(int columnIndex, Calendar cal) throws SQLException {
var date = getDate(columnIndex, cal);
return date == null ? null : date.toLocalDate();
return get(columnIndex, localDate(cal));
}

/**
Expand Down Expand Up @@ -1419,8 +1405,7 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
*/
@Deprecated
public LocalDate getLocalDate(String columnLabel, Calendar cal) throws SQLException {
var date = getDate(columnLabel, cal);
return date == null ? null : date.toLocalDate();
return get(columnLabel, localDate(cal));
}

/**
Expand Down Expand Up @@ -1466,8 +1451,7 @@ public Time getTime(int columnIndex, Calendar cal) throws SQLException {
*/
@Deprecated
public LocalTime getLocalTime(int columnIndex, Calendar cal) throws SQLException {
var time = getTime(columnIndex, cal);
return time != null ? time.toLocalTime() : null;
return get(columnIndex, localTime(cal));
}


Expand Down Expand Up @@ -1514,8 +1498,7 @@ public Time getTime(String columnLabel, Calendar cal) throws SQLException {
*/
@Deprecated
public LocalTime getLocalTime(String columnLabel, Calendar cal) throws SQLException {
var time = getTime(columnLabel, cal);
return time != null ? time.toLocalTime() : null;
return get(columnLabel, localTime(cal));
}

/**
Expand Down Expand Up @@ -1584,8 +1567,7 @@ public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLExcept
*/
@Deprecated
public LocalDateTime getLocalDateTime(int columnIndex, Calendar cal) throws SQLException {
Timestamp timestamp = getTimestamp(columnIndex, cal);
return timestamp == null ? null : timestamp.toLocalDateTime();
return get(columnIndex, localDateTime(cal));
}


Expand All @@ -1610,8 +1592,7 @@ public LocalDateTime getLocalDateTime(int columnIndex, Calendar cal) throws SQLE
*/
@Deprecated
public LocalDateTime getLocalDateTime(String columnLabel, Calendar cal) throws SQLException {
Timestamp timestamp = getTimestamp(columnLabel, cal);
return timestamp == null ? null : timestamp.toLocalDateTime();
return get(columnLabel, localDateTime(cal));
}

/**
Expand All @@ -1635,8 +1616,7 @@ public LocalDateTime getLocalDateTime(String columnLabel, Calendar cal) throws S
*/
@Deprecated
public ZonedDateTime getZonedDateTime(int columnIndex, Calendar cal) throws SQLException {
var timestamp = getTimestamp(columnIndex, cal);
return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault());
return get(columnIndex, zonedDateTime(cal));
}

/**
Expand All @@ -1660,8 +1640,7 @@ public ZonedDateTime getZonedDateTime(int columnIndex, Calendar cal) throws SQLE
*/
@Deprecated
public ZonedDateTime getZonedDateTime(String columnLabel, Calendar cal) throws SQLException {
var timestamp = getTimestamp(columnLabel, cal);
return timestamp == null ? null : timestamp.toInstant().atZone(ZoneId.systemDefault());
return get(columnLabel, zonedDateTime(cal));
}

/**
Expand All @@ -1684,8 +1663,7 @@ public ZonedDateTime getZonedDateTime(String columnLabel, Calendar cal) throws S
*/
@Deprecated
public OffsetDateTime getOffsetDateTime(int columnIndex, Calendar cal) throws SQLException {
LocalDateTime localDateTime = getLocalDateTime(columnIndex, cal);
return localDateTime == null ? null : OffsetDateTime.from(localDateTime);
return get(columnIndex, offsetDateTime(cal));
}

/**
Expand All @@ -1709,8 +1687,7 @@ public OffsetDateTime getOffsetDateTime(int columnIndex, Calendar cal) throws SQ
*/
@Deprecated
public OffsetDateTime getOffsetDateTime(String columnLabel, Calendar cal) throws SQLException {
LocalDateTime localDateTime = getLocalDateTime(columnLabel, cal);
return localDateTime == null ? null : OffsetDateTime.from(localDateTime);
return get(columnLabel, offsetDateTime(cal));
}

/**
Expand All @@ -1734,8 +1711,7 @@ public OffsetDateTime getOffsetDateTime(String columnLabel, Calendar cal) throws
*/
@Deprecated
public OffsetTime getOffsetTime(int columnIndex, Calendar cal) throws SQLException {
LocalTime localDateTime = getLocalTime(columnIndex, cal);
return localDateTime == null ? null : OffsetTime.from(localDateTime);
return get(columnIndex, offsetTime(cal));
}

/**
Expand All @@ -1759,8 +1735,7 @@ public OffsetTime getOffsetTime(int columnIndex, Calendar cal) throws SQLExcepti
*/
@Deprecated
public OffsetTime getOffsetTime(String columnLabel, Calendar cal) throws SQLException {
LocalTime localDateTime = getLocalTime(columnLabel, cal);
return localDateTime == null ? null : OffsetTime.from(localDateTime);
return get(columnLabel, offsetTime(cal));
}

/**
Expand Down Expand Up @@ -2089,6 +2064,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);
if (value == null) return null;
return reader.parser().apply(value);
}

Expand Down

0 comments on commit fe59e66

Please sign in to comment.