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;
new PostgresqlDriver()
};
private static final Stmt[] READ_ONLY_STATEMENTS = {
+ new Describe(),
new Select()
};
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());
}
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
- pw.println("??? \"" + line + "\"");
+ 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();
}
}
}