Skip to content

Commit

Permalink
Add delegating prepared statement (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada authored Oct 20, 2024
1 parent 1244e24 commit d08dca6
Show file tree
Hide file tree
Showing 32 changed files with 1,626 additions and 596 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.classpath
/bin/
/.settings/org.eclipse.buildship.core.prefs
/.idea/
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.0] - unreleased
## [0.9.0] - unreleased

## [0.8.0] - 2024-10-20

- [PR #27](https://github.com/itsallcode/simple-jdbc/pull/27): Update dependencies
- [PR #28](https://github.com/itsallcode/simple-jdbc/pull/28): Refactored batch inserts (**Breaking change**)
- [PR #29](https://github.com/itsallcode/simple-jdbc/pull/29): Setting values for a `PreparedStatement` (**Breaking change**)

## [0.7.1] - 2024-09-01

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def mockitoAgentPath = getMockitoAgentPath()
testing {
suites {
configureEach {
useJUnitJupiter()
useJUnitJupiter(libs.versions.junitJupiter.get())
dependencies {
implementation project()
implementation libs.assertj
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencyResolutionManagement {
versionCatalogs {
libs {
version('mockito', '5.14.2')
version('junitJupiter', '5.11.2')

library('assertj', 'org.assertj:assertj-core:3.26.3')
library('h2', 'com.h2database:h2:2.3.232')
library('junitPioneer', 'org.junit-pioneer:junit-pioneer:2.2.0')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import java.lang.reflect.*;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -34,10 +35,25 @@ void performanceTestRowStmtSetter() {
}

private BatchInsertBuilder<NameRow> testee() {
final PreparedStatement stmt = new NoopPreparedStatement();
final PreparedStatement stmt = createNoopPreparedStatement();
when(connectionMock.prepareStatement(anyString()))
.thenReturn(new SimplePreparedStatement(null, null, stmt, "sql"));
return new BatchInsertBuilder<NameRow>(connectionMock::prepareStatement, Context.builder().build());
return new BatchInsertBuilder<NameRow>(connectionMock::prepareStatement);
}

private PreparedStatement createNoopPreparedStatement() {
final InvocationHandler invocationHandler = (final Object proxy, final Method method,
final Object[] args) -> {
switch (method.getName()) {
case "executeBatch":
return new int[0];

default:
return null;
}
};
return (PreparedStatement) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class<?>[] { PreparedStatement.class }, invocationHandler);
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions src/integrationTest/java/org/itsallcode/jdbc/ExasolTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.JDBCType;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;

import org.itsallcode.jdbc.resultset.SimpleResultSet;
Expand Down Expand Up @@ -172,6 +174,20 @@ private static Arguments typeTest(final String value, final String type, final O

record TypeTest(String value, String type, Object expectedValue, JDBCType expectedType, String expectedTypeName,
String expectedClassName) {
}

@Test
void batchInsert() {
final LocalDate date = LocalDate.parse("2024-10-20");
try (final SimpleConnection connection = connect()) {
connection.executeStatement("create schema test");
connection.executeStatement("create table tab(col date)");
connection.batchInsert(LocalDate.class).into("TAB", List.of("COL"))
.mapping((row, stmt) -> stmt.setObject(1, row)).rows(Stream.of(date)).start();
try (SimpleResultSet<LocalDate> resultSet = connection.query("select * from tab",
(rs, rowNum) -> rs.getObject(1, LocalDate.class))) {
assertEquals(date, resultSet.toList().get(0));
}
}
}
}
Loading

0 comments on commit d08dca6

Please sign in to comment.