2023-07-19 22:19:57 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2022-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 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.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
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-08-01 10:10:59 +08:00
|
|
|
import javax.annotation.Nonnull;
|
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-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 {
|
|
|
|
|
|
|
|
|
|
public static JdbcExecutor connect(final Connection conn) {
|
|
|
|
|
return new JdbcExecutor(conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String paramsToString(Object[] params) {
|
|
|
|
|
return Arrays.toString(params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String paramsToString(final Collection<Object[]> params) {
|
|
|
|
|
if (params == null) {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
|
|
|
|
if (params.isEmpty()) {
|
|
|
|
|
return "[]";
|
|
|
|
|
}
|
|
|
|
|
int iMax = params.size() - 1;
|
|
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
|
b.append('[');
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (Object[] p : params) {
|
|
|
|
|
b.append(Arrays.toString(p));
|
|
|
|
|
if (i == iMax) {
|
|
|
|
|
return b.append(']').toString();
|
|
|
|
|
}
|
|
|
|
|
b.append(',');
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
return b.append(']').toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SimpleJdbcTemplate() {
|
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class JdbcExecutor {
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> List<T> query(String sql, Object[] params, ResultMap<T> resultMap) throws SQLException {
|
|
|
|
|
try (PreparedStatement stmt = this.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> Optional<T> queryFirst(String sql, Object[] params, ResultMap<T> resultMap) throws SQLException {
|
2024-03-18 20:30:53 +08:00
|
|
|
return query(sql, params, resultMap).stream().findFirst();
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public List<Map<String, Object>> query(String sql, Object[] params) throws SQLException {
|
2024-08-01 10:10:59 +08:00
|
|
|
return query(sql, params, ResultMap.mapResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public Optional<Map<String, Object>> queryFirst(String sql, Object[] params) throws SQLException {
|
2024-08-01 10:10:59 +08:00
|
|
|
return queryFirst(sql, params, ResultMap.mapResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public List<DbRecord> queryToRecordList(String sql, Object[] params) throws SQLException {
|
2024-08-01 10:10:59 +08:00
|
|
|
return query(sql, params, ResultMap.recordResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params) throws SQLException {
|
2024-08-01 10:10:59 +08:00
|
|
|
return queryFirst(sql, params, ResultMap.recordResultMap);
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public Optional<String> queryToString(String sql, Object[] params) throws SQLException {
|
2023-08-11 01:34:37 +08:00
|
|
|
return queryFirst(sql, params, (rs, rowNumber) -> rs.getString(1));
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public OptionalInt queryToInt(String sql, Object[] params) throws SQLException {
|
2023-08-11 01:34:37 +08:00
|
|
|
Optional<Integer> result = queryFirst(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 08:46:24 +08:00
|
|
|
public OptionalLong queryToLong(String sql, Object[] params) throws SQLException {
|
2023-08-11 01:34:37 +08:00
|
|
|
Optional<Long> result = queryFirst(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 08:46:24 +08:00
|
|
|
public OptionalDouble queryToDouble(String sql, Object[] params) throws SQLException {
|
2023-08-11 01:34:37 +08:00
|
|
|
Optional<Double> result = queryFirst(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 08:46:24 +08:00
|
|
|
public Optional<BigDecimal> queryToBigDecimal(String sql, Object[] params) throws SQLException {
|
2023-08-11 01:34:37 +08:00
|
|
|
return queryFirst(sql, params, (rs, rowNumber) -> rs.getBigDecimal(1));
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public int update(String sql, Object[] params) throws SQLException {
|
2023-07-19 22:19:57 +08:00
|
|
|
try (PreparedStatement stmt = this.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 遇到异常情况将抛出
|
|
|
|
|
*/
|
|
|
|
|
public <T> List<T> update(@Nonnull String sql, @Nonnull Object[] params, ResultMap<T> resultMap)
|
|
|
|
|
throws SQLException {
|
|
|
|
|
Preconditions.checkNotNull(sql, "The sql could not be null.");
|
|
|
|
|
Preconditions.checkNotNull(params, "The params could not be null.");
|
|
|
|
|
Preconditions.checkNotNull(resultMap, "The resultMap could not be null.");
|
|
|
|
|
final List<T> result = new ArrayList<>();
|
|
|
|
|
try (PreparedStatement stmt = this.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
|
|
|
|
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 08:46:24 +08:00
|
|
|
public List<int[]> batchUpdate(String sql, Collection<Object[]> params, int batchSize) throws SQLException {
|
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);
|
|
|
|
|
|
|
|
|
|
try (PreparedStatement stmt = this.conn.prepareStatement(sql)) {
|
|
|
|
|
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 08:46:24 +08:00
|
|
|
public <E extends Exception> void executeTransaction(@Nonnull final DbOperations<E> operations)
|
|
|
|
|
throws SQLException, E {
|
|
|
|
|
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
2024-03-18 20:30:53 +08:00
|
|
|
final boolean autoCommit = this.conn.getAutoCommit();
|
2023-07-19 22:19:57 +08:00
|
|
|
try {
|
|
|
|
|
this.conn.setAutoCommit(false);
|
2024-10-03 08:46:24 +08:00
|
|
|
operations.execute(this);
|
2024-03-18 20:30:53 +08:00
|
|
|
this.conn.commit();
|
2023-08-11 01:34:37 +08:00
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
2024-03-18 20:30:53 +08:00
|
|
|
this.conn.rollback();
|
2023-07-19 22:19:57 +08:00
|
|
|
throw e;
|
|
|
|
|
}
|
2024-03-18 20:30:53 +08:00
|
|
|
finally {
|
|
|
|
|
this.conn.setAutoCommit(autoCommit);
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 08:46:24 +08:00
|
|
|
public <E extends Exception> void commitIfTrue(@Nonnull final PredicateWithThrowable<E> operations)
|
|
|
|
|
throws SQLException, E {
|
|
|
|
|
Preconditions.checkNotNull(operations, "Operations can not be null.");
|
|
|
|
|
final boolean autoCommit = this.conn.getAutoCommit();
|
|
|
|
|
try {
|
|
|
|
|
this.conn.setAutoCommit(false);
|
|
|
|
|
if (operations.test(this)) {
|
|
|
|
|
this.conn.commit();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.conn.rollback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
this.conn.rollback();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
this.conn.setAutoCommit(autoCommit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@FunctionalInterface
|
|
|
|
|
public interface DbOperations<E extends Exception> {
|
|
|
|
|
void execute(JdbcExecutor jdbcExecutor) throws E;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-19 22:19:57 +08:00
|
|
|
@FunctionalInterface
|
2024-10-03 08:46:24 +08:00
|
|
|
public interface PredicateWithThrowable<E extends Throwable> {
|
|
|
|
|
boolean test(JdbcExecutor jdbcExecutor) throws E;
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
2023-09-21 21:40:18 +08:00
|
|
|
|
|
|
|
|
private static void fillStatement(PreparedStatement stmt, Object[] params) throws SQLException {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-19 22:19:57 +08:00
|
|
|
}
|
|
|
|
|
}
|