5985338bf00a6f6b22bb8e03110b7337ed9e4873
[squelch.git] / src / main / java / net / jaekl / squelch / Squelch.java
1 package net.jaekl.squelch;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Locale;
6
7 import net.jaekl.squelch.stmt.Select;
8 import net.jaekl.squelch.stmt.Stmt;
9 import net.jaekl.squelch.util.ConsoleInput;
10 import net.jaekl.squelch.util.ConsoleInputImpl;
11 import net.jaekl.squelch.util.ConsoleUtil;
12
13 public class Squelch {
14         static final String PROMPT = "> ";
15         private static final Stmt[] READ_ONLY_STATEMENTS = {
16                 new Select()
17         };
18         
19         private Stmt[] m_statements;
20
21         public Squelch() {
22                 m_statements = READ_ONLY_STATEMENTS;
23         }
24         
25         public void doMain(String[] args)
26         {
27                 ConsoleInputImpl ci;
28                 try (PrintWriter pw = new PrintWriter(System.out))
29                 {
30                         ci = ConsoleUtil.getInst().getInput();
31                         pumpLines(pw, ci);                      
32                 } catch (IOException e) {
33                         e.printStackTrace();
34                 }
35         }
36         
37         public static void main(String[] args) {
38                 new Squelch().doMain(args);
39         }
40         
41         void pumpLines(PrintWriter pw, ConsoleInput ci) throws IOException {
42                 String line = null;
43                 while (true) {
44                         boolean processed = false;
45                         line = ci.readLine(PROMPT);
46                         
47                         for (Stmt statement : m_statements) {
48                                 if (statement.handles(line)) {
49                                         //statement.exec(conn, pw, line);
50                                         processed = true;
51                                         break;
52                                 }
53                         }
54                         
55                         if ((!processed)) {
56                                 if (isQuit(line)) {
57                                         break;
58                                 }
59                                 // Unrecognized command
60                                 pw.println("??? \"" + line + "\"");
61                         }
62                 }
63         }
64         
65         boolean isQuit(String line) {
66                 if ((null == line)) {
67                         return true;
68                 }
69                 String trimmed = line.trim();
70                 if (trimmed.endsWith(";")) {
71                         trimmed = trimmed.substring(0, trimmed.length() - 1).trim();
72                 }
73                 String upperCased = trimmed.toUpperCase(Locale.CANADA);
74                 
75                 if (  "EXIT".equals(upperCased)
76                    || "QUIT".equals(upperCased)
77                    || "\\q".equals(trimmed) ) 
78                 {
79                         return true;
80                 }
81                 return false;
82         }
83 }