select queries now work, and print tabular output.
authorChris Jaekl <cejaekl@yahoo.com>
Sun, 12 Jun 2016 13:01:06 +0000 (22:01 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Sun, 12 Jun 2016 13:01:06 +0000 (22:01 +0900)
There is support in the output code for CSV formatting as well, but I still need to add a UI feature to enable it.

src/main/java/net/jaekl/squelch/Squelch.java
src/main/java/net/jaekl/squelch/sql/Row.java
src/main/java/net/jaekl/squelch/stmt/Select.java
src/main/java/net/jaekl/squelch/stmt/Tabular.java
src/main/java/net/jaekl/squelch/stmt/TabularResultSet.java
src/test/java/net/jaekl/squelch/stmt/TabularMock.java [new file with mode: 0644]
src/test/java/net/jaekl/squelch/stmt/TabularTest.java

index 383b541debbe58dcd6251e7ae3638c32c0edf043..88515e0bd6502b11ed87f9429bdc573991478ebf 100644 (file)
@@ -116,7 +116,9 @@ public class Squelch {
                                                break;
                                        }
                                        // Unrecognized command
                                                break;
                                        }
                                        // Unrecognized command
+                                       // TODO:  add a string table, and a natural-language error message.
                                        pw.println("??? \"" + line + "\"");
                                        pw.println("??? \"" + line + "\"");
+                                       pw.flush();
                                }
                        }
                }
                                }
                        }
                }
index 0496d23b156ad90d1891949a14f3d33ce4e02c47..fd47301307e908e3122922ac367eab538155c4e8 100644 (file)
@@ -7,6 +7,7 @@ public class Row {
                m_args = new Object[numCols];
        }
        
                m_args = new Object[numCols];
        }
        
+       // 1-based index, like JDBC
        public void setValue(int idx, Object value) {
                m_args[idx - 1] = value;
        }
        public void setValue(int idx, Object value) {
                m_args[idx - 1] = value;
        }
index afad262856bfec4d0c834951fa778c7573cbe1b1..f34b8c9392014b4e210972f3f1dae4b5be10befe 100644 (file)
@@ -29,8 +29,7 @@ public class Select extends Query {
                {
                        try (ResultSet rs = ps.executeQuery()) 
                        {
                {
                        try (ResultSet rs = ps.executeQuery()) 
                        {
-                               rowCount = printFormatted(pw, rs);
-                               
+                               rowCount = printFormatted(pw, rs);              
                        }
                }
                return rowCount;
                        }
                }
                return rowCount;
index 03c403ef3f037684977376d14e3f3dd3d8b8472b..af3d5da360933f8c37e48c216187236fd047bdde 100644 (file)
@@ -38,7 +38,7 @@ abstract public class Tabular {
        }
        
        abstract Column[] getCols() throws SQLException;
        }
        
        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 {
        
        // Returns the number of (data) rows that were output
        public int printTable(PrintWriter pw) throws SQLException {
@@ -66,6 +66,10 @@ abstract public class Tabular {
                        rowCount += pending;
                }
                
                        rowCount += pending;
                }
                
+               if (rowCount > 0) {
+                       writeDivider(pw, colWidths);
+               }
+               
                // TODO:  Implement a String table for i18n
                pw.println("" + rowCount + " row(s) returned.");
                pw.flush();
                // TODO:  Implement a String table for i18n
                pw.println("" + rowCount + " row(s) returned.");
                pw.flush();
@@ -119,7 +123,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).
        
        // 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();
                
        {
                RowBuffer rowBuf = new RowBuffer();
                
@@ -222,7 +226,7 @@ abstract public class Tabular {
        void writeHeader(PrintWriter pw, Column[] cols, int[] colWidths) {
                writeDivider(pw, colWidths);
 
        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]));
                }
                        Column col = cols[idx];
                        pw.print("|" + centrePad(col.getLabel(), colWidths[idx]));
                }
