package lc.jdbc;

import lc.mecha.log.MechaLogger;
import lc.mecha.util.StringAccumulatorV2;

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

public class JDBCUtil {
    public static void printResultSet(MechaLogger log, ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnsNumber = rsmd.getColumnCount();

        log.info("ResultSet (col: {})", columnsNumber);

        while (rs.next()) {
            StringAccumulatorV2 sa = new StringAccumulatorV2(",");
            for (int i = 1; i <= columnsNumber; i++) {
                if (i > 1) System.out.print(",  ");
                String columnValue = rs.getString(i);
                sa.push(rsmd.getColumnName(i) + ":" + columnValue);
            }
            log.info(sa.asString());
        }
    }
}
