Fix and fine-tune suppress_nulls.
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / Tabular.java
index eef757d9f54f0d52b9a2ad89066b1099540df738..6a060f1a2b0e94c46e77b159289f278272caa1b7 100644 (file)
@@ -69,21 +69,21 @@ abstract public class Tabular {
                if (pending > 0) {
                        writeHeader(pw, cols, colWidths, suppressed);
                        writeRowBuffer(pw, rowBuf, colWidths, suppressed);
-                       rowCount = rowBuf.getPending();
+                       rowCount = pending;
                }
                
-               if (driver.isSuppressNulls()) {
-                       // TODO:  StringTable
-                       pw.println("Row limit for suppress_nulls has been reached; output may have been truncated.");
-                       writeDivider(pw, colWidths, suppressed);
-               }
-               else {
-                       while (pending > 0) {
-                               rowBuf = bufferRows(driver, colWidths);
-                               writeRowBuffer(pw, rowBuf, colWidths, suppressed);
-                               pending = rowBuf.getPending();
-                               rowCount += pending;
+               rowBuf = bufferRows(driver, colWidths);
+               pending = rowBuf.getPending();
+               while (pending > 0) {
+                       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) {
@@ -164,13 +164,13 @@ abstract public class Tabular {
                                }
                        }
 
-                       if ((!allColsNull) || (!driver.isSuppressNulls())) 
+                       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 = ("" + row.getValue(colIdx + 1)).length();
+                                       int width = stringify(row.getValue(colIdx + 1)).length();
                                        if (width > colWidths[colIdx]) {
                                                // Widen the column to fit this value
                                                colWidths[colIdx] = width;
@@ -307,8 +307,12 @@ abstract public class Tabular {
        {
                boolean[] result = new boolean[cols.length];
                
-               if (rowBuf.getPending() < 1) {
-                       // No data rows, so do not suppress any columns.
+               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;
                        }
@@ -346,8 +350,10 @@ abstract public class Tabular {
                writeDivider(pw, colWidths, suppressed);
 
                for (int idx = 0; idx < cols.length; ++idx) {
-                       Column col = cols[idx];
-                       pw.print("| " + centrePad(col.getLabel(), colWidths[idx]) + " ");
+                       if (!suppressed[idx]) {
+                               Column col = cols[idx];
+                               pw.print("| " + centrePad(col.getLabel(), colWidths[idx]) + " ");
+                       }
                }
                pw.println("|");
                
@@ -362,7 +368,7 @@ abstract public class Tabular {
                                if (!suppressed[colIdx]) {
                                        Object obj = row.getValue(colIdx + 1);
                                        String value = stringify(obj);
-                                       int width = stringWidth(obj);
+                                       int width = stringWidth(value);
                                        String padding = repChar(' ', colWidths[colIdx] - width);
                                        pw.print("| " + value + padding + " ");
                                }