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)
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Sun Dec 29 18:26:43 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Mon Dec 30 00:01:39 2013 +0100
@@ -141,8 +141,9 @@
FormatterDefinition fd = configuration.getFormatter(options.getFormatterName());
try (DatabaseConnection c = dd.connect()) {
log.log(Level.FINE, "Database connected");
- Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()));
- c.executeQuery(options.getSQLCommand(), f);
+ try (Formatter f = fd.getInstance(new FormatterContext(options.getOutputStream()))) {
+ c.executeQuery(options.getSQLCommand(), f);
+ }
}
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Sun Dec 29 18:26:43 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Mon Dec 30 00:01:39 2013 +0100
@@ -66,20 +66,26 @@
case DATABASES:
case FORMATTERS:
case TYPES:
- formatter = getFormatter();
- formatter.writeStartBatch();
- formatter.writeStartDatabase(new DatabaseDefinition());
+ try (Formatter f = getFormatter()) {
+ formatter = f;
+ formatter.writeStartBatch();
+ formatter.writeStartDatabase(new DatabaseDefinition());
+ showInfos(commands);
+ formatter.writeEndDatabase();
+ formatter.writeEndBatch();
+ formatter.close();
+ }
+ break;
+ default:
+ showInfos(commands);
}
}
+ }
+ private void showInfos(EnumSet<InfoType> commands) throws ConfigurationException, FormatterException {
for (InfoType infoType : commands) {
infoType.showInfo(this);
}
-
- if (formatter != null) {
- formatter.writeEndDatabase();
- formatter.writeEndBatch();
- }
}
private void listFormatters() throws ConfigurationException, FormatterException {
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java Sun Dec 29 18:26:43 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java Mon Dec 30 00:01:39 2013 +0100
@@ -227,6 +227,10 @@
peekState(EnumSet.of(State.UPDATES_RESULT));
}
+ @Override
+ public void close() throws FormatterException {
+ }
+
public FormatterContext getFormatterContext() {
return formatterContext;
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/Formatter.java Sun Dec 29 18:26:43 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/Formatter.java Mon Dec 30 00:01:39 2013 +0100
@@ -25,10 +25,10 @@
*
* @author Ing. František Kučera (frantovo.cz)
*/
-public interface Formatter {
+public interface Formatter extends AutoCloseable {
void writeStartBatch();
-
+
void writeStartDatabase(DatabaseDefinition databaseDefinition);
void writeEndDatabase();
@@ -54,6 +54,13 @@
void writeUpdatedRowsCount(int updatedRowsCount);
void writeEndUpdatesResult();
-
+
void writeEndBatch();
+
+ /**
+ * If an error occurs (e.g. lost connection during result set reading) this method will be
+ * called even if there was no {@linkplain #writeEndBach()}.
+ */
+ @Override
+ void close() throws FormatterException;
}