forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update KeyHolder in JdbcClient when using positional parameters
Prior to this commit, DefaultJdbcClient updated the supplied KeyHolder when using named parameters but not for positional parameters. This commit refactors the creation of the PreparedStatementCreatorFactory so that the PreparedStatementCreator properly creates a PreparedStatement which returns generated keys. Closes spring-projectsgh-31297
- Loading branch information
Showing
2 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...g-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.jdbc.core.simple; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.io.ClassRelativeResourceLoader; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.init.DatabasePopulator; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2; | ||
|
||
/** | ||
* Integration tests for {@link JdbcClient} using an embedded H2 database. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.1 | ||
* @see JdbcClientIndexedParameterTests | ||
* @see JdbcClientNamedParameterTests | ||
*/ | ||
class JdbcClientIntegrationTests { | ||
|
||
private static final String INSERT_WITH_NAMED_PARAMS = | ||
"INSERT INTO users (first_name, last_name) VALUES(:firstName, :lastName)"; | ||
private static final String INSERT_WITH_POSITIONAL_PARAMS = | ||
"INSERT INTO users (first_name, last_name) VALUES(?, ?)"; | ||
|
||
|
||
private final EmbeddedDatabase embeddedDatabase = | ||
new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(DatabasePopulator.class)) | ||
.generateUniqueName(true) | ||
.setType(H2) | ||
.addScripts("users-schema.sql", "users-data.sql") | ||
.build(); | ||
|
||
private final JdbcClient jdbcClient = JdbcClient.create(this.embeddedDatabase); | ||
|
||
|
||
@BeforeEach | ||
void checkDatabase() { | ||
assertNumUsers(1); | ||
} | ||
|
||
@AfterEach | ||
void shutdownDatabase() { | ||
this.embeddedDatabase.shutdown(); | ||
} | ||
|
||
@Test | ||
void updateWithGeneratedKeysAndPositionalParameters() { | ||
int expectedId = 2; | ||
String firstName = "Jane"; | ||
String lastName = "Smith"; | ||
|
||
KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); | ||
|
||
int rowsAffected = this.jdbcClient.sql(INSERT_WITH_POSITIONAL_PARAMS) | ||
.params(firstName, lastName) | ||
.update(generatedKeyHolder); | ||
|
||
assertThat(rowsAffected).isEqualTo(1); | ||
assertThat(generatedKeyHolder.getKey()).isEqualTo(expectedId); | ||
assertNumUsers(2); | ||
assertUser(expectedId, firstName, lastName); | ||
} | ||
|
||
@Test | ||
void updateWithGeneratedKeysAndNamedParameters() { | ||
int expectedId = 2; | ||
String firstName = "Jane"; | ||
String lastName = "Smith"; | ||
|
||
KeyHolder generatedKeyHolder = new GeneratedKeyHolder(); | ||
|
||
int rowsAffected = this.jdbcClient.sql(INSERT_WITH_NAMED_PARAMS) | ||
.param("firstName", firstName) | ||
.param("lastName", lastName) | ||
.update(generatedKeyHolder); | ||
|
||
assertThat(rowsAffected).isEqualTo(1); | ||
assertThat(generatedKeyHolder.getKey()).isEqualTo(expectedId); | ||
assertNumUsers(2); | ||
assertUser(expectedId, firstName, lastName); | ||
} | ||
|
||
private void assertNumUsers(long count) { | ||
long numUsers = this.jdbcClient.sql("select count(id) from users").query(Long.class).single(); | ||
assertThat(numUsers).isEqualTo(count); | ||
} | ||
|
||
private void assertUser(long id, String firstName, String lastName) { | ||
User user = this.jdbcClient.sql("select * from users where id = ?").param(id).query(User.class).single(); | ||
assertThat(user).isEqualTo(new User(id, firstName, lastName)); | ||
} | ||
|
||
record User(long id, String firstName, String lastName) {}; | ||
|
||
} |