All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.itsallcode.jdbc.resultset.ContextRowMapper Maven / Gradle / Ivy

There is a newer version: 0.7.1
Show newest version
package org.itsallcode.jdbc.resultset;

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

import org.itsallcode.jdbc.Context;
import org.itsallcode.jdbc.dialect.DbDialect;
import org.itsallcode.jdbc.resultset.generic.*;
import org.itsallcode.jdbc.resultset.generic.GenericRowMapper.ColumnValuesConverter;

/**
 * Converts a single row from a {@link ResultSet} to a generic row type.
 * 
 * @param  generic row type
 */
@FunctionalInterface
public interface ContextRowMapper {

    /**
     * Converts a single row from a {@link ResultSet} to a generic row type.
     * 
     * @param context   database context
     * @param resultSet result set
     * @param rowNum    the current row number (zero based)
     * @return the converted row
     * @throws SQLException if accessing the result set fails
     */
    T mapRow(Context context, ResultSet resultSet, int rowNum) throws SQLException;

    /**
     * Create a {@link ContextRowMapper} that creates generic {@link Row} objects.
     * 
     * @param dialect DB dialect
     * @return a new row mapper
     */
    public static RowMapper generic(final DbDialect dialect) {
        return generic(dialect, row -> row);
    }

    /**
     * Create a {@link ContextRowMapper} that creates {@link List}s of simple column
     * objects.
     * 
     * @param dialect DB dialect
     * @return a new row mapper
     */
    public static RowMapper> columnValueList(final DbDialect dialect) {
        return generic(dialect, row -> row.columnValues().stream().map(ColumnValue::value).toList());
    }

    private static  RowMapper generic(final DbDialect dialect, final ColumnValuesConverter converter) {
        return new GenericRowMapper<>(dialect, converter);
    }

    /**
     * Creates a new new {@link ContextRowMapper} from a {@link RowMapper}.
     * 

* Use this if the mapper doesn't need the {@link Context}. * * @param generic row type * @param mapper the simple row mapper * @return a new {@link ContextRowMapper} */ public static ContextRowMapper create(final RowMapper mapper) { return (context, resultSet, rowNum) -> mapper.mapRow(resultSet, rowNum); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy