adds support for null suppression
[squelch.git] / src / main / java / net / jaekl / squelch / Squelch.java
index 88515e0bd6502b11ed87f9429bdc573991478ebf..953a1790031f93b05722a3f7ca3517bb725daa61 100644 (file)
@@ -11,6 +11,8 @@ 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.PSet;
 import net.jaekl.squelch.stmt.Select;
 import net.jaekl.squelch.stmt.Stmt;
 import net.jaekl.squelch.util.ConsoleInput;
@@ -26,6 +28,8 @@ public class Squelch {
                new PostgresqlDriver()
        };
        private static final Stmt[] READ_ONLY_STATEMENTS = {
+               new Describe(),
+               new PSet(),
                new Select()
        };
        
@@ -76,11 +80,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,30 +99,35 @@ public class Squelch {
        void pumpLines(PrintWriter pw, ConsoleInput ci) throws IOException, ClassNotFoundException, SQLException, SquelchException 
        {
                String line = null;
-               try (Connection conn = getConnection())
-               {
-                       while (true) {
-                               boolean processed = false;
-                               line = ci.readLine(PROMPT);
-                               
-                               for (Stmt statement : m_statements) {
-                                       if (statement.handles(line)) {
-                                               statement.exec(conn, pw, line);
-                                               processed = true;
-                                               break;
+               String jdbcUrl = m_args.getUrl();
+               DbDriver driver = getDriverFor(jdbcUrl);
+
+               while (true) {
+                       boolean processed = false;
+                       line = ci.readLine(PROMPT);
+                       
+                       for (Stmt statement : m_statements) {
+                               if (statement.handles(line)) {
+                                       try (Connection conn = getConnection(driver, jdbcUrl)){
+                                               statement.exec(driver, conn, pw, line);
                                        }
-                               }
-                               
-                               if ((!processed)) {
-                                       if (isQuit(line)) {
-                                               break;
+                                       catch (SQLException exc) {
+                                               exc.printStackTrace(pw);
                                        }
-                                       // Unrecognized command
-                                       // TODO:  add a string table, and a natural-language error message.
-                                       pw.println("??? \"" + line + "\"");
-                                       pw.flush();
+                                       processed = true;
+                                       break;
+                               }
+                       }
+                       
+                       if ((!processed)) {
+                               if (isQuit(line)) {
+                                       break;
                                }
+                               // Unrecognized command
+                               // TODO:  add a string table, and a natural-language error message.
+                               pw.println("??? \"" + line + "\"");
                        }
+                       pw.flush();
                }
        }       
 }