d864b6dc8681b9ae3de5d32b6603329385cc83ca
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 import java.io.PrintWriter;
4 import java.sql.Connection;
5 import java.sql.SQLException;
6
7 import org.apache.commons.cli.CommandLine;
8 import org.apache.commons.cli.GnuParser;
9 import org.apache.commons.cli.HelpFormatter;
10 import org.apache.commons.cli.Options;
11 import org.apache.commons.cli.ParseException;
12
13 import net.jaekl.cfb.db.CfbSchema;
14 import net.jaekl.cfb.db.driver.DbDriver;
15 import net.jaekl.cfb.db.driver.PostgresqlDriver;
16
17 public class CFB {
18         DbDriver m_driver;
19         CfbSchema m_schema;
20         
21         // Command-line parameters
22         String m_dbName; // db name
23         String m_host;  // db host
24         int m_port;             // db port
25         String m_user;  // db user
26         String m_pass;  // db password
27         
28         CFB() {
29                 m_driver = new PostgresqlDriver();
30                 m_schema = new CfbSchema(m_driver);
31                 
32                 m_dbName = "CFB";
33                 m_host = "localhost";
34                 m_port = 5432;
35                 m_pass = "";
36                 m_user = "user";
37         }
38         
39         Options createOptions() {
40                 Options opt = new Options();
41                 
42                 opt.addOption("d", "dbname", true, "DB name");
43                 opt.addOption("h", "host", true, "DB hostname");
44                 opt.addOption("p", "pass", true, "DB password");
45                 opt.addOption("t", "port", true, "DB port");
46                 opt.addOption("u", "user", true, "DB username");
47                 
48                 return opt;
49         }
50         
51         boolean parseArgs(PrintWriter pw, String[] args) {
52                 Options opt = createOptions();
53                 
54                 try {
55                         CommandLine line = new GnuParser().parse(opt, args);
56                         if (line.hasOption("d")) {
57                                 m_dbName = line.getOptionValue("d");
58                         }
59                         if (line.hasOption("h")) {
60                                 m_host = line.getOptionValue("h");
61                         }
62                         if (line.hasOption("p")) {
63                                 m_pass = line.getOptionValue("p");
64                         }
65                         if (line.hasOption("t")) {
66                                 m_port = Integer.parseInt(line.getOptionValue("t"));
67                         }
68                         if (line.hasOption("u")) {
69                                 m_user = line.getOptionValue("u");
70                         }
71                 } 
72                 catch (ParseException exc) {
73                         usage(pw, opt);
74                         return false;
75                 }
76                 
77                 return true;
78         }
79         
80         void usage(PrintWriter pw, Options opt) {
81                 HelpFormatter help = new HelpFormatter();
82                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
83         }
84         
85         void doMain(PrintWriter pw, String[] args) throws SQLException {
86                 if ( ! parseArgs(pw, args) ) {
87                         return;
88                 }
89                 
90                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
91                         if (null == con) {
92                                 // TODO:  string table
93                                 pw.println("FATAL:  Cannot connect to db.");
94                                 return;
95                         }
96                         m_schema.ensureDbInitialized(con);                      
97                 }
98         }
99         
100         public static void main(String[] args) {
101                 CFB cfb = new CFB();
102                 
103                 try (PrintWriter pw = new PrintWriter(System.out)){
104                         cfb.doMain(pw, args);
105                 } catch (SQLException exc) {
106                         exc.printStackTrace();
107                 }
108         }
109
110 }