@@ -236,7 +240,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) {
                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);
                        }
                                String padding = repChar(' ', colWidths[colIdx] - value.length());
                                pw.print("|" + value + padding);
                        }
index 92da23dc1872f9b4764a65eabb21fbbbe89f4129..f8e233c4061335bf186afdd7c9f808ce32a96164 100644 (file)
@@ -9,9 +9,11 @@ import net.jaekl.squelch.sql.Row;
 
 public class TabularResultSet extends Tabular {
        private ResultSet m_resultSet;
 
 public class TabularResultSet extends Tabular {
        private ResultSet m_resultSet;
+       private Column[] m_cols;
        
        public TabularResultSet(ResultSet resultSet) {
                m_resultSet = resultSet;
        
        public TabularResultSet(ResultSet resultSet) {
                m_resultSet = resultSet;
+               m_cols = null;
        }
 
        @Override
        }
 
        @Override
@@ -26,13 +28,24 @@ public class TabularResultSet extends Tabular {
                        int width = metaData.getColumnDisplaySize(idx);
                        cols[idx - 1] = new Column(label, clazz, width);
                }
                        int width = metaData.getColumnDisplaySize(idx);
                        cols[idx - 1] = new Column(label, clazz, width);
                }
-               
+               m_cols = cols;
                return cols;
        }
 
        @Override
                return cols;
        }
 
        @Override
-       Row getNext() {
-               // TODO Auto-generated method stub
-               return null;
+       Row getNext() throws SQLException {
+               if (null == m_cols) {
+                       m_cols = getCols();
+               }
+               if (! m_resultSet.next()) {
+                       return null;
+               }
+               
+               Row row = new Row(m_cols.length);
+               for (int idx = 0; idx < m_cols.length; ++idx) {
+                       row.setValue(idx + 1, m_resultSet.getObject(idx + 1));
+               }
+               
+               return row;
        }
 }
        }
 }
diff --git a/src/test/java/net/jaekl/squelch/stmt/TabularMock.java b/src/test/java/net/jaekl/squelch/stmt/TabularMock.java
new file mode 100644 (file)
index 0000000..b19a946
--- /dev/null
@@ -0,0 +1,41 @@
+package net.jaekl.squelch.stmt;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+
+import net.jaekl.squelch.sql.Column;
+import net.jaekl.squelch.sql.Row;
+
+public class TabularMock extends Tabular {
+       private Column[] m_cols;
+       private ArrayList<Row> m_rows;
+       private int m_rowIdx;
+       
+       public TabularMock() {
+               m_cols = new Column[0];
+               m_rows = new ArrayList<Row>();
+               m_rowIdx = (-1);
+       }
+       
+       @Override
+       Column[] getCols() throws SQLException {
+               return m_cols.clone();
+       }
+
+       @Override
+       Row getNext() {
+               if (m_rowIdx >= (m_rows.size() - 1)) {
+                       return null;
+               }
+               m_rowIdx++;
+               return m_rows.get(m_rowIdx);
+       }
+       
+       public void mock_setCols(Column[] cols) {
+               m_cols = cols.clone();
+       }
+       
+       public void mock_addRow(Row row) {
+               m_rows.add(row);
+       }
+}
index 6b5462dac3f00320f1a4b0b4cbdeebc0344fffbb..f6749f29293218d6c93464f438ea371fdb2c9ebd 100644 (file)
@@ -1,7 +1,12 @@
 package net.jaekl.squelch.stmt;
 
 package net.jaekl.squelch.stmt;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
 import java.sql.SQLException;
 import java.sql.Types;
 
 import java.sql.SQLException;
 import java.sql.Types;
 
