Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [PR #40](https://github.com/itsallcode/simple-jdbc/pull/40): Verify that operation on `SimpleConnection` are allowed
- [PR #41](https://github.com/itsallcode/simple-jdbc/pull/41): Allow direct access to `PreparedStatement` for batch insert
- [PR #42](https://github.com/itsallcode/simple-jdbc/pull/42): Allow direct access to `Connection`
- [PR #43](https://github.com/itsallcode/simple-jdbc/pull/43): Allow direct access to `Connection` from `DbOperations`

## [0.9.0] - 2024-12-23

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/itsallcode/jdbc/DbOperations.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.itsallcode.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -135,6 +136,15 @@ <T> SimpleResultSet<T> query(final String sql, final PreparedStatementSetter pre
*/
<T> RowBatchInsertBuilder<T> batchInsert(final Class<T> rowType);

/**
* Get the original wrapped connection.
* <p>
* Use this in case of missing features in {@link DbOperations}.
*
* @return original wrapped connection
*/
public Connection getOriginalConnection();

@Override
void close();
}
8 changes: 1 addition & 7 deletions src/main/java/org/itsallcode/jdbc/SimpleConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,8 @@ public <T> RowBatchInsertBuilder<T> batchInsert(final Class<T> rowType) {
return connection.rowBatchInsert();
}

/**
* Get the original wrapped connection.
* <p>
* Use this in case of missing features in {@link SimpleConnection}.
*
* @return original wrapped connection
*/
public Connection getOriginalConnection() {
checkOperationAllowed();
return connection.getOriginalConnection();
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/itsallcode/jdbc/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public <T> RowBatchInsertBuilder<T> batchInsert(final Class<T> rowType) {
return connection.rowBatchInsert();
}

public Connection getOriginalConnection() {
checkOperationAllowed();
return connection.getOriginalConnection();
}

private void checkOperationAllowed() {
if (this.closed) {
throw new IllegalStateException("Operation not allowed on closed transaction");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ static Stream<Arguments> operations() {
operation(con -> con.query("sql", rowMapperMock)),
operation(con -> con.query("sql", preparedStatementSetterMock, rowMapperMock)),
operation(con -> con.query("sql", List.of(), rowMapperMock)),
operation(con -> con.getOriginalConnection()),
operation(con -> con.batchInsert()),
operation(con -> con.batchInsert(null)));
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/itsallcode/jdbc/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.itsallcode.jdbc;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.*;

import java.sql.Connection;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,6 +76,7 @@ static Stream<Arguments> operations() {
operation(tx -> tx.query("sql", List.of(), rowMapperMock)),
operation(tx -> tx.batchInsert()),
operation(tx -> tx.batchInsert(null)),
operation(tx -> tx.getOriginalConnection()),
operation(tx -> tx.commit()),
operation(tx -> tx.rollback()));
}
Expand Down Expand Up @@ -198,6 +201,13 @@ void rollbackCallsTransactionFinishedCallback() {
verify(transactionFinishedCallbackMock).accept(same(testee));
}

@Test
void getOriginalConnection() {
final Connection jdbcConnectionMock = mock(Connection.class);
when(connectionMock.getOriginalConnection()).thenReturn(jdbcConnectionMock);
assertThat(startTransaction().getOriginalConnection()).isSameAs(jdbcConnectionMock);
}

private Transaction startTransaction() {
return Transaction.start(connectionMock, transactionFinishedCallbackMock);
}
Expand Down
Loading