package meerkat.bulletinboard.sqlserver.mappers;

import meerkat.comm.MessageOutputStream;
import meerkat.protobuf.BulletinBoardApi.BatchChunk;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Arbel Deutsch Peled on 19-Dec-15.
 */
public class BatchDataCallbackHandler implements RowCallbackHandler {

    private final MessageOutputStream<BatchChunk> out;

    public BatchDataCallbackHandler(MessageOutputStream<BatchChunk> out) {
        this.out = out;
    }

    @Override
    public void processRow(ResultSet rs) throws SQLException {
        try {
            out.writeMessage(BatchChunk.parseFrom(rs.getBytes(1)));
        } catch (IOException e) {
            //TODO: Log
        }
    }
}