@@ -11,18 +16,17 @@ import net.jaekl.squelch.sql.Row;
 import org.junit.Test;
 
 public class TabularTest {
 import org.junit.Test;
 
 public class TabularTest {
-       private static class TabularMock extends Tabular {
-               @Override
-               Column[] getCols() throws SQLException {
-                       return null;
-               }
-
-               @Override
-               Row getNext() {
-                       return null;
-               }
+       @Test
+       public void test_centrePad() {
+               Tabular tabular = new TabularMock();
+               
+               assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
+               assertEquals("  Fred  ", tabular.centrePad("Fred", 8));
+               assertEquals("NULL", tabular.centrePad(null, 0));
+               assertEquals("  NULL  ", tabular.centrePad(null, 8));
+               assertEquals("   Wilma    ", tabular.centrePad("Wilma", 12));
        }
        }
-       
+
        @Test
        public void test_classForSqlType() {
                Object[][] data = { 
        @Test
        public void test_classForSqlType() {
                Object[][] data = { 
@@ -70,7 +74,53 @@ public class TabularTest {
                        assertEquals(expected, actual);
                }
        }
                        assertEquals(expected, actual);
                }
        }
+       
+       @Test
+       public void test_printCsv_empTable() throws IOException, SQLException
+       {
+               TabularMock tabular = createEmpTable();
+               
+               try (
+                               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                               PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+                       )
+               {
+                       tabular.printCsv(pw);
+                       pw.close();
+                       baos.close();
+                       String actual = baos.toString();
+                       assertEquals(  "EmpId,FirstName,LastName\n"
+                                            + "12345,Fred,Flintstone\n"
+                                            + "7654321,Barney,Rubble\n",
+                                            actual);
+               }               
+       }
 
 
+       @Test
+       public void test_printTable_empTable() throws IOException, SQLException
+       {
+               TabularMock tabular = createEmpTable();
+               
+               try (
+                               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                               PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+                       )
+               {
+                       tabular.printTable(pw);
+                       pw.close();
+                       baos.close();
+                       String actual = baos.toString();
+                       assertEquals(  "+-------+---------+----------+\n"
+                                            + "| EmpId |FirstName| LastName |\n"
+                                            + "+-------+---------+----------+\n"
+                                            + "|12345  |Fred     |Flintstone|\n"
+                                            + "|7654321|Barney   |Rubble    |\n"
+                                            + "+-------+---------+----------+\n"
+                                            + "2 row(s) returned.\n",
+                                            actual);
+               }
+       }
+       
        @Test
        public void test_repChar() {
                Tabular tabular = new TabularMock();
        @Test
        public void test_repChar() {
                Tabular tabular = new TabularMock();
@@ -80,15 +130,30 @@ public class TabularTest {
                assertEquals("###", tabular.repChar('#', 3));
                assertEquals("------", tabular.repChar('-', 6));
        }
                assertEquals("###", tabular.repChar('#', 3));
                assertEquals("------", tabular.repChar('-', 6));
        }
-
-       @Test
-       public void test_centrePad() {
-               Tabular tabular = new TabularMock();
+       
+       private TabularMock createEmpTable()
+       {
+               TabularMock tabular = new TabularMock();
                
                
-               assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
-               assertEquals("  Fred  ", tabular.centrePad("Fred", 8));
-               assertEquals("NULL", tabular.centrePad(null, 0));
-               assertEquals("  NULL  ", tabular.centrePad(null, 8));
-               assertEquals("   Wilma    ", tabular.centrePad("Wilma", 12));
+               Column[] cols = { 
+                               new Column("EmpId", Long.class, 10),
+                               new Column("FirstName", String.class, 14),
+                               new Column("LastName", String.class, 14)
+               };
+               tabular.mock_setCols(cols);
+               
+               Row row = new Row(cols.length);
+               row.setValue(1, Long.valueOf(12345));
+               row.setValue(2, "Fred");
+               row.setValue(3, "Flintstone");
+               tabular.mock_addRow(row);
+               
+               row = new Row(cols.length);
+               row.setValue(1, Long.valueOf(7654321));
+               row.setValue(2, "Barney");
+               row.setValue(3, "Rubble");
+               tabular.mock_addRow(row);
+               
+               return tabular;
        }
 }
        }
 }