[#6836] Add Record.format()

This commit is contained in:
lukaseder 2017-11-22 11:35:08 +01:00
parent e54bd9b888
commit 1c6c18ccd8
5 changed files with 108 additions and 16 deletions

View File

@ -1324,6 +1324,59 @@ public interface Record extends Attachable, Comparable<Record> {
// Formatting methods
// -------------------------------------------------------------------------
/**
* Get a simple formatted representation of this result as a text table.
* <p>
* The format is the following:<p>
* <code><pre>
* +------+------+------+
* | COL1 | COL2 | COL3 |
* +------+------+------+
* | VAL1 | VAL2 | VAL3 |
* +------+------+------+
* </pre></code>
*
* @return The formatted result
*/
String format();
/**
* Get a simple formatted representation of this result as a text data
* structure, according to the format.
*
* @return The formatted result
* @see TXTFormat
*/
String format(TXTFormat format);
/**
* Like {@link #format()}, 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) 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 #format()}, 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) 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;
/**
* Get a simple formatted representation of this result as a JSON array.
* <p>

View File

@ -41,15 +41,17 @@ package org.jooq;
*/
public final class TXTFormat {
final int maxRows;
final int minColWidth;
final int maxColWidth;
final boolean horizontalTableBorder;
final boolean horizontalHeaderBorder;
final boolean horizontalCellBorder;
final boolean verticalTableBorder;
final boolean verticalCellBorder;
final boolean intersectLines;
public static final TXTFormat DEFAULT = new TXTFormat();
final int maxRows;
final int minColWidth;
final int maxColWidth;
final boolean horizontalTableBorder;
final boolean horizontalHeaderBorder;
final boolean horizontalCellBorder;
final boolean verticalTableBorder;
final boolean verticalCellBorder;
final boolean intersectLines;
public TXTFormat() {
this(

View File

@ -88,6 +88,7 @@ import org.jooq.Record8;
import org.jooq.Record9;
import org.jooq.RecordMapper;
import org.jooq.Result;
import org.jooq.TXTFormat;
import org.jooq.Table;
import org.jooq.UniqueKey;
import org.jooq.XMLFormat;
@ -911,6 +912,42 @@ abstract class AbstractRecord extends AbstractStore implements Record {
// Formatting methods
// -------------------------------------------------------------------------
@Override
public final String format() {
StringWriter writer = new StringWriter();
format(writer);
return writer.toString();
}
@Override
public final String format(TXTFormat format) {
StringWriter writer = new StringWriter();
format(writer, format);
return writer.toString();
}
@Override
public final void format(OutputStream stream) {
format(new OutputStreamWriter(stream));
}
@Override
public final void format(OutputStream stream, TXTFormat format) {
format(new OutputStreamWriter(stream), format);
}
@Override
public final void format(Writer writer) {
format(writer, TXTFormat.DEFAULT);
}
@Override
public final void format(Writer writer, TXTFormat format) {
Result<AbstractRecord> result = new ResultImpl<AbstractRecord>(configuration(), fields.fields.fields);
result.add(AbstractRecord.this);
result.format(writer, format);
}
@Override
public final String formatJSON() {
StringWriter writer = new StringWriter();

View File

@ -393,12 +393,12 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public final String format() {
return format(new TXTFormat());
return format(TXTFormat.DEFAULT);
}
@Override
public final String format(int maxRecords) {
return format(new TXTFormat().maxRows(maxRecords));
return format(TXTFormat.DEFAULT.maxRows(maxRecords));
}
@Override
@ -425,12 +425,12 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public final void format(Writer writer) {
format(writer, new TXTFormat());
format(writer, TXTFormat.DEFAULT);
}
@Override
public final void format(Writer writer, int maxRecords) {
format(writer, new TXTFormat().maxRows(maxRecords));
format(writer, TXTFormat.DEFAULT.maxRows(maxRecords));
}
@Override
@ -2967,7 +2967,7 @@ final class ResultImpl<R extends Record> implements Result<R> {
@Override
public String toString() {
return format(new TXTFormat().maxRows(50).maxColWidth(50));
return format(TXTFormat.DEFAULT.maxRows(50).maxColWidth(50));
}
@Override

View File

@ -141,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(new TXTFormat().maxRows(500).maxColWidth(500)), Level.FINE);
logMultiline("Fetched result", ctx.result().format(TXTFormat.DEFAULT.maxRows(500).maxColWidth(500)), Level.FINE);
else if (log.isDebugEnabled())
logMultiline("Fetched result", ctx.result().format(new TXTFormat().maxRows(5).maxColWidth(50)), Level.FINE);
logMultiline("Fetched result", ctx.result().format(TXTFormat.DEFAULT.maxRows(5).maxColWidth(50)), Level.FINE);
}
@Override