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

com.github.housepower.jdbc.protocol.QueryResponse Maven / Gradle / Ivy

package com.github.housepower.jdbc.protocol;

import com.github.housepower.jdbc.data.Block;
import com.github.housepower.jdbc.misc.CheckedIterator;
import com.github.housepower.jdbc.misc.CheckedSupplier;

import java.sql.SQLException;
import java.util.function.Supplier;

public class QueryResponse {
    private final CheckedSupplier responseSupplier;
    private Block header;
    private boolean atEnd;
    // Progress
    // Totals
    // Extremes
    // ProfileInfo
    // EndOfStream

    public QueryResponse(CheckedSupplier responseSupplier) {
        this.responseSupplier = responseSupplier;
    }

    public Block header() throws SQLException {
        ensureHeaderConsumed();

        return header;
    }

    private void ensureHeaderConsumed() throws SQLException {
        if (header == null) {
            DataResponse firstDataResponse = consumeDataResponse();
            header = firstDataResponse != null ? firstDataResponse.block() : new Block();
        }
    }

    private DataResponse consumeDataResponse() throws SQLException {
        while (!atEnd) {
            RequestOrResponse response = responseSupplier.get();
            if (response instanceof DataResponse) {
                return (DataResponse) response;
            } else if (response instanceof EOFStreamResponse || response == null) {
                atEnd = true;
            }
        }

        return null;
    }

    public Supplier> data() {
        return () -> new CheckedIterator() {
            DataResponse current;

            private DataResponse fill() throws SQLException {
                ensureHeaderConsumed();
                return current = consumeDataResponse();
            }

            private DataResponse drain() throws SQLException {
                if (current == null) {
                    fill();
                }

                DataResponse top = current;
                current = null;
                return top;
            }

            @Override
            public boolean hasNext() throws SQLException {
                return current != null || fill() != null;
            }

            @Override
            public DataResponse next() throws SQLException {
                return drain();
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy