franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@14
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@14
|
19 |
|
franta-hg@67
|
20 |
import info.globalcode.sql.dk.configuration.Configuration;
|
franta-hg@65
|
21 |
import info.globalcode.sql.dk.configuration.ConfigurationException;
|
franta-hg@26
|
22 |
import info.globalcode.sql.dk.configuration.ConfigurationProvider;
|
franta-hg@65
|
23 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@67
|
24 |
import info.globalcode.sql.dk.configuration.FormatterDefinition;
|
franta-hg@69
|
25 |
import info.globalcode.sql.dk.formatting.ColumnsHeader;
|
franta-hg@69
|
26 |
import info.globalcode.sql.dk.formatting.Formatter;
|
franta-hg@69
|
27 |
import info.globalcode.sql.dk.formatting.FormatterContext;
|
franta-hg@69
|
28 |
import info.globalcode.sql.dk.formatting.FormatterException;
|
franta-hg@17
|
29 |
import java.io.BufferedReader;
|
franta-hg@17
|
30 |
import java.io.InputStreamReader;
|
franta-hg@14
|
31 |
import java.io.PrintStream;
|
franta-hg@158
|
32 |
import java.sql.Driver;
|
franta-hg@65
|
33 |
import java.sql.SQLException;
|
franta-hg@70
|
34 |
import java.util.ArrayList;
|
franta-hg@70
|
35 |
import java.util.EnumSet;
|
franta-hg@66
|
36 |
import java.util.List;
|
franta-hg@158
|
37 |
import java.util.ServiceLoader;
|
franta-hg@17
|
38 |
import java.util.logging.Level;
|
franta-hg@17
|
39 |
import java.util.logging.Logger;
|
franta-hg@69
|
40 |
import javax.sql.rowset.RowSetMetaDataImpl;
|
franta-hg@14
|
41 |
|
franta-hg@14
|
42 |
/**
|
franta-hg@14
|
43 |
* Displays info like help, version etc.
|
franta-hg@14
|
44 |
*
|
franta-hg@14
|
45 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@14
|
46 |
*/
|
franta-hg@14
|
47 |
public class InfoLister {
|
franta-hg@14
|
48 |
|
franta-hg@17
|
49 |
private static final Logger log = Logger.getLogger(InfoLister.class.getName());
|
franta-hg@17
|
50 |
private PrintStream out;
|
franta-hg@20
|
51 |
private ConfigurationProvider configurationProvider;
|
franta-hg@69
|
52 |
private CLIOptions options;
|
franta-hg@70
|
53 |
private Formatter formatter;
|
franta-hg@17
|
54 |
|
franta-hg@69
|
55 |
public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
|
franta-hg@17
|
56 |
this.out = out;
|
franta-hg@20
|
57 |
this.configurationProvider = configurationProvider;
|
franta-hg@69
|
58 |
this.options = options;
|
franta-hg@17
|
59 |
}
|
franta-hg@17
|
60 |
|
franta-hg@70
|
61 |
public void showInfo() throws ConfigurationException, FormatterException {
|
franta-hg@70
|
62 |
EnumSet<InfoType> commands = options.getShowInfo();
|
franta-hg@70
|
63 |
|
franta-hg@139
|
64 |
boolean formattinNeeded = false;
|
franta-hg@139
|
65 |
|
franta-hg@70
|
66 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
67 |
switch (infoType) {
|
franta-hg@70
|
68 |
case CONNECTION:
|
franta-hg@158
|
69 |
case JDBC_DRIVERS:
|
franta-hg@70
|
70 |
case DATABASES:
|
franta-hg@70
|
71 |
case FORMATTERS:
|
franta-hg@70
|
72 |
case TYPES:
|
franta-hg@139
|
73 |
formattinNeeded = true;
|
franta-hg@101
|
74 |
break;
|
franta-hg@70
|
75 |
}
|
franta-hg@70
|
76 |
}
|
franta-hg@139
|
77 |
|
franta-hg@139
|
78 |
if (formattinNeeded) {
|
franta-hg@139
|
79 |
try (Formatter f = getFormatter()) {
|
franta-hg@139
|
80 |
formatter = f;
|
franta-hg@139
|
81 |
formatter.writeStartBatch();
|
franta-hg@139
|
82 |
formatter.writeStartDatabase(new DatabaseDefinition());
|
franta-hg@142
|
83 |
formatter.writeStartStatement();
|
franta-hg@139
|
84 |
showInfos(commands);
|
franta-hg@142
|
85 |
formatter.writeEndStatement();
|
franta-hg@139
|
86 |
formatter.writeEndDatabase();
|
franta-hg@139
|
87 |
formatter.writeEndBatch();
|
franta-hg@139
|
88 |
formatter.close();
|
franta-hg@139
|
89 |
}
|
franta-hg@139
|
90 |
} else {
|
franta-hg@139
|
91 |
showInfos(commands);
|
franta-hg@139
|
92 |
}
|
franta-hg@101
|
93 |
}
|
franta-hg@70
|
94 |
|
franta-hg@101
|
95 |
private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
|
franta-hg@70
|
96 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
97 |
infoType.showInfo(this);
|
franta-hg@70
|
98 |
}
|
franta-hg@70
|
99 |
}
|
franta-hg@70
|
100 |
|
franta-hg@72
|
101 |
private void listFormatters() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
102 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
103 |
new HeaderField("name", SQLType.VARCHAR),
|
franta-hg@72
|
104 |
new HeaderField("built_in", SQLType.BOOLEAN),
|
franta-hg@72
|
105 |
new HeaderField("default", SQLType.BOOLEAN),
|
franta-hg@72
|
106 |
new HeaderField("class_name", SQLType.VARCHAR));
|
franta-hg@72
|
107 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
108 |
|
franta-hg@72
|
109 |
String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
|
franta-hg@72
|
110 |
defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
|
franta-hg@72
|
111 |
|
franta-hg@69
|
112 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
|
franta-hg@72
|
113 |
data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName()});
|
franta-hg@69
|
114 |
}
|
franta-hg@72
|
115 |
|
franta-hg@72
|
116 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
|
franta-hg@72
|
117 |
data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName()});
|
franta-hg@69
|
118 |
}
|
franta-hg@72
|
119 |
|
franta-hg@72
|
120 |
printTable(formatter, header, data);
|
franta-hg@72
|
121 |
|
franta-hg@72
|
122 |
|
franta-hg@69
|
123 |
}
|
franta-hg@67
|
124 |
|
franta-hg@70
|
125 |
public void listTypes() throws FormatterException, ConfigurationException {
|
franta-hg@70
|
126 |
ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
|
franta-hg@70
|
127 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@70
|
128 |
for (SQLType sqlType : SQLType.values()) {
|
franta-hg@70
|
129 |
data.add(new Object[]{sqlType.name(), sqlType.getCode()});
|
franta-hg@70
|
130 |
}
|
franta-hg@70
|
131 |
printTable(formatter, header, data);
|
franta-hg@93
|
132 |
log.log(Level.INFO, "Type names in --types option are case insensitive");
|
franta-hg@69
|
133 |
}
|
franta-hg@67
|
134 |
|
franta-hg@72
|
135 |
public void listDatabases() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
136 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
137 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@72
|
138 |
new HeaderField("user_name", SQLType.VARCHAR),
|
franta-hg@72
|
139 |
new HeaderField("database_url", SQLType.VARCHAR));
|
franta-hg@72
|
140 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
141 |
|
franta-hg@69
|
142 |
final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
|
franta-hg@69
|
143 |
if (configuredDatabases.isEmpty()) {
|
franta-hg@69
|
144 |
log.log(Level.WARNING, "No databases are configured.");
|
franta-hg@69
|
145 |
} else {
|
franta-hg@69
|
146 |
for (DatabaseDefinition dd : configuredDatabases) {
|
franta-hg@72
|
147 |
data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
|
franta-hg@14
|
148 |
}
|
franta-hg@14
|
149 |
}
|
franta-hg@72
|
150 |
|
franta-hg@72
|
151 |
printTable(formatter, header, data);
|
franta-hg@14
|
152 |
}
|
franta-hg@17
|
153 |
|
franta-hg@158
|
154 |
public void listJdbcDrivers() throws FormatterException, ConfigurationException {
|
franta-hg@158
|
155 |
ColumnsHeader header = constructHeader(
|
franta-hg@158
|
156 |
new HeaderField("class", SQLType.VARCHAR),
|
franta-hg@158
|
157 |
new HeaderField("version", SQLType.VARCHAR),
|
franta-hg@158
|
158 |
new HeaderField("major", SQLType.INTEGER),
|
franta-hg@158
|
159 |
new HeaderField("minor", SQLType.INTEGER),
|
franta-hg@158
|
160 |
new HeaderField("jdbc_compliant", SQLType.BOOLEAN));
|
franta-hg@158
|
161 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@158
|
162 |
|
franta-hg@158
|
163 |
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
|
franta-hg@158
|
164 |
for (Driver d : drivers) {
|
franta-hg@158
|
165 |
data.add(new Object[]{
|
franta-hg@158
|
166 |
d.getClass().getName(),
|
franta-hg@158
|
167 |
d.getMajorVersion() + "." + d.getMinorVersion(),
|
franta-hg@158
|
168 |
d.getMajorVersion(),
|
franta-hg@158
|
169 |
d.getMinorVersion(),
|
franta-hg@158
|
170 |
d.jdbcCompliant()});
|
franta-hg@158
|
171 |
}
|
franta-hg@158
|
172 |
|
franta-hg@158
|
173 |
printTable(formatter, header, data);
|
franta-hg@158
|
174 |
|
franta-hg@158
|
175 |
}
|
franta-hg@158
|
176 |
|
franta-hg@73
|
177 |
public void testConnection() throws FormatterException, ConfigurationException {
|
franta-hg@73
|
178 |
ColumnsHeader header = constructHeader(
|
franta-hg@73
|
179 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@74
|
180 |
new HeaderField("configured", SQLType.BOOLEAN),
|
franta-hg@74
|
181 |
new HeaderField("connected", SQLType.BOOLEAN));
|
franta-hg@73
|
182 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@73
|
183 |
|
franta-hg@155
|
184 |
for (String dbName : options.getDatabaseNamesToTest()) {
|
franta-hg@74
|
185 |
data.add(testConnection(dbName));
|
franta-hg@74
|
186 |
}
|
franta-hg@73
|
187 |
|
franta-hg@73
|
188 |
printTable(formatter, header, data);
|
franta-hg@73
|
189 |
}
|
franta-hg@73
|
190 |
|
franta-hg@73
|
191 |
public Object[] testConnection(String dbName) {
|
franta-hg@69
|
192 |
log.log(Level.FINE, "Testing connection to database: {0}", dbName);
|
franta-hg@73
|
193 |
|
franta-hg@73
|
194 |
boolean succesfullyConnected = false;
|
franta-hg@73
|
195 |
boolean succesfullyConfigured = false;
|
franta-hg@73
|
196 |
|
franta-hg@69
|
197 |
try {
|
franta-hg@69
|
198 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
|
franta-hg@75
|
199 |
log.log(Level.FINE, "Database definition was loaded from configuration");
|
franta-hg@75
|
200 |
succesfullyConfigured = true;
|
franta-hg@106
|
201 |
try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
|
franta-hg@75
|
202 |
succesfullyConnected = dc.test();
|
franta-hg@69
|
203 |
}
|
franta-hg@75
|
204 |
log.log(Level.FINE, "Database connection test was successful");
|
franta-hg@69
|
205 |
} catch (ConfigurationException | SQLException e) {
|
franta-hg@69
|
206 |
log.log(Level.SEVERE, "Error during testing connection", e);
|
franta-hg@69
|
207 |
}
|
franta-hg@73
|
208 |
|
franta-hg@73
|
209 |
return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
|
franta-hg@69
|
210 |
}
|
franta-hg@69
|
211 |
|
franta-hg@69
|
212 |
public void printResource(String fileName) {
|
franta-hg@18
|
213 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
|
franta-hg@17
|
214 |
while (true) {
|
franta-hg@18
|
215 |
String line = reader.readLine();
|
franta-hg@17
|
216 |
if (line == null) {
|
franta-hg@17
|
217 |
break;
|
franta-hg@17
|
218 |
} else {
|
franta-hg@17
|
219 |
println(line);
|
franta-hg@17
|
220 |
}
|
franta-hg@17
|
221 |
}
|
franta-hg@17
|
222 |
} catch (Exception e) {
|
franta-hg@18
|
223 |
log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
|
franta-hg@17
|
224 |
}
|
franta-hg@17
|
225 |
}
|
franta-hg@17
|
226 |
|
franta-hg@17
|
227 |
private void println(String line) {
|
franta-hg@17
|
228 |
out.println(line);
|
franta-hg@17
|
229 |
}
|
franta-hg@69
|
230 |
|
franta-hg@69
|
231 |
private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data) throws ConfigurationException, FormatterException {
|
franta-hg@142
|
232 |
formatter.writeStartResultSet(header);
|
franta-hg@69
|
233 |
|
franta-hg@69
|
234 |
for (Object[] row : data) {
|
franta-hg@69
|
235 |
formatter.writeStartRow();
|
franta-hg@69
|
236 |
for (Object cell : row) {
|
franta-hg@69
|
237 |
formatter.writeColumnValue(cell);
|
franta-hg@69
|
238 |
}
|
franta-hg@69
|
239 |
formatter.writeEndRow();
|
franta-hg@69
|
240 |
}
|
franta-hg@69
|
241 |
|
franta-hg@69
|
242 |
formatter.writeEndResultSet();
|
franta-hg@69
|
243 |
}
|
franta-hg@69
|
244 |
|
franta-hg@69
|
245 |
private Formatter getFormatter() throws ConfigurationException, FormatterException {
|
franta-hg@89
|
246 |
String formatterName = options.getFormatterName();
|
franta-hg@89
|
247 |
formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
|
franta-hg@89
|
248 |
FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
|
franta-hg@104
|
249 |
FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
|
franta-hg@69
|
250 |
return fd.getInstance(context);
|
franta-hg@69
|
251 |
}
|
franta-hg@69
|
252 |
|
franta-hg@69
|
253 |
private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
|
franta-hg@69
|
254 |
try {
|
franta-hg@69
|
255 |
RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
|
franta-hg@69
|
256 |
metaData.setColumnCount(fields.length);
|
franta-hg@69
|
257 |
|
franta-hg@69
|
258 |
for (int i = 0; i < fields.length; i++) {
|
franta-hg@69
|
259 |
HeaderField hf = fields[i];
|
franta-hg@69
|
260 |
int sqlIndex = i + 1;
|
franta-hg@69
|
261 |
metaData.setColumnName(sqlIndex, hf.name);
|
franta-hg@69
|
262 |
metaData.setColumnLabel(sqlIndex, hf.name);
|
franta-hg@69
|
263 |
metaData.setColumnType(sqlIndex, hf.type.getCode());
|
franta-hg@69
|
264 |
metaData.setColumnTypeName(sqlIndex, hf.type.name());
|
franta-hg@69
|
265 |
}
|
franta-hg@69
|
266 |
|
franta-hg@69
|
267 |
return new ColumnsHeader(metaData);
|
franta-hg@69
|
268 |
} catch (SQLException e) {
|
franta-hg@69
|
269 |
throw new FormatterException("Error while constructing table headers", e);
|
franta-hg@69
|
270 |
}
|
franta-hg@69
|
271 |
}
|
franta-hg@69
|
272 |
|
franta-hg@69
|
273 |
private static class HeaderField {
|
franta-hg@69
|
274 |
|
franta-hg@69
|
275 |
String name;
|
franta-hg@69
|
276 |
SQLType type;
|
franta-hg@69
|
277 |
|
franta-hg@69
|
278 |
public HeaderField(String name, SQLType type) {
|
franta-hg@69
|
279 |
this.name = name;
|
franta-hg@69
|
280 |
this.type = type;
|
franta-hg@69
|
281 |
}
|
franta-hg@69
|
282 |
}
|
franta-hg@69
|
283 |
|
franta-hg@69
|
284 |
public enum InfoType {
|
franta-hg@69
|
285 |
|
franta-hg@69
|
286 |
HELP {
|
franta-hg@69
|
287 |
@Override
|
franta-hg@69
|
288 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
289 |
infoLister.printResource(Constants.HELP_FILE);
|
franta-hg@69
|
290 |
}
|
franta-hg@69
|
291 |
},
|
franta-hg@69
|
292 |
VERSION {
|
franta-hg@69
|
293 |
@Override
|
franta-hg@69
|
294 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
295 |
infoLister.printResource(Constants.VERSION_FILE);
|
franta-hg@69
|
296 |
}
|
franta-hg@69
|
297 |
},
|
franta-hg@69
|
298 |
LICENSE {
|
franta-hg@69
|
299 |
@Override
|
franta-hg@69
|
300 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
301 |
infoLister.printResource(Constants.LICENSE_FILE);
|
franta-hg@69
|
302 |
}
|
franta-hg@69
|
303 |
},
|
franta-hg@69
|
304 |
FORMATTERS {
|
franta-hg@69
|
305 |
@Override
|
franta-hg@70
|
306 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
307 |
infoLister.listFormatters();
|
franta-hg@69
|
308 |
}
|
franta-hg@69
|
309 |
},
|
franta-hg@69
|
310 |
TYPES {
|
franta-hg@69
|
311 |
@Override
|
franta-hg@70
|
312 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
313 |
infoLister.listTypes();
|
franta-hg@69
|
314 |
}
|
franta-hg@69
|
315 |
},
|
franta-hg@158
|
316 |
JDBC_DRIVERS {
|
franta-hg@158
|
317 |
@Override
|
franta-hg@158
|
318 |
public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
|
franta-hg@158
|
319 |
infoLister.listJdbcDrivers();
|
franta-hg@158
|
320 |
}
|
franta-hg@158
|
321 |
},
|
franta-hg@69
|
322 |
DATABASES {
|
franta-hg@69
|
323 |
@Override
|
franta-hg@70
|
324 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
325 |
infoLister.listDatabases();
|
franta-hg@69
|
326 |
}
|
franta-hg@69
|
327 |
},
|
franta-hg@69
|
328 |
CONNECTION {
|
franta-hg@69
|
329 |
@Override
|
franta-hg@73
|
330 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
331 |
infoLister.testConnection();
|
franta-hg@69
|
332 |
}
|
franta-hg@69
|
333 |
};
|
franta-hg@69
|
334 |
|
franta-hg@69
|
335 |
public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
|
franta-hg@69
|
336 |
}
|
franta-hg@14
|
337 |
}
|