package net.jaekl.squelch.stmt;
import java.io.PrintWriter;
+import java.nio.ByteBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
+import javax.xml.bind.DatatypeConverter;
+
+import net.jaekl.squelch.db.DbDriver;
import net.jaekl.squelch.sql.Column;
import net.jaekl.squelch.sql.Row;
// Includes routines to output the data as a human-friendly table, or as CSV.
abstract public class Tabular {
+ private static final long BLOB_MAX = 65536;
+
private static class RowBuffer {
private final int ROW_BUF_SIZE = 50;
}
abstract Column[] getCols() throws SQLException;
- abstract Row getNext();
+ abstract Row getNext() throws SQLException;
// Returns the number of (data) rows that were output
- public int printTable(PrintWriter pw) throws SQLException {
+ public int printTable(DbDriver driver, PrintWriter pw, String noRowsMessage)
+ throws SQLException
+ {
int rowCount = 0;
Column[] cols = getCols();
RowBuffer rowBuf;
int[] colWidths = initColWidthsFromColNames(cols);
// Examine and buffer up to ROW_BUF_SIZE rows, adjusting (widening) colWidths where needed
- rowBuf = bufferRows(colWidths);
+ rowBuf = bufferRows(driver, colWidths);
+ boolean[] suppressed = suppressNulls(driver, rowBuf, cols);
int pending = rowBuf.getPending();
if (pending > 0) {
- writeHeader(pw, cols, colWidths);
- writeRowBuffer(pw, rowBuf, colWidths);
- rowCount = rowBuf.getPending();
+ writeHeader(pw, cols, colWidths, suppressed);
+ writeRowBuffer(pw, rowBuf, colWidths, suppressed);
+ rowCount = pending;
}
+ rowBuf = bufferRows(driver, colWidths);
+ pending = rowBuf.getPending();
while (pending > 0) {
- rowBuf = bufferRows(colWidths);
- writeRowBuffer(pw, rowBuf, colWidths);
- pending = rowBuf.getPending();
+ if (driver.isSet(DbDriver.SUPPRESS_NULLS)) {
+ writeDivider(pw, colWidths, suppressed);
+ pw.println("Row limit for suppress_nulls has been reached; output may have been truncated.");
+ break;
+ }
+ writeRowBuffer(pw, rowBuf, colWidths, suppressed);
rowCount += pending;
+ rowBuf = bufferRows(driver, colWidths);
+ pending = rowBuf.getPending();
+ }
+
+ if (rowCount > 0) {
+ writeDivider(pw, colWidths, suppressed);
+ // TODO: Implement a StringTable for i18n
+ pw.println("" + rowCount + " row(s) returned.");
+ }
+ else {
+ pw.println(noRowsMessage);
}
- // TODO: Implement a String table for i18n
- pw.println("" + rowCount + " row(s) returned.");
pw.flush();
return rowCount;
// Examine and buffer up to rowBuf.length rows.
// Returns the number of actual rows that were buffered (zero if no more rows are available).
- RowBuffer bufferRows(int[] colWidths)
+ RowBuffer bufferRows(DbDriver driver, int[] colWidths) throws SQLException
{
RowBuffer rowBuf = new RowBuffer();
// No more rows available
return rowBuf;
}
- rowBuf.addRow(row);
+
+ boolean allColsNull = true;
+ for (int idx = 1; idx <= colWidths.length; ++idx) {
+ if (null != row.getValue(idx)) {
+ allColsNull = false;
+ }
+ }
- // Check whether all values in this row will fit in the current column widths
- for (int colIdx = 0; colIdx < colWidths.length; ++colIdx) {
- int width = ("" + row.getValue(colIdx + 1)).length();
- if (width > colWidths[colIdx]) {
- // Widen the column to fit this value
- colWidths[colIdx] = width;
+ if ((!allColsNull) || (!driver.isSet(DbDriver.SUPPRESS_NULLS)))
+ {
+ rowBuf.addRow(row);
+
+ // Check whether all values in this row will fit in the current column widths
+ for (int colIdx = 0; colIdx < colWidths.length; ++colIdx) {
+ int width = stringify(row.getValue(colIdx + 1)).length();
+ if (width > colWidths[colIdx]) {
+ // Widen the column to fit this value
+ colWidths[colIdx] = width;
+ }
}
}
}
}
return Object.class;
}
-
+
String repChar(char chr, int times)
{
StringBuffer sb = new StringBuffer();
return sb.toString();
}
- void writeDivider(PrintWriter pw, int[] colWidths) {
+ String stringify(Object obj) throws SQLException
+ {
+ if (obj instanceof java.sql.Blob) {
+ java.sql.Blob blob = null;
+
+ try {
+ blob = (java.sql.Blob)obj;
+ long length = blob.length();
+ if (length > BLOB_MAX) {
+ length = BLOB_MAX;
+ }
+ byte[] content = blob.getBytes(1, (int)length);
+
+ try {
+ // Assume that the BLOB is actually UTF-8 text, and attempt to return it that way.
+ // This happens to work well for the databases that I deal with regularly.
+ // Other users may want to change this assumption here...
+ return StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
+ .onUnmappableCharacter(CodingErrorAction.REPORT)
+ .decode(ByteBuffer.wrap(content))
+ .toString();
+ }
+ catch (CharacterCodingException exc) {
+ // If we get here, then the BLOB's content is not valid UTF-8.
+ // This may be because it's in a different character set, or because it's non-text
+ // (e.g., a bitmap image).
+ // We'll hex-dump it instead.
+ return DatatypeConverter.printHexBinary(content);
+ }
+ }
+ finally {
+ if (null != blob) {
+ try {
+ blob.free();
+ }
+ catch (UnsupportedOperationException | SQLFeatureNotSupportedException exc) {
+ // free() was only a hint; if the hint is not welcome, then that's OK.
+ }
+ }
+ }
+ }
+
+ return "" + obj;
+ }
+
+ int stringWidth(Object obj)
+ {
+ if (obj instanceof java.sql.Blob) {
+ return 1;
+ }
+ return ("" + obj).length();
+ }
+
+ boolean[] suppressNulls(DbDriver driver, RowBuffer rowBuf, Column[] cols)
+ {
+ boolean[] result = new boolean[cols.length];
+
+ if ( !(driver.isSet(DbDriver.SUPPRESS_NULLS))
+ || (rowBuf.getPending() < 1) )
+ {
+ // Null-suppression is turned off, or
+ // there are no data rows,
+ // so do not suppress any columns.
+ for (int colIdx = 0; colIdx < cols.length; ++colIdx) {
+ result[colIdx] = false;
+ }
+ return result;
+ }
+
+ // Start with the assumption that we'll suppress all columns
+ for (int colIdx = 0; colIdx < cols.length; ++colIdx) {
+ result[colIdx] = true;
+ }
+
+ for (int rowIdx = 0; rowIdx < rowBuf.getPending(); ++rowIdx) {
+ Row row = rowBuf.getRow(rowIdx);
+ for (int colIdx = 1; colIdx <= cols.length; ++colIdx) {
+ if (null != row.getValue(colIdx)) {
+ // This column has data, so do not suppress it
+ result[colIdx - 1] = false;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ void writeDivider(PrintWriter pw, int[] colWidths, boolean[] suppressed) {
for (int idx = 0; idx < colWidths.length; ++idx) {
- pw.print("+" + repChar('-', colWidths[idx]));
+ if (!suppressed[idx]) {
+ pw.print("+" + repChar('-', colWidths[idx] + 2));
+ }
}
pw.println("+");
}
- void writeHeader(PrintWriter pw, Column[] cols, int[] colWidths) {
- writeDivider(pw, colWidths);
+ void writeHeader(PrintWriter pw, Column[] cols, int[] colWidths, boolean[] suppressed) {
+ writeDivider(pw, colWidths, suppressed);
- for (int idx = 1; idx <= cols.length; ++idx) {
- Column col = cols[idx];
- pw.print("|" + centrePad(col.getLabel(), colWidths[idx]));
+ for (int idx = 0; idx < cols.length; ++idx) {
+ if (!suppressed[idx]) {
+ Column col = cols[idx];
+ pw.print("| " + centrePad(col.getLabel(), colWidths[idx]) + " ");
+ }
}
pw.println("|");
- writeDivider(pw, colWidths);
+ writeDivider(pw, colWidths, suppressed);
}
- void writeRowBuffer(PrintWriter pw, RowBuffer rowBuf, int[] colWidths) {
+ void writeRowBuffer(PrintWriter pw, RowBuffer rowBuf, int[] colWidths, boolean[] suppressed) throws SQLException {
for (int rowIdx = 0; rowIdx < rowBuf.getPending(); ++rowIdx) {
Row row = rowBuf.getRow(rowIdx);
for (int colIdx = 0; colIdx < colWidths.length; ++colIdx) {
- String value = "" + row.getValue(colIdx);
- String padding = repChar(' ', colWidths[colIdx] - value.length());
- pw.print("|" + value + padding);
+ if (!suppressed[colIdx]) {
+ Object obj = row.getValue(colIdx + 1);
+ String value = stringify(obj);
+ int width = stringWidth(value);
+ String padding = repChar(' ', colWidths[colIdx] - width);
+ pw.print("| " + value + padding + " ");
+ }
}
pw.println("|");
}