1. 删除 DbRecord;2. plusone-commons 删除 SQL Builder。
This commit is contained in:
parent
ce86d97e38
commit
feb421cfa0
@ -1,106 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022-2024 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;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalDouble;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.OptionalLong;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.OptionalTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
* DbRecord
|
||||
*
|
||||
* <p>
|
||||
* 封装 Map<String, Object>,表示一条 DB 记录
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Beta
|
||||
public class DbRecord extends AbstractMapWrapper<String, Object, DbRecord> {
|
||||
|
||||
public DbRecord() {
|
||||
super(new HashMap<>(),
|
||||
k -> AssertTools.checkArgument(StringTools.isNotBlank(k), "Key must has text."),
|
||||
null);
|
||||
}
|
||||
|
||||
public DbRecord(Map<String, Object> map) {
|
||||
super(map,
|
||||
k -> AssertTools.checkArgument(StringTools.isNotBlank(k), "Key must has text."),
|
||||
null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将值强转为 {@link String},并放在 {@link Optional} 中。
|
||||
* 如果 {@code key} 存在,而值不存在,则返回 {@link Optional#empty()}。
|
||||
*/
|
||||
public Optional<String> getValueAsString(String key) {
|
||||
return this.getAndConvert(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将值强转为 {@code int},并放在 {@link OptionalInt} 中。
|
||||
* 如果 {@code key} 存在,而值不存在,则返回 {@link OptionalInt#empty()}。
|
||||
*/
|
||||
@Nonnull
|
||||
public OptionalInt getValueAsInt(String key) {
|
||||
return OptionalTools.toOptionalInt(this.getAndConvert(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将值强转为 {@code long},并放在 {@link OptionalLong} 中。
|
||||
* 如果 {@code key} 存在,而值不存在,则返回 {@link OptionalLong#empty()}。
|
||||
*/
|
||||
@Nonnull
|
||||
public OptionalLong getValueAsLong(String key) {
|
||||
return OptionalTools.toOptionalLong(this.getAndConvert(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将值强转为 {@code double},并放在 {@link OptionalDouble} 中。
|
||||
* 如果 {@code key} 存在,而值不存在,则返回 {@link OptionalDouble#empty()}。
|
||||
*/
|
||||
@Nonnull
|
||||
public OptionalDouble getValueAsDouble(String key) {
|
||||
return OptionalTools.toOptionalDouble(this.getAndConvert(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DbRecord getSelf() {
|
||||
return this;
|
||||
}
|
||||
|
||||
private static final String STR_PREFIX = DbRecord.class.getName() + '@';
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return STR_PREFIX + super.toString();
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@ -444,23 +445,28 @@ class JdbcOperationSupport {
|
||||
// #region - 参数校验
|
||||
|
||||
private static void assertConnectionNotNull(Connection conn) {
|
||||
AssertTools.checkArgumentNotNull(conn, "The argument \"conn\" could not be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(conn),
|
||||
"The argument \"conn\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertSqlNotNull(String sql) {
|
||||
AssertTools.checkArgumentNotNull(sql, "The argument \"sql\" could not be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sql),
|
||||
"The argument \"sql\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertRowMapperNotNull(RowMapper<?> rowMapper) {
|
||||
AssertTools.checkArgumentNotNull(rowMapper, "The argument \"rowMapper\" could not be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(rowMapper),
|
||||
"The argument \"rowMapper\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertResultHandlerNotNull(ResultHandler<?> resultHandler) {
|
||||
AssertTools.checkArgumentNotNull(resultHandler, "The argument \"resultHandler\" could not be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(resultHandler),
|
||||
"The argument \"resultHandler\" could not be null.");
|
||||
}
|
||||
|
||||
private static void assertClazzNotNull(Class<?> clazz) {
|
||||
AssertTools.checkArgumentNotNull(clazz, "The argument \"clazz\" could not be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(clazz),
|
||||
"The argument \"clazz\" could not be null.");
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
@ -79,15 +79,6 @@ interface JdbcOperations {
|
||||
List<Map<String, Object>> queryList(String sql, Object[] params)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行查询,每一行数据映射为 {@link DbRecord},返回结果列表
|
||||
*
|
||||
* @param sql SQL
|
||||
* @param params 参数列表
|
||||
*/
|
||||
List<DbRecord> queryRecordList(String sql, Object[] params)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行查询,将查询结果的每一行数据按照指定逻辑进行处理,返回结果列表
|
||||
*
|
||||
@ -114,14 +105,6 @@ interface JdbcOperations {
|
||||
List<Map<String, Object>> queryList(String sql)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行查询,每一行数据映射为 {@link DbRecord},返回结果列表
|
||||
*
|
||||
* @param sql SQL
|
||||
*/
|
||||
List<DbRecord> queryRecordList(String sql)
|
||||
throws SQLException;
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
@ -156,15 +139,6 @@ interface JdbcOperations {
|
||||
Optional<Map<String, Object>> queryFirst(String sql, Object[] params)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行查询,将第一行数据转为 DbRecord
|
||||
*
|
||||
* @param sql SQL
|
||||
* @param params 参数
|
||||
*/
|
||||
Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 查询第一行第一列,并转换为字符串
|
||||
*
|
||||
@ -237,14 +211,6 @@ interface JdbcOperations {
|
||||
Optional<Map<String, Object>> queryFirst(String sql)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 执行查询,将第一行数据转为 DbRecord
|
||||
*
|
||||
* @param sql SQL
|
||||
*/
|
||||
Optional<DbRecord> queryFirstRecord(String sql)
|
||||
throws SQLException;
|
||||
|
||||
/**
|
||||
* 查询第一行第一列,并转换为字符串
|
||||
*
|
||||
|
@ -48,10 +48,6 @@ public interface RowMapper<T> {
|
||||
return result;
|
||||
};
|
||||
|
||||
/** 每一行数据转换为 {@link DbRecord} */
|
||||
public static final RowMapper<DbRecord> RECORD_MAPPER =
|
||||
(rs, rowNumber) -> new DbRecord(HASH_MAP_MAPPER.mapRow(rs, rowNumber));
|
||||
|
||||
/** 默认实现的将 {@link ResultSet} 转换为 Java Bean 的 {@link RowMapper}。 */
|
||||
public static <T> RowMapper<T> beanRowMapper(Class<T> beanType) throws SQLException {
|
||||
return DefaultBeanRowMapper.of(beanType);
|
||||
|
@ -107,15 +107,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<DbRecord> queryRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcOperationSupport.queryList(conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
||||
@ -144,16 +135,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<DbRecord> queryRecordList(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
return JdbcOperationSupport
|
||||
.queryList(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
@ -189,17 +170,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
final DbRecord result = JdbcOperationSupport
|
||||
.queryFirst(conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||
@ -283,17 +253,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||
throws SQLException {
|
||||
try (Connection conn = this.dataSource.getConnection()) {
|
||||
final DbRecord result = JdbcOperationSupport
|
||||
.queryFirst(conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<String> queryFirstString(String sql)
|
||||
@ -555,13 +514,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
return JdbcOperationSupport.queryList(this.conn, sql, params, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<DbRecord> queryRecordList(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
return JdbcOperationSupport.queryList(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public <T> List<T> queryList(String sql, RowMapper<T> rowMapper)
|
||||
@ -585,14 +537,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.HASH_MAP_MAPPER);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public List<DbRecord> queryRecordList(String sql)
|
||||
throws SQLException {
|
||||
return JdbcOperationSupport
|
||||
.queryList(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region - queryFirst
|
||||
@ -622,15 +566,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<DbRecord> queryFirstRecord(String sql, Object[] params)
|
||||
throws SQLException {
|
||||
final DbRecord result = JdbcOperationSupport
|
||||
.queryFirst(this.conn, sql, params, RowMapper.RECORD_MAPPER);
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<String> queryFirstString(String sql, Object[] params)
|
||||
@ -698,15 +633,6 @@ public class SimpleJdbcTemplate implements JdbcOperations {
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<DbRecord> queryFirstRecord(String sql)
|
||||
throws SQLException {
|
||||
final DbRecord result = JdbcOperationSupport
|
||||
.queryFirst(this.conn, sql, ParamBuilder.EMPTY_OBJECT_ARRAY, RowMapper.RECORD_MAPPER);
|
||||
return Optional.ofNullable(result);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public Optional<String> queryFirstString(String sql)
|
||||
|
@ -2,7 +2,6 @@ package xyz.zhouxy.jdbc.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static xyz.zhouxy.jdbc.ParamBuilder.*;
|
||||
import static xyz.zhouxy.plusone.commons.sql.JdbcSql.*;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
@ -22,11 +21,9 @@ import org.slf4j.LoggerFactory;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import xyz.zhouxy.jdbc.DbRecord;
|
||||
import xyz.zhouxy.jdbc.RowMapper;
|
||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate;
|
||||
import xyz.zhouxy.jdbc.SimpleJdbcTemplate.JdbcExecutor;
|
||||
import xyz.zhouxy.plusone.commons.sql.SQL;
|
||||
import xyz.zhouxy.plusone.commons.util.IdGenerator;
|
||||
import xyz.zhouxy.plusone.commons.util.IdWorker;
|
||||
|
||||
@ -85,24 +82,24 @@ class SimpleJdbcTemplateTests {
|
||||
@Test
|
||||
void testQuery() throws SQLException {
|
||||
Object[] ids = buildParams(5, 9, 13, 14, 17, 20, 108);
|
||||
String sql = SQL.newJdbcSql()
|
||||
.SELECT("id", "username", "account_status")
|
||||
.FROM("sys_account")
|
||||
.WHERE(IN("id", ids))
|
||||
.toString();
|
||||
String sql = "SELECT id, username, account_status"
|
||||
+ "\n FROM sys_account"
|
||||
+ "\n WHERE id IN ("
|
||||
+ "\n ?, ?, ?, ?, ?, ?, ?"
|
||||
+ "\n )";
|
||||
log.info(sql);
|
||||
List<DbRecord> rs = jdbcTemplate.queryRecordList(sql, ids);
|
||||
for (DbRecord dbRecord : rs) {
|
||||
List<Map<String, Object>> rs = jdbcTemplate.queryList(sql, ids);
|
||||
for (Map<String, Object> dbRecord : rs) {
|
||||
log.info("{}", dbRecord);
|
||||
}
|
||||
assertEquals(
|
||||
Lists.newArrayList(
|
||||
new DbRecord(ImmutableMap.of("id", 5L, "account_status", "0", "username", "zhouxy5")),
|
||||
new DbRecord(ImmutableMap.of("id", 9L, "account_status", "0", "username", "zhouxy9")),
|
||||
new DbRecord(ImmutableMap.of("id", 13L, "account_status", "1", "username", "zhouxy13")),
|
||||
new DbRecord(ImmutableMap.of("id", 14L, "account_status", "1", "username", "zhouxy14")),
|
||||
new DbRecord(ImmutableMap.of("id", 17L, "account_status", "1", "username", "zhouxy17")),
|
||||
new DbRecord(ImmutableMap.of("id", 20L, "account_status", "2", "username", "zhouxy20"))
|
||||
ImmutableMap.of("id", 5L, "account_status", "0", "username", "zhouxy5"),
|
||||
ImmutableMap.of("id", 9L, "account_status", "0", "username", "zhouxy9"),
|
||||
ImmutableMap.of("id", 13L, "account_status", "1", "username", "zhouxy13"),
|
||||
ImmutableMap.of("id", 14L, "account_status", "1", "username", "zhouxy14"),
|
||||
ImmutableMap.of("id", 17L, "account_status", "1", "username", "zhouxy17"),
|
||||
ImmutableMap.of("id", 20L, "account_status", "2", "username", "zhouxy20")
|
||||
),
|
||||
rs
|
||||
);
|
||||
@ -140,16 +137,16 @@ class SimpleJdbcTemplateTests {
|
||||
|
||||
@Test
|
||||
void testUpdate() throws SQLException {
|
||||
List<DbRecord> keys = jdbcTemplate.update(
|
||||
List<Map<String, Object>> keys = jdbcTemplate.update(
|
||||
"UPDATE sys_account SET account_status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ? AND version = ?",
|
||||
buildParams("7", 886L, 20L, 88L),
|
||||
RowMapper.RECORD_MAPPER);
|
||||
RowMapper.HASH_MAP_MAPPER);
|
||||
assertEquals(1, keys.size());
|
||||
log.info("keys: {}", keys);
|
||||
keys = jdbcTemplate.update(
|
||||
"UPDATE sys_account SET account_status = ?, version = version + 1, update_time = now(), updated_by = ? WHERE id = ? AND version = ?",
|
||||
buildParams("-1", 886L, 20L, 88L),
|
||||
RowMapper.RECORD_MAPPER);
|
||||
RowMapper.HASH_MAP_MAPPER);
|
||||
assertEquals(0, keys.size());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user