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

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

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

import static java.util.stream.Collectors.toList;

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

import org.itsallcode.jdbc.dialect.DbDialect;
import org.itsallcode.jdbc.resultset.generic.SimpleMetaData;

/**
 * A {@link ResultSet} that automatically converts values to modern types in the
 * following methods:
 * 
    *
  • {@link ResultSet#getObject(String)}
  • *
  • {@link ResultSet#getObject(String, Class)}
  • *
  • {@link ResultSet#getObject(int)}
  • *
  • {@link ResultSet#getObject(int, Class)}
  • *
*/ public class ConvertingResultSet extends DelegatingResultSet { private final ResultSet delegate; private final ResultSetValueConverter converter; private ConvertingResultSet(final ResultSet delegate, final ResultSetValueConverter converter) { super(delegate); this.delegate = delegate; this.converter = converter; } /** * Create a new converting result set. * * @param dialect DB dialect * @param delegate the original result set. * @return a new converting result set. */ public static ConvertingResultSet create(final DbDialect dialect, final ResultSet delegate) { final SimpleMetaData metaData = SimpleMetaData.create(delegate); final List converters = metaData.columns().stream() .map(col -> ColumnValueConverter.simple(dialect.createExtractor(col))) .collect(toList()); return new ConvertingResultSet(delegate, ResultSetValueConverter.create(metaData, converters)); } @Override public T getObject(final int columnIndex, final Class type) throws SQLException { return converter.getObject(delegate, columnIndex, type); } @Override public T getObject(final String columnLabel, final Class type) throws SQLException { return converter.getObject(delegate, columnLabel, type); } @Override public Object getObject(final int columnIndex) throws SQLException { return converter.getObject(delegate, columnIndex); } @Override public Object getObject(final String columnLabel) throws SQLException { return converter.getObject(delegate, columnLabel); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy