Introduce overloaded methods in JdbcTestUtils that accept JdbcClient
Closes gh-31066
This commit is contained in:
parent
e0fb777325
commit
ca118ca4a0
|
|
@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.SqlParameterValue;
|
||||
import org.springframework.jdbc.core.simple.JdbcClient;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
|
@ -53,7 +54,19 @@ public abstract class JdbcTestUtils {
|
|||
* @return the number of rows in the table
|
||||
*/
|
||||
public static int countRowsInTable(JdbcOperations jdbcTemplate, String tableName) {
|
||||
return countRowsInTableWhere(jdbcTemplate, tableName, null);
|
||||
return countRowsInTable(JdbcClient.create(jdbcTemplate), tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the rows in the given table.
|
||||
* @param jdbcClient the {@link JdbcClient} with which to perform JDBC
|
||||
* operations
|
||||
* @param tableName name of the table to count rows in
|
||||
* @return the number of rows in the table
|
||||
* @since 6.1
|
||||
*/
|
||||
public static int countRowsInTable(JdbcClient jdbcClient, String tableName) {
|
||||
return countRowsInTableWhere(jdbcClient, tableName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -74,12 +87,33 @@ public abstract class JdbcTestUtils {
|
|||
public static int countRowsInTableWhere(
|
||||
JdbcOperations jdbcTemplate, String tableName, @Nullable String whereClause) {
|
||||
|
||||
return countRowsInTableWhere(JdbcClient.create(jdbcTemplate), tableName, whereClause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the rows in the given table, using the provided {@code WHERE} clause.
|
||||
* <p>If the provided {@code WHERE} clause contains text, it will be prefixed
|
||||
* with {@code " WHERE "} and then appended to the generated {@code SELECT}
|
||||
* statement. For example, if the provided table name is {@code "person"} and
|
||||
* the provided where clause is {@code "name = 'Bob' and age > 25"}, the
|
||||
* resulting SQL statement to execute will be
|
||||
* {@code "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"}.
|
||||
* @param jdbcClient the {@link JdbcClient} with which to perform JDBC
|
||||
* operations
|
||||
* @param tableName the name of the table to count rows in
|
||||
* @param whereClause the {@code WHERE} clause to append to the query
|
||||
* @return the number of rows in the table that match the provided
|
||||
* {@code WHERE} clause
|
||||
* @since 6.1
|
||||
*/
|
||||
public static int countRowsInTableWhere(
|
||||
JdbcClient jdbcClient, String tableName, @Nullable String whereClause) {
|
||||
|
||||
String sql = "SELECT COUNT(0) FROM " + tableName;
|
||||
if (StringUtils.hasText(whereClause)) {
|
||||
sql += " WHERE " + whereClause;
|
||||
}
|
||||
Integer result = jdbcTemplate.queryForObject(sql, Integer.class);
|
||||
return (result != null ? result : 0);
|
||||
return jdbcClient.sql(sql).query(Integer.class).single();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -90,9 +124,21 @@ public abstract class JdbcTestUtils {
|
|||
* @return the total number of rows deleted from all specified tables
|
||||
*/
|
||||
public static int deleteFromTables(JdbcOperations jdbcTemplate, String... tableNames) {
|
||||
return deleteFromTables(JdbcClient.create(jdbcTemplate), tableNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all rows from the specified tables.
|
||||
* @param jdbcClient the {@link JdbcClient} with which to perform JDBC
|
||||
* operations
|
||||
* @param tableNames the names of the tables to delete from
|
||||
* @return the total number of rows deleted from all specified tables
|
||||
* @since 6.1
|
||||
*/
|
||||
public static int deleteFromTables(JdbcClient jdbcClient, String... tableNames) {
|
||||
int totalRowCount = 0;
|
||||
for (String tableName : tableNames) {
|
||||
int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);
|
||||
int rowCount = jdbcClient.sql("DELETE FROM " + tableName).update();
|
||||
totalRowCount += rowCount;
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Deleted " + rowCount + " rows from table " + tableName);
|
||||
|
|
@ -124,11 +170,38 @@ public abstract class JdbcTestUtils {
|
|||
public static int deleteFromTableWhere(
|
||||
JdbcOperations jdbcTemplate, String tableName, String whereClause, Object... args) {
|
||||
|
||||
return deleteFromTableWhere(JdbcClient.create(jdbcTemplate), tableName, whereClause, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete rows from the given table, using the provided {@code WHERE} clause.
|
||||
* <p>If the provided {@code WHERE} clause contains text, it will be prefixed
|
||||
* with {@code " WHERE "} and then appended to the generated {@code DELETE}
|
||||
* statement. For example, if the provided table name is {@code "person"} and
|
||||
* the provided where clause is {@code "name = 'Bob' and age > 25"}, the
|
||||
* resulting SQL statement to execute will be
|
||||
* {@code "DELETE FROM person WHERE name = 'Bob' and age > 25"}.
|
||||
* <p>As an alternative to hard-coded values, the {@code "?"} placeholder can
|
||||
* be used within the {@code WHERE} clause, binding to the given arguments.
|
||||
* @param jdbcClient the {@link JdbcClient} with which to perform JDBC
|
||||
* operations
|
||||
* @param tableName the name of the table to delete rows from
|
||||
* @param whereClause the {@code WHERE} clause to append to the query
|
||||
* @param args arguments to bind to the query (leaving it to the PreparedStatement
|
||||
* to guess the corresponding SQL type); may also contain {@link SqlParameterValue}
|
||||
* objects which indicate not only the argument value but also the SQL type and
|
||||
* optionally the scale.
|
||||
* @return the number of rows deleted from the table
|
||||
* @since 6.1
|
||||
*/
|
||||
public static int deleteFromTableWhere(
|
||||
JdbcClient jdbcClient, String tableName, String whereClause, Object... args) {
|
||||
|
||||
String sql = "DELETE FROM " + tableName;
|
||||
if (StringUtils.hasText(whereClause)) {
|
||||
sql += " WHERE " + whereClause;
|
||||
}
|
||||
int rowCount = jdbcTemplate.update(sql, args);
|
||||
int rowCount = jdbcClient.sql(sql).params(args).update();
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Deleted " + rowCount + " rows from table " + tableName);
|
||||
}
|
||||
|
|
@ -137,12 +210,23 @@ public abstract class JdbcTestUtils {
|
|||
|
||||
/**
|
||||
* Drop the specified tables.
|
||||
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC operations
|
||||
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC
|
||||
* operations
|
||||
* @param tableNames the names of the tables to drop
|
||||
*/
|
||||
public static void dropTables(JdbcOperations jdbcTemplate, String... tableNames) {
|
||||
dropTables(JdbcClient.create(jdbcTemplate), tableNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the specified tables.
|
||||
* @param jdbcClient the {@link JdbcClient} with which to perform JDBC operations
|
||||
* @param tableNames the names of the tables to drop
|
||||
* @since 6.1
|
||||
*/
|
||||
public static void dropTables(JdbcClient jdbcClient, String... tableNames) {
|
||||
for (String tableName : tableNames) {
|
||||
jdbcTemplate.execute("DROP TABLE " + tableName);
|
||||
jdbcClient.sql("DROP TABLE " + tableName).update();
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Dropped table " + tableName);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue