Add DbDriver, with support for a few popular JDBC drivers.
[squelch.git] / src / main / java / net / jaekl / squelch / Squelch.java
index 5985338bf00a6f6b22bb8e03110b7337ed9e4873..383b541debbe58dcd6251e7ae3638c32c0edf043 100644 (file)
@@ -2,8 +2,15 @@ package net.jaekl.squelch;
 
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.Locale;
 
+import net.jaekl.squelch.db.DbDriver;
+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.Select;
 import net.jaekl.squelch.stmt.Stmt;
 import net.jaekl.squelch.util.ConsoleInput;
@@ -12,24 +19,35 @@ import net.jaekl.squelch.util.ConsoleUtil;
 
 public class Squelch {
        static final String PROMPT = "> ";
+       private static final DbDriver[] DB_DRIVERS = {
+               new MsSqlDriver(),
+               new MySqlDriver(),
+               new OracleDriver(),
+               new PostgresqlDriver()
+       };
        private static final Stmt[] READ_ONLY_STATEMENTS = {
                new Select()
        };
        
+       private Args m_args;
        private Stmt[] m_statements;
 
        public Squelch() {
+               m_args = new Args(this.getClass().getName());
                m_statements = READ_ONLY_STATEMENTS;
        }
        
-       public void doMain(String[] args)
+       public void doMain(String[] params)
        {
-               ConsoleInputImpl ci;
                try (PrintWriter pw = new PrintWriter(System.out))
                {
-                       ci = ConsoleUtil.getInst().getInput();
+                       if (!m_args.parseArgs(pw, params)) {
+                               return;
+                       }
+                       
+                       ConsoleInputImpl ci = ConsoleUtil.getInst().getInput();
                        pumpLines(pw, ci);                      
-               } catch (IOException e) {
+               } catch (IOException | ClassNotFoundException | SQLException | SquelchException e) {
                        e.printStackTrace();
                }
        }
@@ -38,31 +56,8 @@ public class Squelch {
                new Squelch().doMain(args);
        }
        
-       void pumpLines(PrintWriter pw, ConsoleInput ci) throws IOException {
-               String line = null;
-               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;
-                               }
-                       }
-                       
-                       if ((!processed)) {
-                               if (isQuit(line)) {
-                                       break;
-                               }
-                               // Unrecognized command
-                               pw.println("??? \"" + line + "\"");
-                       }
-               }
-       }
-       
-       boolean isQuit(String line) {
+       boolean isQuit(String line) 
+       {
                if ((null == line)) {
                        return true;
                }
@@ -80,4 +75,50 @@ public class Squelch {
                }
                return false;
        }
+       
+       Connection getConnection() throws ClassNotFoundException, SQLException, SquelchException 
+       {
+               String jdbcUrl = m_args.getUrl();
+               DbDriver driver = getDriverFor(jdbcUrl);
+
+               return driver.connect(jdbcUrl, m_args.getUser(), m_args.getPass());
+       }
+       
+       DbDriver getDriverFor(String jdbcUrl) throws SquelchException 
+       {
+               for (DbDriver driver : DB_DRIVERS) {
+                       if (driver.handles(jdbcUrl)) {
+                               return driver;
+                       }
+               }
+               throw new SquelchException("Cannot determine DB Driver for JDBC URL:  \"" + jdbcUrl + "\".");
+       }
+       
+       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;
+                                       }
+                               }
+                               
+                               if ((!processed)) {
+                                       if (isQuit(line)) {
+                                               break;
+                                       }
+                                       // Unrecognized command
+                                       pw.println("??? \"" + line + "\"");
+                               }
+                       }
+               }
+       }       
 }