Add "Describe" command, with support for describing both (a) specific table(s) and...
[squelch.git] / src / main / java / net / jaekl / squelch / Squelch.java
index 88515e0bd6502b11ed87f9429bdc573991478ebf..848ede7db05c9a5d88c783f66bd250a7c4211e55 100644 (file)
@@ -11,6 +11,7 @@ import net.jaekl.squelch.db.MsSqlDriver;
 import net.jaekl.squelch.db.MySqlDriver;
 import net.jaekl.squelch.db.OracleDriver;
 import net.jaekl.squelch.db.PostgresqlDriver;
+import net.jaekl.squelch.stmt.Describe;
 import net.jaekl.squelch.stmt.Select;
 import net.jaekl.squelch.stmt.Stmt;
 import net.jaekl.squelch.util.ConsoleInput;
@@ -26,6 +27,7 @@ public class Squelch {
                new PostgresqlDriver()
        };
        private static final Stmt[] READ_ONLY_STATEMENTS = {
+               new Describe(),
                new Select()
        };
        
@@ -76,11 +78,9 @@ public class Squelch {
                return false;
        }
        
-       Connection getConnection() throws ClassNotFoundException, SQLException, SquelchException 
+       Connection getConnection(DbDriver driver, String jdbcUrl) 
+               throws ClassNotFoundException, SQLException, SquelchException 
        {
-               String jdbcUrl = m_args.getUrl();
-               DbDriver driver = getDriverFor(jdbcUrl);
-
                return driver.connect(jdbcUrl, m_args.getUser(), m_args.getPass());
        }
        
@@ -97,7 +97,10 @@ public class Squelch {
        void pumpLines(PrintWriter pw, ConsoleInput ci) throws IOException, ClassNotFoundException, SQLException, SquelchException 
        {
                String line = null;
-               try (Connection conn = getConnection())
+               String jdbcUrl = m_args.getUrl();
+               DbDriver driver = getDriverFor(jdbcUrl);
+
+               try (Connection conn = getConnection(driver, jdbcUrl))
                {
                        while (true) {
                                boolean processed = false;
@@ -105,7 +108,12 @@ public class Squelch {
                                
                                for (Stmt statement : m_statements) {
                                        if (statement.handles(line)) {
-                                               statement.exec(conn, pw, line);
+                                               try {
+                                                       statement.exec(driver, conn, pw, line);
+                                               }
+                                               catch (SQLException exc) {
+                                                       exc.printStackTrace(pw);
+                                               }
                                                processed = true;
                                                break;
                                        }
@@ -118,8 +126,8 @@ public class Squelch {
                                        // Unrecognized command
                                        // TODO:  add a string table, and a natural-language error message.
                                        pw.println("??? \"" + line + "\"");
-                                       pw.flush();
                                }
+                               pw.flush();
                        }
                }
        }