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

com.datastax.stargate.sdk.grpc.domain.ResultSetGrpc Maven / Gradle / Ivy

There is a newer version: 2.3.7
Show newest version
package com.datastax.stargate.sdk.grpc.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.stargate.proto.QueryOuterClass.ColumnSpec;
import io.stargate.proto.QueryOuterClass.ResultSet;
import io.stargate.proto.QueryOuterClass.Row;

/**
 * Helper to parse the grpoc output.
 * 
 * @author Cedrick LUNVEN (@clunven)
 */
public class ResultSetGrpc {
    
    /** Object returned by the grpc.*/
    private final ResultSet rs;
    
    /** Index columns names. */
    private final List columnsNames = new ArrayList<>();
    
    /** Access one column in particular. */
    private final Map columnsSpecs = new HashMap<>();
    
    /** Access one column in particular. */
    private final Map columnsIndexes = new HashMap<>();
    
    /**
     * Constructor for the wrapper.
     * 
     * @param rs
     *      resultset
     */
    public ResultSetGrpc(ResultSet rs) {
        this.rs = rs;
        for (int i=0; i rs.getRowsCount()) {
            throw new IllegalArgumentException("Resulset contains only " +  rs.getRowsCount() + " row(s).");
        }
        return rs.getRowsList().get(idx);
    }
    
    /**
     * Access Rows.
     * 
     * @return
     *      list if items
     */
    public List getRows() {
        return rs.getRowsList()
                 .stream()
                 .map(r -> new RowGrpc(this, r))
                 .collect(Collectors.toList());
    }
    
    /**
     * Accessor for internal object.
     * 
     * @return
     *      internal object
     */
    public ResultSet getResultSet() {
        return rs;
    }
    
    /**
     * Access column index based on offset.
     * 
     * @param name
     *      column name
     * @return
     *      column index
     */
    public int getColumnIndex(String name) {
        if (!columnsIndexes.containsKey(name)) {
            throw new IllegalArgumentException("Column '" + name + "' is unknown, use " + columnsNames);
        }
        return columnsIndexes.get(name);
    }
    
    /**
     * Return column name based on index.
     * @param idx
     *      column index
     * @return
     *      column name
     */
    public String getColumnName(int idx) {
        if (idx > columnsNames.size()) {
            throw new IllegalArgumentException("Invalid index, only '" 
                            + columnsNames.size() + "' size available");
        }
        return columnsNames.get(idx);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy