This commit is contained in:
Looly 2023-04-18 22:40:05 +08:00
parent ff382aff85
commit 9d3551389e
5 changed files with 47 additions and 16 deletions

View File

@ -321,7 +321,7 @@ public class Dict extends CustomKeyMap<String, Object> implements TypeGetter<Str
* @return Dict 结果 * @return Dict 结果
* @since 4.0.10 * @since 4.0.10
*/ */
public Dict filter(final String... keys) { public Dict filterNew(final String... keys) {
final Dict result = new Dict(keys.length, 1); final Dict result = new Dict(keys.length, 1);
for (final String key : keys) { for (final String key : keys) {
@ -332,6 +332,17 @@ public class Dict extends CustomKeyMap<String, Object> implements TypeGetter<Str
return result; return result;
} }
/**
* 过滤Map去除指定键值对如果键不存在跳过
*
* @param keys 键列表
* @return Dict 结果
* @since 4.0.10
*/
public Dict removeNew(final String... keys) {
return MapUtil.removeAny(this.clone(), keys);
}
// -------------------------------------------------------------------- Set start // -------------------------------------------------------------------- Set start
/** /**

View File

@ -951,13 +951,13 @@ public class MapUtil extends MapGetUtil {
* *
* @param <K> Key类型 * @param <K> Key类型
* @param <V> Value类型 * @param <V> Value类型
* @param <T> Map类型
* @param map Map * @param map Map
* @param keys 键列表 * @param keys 键列表
* @return 修改后的key * @return 修改后的key
* @since 5.0.5
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> removeAny(final Map<K, V> map, final K... keys) { public static <K, V, T extends Map<K, V>> T removeAny(final T map, final K... keys) {
for (final K key : keys) { for (final K key : keys) {
map.remove(key); map.remove(key);
} }

View File

@ -83,4 +83,16 @@ public class DictTest {
private String username; private String username;
private String nickname; private String nickname;
} }
@Test
public void removeNewTest(){
final Dict dict = Dict.of()
.set("key1", 1)//int
.set("key2", 1000L)//long
.set("key3", DateTime.now());//Date
final Dict dict2 = dict.removeNew("key1");
Assertions.assertEquals(3, dict.size());
Assertions.assertEquals(2, dict2.size());
}
} }

View File

@ -145,9 +145,9 @@ public class DialectRunner implements Serializable {
* @throws DbRuntimeException SQL执行异常 * @throws DbRuntimeException SQL执行异常
*/ */
public int insertOrUpdate(final Connection conn, final Entity record, final String... keys) throws DbRuntimeException { public int insertOrUpdate(final Connection conn, final Entity record, final String... keys) throws DbRuntimeException {
final Entity where = record.filter(keys); final Entity where = record.filterNew(keys);
if (MapUtil.isNotEmpty(where) && count(conn, Query.of(where)) > 0) { if (MapUtil.isNotEmpty(where) && count(conn, Query.of(where)) > 0) {
return update(conn, record, where); return update(conn, record.removeNew(keys), where);
} else { } else {
return insert(conn, record)[0]; return insert(conn, record)[0];
} }

View File

@ -107,7 +107,10 @@ public class Entity extends Dict {
/* 字段名列表,用于限制加入的字段的值 */ /* 字段名列表,用于限制加入的字段的值 */
private Set<String> fieldNames; private Set<String> fieldNames;
// --------------------------------------------------------------- Constructor start // region ----- Constructor
/**
* 构造
*/
public Entity() { public Entity() {
} }
@ -132,7 +135,7 @@ public class Entity extends Dict {
super(caseInsensitive); super(caseInsensitive);
this.tableName = tableName; this.tableName = tableName;
} }
// --------------------------------------------------------------- Constructor end // endregion
// --------------------------------------------------------------- Getters and Setters start // --------------------------------------------------------------- Getters and Setters start
@ -259,7 +262,7 @@ public class Entity extends Dict {
* @since 4.0.10 * @since 4.0.10
*/ */
@Override @Override
public Entity filter(final String... keys) { public Entity filterNew(final String... keys) {
final Entity result = new Entity(this.tableName); final Entity result = new Entity(this.tableName);
result.setFieldNames(this.fieldNames); result.setFieldNames(this.fieldNames);
@ -271,6 +274,11 @@ public class Entity extends Dict {
return result; return result;
} }
@Override
public Entity removeNew(final String... keys) {
return (Entity) super.removeNew(keys);
}
// -------------------------------------------------------------------- Put and Set start // -------------------------------------------------------------------- Put and Set start
@Override @Override
public Entity set(final String field, final Object value) { public Entity set(final String field, final Object value) {