feat: updateWrapper add setApply api

This commit is contained in:
yulichang 2024-09-14 11:23:52 +08:00
parent 24212523d7
commit 18b2394e7e
3 changed files with 114 additions and 47 deletions

View File

@ -12,14 +12,11 @@ import com.github.yulichang.toolkit.ReflectionKit;
import com.github.yulichang.toolkit.*;
import com.github.yulichang.wrapper.interfaces.Update;
import com.github.yulichang.wrapper.interfaces.UpdateChain;
import lombok.AllArgsConstructor;
import com.github.yulichang.wrapper.segments.FuncConsumer;
import lombok.Data;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -111,42 +108,36 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
@Override
public <R> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, Object val, String mapping) {
return maybeDo(condition, () -> {
if (Objects.isNull(updateSet)) {
updateSet = new ArrayList<>();
}
updateSet.add(new UpdateSet(column, val, mapping, false, null));
});
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null)));
}
@Override
public <R, V> UpdateJoinWrapper<T> set(boolean condition, SFunction<R, ?> column, SFunction<V, ?> val, String mapping) {
return maybeDo(condition, () -> {
if (Objects.isNull(updateSet)) {
updateSet = new ArrayList<>();
}
updateSet.add(new UpdateSet(column, val, mapping, false, null));
});
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, mapping, false, null)));
}
@Override
public <R> UpdateJoinWrapper<T> setIncrBy(boolean condition, SFunction<R, ?> column, Number val) {
return maybeDo(condition, () -> {
if (Objects.isNull(updateSet)) {
updateSet = new ArrayList<>();
}
updateSet.add(new UpdateSet(column, val, null, true, Constant.PLUS));
});
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.PLUS)));
}
@Override
public <R> UpdateJoinWrapper<T> setDecrBy(boolean condition, SFunction<R, ?> column, Number val) {
return maybeDo(condition, () -> {
if (Objects.isNull(updateSet)) {
updateSet = new ArrayList<>();
}
updateSet.add(new UpdateSet(column, val, null, true, Constant.DASH));
});
return maybeDo(condition, () -> getUpdateSet().add(new UpdateSet(column, val, null, true, Constant.DASH)));
}
@Override
public UpdateJoinWrapper<T> setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
if (condition && StringUtils.isNotBlank(applySql)) {
SFunction<?, ?>[] arg = consumerFunction.apply(FuncConsumer.func);
UpdateSet set = new UpdateSet();
set.setApply(true);
set.setFormat(applySql);
set.setColumns(arg);
set.setArgs(values);
getUpdateSet().add(set);
}
return typedThis;
}
@Override
@ -168,22 +159,29 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
}
StringBuilder set = new StringBuilder(StringPool.EMPTY);
if (CollectionUtils.isNotEmpty(updateSet)) {
set = new StringBuilder(updateSet.stream().map(i -> {
String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
Constants.DOT + getCache(i.getColumn()).getColumn();
if (i.incOrDnc) {
return col + Constants.EQUALS + col + i.cal + i.value;
String setSql = updateSet.stream().map(i -> {
if (i.isApply) {
String col = String.format(i.format, Arrays.stream(i.columns).map(f ->
tableList.getPrefixByClass(LambdaUtils.getEntityClass(f)) +
Constants.DOT + getCache(f).getColumn()).toArray());
return formatSqlMaybeWithParam(col, null, i.args);
} else {
String col = tableList.getPrefixByClass(LambdaUtils.getEntityClass(i.getColumn())) +
Constants.DOT + getCache(i.getColumn()).getColumn();
if (i.incOrDnc) {
return col + Constants.EQUALS + col + i.cal + i.value;
} else {
if (i.value instanceof Function) {
SFunction<?, ?> value = (SFunction<?, ?>) i.getValue();
return col + Constants.EQUALS + tableList.getPrefixByClass(LambdaUtils.getEntityClass(value)) +
Constants.DOT + getCache(value).getColumn();
} else {
if (i.value instanceof Function) {
SFunction<?, ?> value = (SFunction<?, ?>) i.getValue();
return col + Constants.EQUALS + tableList.getPrefixByClass(LambdaUtils.getEntityClass(value)) +
Constants.DOT + getCache(value).getColumn();
} else {
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
}
return col + Constants.EQUALS + formatParam(i.mapping, i.value);
}
})
.collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA);
}
}
}).collect(Collectors.joining(StringPool.COMMA)) + StringPool.COMMA;
set.append(setSql);
}
if (CollectionUtils.isNotEmpty(sqlSet)) {
set.append(String.join(StringPool.COMMA, sqlSet)).append(StringPool.COMMA);
@ -219,13 +217,20 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
this.tableList, index, keyWord, joinClass, tableName);
}
public List<UpdateSet> getUpdateSet() {
if (updateSet == null) {
updateSet = new ArrayList<>();
}
return updateSet;
}
/**
* 不建议直接 new 该实例使用 JoinWrappers.update(User.class)
*/
protected UpdateJoinWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
TableList tableList, Integer index, String keyWord, Class<?> joinClass, String tableName) {
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
TableList tableList, Integer index, String keyWord, Class<?> joinClass, String tableName) {
super.setEntity(entity);
super.setEntityClass(entityClass);
this.paramNameSeq = paramNameSeq;
@ -293,7 +298,6 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
@Data
@AllArgsConstructor
public static class UpdateSet {
private SFunction<?, ?> column;
@ -305,5 +309,24 @@ public class UpdateJoinWrapper<T> extends JoinAbstractLambdaWrapper<T, UpdateJoi
private boolean incOrDnc;
private String cal;
private boolean isApply = false;
private String format;
private SFunction<?, ?>[] columns;
private Object[] args;
public UpdateSet() {
}
public UpdateSet(SFunction<?, ?> column, Object value, String mapping, boolean incOrDnc, String cal) {
this.column = column;
this.value = value;
this.mapping = mapping;
this.incOrDnc = incOrDnc;
this.cal = cal;
}
}
}

View File

@ -1,6 +1,7 @@
package com.github.yulichang.wrapper.interfaces;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.wrapper.segments.FuncConsumer;
import java.io.Serializable;
@ -79,6 +80,12 @@ public interface Update<Children> extends Serializable {
<R> Children setDecrBy(boolean condition, SFunction<R, ?> column, Number val);
default Children setApply(String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values) {
return setApply(true, applySql, consumerFunction, values);
}
Children setApply(boolean condition, String applySql, SFunction<FuncConsumer, SFunction<?, ?>[]> consumerFunction, Object... values);
/**
* ignore
*/

View File

@ -0,0 +1,37 @@
package com.github.yulichang.test.join.unit;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.test.util.ThreadLocalUtils;
import com.github.yulichang.toolkit.JoinWrappers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class SetApplyTest {
@BeforeEach
void setUp() {
Reset.reset();
}
@Test
void applyFunc() {
ThreadLocalUtils.set("UPDATE `user` t SET t.id = t.id + ? + ? WHERE t.del = false");
int update = JoinWrappers.update(UserDO.class)
.setApply("%s = %s + {0} + {1}", arg -> arg.accept(UserDO::getId, UserDO::getId), "100", "1000")
.update();
assert update > 0;
List<UserDO> list = JoinWrappers.lambda(UserDO.class).list();
System.out.println(list);
}
}