Formatter is now AutoCloseable – so have chance to do some clean up and close the stream, if some error occurs (e.g. lost connection during result set reading)
1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 29 18:26:43 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Mon Dec 30 00:01:39 2013 +0100
1.3 @@ -141,8 +141,9 @@
1.4 FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
1.5 try (DatabaseConnection c = dd.connect()) {
1.6 log.log(Level.FINE, "Database connected");
1.7 - Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
1.8 - c.executeQuery(options.getSQLCommand(), f);
1.9 + try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()))) {
1.10 + c.executeQuery(options.getSQLCommand(), f);
1.11 + }
1.12 }
1.13 }
1.14
2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Sun Dec 29 18:26:43 2013 +0100
2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Mon Dec 30 00:01:39 2013 +0100
2.3 @@ -66,20 +66,26 @@
2.4 case DATABASES:
2.5 case FORMATTERS:
2.6 case TYPES:
2.7 - formatter = getFormatter();
2.8 - formatter.writeStartBatch();
2.9 - formatter.writeStartDatabase(new DatabaseDefinition());
2.10 + try (Formatter f = getFormatter()) {
2.11 + formatter = f;
2.12 + formatter.writeStartBatch();
2.13 + formatter.writeStartDatabase(new DatabaseDefinition());
2.14 + showInfos(commands);
2.15 + formatter.writeEndDatabase();
2.16 + formatter.writeEndBatch();
2.17 + formatter.close();
2.18 + }
2.19 + break;
2.20 + default:
2.21 + showInfos(commands);
2.22 }
2.23 }
2.24 + }
2.25
2.26 + private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
2.27 for (InfoType infoType : commands) {
2.28 infoType.showInfo(this);
2.29 }
2.30 -
2.31 - if (formatter != null) {
2.32 - formatter.writeEndDatabase();
2.33 - formatter.writeEndBatch();
2.34 - }
2.35 }
2.36
2.37 private void listFormatters() throws ConfigurationException, FormatterException {
3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java Sun Dec 29 18:26:43 2013 +0100
3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java Mon Dec 30 00:01:39 2013 +0100
3.3 @@ -227,6 +227,10 @@
3.4 peekState(EnumSet.of(State.UPDATES_RESULT));
3.5 }
3.6
3.7 + @Override
3.8 + public void close() throws FormatterException {
3.9 + }
3.10 +
3.11 public FormatterContext getFormatterContext() {
3.12 return formatterContext;
3.13 }
4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/Formatter.java Sun Dec 29 18:26:43 2013 +0100
4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/Formatter.java Mon Dec 30 00:01:39 2013 +0100
4.3 @@ -25,10 +25,10 @@
4.4 *
4.5 * @author Ing. František Kučera (frantovo.cz)
4.6 */
4.7 -public interface Formatter {
4.8 +public interface Formatter extends AutoCloseable {
4.9
4.10 void writeStartBatch();
4.11 -
4.12 +
4.13 void writeStartDatabase(DatabaseDefinition databaseDefinition);
4.14
4.15 void writeEndDatabase();
4.16 @@ -54,6 +54,13 @@
4.17 void writeUpdatedRowsCount(int updatedRowsCount);
4.18
4.19 void writeEndUpdatesResult();
4.20 -
4.21 +
4.22 void writeEndBatch();
4.23 +
4.24 + /**
4.25 + * If an error occurs (e.g. lost connection during result set reading) this method will be
4.26 + * called even if there was no {@linkplain #writeEndBach()}.
4.27 + */
4.28 + @Override
4.29 + void close() throws FormatterException;
4.30 }