Add "Describe" command, with support for describing both (a) specific table(s) and...
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / Tabular.java
index 03c403ef3f037684977376d14e3f3dd3d8b8472b..130f4a563eefaef9977c7e56ccc12ea597368557 100644 (file)
@@ -38,10 +38,12 @@ abstract public class Tabular {
        }
        
        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(PrintWriter pw, String noRowsMessage) 
+               throws SQLException 
+       {
                int rowCount = 0;
                Column[] cols = getCols();
                RowBuffer rowBuf;
@@ -66,8 +68,15 @@ abstract public class Tabular {
                        rowCount += pending;
                }
                
-               // TODO:  Implement a String table for i18n
-               pw.println("" + rowCount + " row(s) returned.");
+               if (rowCount > 0) {
+                       writeDivider(pw, colWidths);
+                       // TODO:  Implement a String table for i18n
+                       pw.println("" + rowCount + " row(s) returned.");
+               }
+               else {
+                       pw.println(noRowsMessage);
+               }
+               
                pw.flush();
                
                return rowCount;
@@ -119,7 +128,7 @@ abstract public class Tabular {
        
        // 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(int[] colWidths) throws SQLException
        {
                RowBuffer rowBuf = new RowBuffer();
                
@@ -202,7 +211,7 @@ abstract public class Tabular {
                }
                return Object.class;
        }
-
+       
        String repChar(char chr, int times)
        {
                StringBuffer sb = new StringBuffer();
@@ -222,7 +231,7 @@ abstract public class Tabular {
        void writeHeader(PrintWriter pw, Column[] cols, int[] colWidths) {
                writeDivider(pw, colWidths);
 
-               for (int idx = 1; idx <= cols.length; ++idx) {
+               for (int idx = 0; idx < cols.length; ++idx) {
                        Column col = cols[idx];
                        pw.print("|" + centrePad(col.getLabel(), colWidths[idx]));
                }
@@ -236,7 +245,7 @@ abstract public class Tabular {
                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 value = "" + row.getValue(colIdx + 1);
                                String padding = repChar(' ', colWidths[colIdx] - value.length());
                                pw.print("|" + value + padding);
                        }