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

org.sfm.jdbc.impl.JoinJdbcMapper Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.jdbc.impl;

import org.sfm.jdbc.JdbcMapper;
import org.sfm.map.*;
import org.sfm.utils.ForEachIterator;
import org.sfm.utils.RowHandler;

import java.sql.ResultSet;
import java.sql.SQLException;

public final class JoinJdbcMapper extends AbstractForEachDynamicJdbcMapper {

    private final JdbcMapper mapper;

    public JoinJdbcMapper(JdbcMapper mapper, RowHandlerErrorHandler errorHandler) {
        super(errorHandler);
        this.mapper = mapper;
    }

    @Override
    protected Mapper getMapper(ResultSet source) throws SQLException {
        return mapper;
    }

    @Override
    protected JoinForEach newForEachIterator(ResultSet rs) {
        return new JoinForEach(mapper, newMappingContext(rs), errorHandler, rs);
    }

    @Override
    public MappingContext newMappingContext(ResultSet source) {
        return mapper.newMappingContext(source);
    }

    private static class JoinForEach implements ForEachIterator {

        private final Mapper mapper;
        private final MappingContext mappingContext;
        private final RowHandlerErrorHandler rowHandlerErrorHandler;


        private final ResultSet resultSet;
        private T currentValue;

        private JoinForEach(Mapper mapper, MappingContext mappingContext, RowHandlerErrorHandler rowHandlerErrorHandler, ResultSet resultSet) {
            this.mapper = mapper;
            this.mappingContext = mappingContext;
            this.rowHandlerErrorHandler = rowHandlerErrorHandler;
            this.resultSet = resultSet;
        }


        @Override
        public boolean next(RowHandler rowHandler) throws Exception {
            return forEach(true, rowHandler);
        }

        @Override
        public void forEach(RowHandler rowHandler) throws Exception {
            forEach(false, rowHandler);
        }

        private boolean forEach(boolean stopOnNext, RowHandler rowHandler) throws Exception {
            while (resultSet.next()) {

                mappingContext.handle(resultSet);

                if (mappingContext.rootBroke()) {
                    if (currentValue != null) {
                        callHandler(rowHandler);
                        currentValue = mapper.map(resultSet, mappingContext);
                        if (stopOnNext) {
                            return true;
                        }
                    } else {
                        currentValue = mapper.map(resultSet, mappingContext);
                    }
                } else {
                    mapper.mapTo(resultSet, currentValue, mappingContext);
                }
            }

            if (currentValue != null) {
                callHandler(rowHandler);
                currentValue = null;
                return true;
            } else {
                return false;
            }
        }

        private void callHandler(RowHandler rowHandler) throws Exception {
            try {
                rowHandler.handle(currentValue);
            } catch(Exception e) {
                rowHandlerErrorHandler.handlerError(e, currentValue);
            }

        }
    }

    @Override
    public String toString() {
        return "JoinJdbcMapper{" +
                "mapper=" + mapper +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy