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

org.echocat.jsu.JdbcUtils Maven / Gradle / Ivy

package org.echocat.jsu;

import org.echocat.jsu.support.SqlFunction;
import org.echocat.jsu.support.UncheckedSqlException;

import javax.annotation.Nonnull;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.Function;
import java.util.stream.Stream;

import static org.echocat.jsu.AutoCloseableUtils.closeQuietly;
import static org.echocat.jsu.Generator.Value.end;
import static org.echocat.jsu.Generator.Value.valueOf;
import static org.echocat.jsu.StreamUtils.generate;

public final class JdbcUtils {

    @Nonnull
    public static Stream toStream(@Nonnull ResultSet resultSet) {
        return generate(() -> {
            try {
                if (!resultSet.next()) {
                    return end();
                }
                return valueOf(resultSet);
            } catch (final SQLException e) {
                throw new UncheckedSqlException(e);
            }
        }).onClose(() -> closeQuietly(resultSet));
    }

    @Nonnull
    public static  Stream toStream(@Nonnull ResultSet resultSet, @Nonnull Function mapper) {
        return toStream(resultSet)
            .map(mapper);
    }

    @Nonnull
    public static  Stream toStream(@Nonnull ResultSet resultSet, @Nonnull SqlFunction mapper) {
        return toStream(resultSet, (Function) input -> {
            try {
                return mapper.apply(input);
            } catch (final SQLException e) {
                throw new UncheckedSqlException(e);
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy