Skip to content

Commit adb46c2

Browse files
author
mareks
committed
if temp file has been created during creation of connection, it will be
deleted on connection close
1 parent 619963f commit adb46c2

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ publishing {
5151
maven(MavenPublication) {
5252
groupId = 'de.inetsoftware'
5353
artifactId = 'exceljconnect'
54-
version = '1.5'
54+
version = '1.6'
5555
from components.java
5656
pom {
5757
name = 'Excel driver for Java'

src/com/inet/excel/ExcelConnection.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ public class ExcelConnection implements Connection {
4242

4343
private final ExcelParser parser;
4444
private boolean closed;
45+
private Runnable onConnectionClose;
4546

4647
/** Constructor of the class.
4748
* @param parser component responsible for reading data from Excel document.
49+
* @param onConnectionClose optional runnable to be executed on connection close.
4850
* @throws IllegalArgumentException if given parser is null.
4951
*/
50-
public ExcelConnection( ExcelParser parser ) {
52+
public ExcelConnection( ExcelParser parser, Runnable onConnectionClose ) {
5153
if( parser == null ) {
5254
throw new IllegalArgumentException( "parser must not be null" );
5355
}
5456
this.parser = parser;
57+
this.onConnectionClose = onConnectionClose;
5558
this.closed = false;
5659
}
5760

@@ -169,6 +172,9 @@ public void rollback() throws SQLException {
169172
*/
170173
@Override
171174
public void close() throws SQLException {
175+
if( !closed && onConnectionClose != null ) {
176+
onConnectionClose.run();
177+
}
172178
closed = true;
173179
}
174180

src/com/inet/excel/ExcelDriver.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ExcelDriver implements Driver {
4040
public static final String URL_PREFIX = "jdbc:inetexcel:";
4141
public static final String DRIVER_NAME = "inetexcel";
4242
public static final int MAJOR_VERSION = 1;
43-
public static final int MINOR_VERSION = 5;
43+
public static final int MINOR_VERSION = 6;
4444

4545
/** Throws exception indicating that requested operation is not supported.
4646
* @throws SQLException exception indicating that requested operation is not supported.
@@ -87,6 +87,8 @@ public Connection connect( String url, Properties info ) throws SQLException {
8787
throw new SQLException( "Excel file is not specified" );
8888
}
8989

90+
Runnable onConnectionClose = null;
91+
9092
String lowerCasedFilePath = filePath.toLowerCase();
9193
String fileProtocol = "file:";
9294
if( lowerCasedFilePath.startsWith( fileProtocol ) ) {
@@ -109,6 +111,13 @@ public Connection connect( String url, Properties info ) throws SQLException {
109111
Path tempFile = Files.createTempFile( null, null ).toAbsolutePath();
110112
Files.copy( in, tempFile, StandardCopyOption.REPLACE_EXISTING );
111113
filePath = tempFile.toString();
114+
onConnectionClose = () -> {
115+
try {
116+
Files.deleteIfExists( tempFile );
117+
} catch( IOException e ) {
118+
// ignore
119+
}
120+
};
112121
} catch( IOException e ) {
113122
throw new SQLException( "An error occurred while accessing the file", e );
114123
}
@@ -120,7 +129,7 @@ public Connection connect( String url, Properties info ) throws SQLException {
120129
}
121130

122131
ExcelParser parser = new ExcelParser( file, hasHeaderRow );
123-
return new ExcelConnection( parser );
132+
return new ExcelConnection( parser, onConnectionClose );
124133
}
125134

126135
/**

test/com/inet/excel/ExcelConnectionTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@
1515
*/
1616
package com.inet.excel;
1717

18-
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.nio.file.Paths;
1921

2022
import org.junit.jupiter.api.Test;
2123

24+
import com.inet.excel.parser.ExcelParser;
25+
2226
public class ExcelConnectionTest {
2327

2428
@Test
2529
public void constructor_throws_exception_if_parser_is_null() {
26-
assertThrows( IllegalArgumentException.class, () -> new ExcelConnection( null ) );
30+
assertThrows( IllegalArgumentException.class, () -> new ExcelConnection( null, null ) );
31+
}
32+
33+
@Test
34+
public void constructor_accepts_null_as_runnable_to_be_executed_on_connection_close() {
35+
ExcelParser parser = new ExcelParser( Paths.get( "" ), false );
36+
assertDoesNotThrow( () -> new ExcelConnection( parser, null ) );
2737
}
2838
}

0 commit comments

Comments
 (0)