com.github.wz2cool.dynamic.UpdateQuery Maven / Gradle / Ivy
package com.github.wz2cool.dynamic;
import com.github.wz2cool.dynamic.exception.InternalRuntimeException;
import com.github.wz2cool.dynamic.helper.CommonsHelper;
import com.github.wz2cool.dynamic.helper.ParamResolverHelper;
import com.github.wz2cool.dynamic.lambda.*;
import com.github.wz2cool.dynamic.model.SelectPropertyConfig;
import com.github.wz2cool.dynamic.mybatis.ColumnInfo;
import com.github.wz2cool.dynamic.mybatis.ParamExpression;
import com.github.wz2cool.dynamic.mybatis.QueryHelper;
import com.github.wz2cool.dynamic.mybatis.mapper.constant.MapperConstants;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.UnaryOperator;
/**
* @author Frank
**/
public class UpdateQuery extends BaseFilterGroup> {
private static final QueryHelper QUERY_HELPER = new QueryHelper();
private final Map setColumnValueMap = new HashMap<>();
private Class entityClass;
private static final String FIRST_SQL_KEY = "mdq_first_sql";
private static final String LAST_SQL_KEY = "mdq_last_sql";
private static final String HINT_SQL_KEY = "mdq_hint_sql";
protected Map customDynamicQueryParams = new HashMap<>();
public UpdateQuery() {
// for json
}
public UpdateQuery(Class entityClass) {
this.entityClass = entityClass;
}
public static UpdateQuery createQuery(Class entityClass) {
return new UpdateQuery<>(entityClass);
}
public Class getEntityClass() {
return entityClass;
}
public UpdateQuery set(GetBigDecimalPropertyFunction getPropertyFunc, BigDecimal value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetBytePropertyFunction getPropertyFunc, Byte value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetDatePropertyFunction getPropertyFunc, Date value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetDoublePropertyFunction getPropertyFunc, Double value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetFloatPropertyFunction getPropertyFunc, Float value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetIntegerPropertyFunction getPropertyFunc, Integer value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetLongPropertyFunction getPropertyFunc, Long value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetShortPropertyFunction getPropertyFunc, Short value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(GetStringPropertyFunction getPropertyFunc, String value) {
return set(true, getPropertyFunc, value);
}
public UpdateQuery set(T record) {
return set(true, record);
}
public UpdateQuery set(boolean enable, T record) {
return set(enable, record, null);
}
public UpdateQuery set(T record, UnaryOperator> getConfigFunc) {
return set(true, record, getConfigFunc);
}
public UpdateQuery set(boolean enable, T record, UnaryOperator> getConfigFunc) {
if (enable) {
SelectPropertyConfig config = null;
if (Objects.nonNull(getConfigFunc)) {
config = getConfigFunc.apply(new SelectPropertyConfig<>());
}
Map propertyColumnInfoMap = QUERY_HELPER.getPropertyColumnInfoMap(record.getClass());
final Set needUpdatePropertyNames = getNeedUpdatePropertyNames(propertyColumnInfoMap.keySet(), config);
for (Map.Entry propertyColumnInfoEntry : propertyColumnInfoMap.entrySet()) {
String propertyName = propertyColumnInfoEntry.getKey();
ColumnInfo columnInfo = propertyColumnInfoEntry.getValue();
try {
if (needUpdatePropertyNames.contains(propertyName)) {
Object value = columnInfo.getField().get(record);
setColumnValueMap.put(propertyName, value);
}
} catch (IllegalAccessException e) {
throw new InternalRuntimeException(e);
}
}
}
return this;
}
public UpdateQuery set(boolean enable, GetBigDecimalPropertyFunction getPropertyFunc, BigDecimal value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetBytePropertyFunction getPropertyFunc, Byte value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetDatePropertyFunction getPropertyFunc, Date value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetDoublePropertyFunction getPropertyFunc, Double value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetFloatPropertyFunction getPropertyFunc, Float value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetIntegerPropertyFunction getPropertyFunc, Integer value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetLongPropertyFunction getPropertyFunc, Long value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetShortPropertyFunction getPropertyFunc, Short value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public UpdateQuery set(boolean enable, GetStringPropertyFunction getPropertyFunc, String value) {
if (enable) {
final String propertyName = CommonsHelper.getPropertyName(getPropertyFunc);
setColumnValueMap.put(propertyName, value);
}
return this;
}
public Map toQueryParamMap(boolean isMapUnderscoreToCamelCase) {
BaseFilterDescriptor[] filters = this.getFilters();
ParamExpression whereParamExpression = QUERY_HELPER.toWhereExpression(entityClass, filters);
String whereExpression = whereParamExpression.getExpression();
Map paramMap = whereParamExpression.getParamMap();
for (Map.Entry param : paramMap.entrySet()) {
String key = param.getKey();
String newKey = String.format("%s.%s", MapperConstants.DYNAMIC_QUERY_PARAMS, key);
whereExpression = whereExpression.replace(key, newKey);
}
paramMap.put(MapperConstants.WHERE_EXPRESSION, whereExpression);
final Map updateParamMap = toUpdateParamMap(isMapUnderscoreToCamelCase, setColumnValueMap);
paramMap.putAll(updateParamMap);
initDefaultQueryParams();
paramMap.putAll(this.customDynamicQueryParams);
return paramMap;
}
private Map toUpdateParamMap(boolean isMapUnderscoreToCamelCase, Map setColumnValueMap) {
Map result = new HashMap<>();
List setExpressionItems = new ArrayList<>();
for (Map.Entry columnValueEntry : setColumnValueMap.entrySet()) {
String propertyName = columnValueEntry.getKey();
String valuePlaceHolder = String.format("param_%s", columnValueEntry.getKey());
final String columnName = QUERY_HELPER.getQueryColumnByProperty(this.getEntityClass(), propertyName);
final String setExpressionItem = String.format("%s=#{%s.%s}",
columnName, MapperConstants.DYNAMIC_QUERY_PARAMS, valuePlaceHolder);
setExpressionItems.add(setExpressionItem);
result.put(valuePlaceHolder, columnValueEntry.getValue());
}
String setExpression = String.join(",", setExpressionItems);
result.put(MapperConstants.SET_EXPRESSION, setExpression);
return result;
}
private Set getNeedUpdatePropertyNames(Set allPropertyNames, SelectPropertyConfig config) {
if (Objects.isNull(config)) {
return allPropertyNames;
}
if (!config.getSelectPropertyNames().isEmpty()) {
return config.getSelectPropertyNames();
}
Set result = new HashSet<>();
for (String propertyName : allPropertyNames) {
if (config.getIgnorePropertyNames().contains(propertyName)) {
continue;
}
result.add(propertyName);
}
return result;
}
public final UpdateQuery last(String lastSql) {
return last(true, lastSql);
}
public final UpdateQuery last(boolean enable, String lastSql) {
if (enable) {
String useLastSql = ParamResolverHelper.resolveExpression(lastSql);
this.customDynamicQueryParams.put(LAST_SQL_KEY, useLastSql);
}
return this;
}
public final UpdateQuery first(String firstSql) {
return first(true, firstSql);
}
public final UpdateQuery first(boolean enable, String firstSql) {
if (enable) {
String useFirstSql = ParamResolverHelper.resolveExpression(firstSql);
this.customDynamicQueryParams.put(FIRST_SQL_KEY, useFirstSql);
}
return this;
}
public final UpdateQuery hint(String hintSql) {
return hint(true, hintSql);
}
/**
* https://docs.oracle.com/cd/B13789_01/server.101/b10759/sql_elements006.htm#i35922
*
* @return
*/
public final UpdateQuery hint(boolean enable, String hintSql) {
if (enable) {
String useHintSql = ParamResolverHelper.resolveExpression(hintSql);
this.customDynamicQueryParams.put(HINT_SQL_KEY, useHintSql);
}
return this;
}
public final UpdateQuery queryParam(String key, Object value) {
return queryParam(true, key, value);
}
public final UpdateQuery queryParam(boolean enable, String key, Object value) {
if (enable) {
this.customDynamicQueryParams.put(key, value);
}
return this;
}
private void initDefaultQueryParams() {
this.customDynamicQueryParams.putIfAbsent(LAST_SQL_KEY, "");
this.customDynamicQueryParams.putIfAbsent(FIRST_SQL_KEY, "");
this.customDynamicQueryParams.putIfAbsent(HINT_SQL_KEY, "");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy