de.chojo.sadu.mapper.rowmapper.RowMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sadu-mapper Show documentation
Show all versions of sadu-mapper Show documentation
SADU module to map values received from a database to java objects using the queries module.
The newest version!
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/
package de.chojo.sadu.mapper.rowmapper;
import de.chojo.sadu.mapper.MapperConfig;
import de.chojo.sadu.mapper.wrapper.Row;
import org.slf4j.Logger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
import static de.chojo.sadu.mapper.util.Results.columnNames;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Class representing the mapping of a column to an object.
*
* @param type of returned object
*/
public class RowMapper implements RowMapping {
private static final Logger log = getLogger(RowMapper.class);
private final Class clazz;
private final RowMapping mapper;
private final Set columns;
RowMapper(Class clazz, RowMapping mapper, Set columns) {
this.clazz = clazz;
this.mapper = mapper;
this.columns = columns;
}
public static PartialRowMapper forClass(Class clazz) {
return new RowMapperBuilder<>(clazz);
}
public Class clazz() {
return clazz;
}
public Set columns() {
return columns;
}
@Override
public T map(Row row) throws SQLException {
return mapper.map(row);
}
public boolean isWildcard() {
return columns.isEmpty();
}
/**
* Checks how many rows of the result set are applicable.
*
* @param resultSet result set
* @return If the result set is not applicable 0 will be returned. Otherwise, the count of applicable rows will be returned.
*/
public int applicable(ResultSet resultSet) throws SQLException {
return applicable(resultSet, MapperConfig.DEFAULT);
}
/**
* Checks how many rows of the result set are applicable.
*
* @param resultSet result set
* @return If the result set is not applicable 0 will be returned. Otherwise, the count of applicable rows will be returned.
*/
public int applicable(ResultSet resultSet, MapperConfig config) throws SQLException {
return applicable(resultSet.getMetaData(), config);
}
/**
* Checks how many rows of the result set are applicable.
*
* @param meta meta of a result set
* @return If the result set is not applicable 0 will be returned. Otherwise, the count of applicable rows will be returned.
*/
public int applicable(ResultSetMetaData meta) {
return applicable(meta, MapperConfig.DEFAULT);
}
/**
* Checks how many rows of the result set are applicable.
*
* @param meta meta of a result set
* @param config mapper config
* @return If the result set is not applicable 0 will be returned. Otherwise, the count of applicable rows will be returned.
*/
public int applicable(ResultSetMetaData meta, MapperConfig config) {
Set names;
try {
names = columnNames(meta);
} catch (SQLException e) {
log.error("Could not read columns", e);
return 0;
}
var columns = new HashSet<>(this.columns);
for (var entry : config.aliases().entrySet()) {
if (columns.remove(entry.getKey())) columns.add(entry.getValue());
}
var size = names.size();
if (columns.size() > size) {
// The result set has fewer rows than we need
return 0;
}
Set overlap = new HashSet<>(names);
overlap.retainAll(columns);
if (overlap.size() != columns.size()) {
// The result set has not all rows we need.
return 0;
}
// Check that the result set has the same size as the expected columns when strict mode is enabled.
if (config.isStrict() && size != columns.size()) {
return 0;
}
return overlap.size();
}
@Override
public String toString() {
return "RowMapper{clazz=%s, columns=%s}".formatted(clazz.getName(), String.join(", ", columns));
}
}