1 package net.jaekl.squelch;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.Connection;
6 import java.sql.SQLException;
7 import java.util.Locale;
9 import net.jaekl.squelch.db.DbDriver;
10 import net.jaekl.squelch.db.MsSqlDriver;
11 import net.jaekl.squelch.db.MySqlDriver;
12 import net.jaekl.squelch.db.OracleDriver;
13 import net.jaekl.squelch.db.PostgresqlDriver;
14 import net.jaekl.squelch.stmt.Select;
15 import net.jaekl.squelch.stmt.Stmt;
16 import net.jaekl.squelch.util.ConsoleInput;
17 import net.jaekl.squelch.util.ConsoleInputImpl;
18 import net.jaekl.squelch.util.ConsoleUtil;
20 public class Squelch {
21 static final String PROMPT = "> ";
22 private static final DbDriver[] DB_DRIVERS = {
26 new PostgresqlDriver()
28 private static final Stmt[] READ_ONLY_STATEMENTS = {
33 private Stmt[] m_statements;
36 m_args = new Args(this.getClass().getName());
37 m_statements = READ_ONLY_STATEMENTS;
40 public void doMain(String[] params)
42 try (PrintWriter pw = new PrintWriter(System.out))
44 if (!m_args.parseArgs(pw, params)) {
48 ConsoleInputImpl ci = ConsoleUtil.getInst().getInput();
50 } catch (IOException | ClassNotFoundException | SQLException | SquelchException e) {
55 public static void main(String[] args) {
56 new Squelch().doMain(args);
59 boolean isQuit(String line)
64 String trimmed = line.trim();
65 if (trimmed.endsWith(";")) {
66 trimmed = trimmed.substring(0, trimmed.length() - 1).trim();
68 String upperCased = trimmed.toUpperCase(Locale.CANADA);
70 if ( "EXIT".equals(upperCased)
71 || "QUIT".equals(upperCased)
72 || "\\q".equals(trimmed) )
79 Connection getConnection() throws ClassNotFoundException, SQLException, SquelchException
81 String jdbcUrl = m_args.getUrl();
82 DbDriver driver = getDriverFor(jdbcUrl);
84 return driver.connect(jdbcUrl, m_args.getUser(), m_args.getPass());
87 DbDriver getDriverFor(String jdbcUrl) throws SquelchException
89 for (DbDriver driver : DB_DRIVERS) {
90 if (driver.handles(jdbcUrl)) {
94 throw new SquelchException("Cannot determine DB Driver for JDBC URL: \"" + jdbcUrl + "\".");
97 void pumpLines(PrintWriter pw, ConsoleInput ci) throws IOException, ClassNotFoundException, SQLException, SquelchException
100 try (Connection conn = getConnection())
103 boolean processed = false;
104 line = ci.readLine(PROMPT);
106 for (Stmt statement : m_statements) {
107 if (statement.handles(line)) {
108 statement.exec(conn, pw, line);
118 // Unrecognized command
119 // TODO: add a string table, and a natural-language error message.
120 pw.println("??? \"" + line + "\"");