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@160
|
25 |
import info.globalcode.sql.dk.configuration.Properties;
|
franta-hg@159
|
26 |
import info.globalcode.sql.dk.configuration.Property;
|
franta-hg@69
|
27 |
import info.globalcode.sql.dk.formatting.ColumnsHeader;
|
franta-hg@159
|
28 |
import info.globalcode.sql.dk.formatting.FakeSqlArray;
|
franta-hg@69
|
29 |
import info.globalcode.sql.dk.formatting.Formatter;
|
franta-hg@69
|
30 |
import info.globalcode.sql.dk.formatting.FormatterContext;
|
franta-hg@69
|
31 |
import info.globalcode.sql.dk.formatting.FormatterException;
|
franta-hg@17
|
32 |
import java.io.BufferedReader;
|
franta-hg@160
|
33 |
import java.io.ByteArrayOutputStream;
|
franta-hg@17
|
34 |
import java.io.InputStreamReader;
|
franta-hg@14
|
35 |
import java.io.PrintStream;
|
franta-hg@159
|
36 |
import java.sql.Array;
|
franta-hg@158
|
37 |
import java.sql.Driver;
|
franta-hg@159
|
38 |
import java.sql.DriverPropertyInfo;
|
franta-hg@65
|
39 |
import java.sql.SQLException;
|
franta-hg@70
|
40 |
import java.util.ArrayList;
|
franta-hg@70
|
41 |
import java.util.EnumSet;
|
franta-hg@159
|
42 |
import java.util.HashSet;
|
franta-hg@66
|
43 |
import java.util.List;
|
franta-hg@158
|
44 |
import java.util.ServiceLoader;
|
franta-hg@159
|
45 |
import java.util.Set;
|
franta-hg@17
|
46 |
import java.util.logging.Level;
|
franta-hg@17
|
47 |
import java.util.logging.Logger;
|
franta-hg@69
|
48 |
import javax.sql.rowset.RowSetMetaDataImpl;
|
franta-hg@14
|
49 |
|
franta-hg@14
|
50 |
/**
|
franta-hg@14
|
51 |
* Displays info like help, version etc.
|
franta-hg@14
|
52 |
*
|
franta-hg@14
|
53 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@14
|
54 |
*/
|
franta-hg@14
|
55 |
public class InfoLister {
|
franta-hg@14
|
56 |
|
franta-hg@17
|
57 |
private static final Logger log = Logger.getLogger(InfoLister.class.getName());
|
franta-hg@159
|
58 |
/**
|
franta-hg@159
|
59 |
* Fake database name for output formatting
|
franta-hg@159
|
60 |
*/
|
franta-hg@159
|
61 |
public static final String CONFIG_DB_NAME = "sqldk_configuration";
|
franta-hg@17
|
62 |
private PrintStream out;
|
franta-hg@20
|
63 |
private ConfigurationProvider configurationProvider;
|
franta-hg@69
|
64 |
private CLIOptions options;
|
franta-hg@70
|
65 |
private Formatter formatter;
|
franta-hg@17
|
66 |
|
franta-hg@69
|
67 |
public InfoLister(PrintStream out, ConfigurationProvider configurationProvider, CLIOptions options) {
|
franta-hg@17
|
68 |
this.out = out;
|
franta-hg@20
|
69 |
this.configurationProvider = configurationProvider;
|
franta-hg@69
|
70 |
this.options = options;
|
franta-hg@17
|
71 |
}
|
franta-hg@17
|
72 |
|
franta-hg@70
|
73 |
public void showInfo() throws ConfigurationException, FormatterException {
|
franta-hg@70
|
74 |
EnumSet<InfoType> commands = options.getShowInfo();
|
franta-hg@70
|
75 |
|
franta-hg@139
|
76 |
boolean formattinNeeded = false;
|
franta-hg@139
|
77 |
|
franta-hg@70
|
78 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
79 |
switch (infoType) {
|
franta-hg@70
|
80 |
case CONNECTION:
|
franta-hg@158
|
81 |
case JDBC_DRIVERS:
|
franta-hg@159
|
82 |
case JDBC_PROPERTIES:
|
franta-hg@70
|
83 |
case DATABASES:
|
franta-hg@70
|
84 |
case FORMATTERS:
|
franta-hg@70
|
85 |
case TYPES:
|
franta-hg@139
|
86 |
formattinNeeded = true;
|
franta-hg@101
|
87 |
break;
|
franta-hg@70
|
88 |
}
|
franta-hg@70
|
89 |
}
|
franta-hg@139
|
90 |
|
franta-hg@139
|
91 |
if (formattinNeeded) {
|
franta-hg@139
|
92 |
try (Formatter f = getFormatter()) {
|
franta-hg@139
|
93 |
formatter = f;
|
franta-hg@139
|
94 |
formatter.writeStartBatch();
|
franta-hg@159
|
95 |
DatabaseDefinition dd = new DatabaseDefinition();
|
franta-hg@159
|
96 |
dd.setName(CONFIG_DB_NAME);
|
franta-hg@159
|
97 |
formatter.writeStartDatabase(dd);
|
franta-hg@139
|
98 |
showInfos(commands);
|
franta-hg@139
|
99 |
formatter.writeEndDatabase();
|
franta-hg@139
|
100 |
formatter.writeEndBatch();
|
franta-hg@139
|
101 |
formatter.close();
|
franta-hg@139
|
102 |
}
|
franta-hg@139
|
103 |
} else {
|
franta-hg@139
|
104 |
showInfos(commands);
|
franta-hg@139
|
105 |
}
|
franta-hg@101
|
106 |
}
|
franta-hg@70
|
107 |
|
franta-hg@101
|
108 |
private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
|
franta-hg@70
|
109 |
for (InfoType infoType : commands) {
|
franta-hg@70
|
110 |
infoType.showInfo(this);
|
franta-hg@70
|
111 |
}
|
franta-hg@70
|
112 |
}
|
franta-hg@70
|
113 |
|
franta-hg@72
|
114 |
private void listFormatters() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
115 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
116 |
new HeaderField("name", SQLType.VARCHAR),
|
franta-hg@72
|
117 |
new HeaderField("built_in", SQLType.BOOLEAN),
|
franta-hg@72
|
118 |
new HeaderField("default", SQLType.BOOLEAN),
|
franta-hg@160
|
119 |
new HeaderField("class_name", SQLType.VARCHAR),
|
franta-hg@160
|
120 |
new HeaderField("valid", SQLType.BOOLEAN));
|
franta-hg@72
|
121 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
122 |
|
franta-hg@72
|
123 |
String defaultFormatter = configurationProvider.getConfiguration().getDefaultFormatter();
|
franta-hg@72
|
124 |
defaultFormatter = defaultFormatter == null ? Configuration.DEFAULT_FORMATTER : defaultFormatter;
|
franta-hg@72
|
125 |
|
franta-hg@69
|
126 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getBuildInFormatters()) {
|
franta-hg@160
|
127 |
data.add(new Object[]{fd.getName(), true, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
|
franta-hg@69
|
128 |
}
|
franta-hg@72
|
129 |
|
franta-hg@72
|
130 |
for (FormatterDefinition fd : configurationProvider.getConfiguration().getFormatters()) {
|
franta-hg@160
|
131 |
data.add(new Object[]{fd.getName(), false, defaultFormatter.equals(fd.getName()), fd.getClassName(), isInstantiable(fd)});
|
franta-hg@69
|
132 |
}
|
franta-hg@72
|
133 |
|
franta-hg@159
|
134 |
printTable(formatter, header, data, "-- configured and built-in output formatters", null);
|
franta-hg@160
|
135 |
}
|
franta-hg@72
|
136 |
|
franta-hg@160
|
137 |
private boolean isInstantiable(FormatterDefinition fd) {
|
franta-hg@160
|
138 |
try {
|
franta-hg@160
|
139 |
try (ByteArrayOutputStream testStream = new ByteArrayOutputStream()) {
|
franta-hg@160
|
140 |
fd.getInstance(new FormatterContext(testStream, new Properties(0)));
|
franta-hg@160
|
141 |
return true;
|
franta-hg@160
|
142 |
}
|
franta-hg@160
|
143 |
} catch (Exception e) {
|
franta-hg@160
|
144 |
log.log(Level.SEVERE, "Unable to create an instance of formatter: " + fd.getName(), e);
|
franta-hg@160
|
145 |
return false;
|
franta-hg@160
|
146 |
}
|
franta-hg@69
|
147 |
}
|
franta-hg@67
|
148 |
|
franta-hg@70
|
149 |
public void listTypes() throws FormatterException, ConfigurationException {
|
franta-hg@70
|
150 |
ColumnsHeader header = constructHeader(new HeaderField("name", SQLType.VARCHAR), new HeaderField("code", SQLType.INTEGER));
|
franta-hg@70
|
151 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@70
|
152 |
for (SQLType sqlType : SQLType.values()) {
|
franta-hg@70
|
153 |
data.add(new Object[]{sqlType.name(), sqlType.getCode()});
|
franta-hg@70
|
154 |
}
|
franta-hg@159
|
155 |
printTable(formatter, header, data, "-- data types", null);
|
franta-hg@93
|
156 |
log.log(Level.INFO, "Type names in --types option are case insensitive");
|
franta-hg@69
|
157 |
}
|
franta-hg@67
|
158 |
|
franta-hg@72
|
159 |
public void listDatabases() throws ConfigurationException, FormatterException {
|
franta-hg@72
|
160 |
ColumnsHeader header = constructHeader(
|
franta-hg@72
|
161 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@72
|
162 |
new HeaderField("user_name", SQLType.VARCHAR),
|
franta-hg@72
|
163 |
new HeaderField("database_url", SQLType.VARCHAR));
|
franta-hg@72
|
164 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@72
|
165 |
|
franta-hg@69
|
166 |
final List<DatabaseDefinition> configuredDatabases = configurationProvider.getConfiguration().getDatabases();
|
franta-hg@69
|
167 |
if (configuredDatabases.isEmpty()) {
|
franta-hg@69
|
168 |
log.log(Level.WARNING, "No databases are configured.");
|
franta-hg@69
|
169 |
} else {
|
franta-hg@69
|
170 |
for (DatabaseDefinition dd : configuredDatabases) {
|
franta-hg@72
|
171 |
data.add(new Object[]{dd.getName(), dd.getUserName(), dd.getUrl()});
|
franta-hg@14
|
172 |
}
|
franta-hg@14
|
173 |
}
|
franta-hg@72
|
174 |
|
franta-hg@159
|
175 |
printTable(formatter, header, data, "-- configured databases", null);
|
franta-hg@14
|
176 |
}
|
franta-hg@17
|
177 |
|
franta-hg@158
|
178 |
public void listJdbcDrivers() throws FormatterException, ConfigurationException {
|
franta-hg@158
|
179 |
ColumnsHeader header = constructHeader(
|
franta-hg@158
|
180 |
new HeaderField("class", SQLType.VARCHAR),
|
franta-hg@158
|
181 |
new HeaderField("version", SQLType.VARCHAR),
|
franta-hg@158
|
182 |
new HeaderField("major", SQLType.INTEGER),
|
franta-hg@158
|
183 |
new HeaderField("minor", SQLType.INTEGER),
|
franta-hg@158
|
184 |
new HeaderField("jdbc_compliant", SQLType.BOOLEAN));
|
franta-hg@158
|
185 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@158
|
186 |
|
franta-hg@158
|
187 |
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
|
franta-hg@158
|
188 |
for (Driver d : drivers) {
|
franta-hg@158
|
189 |
data.add(new Object[]{
|
franta-hg@158
|
190 |
d.getClass().getName(),
|
franta-hg@158
|
191 |
d.getMajorVersion() + "." + d.getMinorVersion(),
|
franta-hg@158
|
192 |
d.getMajorVersion(),
|
franta-hg@158
|
193 |
d.getMinorVersion(),
|
franta-hg@159
|
194 |
d.jdbcCompliant()
|
franta-hg@159
|
195 |
});
|
franta-hg@158
|
196 |
}
|
franta-hg@158
|
197 |
|
franta-hg@159
|
198 |
printTable(formatter, header, data, "-- discovered JDBC drivers (available on the CLASSPATH)", null);
|
franta-hg@159
|
199 |
}
|
franta-hg@158
|
200 |
|
franta-hg@159
|
201 |
public void listJdbcProperties() throws FormatterException, ConfigurationException {
|
franta-hg@159
|
202 |
for (String dbName : options.getDatabaseNamesToListProperties()) {
|
franta-hg@159
|
203 |
ColumnsHeader header = constructHeader(
|
franta-hg@159
|
204 |
new HeaderField("property_name", SQLType.VARCHAR),
|
franta-hg@159
|
205 |
new HeaderField("required", SQLType.BOOLEAN),
|
franta-hg@159
|
206 |
new HeaderField("choices", SQLType.ARRAY),
|
franta-hg@159
|
207 |
new HeaderField("configured_value", SQLType.VARCHAR),
|
franta-hg@159
|
208 |
new HeaderField("description", SQLType.VARCHAR));
|
franta-hg@159
|
209 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@159
|
210 |
|
franta-hg@159
|
211 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
|
franta-hg@159
|
212 |
|
franta-hg@159
|
213 |
Driver driver = findDriver(dd);
|
franta-hg@159
|
214 |
|
franta-hg@159
|
215 |
if (driver == null) {
|
franta-hg@159
|
216 |
log.log(Level.WARNING, "No JDBC driver was found for DB: {0} with URL: {1}", new Object[]{dd.getName(), dd.getUrl()});
|
franta-hg@159
|
217 |
} else {
|
franta-hg@159
|
218 |
log.log(Level.INFO, "For DB: {0} was found JDBC driver: {1}", new Object[]{dd.getName(), driver.getClass().getName()});
|
franta-hg@159
|
219 |
|
franta-hg@159
|
220 |
try {
|
franta-hg@159
|
221 |
DriverPropertyInfo[] propertyInfos = driver.getPropertyInfo(dd.getUrl(), dd.getProperties().getJavaProperties());
|
franta-hg@159
|
222 |
|
franta-hg@159
|
223 |
Set<String> standardProperties = new HashSet<>();
|
franta-hg@159
|
224 |
|
franta-hg@159
|
225 |
for (DriverPropertyInfo pi : propertyInfos) {
|
franta-hg@159
|
226 |
Array choices = new FakeSqlArray(pi.choices, SQLType.VARCHAR);
|
franta-hg@159
|
227 |
data.add(new Object[]{
|
franta-hg@159
|
228 |
pi.name,
|
franta-hg@159
|
229 |
pi.required,
|
franta-hg@159
|
230 |
choices.getArray() == null ? "" : choices,
|
franta-hg@159
|
231 |
pi.value == null ? "" : pi.value,
|
franta-hg@159
|
232 |
pi.description
|
franta-hg@159
|
233 |
});
|
franta-hg@159
|
234 |
standardProperties.add(pi.name);
|
franta-hg@159
|
235 |
}
|
franta-hg@159
|
236 |
|
franta-hg@159
|
237 |
for (Property p : dd.getProperties()) {
|
franta-hg@159
|
238 |
if (!standardProperties.contains(p.getName())) {
|
franta-hg@159
|
239 |
data.add(new Object[]{
|
franta-hg@159
|
240 |
p.getName(),
|
franta-hg@159
|
241 |
"",
|
franta-hg@159
|
242 |
"",
|
franta-hg@159
|
243 |
p.getValue(),
|
franta-hg@159
|
244 |
""
|
franta-hg@159
|
245 |
});
|
franta-hg@159
|
246 |
log.log(Level.WARNING, "Your configuration contains property „{0}“ not declared by the JDBC driver.", p.getName());
|
franta-hg@159
|
247 |
}
|
franta-hg@159
|
248 |
}
|
franta-hg@159
|
249 |
|
franta-hg@159
|
250 |
} catch (SQLException e) {
|
franta-hg@159
|
251 |
log.log(Level.WARNING, "Error during getting property infos.", e);
|
franta-hg@159
|
252 |
}
|
franta-hg@159
|
253 |
|
franta-hg@159
|
254 |
List<Parameter> parameters = new ArrayList<>();
|
franta-hg@159
|
255 |
parameters.add(new NamedParameter("databgase", dbName, SQLType.VARCHAR));
|
franta-hg@159
|
256 |
parameters.add(new NamedParameter("driver_class", driver.getClass().getName(), SQLType.VARCHAR));
|
franta-hg@159
|
257 |
parameters.add(new NamedParameter("driver_major_version", driver.getMajorVersion(), SQLType.INTEGER));
|
franta-hg@159
|
258 |
parameters.add(new NamedParameter("driver_minor_version", driver.getMinorVersion(), SQLType.INTEGER));
|
franta-hg@159
|
259 |
|
franta-hg@159
|
260 |
printTable(formatter, header, data, "-- configured and configurable JDBC driver properties", parameters);
|
franta-hg@159
|
261 |
}
|
franta-hg@159
|
262 |
}
|
franta-hg@159
|
263 |
|
franta-hg@159
|
264 |
}
|
franta-hg@159
|
265 |
|
franta-hg@159
|
266 |
private Driver findDriver(DatabaseDefinition dd) {
|
franta-hg@159
|
267 |
final ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class);
|
franta-hg@159
|
268 |
for (Driver d : drivers) {
|
franta-hg@159
|
269 |
try {
|
franta-hg@159
|
270 |
if (d.acceptsURL(dd.getUrl())) {
|
franta-hg@159
|
271 |
return d;
|
franta-hg@159
|
272 |
}
|
franta-hg@159
|
273 |
} catch (SQLException e) {
|
franta-hg@159
|
274 |
log.log(Level.WARNING, "Error during finding JDBC driver for: " + dd.getName(), e);
|
franta-hg@159
|
275 |
}
|
franta-hg@159
|
276 |
}
|
franta-hg@159
|
277 |
return null;
|
franta-hg@158
|
278 |
}
|
franta-hg@158
|
279 |
|
franta-hg@73
|
280 |
public void testConnection() throws FormatterException, ConfigurationException {
|
franta-hg@73
|
281 |
ColumnsHeader header = constructHeader(
|
franta-hg@73
|
282 |
new HeaderField("database_name", SQLType.VARCHAR),
|
franta-hg@74
|
283 |
new HeaderField("configured", SQLType.BOOLEAN),
|
franta-hg@74
|
284 |
new HeaderField("connected", SQLType.BOOLEAN));
|
franta-hg@73
|
285 |
List<Object[]> data = new ArrayList<>();
|
franta-hg@73
|
286 |
|
franta-hg@155
|
287 |
for (String dbName : options.getDatabaseNamesToTest()) {
|
franta-hg@74
|
288 |
data.add(testConnection(dbName));
|
franta-hg@74
|
289 |
}
|
franta-hg@73
|
290 |
|
franta-hg@159
|
291 |
printTable(formatter, header, data, "-- database configuration and connectivity test", null);
|
franta-hg@73
|
292 |
}
|
franta-hg@73
|
293 |
|
franta-hg@73
|
294 |
public Object[] testConnection(String dbName) {
|
franta-hg@69
|
295 |
log.log(Level.FINE, "Testing connection to database: {0}", dbName);
|
franta-hg@73
|
296 |
|
franta-hg@73
|
297 |
boolean succesfullyConnected = false;
|
franta-hg@73
|
298 |
boolean succesfullyConfigured = false;
|
franta-hg@73
|
299 |
|
franta-hg@69
|
300 |
try {
|
franta-hg@69
|
301 |
DatabaseDefinition dd = configurationProvider.getConfiguration().getDatabase(dbName);
|
franta-hg@75
|
302 |
log.log(Level.FINE, "Database definition was loaded from configuration");
|
franta-hg@75
|
303 |
succesfullyConfigured = true;
|
franta-hg@106
|
304 |
try (DatabaseConnection dc = dd.connect(options.getDatabaseProperties())) {
|
franta-hg@75
|
305 |
succesfullyConnected = dc.test();
|
franta-hg@69
|
306 |
}
|
franta-hg@75
|
307 |
log.log(Level.FINE, "Database connection test was successful");
|
franta-hg@69
|
308 |
} catch (ConfigurationException | SQLException e) {
|
franta-hg@69
|
309 |
log.log(Level.SEVERE, "Error during testing connection", e);
|
franta-hg@69
|
310 |
}
|
franta-hg@73
|
311 |
|
franta-hg@73
|
312 |
return new Object[]{dbName, succesfullyConfigured, succesfullyConnected};
|
franta-hg@69
|
313 |
}
|
franta-hg@69
|
314 |
|
franta-hg@69
|
315 |
public void printResource(String fileName) {
|
franta-hg@18
|
316 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)))) {
|
franta-hg@17
|
317 |
while (true) {
|
franta-hg@18
|
318 |
String line = reader.readLine();
|
franta-hg@17
|
319 |
if (line == null) {
|
franta-hg@17
|
320 |
break;
|
franta-hg@17
|
321 |
} else {
|
franta-hg@17
|
322 |
println(line);
|
franta-hg@17
|
323 |
}
|
franta-hg@17
|
324 |
}
|
franta-hg@17
|
325 |
} catch (Exception e) {
|
franta-hg@18
|
326 |
log.log(Level.SEVERE, "Unable to print this info. Please see our website for it: " + Constants.WEBSITE, e);
|
franta-hg@17
|
327 |
}
|
franta-hg@17
|
328 |
}
|
franta-hg@17
|
329 |
|
franta-hg@17
|
330 |
private void println(String line) {
|
franta-hg@17
|
331 |
out.println(line);
|
franta-hg@17
|
332 |
}
|
franta-hg@69
|
333 |
|
franta-hg@159
|
334 |
private void printTable(Formatter formatter, ColumnsHeader header, List<Object[]> data, String sql, List<Parameter> parameters) throws ConfigurationException, FormatterException {
|
franta-hg@159
|
335 |
formatter.writeStartStatement();
|
franta-hg@159
|
336 |
|
franta-hg@159
|
337 |
if (sql != null) {
|
franta-hg@159
|
338 |
formatter.writeQuery(sql);
|
franta-hg@159
|
339 |
if (parameters != null) {
|
franta-hg@159
|
340 |
formatter.writeParameters(parameters);
|
franta-hg@159
|
341 |
}
|
franta-hg@159
|
342 |
}
|
franta-hg@159
|
343 |
|
franta-hg@142
|
344 |
formatter.writeStartResultSet(header);
|
franta-hg@69
|
345 |
|
franta-hg@69
|
346 |
for (Object[] row : data) {
|
franta-hg@69
|
347 |
formatter.writeStartRow();
|
franta-hg@69
|
348 |
for (Object cell : row) {
|
franta-hg@69
|
349 |
formatter.writeColumnValue(cell);
|
franta-hg@69
|
350 |
}
|
franta-hg@69
|
351 |
formatter.writeEndRow();
|
franta-hg@69
|
352 |
}
|
franta-hg@69
|
353 |
|
franta-hg@69
|
354 |
formatter.writeEndResultSet();
|
franta-hg@159
|
355 |
formatter.writeEndStatement();
|
franta-hg@69
|
356 |
}
|
franta-hg@69
|
357 |
|
franta-hg@69
|
358 |
private Formatter getFormatter() throws ConfigurationException, FormatterException {
|
franta-hg@89
|
359 |
String formatterName = options.getFormatterName();
|
franta-hg@89
|
360 |
formatterName = formatterName == null ? Configuration.DEFAULT_FORMATTER_PREFETCHING : formatterName;
|
franta-hg@89
|
361 |
FormatterDefinition fd = configurationProvider.getConfiguration().getFormatter(formatterName);
|
franta-hg@104
|
362 |
FormatterContext context = new FormatterContext(out, options.getFormatterProperties());
|
franta-hg@69
|
363 |
return fd.getInstance(context);
|
franta-hg@69
|
364 |
}
|
franta-hg@69
|
365 |
|
franta-hg@69
|
366 |
private ColumnsHeader constructHeader(HeaderField... fields) throws FormatterException {
|
franta-hg@69
|
367 |
try {
|
franta-hg@69
|
368 |
RowSetMetaDataImpl metaData = new RowSetMetaDataImpl();
|
franta-hg@69
|
369 |
metaData.setColumnCount(fields.length);
|
franta-hg@69
|
370 |
|
franta-hg@69
|
371 |
for (int i = 0; i < fields.length; i++) {
|
franta-hg@69
|
372 |
HeaderField hf = fields[i];
|
franta-hg@69
|
373 |
int sqlIndex = i + 1;
|
franta-hg@69
|
374 |
metaData.setColumnName(sqlIndex, hf.name);
|
franta-hg@69
|
375 |
metaData.setColumnLabel(sqlIndex, hf.name);
|
franta-hg@69
|
376 |
metaData.setColumnType(sqlIndex, hf.type.getCode());
|
franta-hg@69
|
377 |
metaData.setColumnTypeName(sqlIndex, hf.type.name());
|
franta-hg@69
|
378 |
}
|
franta-hg@69
|
379 |
|
franta-hg@69
|
380 |
return new ColumnsHeader(metaData);
|
franta-hg@69
|
381 |
} catch (SQLException e) {
|
franta-hg@69
|
382 |
throw new FormatterException("Error while constructing table headers", e);
|
franta-hg@69
|
383 |
}
|
franta-hg@69
|
384 |
}
|
franta-hg@69
|
385 |
|
franta-hg@69
|
386 |
private static class HeaderField {
|
franta-hg@69
|
387 |
|
franta-hg@69
|
388 |
String name;
|
franta-hg@69
|
389 |
SQLType type;
|
franta-hg@69
|
390 |
|
franta-hg@69
|
391 |
public HeaderField(String name, SQLType type) {
|
franta-hg@69
|
392 |
this.name = name;
|
franta-hg@69
|
393 |
this.type = type;
|
franta-hg@69
|
394 |
}
|
franta-hg@69
|
395 |
}
|
franta-hg@69
|
396 |
|
franta-hg@69
|
397 |
public enum InfoType {
|
franta-hg@69
|
398 |
|
franta-hg@69
|
399 |
HELP {
|
franta-hg@69
|
400 |
@Override
|
franta-hg@69
|
401 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
402 |
infoLister.printResource(Constants.HELP_FILE);
|
franta-hg@69
|
403 |
}
|
franta-hg@69
|
404 |
},
|
franta-hg@69
|
405 |
VERSION {
|
franta-hg@69
|
406 |
@Override
|
franta-hg@69
|
407 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
408 |
infoLister.printResource(Constants.VERSION_FILE);
|
franta-hg@69
|
409 |
}
|
franta-hg@69
|
410 |
},
|
franta-hg@69
|
411 |
LICENSE {
|
franta-hg@69
|
412 |
@Override
|
franta-hg@69
|
413 |
public void showInfo(InfoLister infoLister) {
|
franta-hg@69
|
414 |
infoLister.printResource(Constants.LICENSE_FILE);
|
franta-hg@69
|
415 |
}
|
franta-hg@69
|
416 |
},
|
franta-hg@69
|
417 |
FORMATTERS {
|
franta-hg@69
|
418 |
@Override
|
franta-hg@70
|
419 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
420 |
infoLister.listFormatters();
|
franta-hg@69
|
421 |
}
|
franta-hg@69
|
422 |
},
|
franta-hg@69
|
423 |
TYPES {
|
franta-hg@69
|
424 |
@Override
|
franta-hg@70
|
425 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
426 |
infoLister.listTypes();
|
franta-hg@69
|
427 |
}
|
franta-hg@69
|
428 |
},
|
franta-hg@158
|
429 |
JDBC_DRIVERS {
|
franta-hg@158
|
430 |
@Override
|
franta-hg@158
|
431 |
public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
|
franta-hg@158
|
432 |
infoLister.listJdbcDrivers();
|
franta-hg@158
|
433 |
}
|
franta-hg@158
|
434 |
},
|
franta-hg@159
|
435 |
JDBC_PROPERTIES {
|
franta-hg@159
|
436 |
@Override
|
franta-hg@159
|
437 |
public void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException {
|
franta-hg@159
|
438 |
infoLister.listJdbcProperties();
|
franta-hg@159
|
439 |
}
|
franta-hg@159
|
440 |
},
|
franta-hg@69
|
441 |
DATABASES {
|
franta-hg@69
|
442 |
@Override
|
franta-hg@70
|
443 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
444 |
infoLister.listDatabases();
|
franta-hg@69
|
445 |
}
|
franta-hg@69
|
446 |
},
|
franta-hg@69
|
447 |
CONNECTION {
|
franta-hg@69
|
448 |
@Override
|
franta-hg@73
|
449 |
public void showInfo(InfoLister infoLister) throws FormatterException, ConfigurationException {
|
franta-hg@69
|
450 |
infoLister.testConnection();
|
franta-hg@69
|
451 |
}
|
franta-hg@69
|
452 |
};
|
franta-hg@69
|
453 |
|
franta-hg@69
|
454 |
public abstract void showInfo(InfoLister infoLister) throws ConfigurationException, FormatterException;
|
franta-hg@69
|
455 |
}
|
franta-hg@14
|
456 |
}
|