19 |
19 |
20 import info.globalcode.sql.dk.configuration.ConfigurationProvider; |
20 import info.globalcode.sql.dk.configuration.ConfigurationProvider; |
21 import info.globalcode.sql.dk.CLIOptions.MODE; |
21 import info.globalcode.sql.dk.CLIOptions.MODE; |
22 import info.globalcode.sql.dk.configuration.Configuration; |
22 import info.globalcode.sql.dk.configuration.Configuration; |
23 import info.globalcode.sql.dk.configuration.ConfigurationException; |
23 import info.globalcode.sql.dk.configuration.ConfigurationException; |
|
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
|
25 import info.globalcode.sql.dk.configuration.FormatterDefinition; |
|
26 import info.globalcode.sql.dk.formatting.Formatter; |
|
27 import info.globalcode.sql.dk.formatting.FormatterContext; |
|
28 import info.globalcode.sql.dk.formatting.FormatterException; |
24 import java.io.IOException; |
29 import java.io.IOException; |
|
30 import java.sql.SQLException; |
25 import java.util.logging.Level; |
31 import java.util.logging.Level; |
26 import java.util.logging.Logger; |
32 import java.util.logging.Logger; |
27 import javax.xml.bind.JAXBContext; |
33 import javax.xml.bind.JAXBContext; |
28 import javax.xml.bind.Unmarshaller; |
34 import javax.xml.bind.Unmarshaller; |
29 |
35 |
52 starter.process(); |
58 starter.process(); |
53 } catch (CLIParserException e) { |
59 } catch (CLIParserException e) { |
54 log.log(Level.SEVERE, "Unable to parse CLI options", e); |
60 log.log(Level.SEVERE, "Unable to parse CLI options", e); |
55 } catch (InvalidOptionsException e) { |
61 } catch (InvalidOptionsException e) { |
56 log.log(Level.SEVERE, "Invalid CLI options", e); |
62 log.log(Level.SEVERE, "Invalid CLI options", e); |
|
63 } catch (ConfigurationException e) { |
|
64 log.log(Level.SEVERE, "Configuration problem", e); |
|
65 } catch (SQLException e) { |
|
66 log.log(Level.SEVERE, "SQL problem", e); |
|
67 } catch (FormatterException e) { |
|
68 log.log(Level.SEVERE, "Formatting problem", e); |
57 } |
69 } |
58 } |
70 } |
59 |
71 |
60 public CLIStarter(CLIOptions options) { |
72 public CLIStarter(CLIOptions options) { |
61 this.options = options; |
73 this.options = options; |
62 } |
74 } |
63 |
75 |
64 private void process() { |
76 private void process() throws ConfigurationException, SQLException, FormatterException { |
65 /** Show info */ |
77 /** Show info */ |
66 if (!options.getShowInfo().isEmpty()) { |
78 if (!options.getShowInfo().isEmpty()) { |
67 InfoLister infoLister = new InfoLister(System.err, this); |
79 InfoLister infoLister = new InfoLister(System.err, this); |
68 infoLister.showInfo(options); |
80 infoLister.showInfo(options); |
69 } |
81 } |
70 |
82 |
71 MODE mode = options.getMode(); |
83 MODE mode = options.getMode(); |
72 switch (mode) { |
84 switch (mode) { |
73 case QUERY_NOW: |
85 case QUERY_NOW: |
|
86 processQueryNow(); |
74 break; |
87 break; |
75 case PREPARE_BATCH: |
88 case PREPARE_BATCH: |
|
89 processPrepareBatch(); |
76 break; |
90 break; |
77 case EXECUTE_BATCH: |
91 case EXECUTE_BATCH: |
|
92 processExecuteBatch(); |
78 break; |
93 break; |
79 case JUST_SHOW_INFO: |
94 case JUST_SHOW_INFO: |
80 // already done above |
95 // already done above |
81 break; |
96 break; |
82 default: |
97 default: |
83 log.log(Level.SEVERE, "Unsupported mode: {0}", mode); |
98 log.log(Level.SEVERE, "Unsupported mode: {0}", mode); |
84 break; |
99 break; |
85 } |
100 } |
86 } |
101 } |
87 |
102 |
|
103 private void processQueryNow() throws ConfigurationException, SQLException, FormatterException { |
|
104 DatabaseDefinition dd = getConfiguration().getDatabase(options.getDatabaseName()); |
|
105 if (dd == null) { |
|
106 throw new ConfigurationException("Database is not configured: " + options.getDatabaseName()); |
|
107 } else { |
|
108 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName()); |
|
109 if (fd == null) { |
|
110 throw new ConfigurationException("Formatter is not configured: " + options.getDatabaseName()); |
|
111 } else { |
|
112 DatabaseConnection c = dd.connect(); |
|
113 Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream())); |
|
114 c.executeQuery(options.getSQLCommand(), f); |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 private void processPrepareBatch() { |
|
120 } |
|
121 |
|
122 private void processExecuteBatch() { |
|
123 } |
|
124 |
88 @Override |
125 @Override |
89 public Configuration getConfiguration() throws ConfigurationException { |
126 public Configuration getConfiguration() throws ConfigurationException { |
90 if (configuration == null) { |
127 if (configuration == null) { |
91 configuration = loadConfiguration(); |
128 configuration = loadConfiguration(); |
92 } |
129 } |
93 return configuration; |
130 return configuration; |
94 } |
131 } |
95 |
132 |
96 private void installDefaultConfiguration() { |
133 private void installDefaultConfiguration() throws ConfigurationException { |
97 Constants.DIR.mkdir(); |
134 Constants.DIR.mkdir(); |
98 |
135 |
99 if (Constants.CONFIG_FILE.exists()) { |
136 if (Constants.CONFIG_FILE.exists()) { |
100 log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE); |
137 log.log(Level.FINE, "Config file already exists: {0}", Constants.CONFIG_FILE); |
101 } else { |
138 } else { |
102 try { |
139 try { |
103 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE); |
140 Functions.installResource(Constants.EXAMPLE_CONFIG_FILE, Constants.CONFIG_FILE); |
104 } catch (IOException e) { |
141 } catch (IOException e) { |
105 log.log(Level.SEVERE, "Unable to write example configuration to " + Constants.CONFIG_FILE, e); |
142 throw new ConfigurationException("Unable to write example configuration to " + Constants.CONFIG_FILE, e); |
106 } |
143 } |
107 } |
144 } |
108 |
|
109 } |
145 } |
110 |
146 |
111 private Configuration loadConfiguration() throws ConfigurationException { |
147 private Configuration loadConfiguration() throws ConfigurationException { |
112 try { |
148 try { |
113 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class); |
149 JAXBContext jaxb = JAXBContext.newInstance(Configuration.class); |