[#6833] Add TXTFormat to allow for additional text formatting options

This commit is contained in:
lukaseder 2017-11-21 12:51:44 +01:00
parent 5b3280c1a2
commit 81251be196
5 changed files with 182 additions and 27 deletions

View File

@ -481,7 +481,7 @@ public interface Result<R extends Record> extends List<R>, Attachable {
* Get a simple formatted representation of this result.
* <p>
* This is the same as calling {@link #format(int)} with
* <code>maxRows = 50</code>
* <code>maxRows = Integer.MAX_VALUE</code>
*
* @return The formatted result
*/
@ -496,6 +496,14 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
String format(int maxRecords);
/**
* Get a simple formatted representation of this result.
*
* @param format The formatting information
* @return The formatted result
*/
String format(TXTFormat format);
/**
* Get a simple formatted representation of this result as HTML.
* <p>
@ -678,6 +686,13 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
void format(OutputStream stream, int maxRecords) throws IOException;
/**
* Like {@link #format(TXTFormat)}, but the data is output onto an {@link OutputStream}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void format(OutputStream stream, TXTFormat format) throws IOException;
/**
* Like {@link #formatHTML()}, but the data is output onto an {@link OutputStream}.
*
@ -804,6 +819,13 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
void format(Writer writer, int maxRecords) throws IOException;
/**
* Like {@link #format(TXTFormat)}, but the data is output onto a {@link Writer}.
*
* @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong.
*/
void format(Writer writer, TXTFormat format) throws IOException;
/**
* Like {@link #formatHTML()}, but the data is output onto a {@link Writer}.
*

View File

@ -0,0 +1,120 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
/**
* A CSV formatting type, which can be used to configure CSV imports / exports.
*
* @author Lukas Eder
*/
public final class TXTFormat {
final int maxRows;
final int minColWidth;
final int maxColWidth;
public TXTFormat() {
this(
Integer.MAX_VALUE,
4,
Integer.MAX_VALUE
);
}
private TXTFormat(
int maxRows,
int minColWidth,
int maxColWidth
) {
this.maxRows = maxRows;
this.minColWidth = minColWidth;
this.maxColWidth = maxColWidth;
}
/**
* The maximum number of rows to be included in the format, defaulting to all rows.
*/
public TXTFormat maxRows(int newMaxRows) {
return new TXTFormat(
newMaxRows,
minColWidth,
maxColWidth
);
}
/**
* The maximum number of rows to be included in the format, defaulting to all rows.
*/
public int maxRows() {
return maxRows;
}
/**
* The minimum column width, defaulting to 4
*/
public TXTFormat minColWidth(int newMinColWidth) {
return new TXTFormat(
maxRows,
newMinColWidth,
maxColWidth
);
}
/**
* The minimum column width, defaulting to 4
*/
public int minColWidth() {
return minColWidth;
}
/**
* The minimum column width, defaulting to no limit.
*/
public TXTFormat maxColWidth(int newMaxColWidth) {
return new TXTFormat(
maxRows,
minColWidth,
newMaxColWidth
);
}
/**
* The maximum column width, defaulting to no limit.
*/
public int maxColWidth() {
return maxColWidth;
}
}

View File

@ -148,7 +148,7 @@ final class ExplainQuery {
}
}
return new ExplainImpl(rows, cost, result.format(Integer.MAX_VALUE));
return new ExplainImpl(rows, cost, result.format());
}
private static final class ExplainImpl implements Explain {

View File

@ -116,6 +116,7 @@ import org.jooq.RecordType;
import org.jooq.Result;
import org.jooq.Row;
import org.jooq.Schema;
import org.jooq.TXTFormat;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableRecord;
@ -392,8 +393,18 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public final String format() {
return format(new TXTFormat());
}
@Override
public final String format(int maxRecords) {
return format(new TXTFormat().maxRows(maxRecords));
}
@Override
public final String format(TXTFormat format) {
StringWriter writer = new StringWriter();
format(writer);
format(writer, format);
return writer.toString();
}
@ -402,34 +413,35 @@ final class ResultImpl<R extends Record> implements Result<R> {
format(new OutputStreamWriter(stream));
}
@Override
public final void format(Writer writer) {
format(writer, 50);
}
@Override
public final String format(int maxRecords) {
StringWriter writer = new StringWriter();
format(writer, maxRecords);
return writer.toString();
}
@Override
public final void format(OutputStream stream, int maxRecords) {
format(new OutputStreamWriter(stream), maxRecords);
}
@Override
public final void format(OutputStream stream, TXTFormat format) {
format(new OutputStreamWriter(stream), format);
}
@Override
public final void format(Writer writer) {
format(writer, new TXTFormat());
}
@Override
public final void format(Writer writer, int maxRecords) {
format(writer, new TXTFormat().maxRows(maxRecords));
}
@Override
public final void format(Writer writer, TXTFormat format) {
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;
final int NUM_COL_MAX_WIDTH = format.maxColWidth() == Integer.MAX_VALUE ? Integer.MAX_VALUE : 2 * format.maxColWidth();
// The max number of records that will be considered for formatting purposes
final int MAX_RECORDS = min(50, maxRecords);
final int MAX_RECORDS = min(50, format.maxRows());
// Get max decimal places for numeric type columns
final int[] decimalPlaces = new int[fields.fields.length];
@ -461,13 +473,13 @@ final class ResultImpl<R extends Record> implements Result<R> {
// Is number column?
boolean isNumCol = Number.class.isAssignableFrom(fields.fields[index].getType());
colMaxWidth = isNumCol ? NUM_COL_MAX_WIDTH : COL_MAX_WIDTH;
colMaxWidth = isNumCol ? NUM_COL_MAX_WIDTH : format.maxColWidth();
// 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())));
widthList.add(min(colMaxWidth, max(format.minColWidth(), fields.fields[index].getName().length())));
// Add column values width
String value;
@ -519,7 +531,7 @@ final class ResultImpl<R extends Record> implements Result<R> {
}
// Write columns
for (int i = 0; i < min(maxRecords, size()); i++) {
for (int i = 0; i < min(format.maxRows(), size()); i++) {
writer.append("\n|");
for (int index = 0; index < fields.fields.length; index++) {
@ -557,9 +569,9 @@ final class ResultImpl<R extends Record> implements Result<R> {
}
// Write truncation message, if applicable
if (maxRecords < size()) {
if (format.maxRows() < size()) {
writer.append("\n|...");
writer.append("" + (size() - maxRecords));
writer.append("" + (size() - format.maxRows()));
writer.append(" record(s) truncated...");
}
@ -2909,7 +2921,7 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public String toString() {
return format();
return format(new TXTFormat().maxRows(50).maxColWidth(50));
}
@Override

View File

@ -55,6 +55,7 @@ import org.jooq.Parameter;
import org.jooq.QueryPart;
import org.jooq.Record;
import org.jooq.Routine;
import org.jooq.TXTFormat;
import org.jooq.VisitContext;
import org.jooq.VisitListener;
import org.jooq.VisitListenerProvider;
@ -140,9 +141,9 @@ public class LoggerListener extends DefaultExecuteListener {
public void resultEnd(ExecuteContext ctx) {
if (ctx.result() != null)
if (log.isTraceEnabled())
logMultiline("Fetched result", ctx.result().format(500), Level.FINE);
logMultiline("Fetched result", ctx.result().format(new TXTFormat().maxRows(500).maxColWidth(500)), Level.FINE);
else if (log.isDebugEnabled())
logMultiline("Fetched result", ctx.result().format(5), Level.FINE);
logMultiline("Fetched result", ctx.result().format(new TXTFormat().maxRows(5).maxColWidth(50)), Level.FINE);
}
@Override