Search in sources :

Example 6 with IOException

use of org.jooq.exception.IOException in project jOOQ by jOOQ.

the class ResultImpl method format.

@Override
public final void format(Writer writer, int maxRecords) {
    try {
        final int COL_MIN_WIDTH = 4;
        final int COL_MAX_WIDTH = 50;
        // Numeric columns have greater max width because values are aligned
        final int NUM_COL_MAX_WIDTH = 100;
        // The max number of records that will be considered for formatting purposes
        final int MAX_RECORDS = min(50, maxRecords);
        // Get max decimal places for numeric type columns
        final int[] decimalPlaces = new int[fields.fields.length];
        final int[] widths = new int[fields.fields.length];
        for (int index = 0; index < fields.fields.length; index++) {
            if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
                List<Integer> decimalPlacesList = new ArrayList<Integer>();
                // Initialize
                decimalPlacesList.add(0);
                // Collect all decimal places for the column values
                String value;
                for (int i = 0; i < min(MAX_RECORDS, size()); i++) {
                    value = format0(getValue(i, index), get(i).changed(index), true);
                    decimalPlacesList.add(getDecimalPlaces(value));
                }
                // Find max
                decimalPlaces[index] = Collections.max(decimalPlacesList);
            }
        }
        // Get max column widths
        int colMaxWidth;
        for (int index = 0; index < fields.fields.length; index++) {
            // Is number column?
            boolean isNumCol = Number.class.isAssignableFrom(fields.fields[index].getType());
            colMaxWidth = isNumCol ? NUM_COL_MAX_WIDTH : COL_MAX_WIDTH;
            // Collect all widths for the column
            List<Integer> widthList = new ArrayList<Integer>();
            // Add column name width first
            widthList.add(min(colMaxWidth, max(COL_MIN_WIDTH, fields.fields[index].getName().length())));
            // Add column values width
            String value;
            for (int i = 0; i < min(MAX_RECORDS, size()); i++) {
                value = format0(getValue(i, index), get(i).changed(index), true);
                // Align number values before width is calculated
                if (isNumCol) {
                    value = alignNumberValue(decimalPlaces[index], value);
                }
                widthList.add(min(colMaxWidth, value.length()));
            }
            // Find max
            widths[index] = Collections.max(widthList);
        }
        // Begin the writing
        // ---------------------------------------------------------------------
        // Write top line
        writer.append("+");
        for (int index = 0; index < fields.fields.length; index++) {
            writer.append(rightPad("", widths[index], "-"));
            writer.append("+");
        }
        // Write headers
        writer.append("\n|");
        for (int index = 0; index < fields.fields.length; index++) {
            String padded;
            if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
                padded = leftPad(fields.fields[index].getName(), widths[index]);
            } else {
                padded = rightPad(fields.fields[index].getName(), widths[index]);
            }
            writer.append(abbreviate(padded, widths[index]));
            writer.append("|");
        }
        // Write separator
        writer.append("\n+");
        for (int index = 0; index < fields.fields.length; index++) {
            writer.append(rightPad("", widths[index], "-"));
            writer.append("+");
        }
        // Write columns
        for (int i = 0; i < min(maxRecords, size()); i++) {
            writer.append("\n|");
            for (int index = 0; index < fields.fields.length; index++) {
                String value = format0(getValue(i, index), get(i).changed(index), true).replace("\n", "{lf}").replace("\r", "{cr}");
                String padded;
                if (Number.class.isAssignableFrom(fields.fields[index].getType())) {
                    // Align number value before left pad
                    value = alignNumberValue(decimalPlaces[index], value);
                    // Left pad
                    padded = leftPad(value, widths[index]);
                } else {
                    // Right pad
                    padded = rightPad(value, widths[index]);
                }
                writer.append(abbreviate(padded, widths[index]));
                writer.append("|");
            }
        }
        // Write bottom line
        if (size() > 0) {
            writer.append("\n+");
            for (int index = 0; index < fields.fields.length; index++) {
                writer.append(rightPad("", widths[index], "-"));
                writer.append("+");
            }
        }
        // Write truncation message, if applicable
        if (maxRecords < size()) {
            writer.append("\n|...");
            writer.append("" + (size() - maxRecords));
            writer.append(" record(s) truncated...");
        }
        writer.flush();
    } catch (java.io.IOException e) {
        throw new IOException("Exception while writing TEXT", e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(org.jooq.exception.IOException)

Aggregations

IOException (org.jooq.exception.IOException)6 Record (org.jooq.Record)4 TableRecord (org.jooq.TableRecord)4 UpdatableRecord (org.jooq.UpdatableRecord)4 Schema (org.jooq.Schema)2 TableField (org.jooq.TableField)2 ArrayList (java.util.ArrayList)1 DSLContext (org.jooq.DSLContext)1