2023-07-19 22:19:57 +08:00
|
|
|
/*
|
2024-10-03 10:23:17 +08:00
|
|
|
* Copyright 2022-2024 the original author or authors.
|
2023-07-19 22:19:57 +08:00
|
|
|
*
|
|
|
|
|
* 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 xyz.zhouxy.jdbc;
|
|
|
|
|
|
2023-09-21 21:40:18 +08:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
import java.sql.SQLException;
|
2024-08-01 10:10:59 +08:00
|
|
|
import java.sql.Statement;
|
2023-09-21 21:40:18 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
2024-10-21 20:56:38 +08:00
|
|
|
import java.util.Collections;
|
2023-09-21 21:40:18 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.OptionalDouble;
|
|
|
|
|
import java.util.OptionalInt;
|
|
|
|
|
import java.util.OptionalLong;
|
2024-10-21 20:56:38 +08:00
|
|
|
|
2024-08-01 10:10:59 +08:00
|
|
|
import javax.annotation.Nonnull;
|
2024-10-21 20:56:38 +08:00
|
|
|
import javax.annotation.Nullable;
|
2024-10-03 10:23:17 +08:00
|
|
|
import javax.sql.DataSource;
|
2023-09-21 21:40:18 +08:00
|
|
|
|
2023-07-19 22:19:57 +08:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
import com.google.common.collect.Lists;
|
2023-09-21 21:40:18 +08:00
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
import xyz.zhouxy.plusone.commons.function.ThrowingConsumer;
|
|
|
|
|
import xyz.zhouxy.plusone.commons.function.ThrowingPredicate;
|
2024-06-17 09:22:06 +08:00
|
|
|
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
2023-07-19 22:19:57 +08:00
|
|
|
|
|
|
|
|
public class SimpleJdbcTemplate {
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
@Nonnull
|
2024-10-03 10:23:17 +08:00
|
|
|
private final DataSource dataSource;
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public SimpleJdbcTemplate(@Nonnull DataSource dataSource) {
|
|
|
|
|
Preconditions.checkNotNull(dataSource);
|
2024-10-03 10:23:17 +08:00
|
|
|
this.dataSource = dataSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> List<T> query(String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.query(conn, sql, params, resultMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> Optional<T> queryFirst(String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirst(conn, sql, params, resultMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> query(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.query(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirst(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DbRecord> queryToRecordList(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToRecordList(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirstRecord(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<String> queryToString(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToString(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalInt queryToInt(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToInt(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalLong queryToLong(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToLong(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalDouble queryToDouble(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToDouble(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<BigDecimal> queryToBigDecimal(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToBigDecimal(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int update(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.update(conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行 SQL 并更新后的数据
|
|
|
|
|
*
|
|
|
|
|
* @param sql 要执行的 SQL 语句
|
|
|
|
|
* @param params 参数
|
|
|
|
|
* @param resultMap 结果映射规则
|
|
|
|
|
*
|
|
|
|
|
* @return 更新的数据
|
|
|
|
|
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
|
|
|
|
*/
|
2024-10-21 20:56:38 +08:00
|
|
|
public <T> List<T> update(String sql, Object[] params, ResultMap<T> resultMap)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.update(conn, sql, params, resultMap);
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public <T> List<T> query(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.query(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> Optional<T> queryFirst(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> query(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.query(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Map<String, Object>> queryFirst(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DbRecord> queryToRecordList(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToRecordList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<DbRecord> queryFirstRecord(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryFirstRecord(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<String> queryToString(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToString(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalInt queryToInt(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToInt(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalLong queryToLong(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToLong(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalDouble queryToDouble(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToDouble(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<BigDecimal> queryToBigDecimal(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.queryToBigDecimal(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int update(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.update(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行 SQL 并更新后的数据
|
|
|
|
|
*
|
|
|
|
|
* @param sql 要执行的 SQL 语句
|
|
|
|
|
* @param params 参数
|
|
|
|
|
* @param resultMap 结果映射规则
|
|
|
|
|
*
|
|
|
|
|
* @return 更新的数据
|
|
|
|
|
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
|
|
|
|
*/
|
|
|
|
|
public <T> List<T> update(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.update(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<int[]> batchUpdate(String sql, @Nullable Collection<Object[]> params, int batchSize)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException {
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
return JdbcExecutor.batchUpdate(conn, sql, params, batchSize);
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public <E extends Exception> void executeTransaction(@Nonnull final ThrowingConsumer<JdbcExecutor, E> operations)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException, E {
|
|
|
|
|
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
final boolean autoCommit = conn.getAutoCommit();
|
|
|
|
|
try {
|
|
|
|
|
conn.setAutoCommit(false);
|
2024-10-21 20:56:38 +08:00
|
|
|
operations.accept(new JdbcExecutor(conn));
|
2024-10-03 10:23:17 +08:00
|
|
|
conn.commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
conn.rollback();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
conn.setAutoCommit(autoCommit);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public <E extends Exception> void commitIfTrue(@Nonnull final ThrowingPredicate<JdbcExecutor, E> operations)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException, E {
|
|
|
|
|
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
|
|
|
|
try (Connection conn = this.dataSource.getConnection()) {
|
|
|
|
|
final boolean autoCommit = conn.getAutoCommit();
|
|
|
|
|
try {
|
|
|
|
|
conn.setAutoCommit(false);
|
|
|
|
|
if (operations.test(new JdbcExecutor(conn))) {
|
|
|
|
|
conn.commit();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
conn.rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
conn.rollback();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
conn.setAutoCommit(autoCommit);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
public static final class JdbcExecutor {
|
2023-07-19 22:19:57 +08:00
|
|
|
|
|
|
|
|
private final Connection conn;
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
private JdbcExecutor(Connection conn) {
|
2023-07-19 22:19:57 +08:00
|
|
|
this.conn = conn;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
public <T> List<T> query(String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.query(this.conn, sql, params, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> Optional<T> queryFirst(String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirst(this.conn, sql, params, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> query(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.query(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirst(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DbRecord> queryToRecordList(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToRecordList(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirstRecord(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<String> queryToString(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToString(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalInt queryToInt(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToInt(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalLong queryToLong(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToLong(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalDouble queryToDouble(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToDouble(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<BigDecimal> queryToBigDecimal(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToBigDecimal(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int update(String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.update(this.conn, sql, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行 SQL 并更新后的数据
|
|
|
|
|
*
|
|
|
|
|
* @param sql 要执行的 SQL 语句
|
|
|
|
|
* @param params 参数
|
|
|
|
|
* @param resultMap 结果映射规则
|
|
|
|
|
*
|
|
|
|
|
* @return 更新的数据
|
|
|
|
|
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
|
|
|
|
*/
|
2024-10-21 20:56:38 +08:00
|
|
|
public <T> List<T> update(String sql, Object[] params, ResultMap<T> resultMap)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.update(this.conn, sql, params, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public List<int[]> batchUpdate(String sql, @Nullable Collection<Object[]> params, int batchSize)
|
2024-10-03 10:23:17 +08:00
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.batchUpdate(this.conn, sql, params, batchSize);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 20:56:38 +08:00
|
|
|
public <T> List<T> query(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> Optional<T> queryFirst(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Map<String, Object>> query(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.query(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<Map<String, Object>> queryFirst(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DbRecord> queryToRecordList(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToRecordList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<DbRecord> queryFirstRecord(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryFirstRecord(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<String> queryToString(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToString(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalInt queryToInt(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToInt(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalLong queryToLong(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToLong(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OptionalDouble queryToDouble(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToDouble(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<BigDecimal> queryToBigDecimal(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.queryToBigDecimal(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int update(String sql)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.update(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行 SQL 并更新后的数据
|
|
|
|
|
*
|
|
|
|
|
* @param sql 要执行的 SQL 语句
|
|
|
|
|
* @param params 参数
|
|
|
|
|
* @param resultMap 结果映射规则
|
|
|
|
|
*
|
|
|
|
|
* @return 更新的数据
|
|
|
|
|
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
|
|
|
|
*/
|
|
|
|
|
public <T> List<T> update(String sql, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return JdbcExecutor.update(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, resultMap);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static <T> List<T> query(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
2024-10-21 20:56:38 +08:00
|
|
|
assertConnectionNotNull(conn);
|
|
|
|
|
assertSqlNotNull(sql);
|
|
|
|
|
assertResultMapNotNull(resultMap);
|
2024-10-03 10:23:17 +08:00
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
2023-09-21 21:40:18 +08:00
|
|
|
fillStatement(stmt, params);
|
2023-07-19 22:19:57 +08:00
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
|
List<T> result = new ArrayList<>();
|
|
|
|
|
int rowNumber = 0;
|
|
|
|
|
while (rs.next()) {
|
|
|
|
|
T e = resultMap.map(rs, rowNumber++);
|
|
|
|
|
result.add(e);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static <T> Optional<T> queryFirst(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return query(conn, sql, params, resultMap).stream().findFirst();
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static List<Map<String, Object>> query(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return query(conn, sql, params, ResultMap.mapResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static Optional<Map<String, Object>> queryFirst(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return queryFirst(conn, sql, params, ResultMap.mapResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static List<DbRecord> queryToRecordList(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return query(conn, sql, params, ResultMap.recordResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static Optional<DbRecord> queryFirstRecord(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return queryFirst(conn, sql, params, ResultMap.recordResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static Optional<String> queryToString(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getString(1));
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static OptionalInt queryToInt(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
Optional<Integer> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getInt(1));
|
2024-06-17 09:22:06 +08:00
|
|
|
return OptionalTools.toOptionalInt(result);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static OptionalLong queryToLong(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
Optional<Long> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getLong(1));
|
2024-06-17 09:22:06 +08:00
|
|
|
return OptionalTools.toOptionalLong(result);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static OptionalDouble queryToDouble(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
Optional<Double> result = queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getDouble(1));
|
2024-06-17 09:22:06 +08:00
|
|
|
return OptionalTools.toOptionalDouble(result);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static Optional<BigDecimal> queryToBigDecimal(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
return queryFirst(conn, sql, params, (rs, rowNumber) -> rs.getBigDecimal(1));
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static int update(Connection conn, String sql, Object[] params)
|
|
|
|
|
throws SQLException {
|
2024-10-21 20:56:38 +08:00
|
|
|
assertConnectionNotNull(conn);
|
|
|
|
|
assertSqlNotNull(sql);
|
2024-10-03 10:23:17 +08:00
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
2023-09-21 21:40:18 +08:00
|
|
|
fillStatement(stmt, params);
|
2023-07-19 22:19:57 +08:00
|
|
|
return stmt.executeUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 10:10:59 +08:00
|
|
|
/**
|
|
|
|
|
* 执行 SQL 并更新后的数据
|
|
|
|
|
*
|
|
|
|
|
* @param sql 要执行的 SQL 语句
|
|
|
|
|
* @param params 参数
|
|
|
|
|
* @param resultMap 结果映射规则
|
|
|
|
|
*
|
|
|
|
|
* @return 更新的数据
|
|
|
|
|
* @throws SQLException 执行 SQL 遇到异常情况将抛出
|
|
|
|
|
*/
|
2024-10-03 10:23:17 +08:00
|
|
|
private static <T> List<T> update(Connection conn, String sql, Object[] params, ResultMap<T> resultMap)
|
2024-08-01 10:10:59 +08:00
|
|
|
throws SQLException {
|
2024-10-21 20:56:38 +08:00
|
|
|
assertConnectionNotNull(conn);
|
|
|
|
|
assertSqlNotNull(sql);
|
|
|
|
|
assertResultMapNotNull(resultMap);
|
2024-08-01 10:10:59 +08:00
|
|
|
final List<T> result = new ArrayList<>();
|
2024-10-03 10:23:17 +08:00
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
2024-08-01 10:10:59 +08:00
|
|
|
fillStatement(stmt, params);
|
|
|
|
|
stmt.executeUpdate();
|
|
|
|
|
try (ResultSet generatedKeys = stmt.getGeneratedKeys();) {
|
|
|
|
|
int rowNumber = 0;
|
|
|
|
|
while (generatedKeys.next()) {
|
|
|
|
|
T e = resultMap.map(generatedKeys, rowNumber++);
|
|
|
|
|
result.add(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static List<int[]> batchUpdate(Connection conn, String sql, Collection<Object[]> params, int batchSize)
|
|
|
|
|
throws SQLException {
|
2024-10-21 20:56:38 +08:00
|
|
|
assertConnectionNotNull(conn);
|
|
|
|
|
assertSqlNotNull(sql);
|
|
|
|
|
|
|
|
|
|
if (params == null || params.isEmpty()) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
int executeCount = params.size() / batchSize;
|
|
|
|
|
executeCount = (params.size() % batchSize == 0) ? executeCount : (executeCount + 1);
|
|
|
|
|
List<int[]> result = Lists.newArrayListWithCapacity(executeCount);
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
2023-07-19 22:19:57 +08:00
|
|
|
int i = 0;
|
|
|
|
|
for (Object[] ps : params) {
|
|
|
|
|
i++;
|
2024-10-03 08:46:24 +08:00
|
|
|
fillStatement(stmt, ps);
|
2023-07-19 22:19:57 +08:00
|
|
|
stmt.addBatch();
|
|
|
|
|
if (i % batchSize == 0 || i >= params.size()) {
|
|
|
|
|
int[] n = stmt.executeBatch();
|
|
|
|
|
result.add(n);
|
|
|
|
|
stmt.clearBatch();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-03 08:46:24 +08:00
|
|
|
return result;
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-03 10:23:17 +08:00
|
|
|
private static void fillStatement(PreparedStatement stmt, Object[] params)
|
|
|
|
|
throws SQLException {
|
2023-09-21 21:40:18 +08:00
|
|
|
if (params != null && params.length > 0) {
|
|
|
|
|
Object param;
|
|
|
|
|
for (int i = 0; i < params.length; i++) {
|
|
|
|
|
param = params[i];
|
|
|
|
|
if (param instanceof java.sql.Date) {
|
|
|
|
|
stmt.setDate(i + 1, (java.sql.Date) param);
|
|
|
|
|
}
|
|
|
|
|
else if (param instanceof java.sql.Time) {
|
|
|
|
|
stmt.setTime(i + 1, (java.sql.Time) param);
|
|
|
|
|
}
|
|
|
|
|
else if (param instanceof java.sql.Timestamp) {
|
|
|
|
|
stmt.setTimestamp(i + 1, (java.sql.Timestamp) param);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
stmt.setObject(i + 1, param);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-21 20:56:38 +08:00
|
|
|
|
|
|
|
|
private static void assertConnectionNotNull(Connection conn) {
|
|
|
|
|
Preconditions.checkArgument(conn != null, "The connection could not be null.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertSqlNotNull(String sql) {
|
|
|
|
|
Preconditions.checkArgument(sql != null, "The sql could not be null.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void assertResultMapNotNull(ResultMap<?> resultMap) {
|
|
|
|
|
Preconditions.checkArgument(resultMap != null, "The resultMap could not be null.");
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
}
|