Add "Describe" command, with support for describing both (a) specific table(s) and...
[squelch.git] / src / main / java / net / jaekl / squelch / stmt / Select.java
index 6d89af5c293c6dbf237e3f8dadf7b170013c7bb7..c5f785e5a3d6acd651bc30b3822f362a255f5f30 100644 (file)
@@ -5,11 +5,10 @@ import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.util.Locale;
 
-import net.jaekl.squelch.sql.Column;
+import net.jaekl.squelch.db.DbDriver;
 
 public class Select extends Query {
 
@@ -24,63 +23,33 @@ public class Select extends Query {
        }
 
        @Override
-       public int exec(Connection conn, PrintWriter pw, String line) throws IOException, SQLException 
+       public int exec(DbDriver driver, Connection conn, PrintWriter pw, String line) throws IOException, SQLException 
        {
                int rowCount = 0;
                
-               try (PreparedStatement ps = conn.prepareStatement(line)) 
+               // If there's a ';' on the end of this line, remove it.
+               String trimmed = line.trim();
+               if (trimmed.endsWith(";")) {
+                       trimmed = trimmed.substring(0, trimmed.length() - 1);
+               }
+               
+               try (PreparedStatement ps = conn.prepareStatement(trimmed)) 
                {
                        try (ResultSet rs = ps.executeQuery()) 
                        {
-                               rowCount = printFormatted(pw, rs);
-                               
+                               rowCount = printFormatted(pw, rs);              
                        }
                }
                return rowCount;
        }
-
-       private Column[] getColumns(ResultSetMetaData metaData) 
-                       throws SQLException, ClassNotFoundException 
-       {
-               int colCount = metaData.getColumnCount();
-               
-               Column cols[] = new Column[colCount];
-               for (int idx = 1; idx <= colCount; ++idx) {
-                       String label = metaData.getColumnLabel(idx);
-                       Class<?> clazz = Class.forName(metaData.getColumnTypeName(idx));
-                       int width = metaData.getColumnDisplaySize(idx);
-                       cols[idx] = new Column(label, clazz, width);
-               }
-               
-               return cols;
-       }
-
+       
        private int printFormatted(PrintWriter pw, ResultSet rs) throws IOException, SQLException
        {
-               try {
-                       ResultSetMetaData metaData = rs.getMetaData();
-                       int rowCount = 0;
-                       Column[] cols = getColumns(metaData);
-                       
-                       pw.println("----------------------------------");
-                       for (int idx = 1; idx <= cols.length; ++idx) {
-                               pw.print("|" + cols[idx - 1].getLabel());
-                       }
-                       pw.println("|");
-                       
-                       while (rs.next()) {
-                               for (int idx = 1; idx <= cols.length; ++idx) {
-                                       pw.print("|" + rs.getObject(idx));
-                               }
-                               pw.println("|");
-                       }
-                       pw.println("----------------------------------");
-                       pw.println("" + rowCount + " row(s) returned.");
-                       
-                       return rowCount;
-               }
-               catch (ClassNotFoundException exc) {
-                       throw new SQLException(exc);
-               }
+               TabularResultSet trs = new TabularResultSet(rs);
+               // TODO:  StringTable i18n
+               int rowCount = trs.printTable(pw, "No rows returned.");
+               pw.flush();
+               
+               return rowCount;
        }
 }