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;
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();
}
}
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;
}
}
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 + "\"");
+ }
+ }
+ }
+ }
}