fixed updateNumberAddByQuery is error if value is float/double or BigDecimal; close #I7CKLO

This commit is contained in:
开源海哥 2023-06-12 08:53:46 +08:00
parent da0279736a
commit 911dc3c852

View File

@ -28,6 +28,8 @@ import com.mybatisflex.core.util.ArrayUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.StringUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -704,7 +706,7 @@ public class CommonsDialectImpl implements IDialect {
sql.append(wrap(getRealSchema(schema))).append(".");
}
sql.append(wrap(getRealTable(tableName))).append(" SET ");
sql.append(wrap(fieldName)).append("=").append(wrap(fieldName)).append(value.intValue() >= 0 ? " + " : " - ").append(Math.abs(value.longValue()));
sql.append(wrap(fieldName)).append("=").append(wrap(fieldName)).append(geZero(value) ? " + " : " - ").append(abs(value));
String whereConditionSql = buildWhereConditionSql(queryWrapper);
@ -721,11 +723,40 @@ public class CommonsDialectImpl implements IDialect {
sql.append(" ").append(endFragment);
}
}
return sql.toString();
}
protected boolean geZero(Number number) {
if (number instanceof BigDecimal) {
return ((BigDecimal) number).signum() >= 0;
} else if (number instanceof BigInteger) {
return ((BigInteger) number).signum() >= 0;
} else if (number instanceof Float) {
return (Float) number >= 0;
} else if (number instanceof Double) {
return (Double) number >= 0;
} else {
return number.longValue() >= 0;
}
}
protected Number abs(Number number) {
if (number instanceof BigDecimal) {
return ((BigDecimal) number).abs();
} else if (number instanceof BigInteger) {
return ((BigInteger) number).abs();
} else if (number instanceof Float) {
return Math.abs((Float) number);
} else if (number instanceof Double) {
return Math.abs((Double) number);
} else {
return Math.abs(number.longValue());
}
}
@Override
public String forSelectOneEntityById(TableInfo tableInfo) {
StringBuilder sql = buildSelectColumnSql(null, null, null);