|
@@ -1,24 +1,11 @@
|
|
|
package org.example.join.domain;
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
|
|
-import org.apache.ibatis.reflection.property.PropertyNamer;
|
|
|
-import org.example.join.util.SqlConstant;
|
|
|
import org.example.join.util.SqlUtil;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.StringJoiner;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
-public class QueryColumn implements IFormat, IQueryColumn {
|
|
|
+public class QueryColumn extends AbstractQueryColumn implements IFormat, IQueryColumn {
|
|
|
|
|
|
/**
|
|
|
* 表
|
|
@@ -30,199 +17,22 @@ public class QueryColumn implements IFormat, IQueryColumn {
|
|
|
*/
|
|
|
private final String name;
|
|
|
|
|
|
- /**
|
|
|
- * 计算列表
|
|
|
- */
|
|
|
- private final List<QueryColumnCompute> queryColumnComputeList;
|
|
|
-
|
|
|
public QueryColumn(Table table, String name) {
|
|
|
this.table = table;
|
|
|
this.name = name;
|
|
|
- this.queryColumnComputeList = new ArrayList<>();
|
|
|
- }
|
|
|
-
|
|
|
- protected QueryColumn(Table table, String name, List<QueryColumnCompute> queryColumnComputeList) {
|
|
|
- this.table = table;
|
|
|
- this.name = name;
|
|
|
- this.queryColumnComputeList = queryColumnComputeList;
|
|
|
- }
|
|
|
-
|
|
|
- public <T> QueryColumnAlias as(SFunction<T, ?> function) {
|
|
|
- LambdaMeta meta = LambdaUtils.extract(function);
|
|
|
- String alias = StrUtil.toUnderlineCase(PropertyNamer.methodToProperty(meta.getImplMethodName()));
|
|
|
- return new QueryColumnAlias(alias, this);
|
|
|
- }
|
|
|
-
|
|
|
- // ===========================================
|
|
|
- // 运算 加 减 乘 除 取余 + - * / %
|
|
|
- // ===========================================
|
|
|
-
|
|
|
- public QueryColumn add(QueryColumn queryColumn) {
|
|
|
- return compute(SqlConstant.ADD, queryColumn);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn add(Number number) {
|
|
|
- return compute(SqlConstant.ADD, number);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn subtract(QueryColumn queryColumn) {
|
|
|
- return compute(SqlConstant.SUBTRACT, queryColumn);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn subtract(Number number) {
|
|
|
- return compute(SqlConstant.SUBTRACT, number);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn multiply(QueryColumn queryColumn) {
|
|
|
- return compute(SqlConstant.MULTIPLY, queryColumn);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn multiply(Number number) {
|
|
|
- return compute(SqlConstant.MULTIPLY, number);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn divide(QueryColumn queryColumn) {
|
|
|
- return compute(SqlConstant.DIVIDE, queryColumn);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn divide(Number number) {
|
|
|
- return compute(SqlConstant.DIVIDE, number);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn remainder(QueryColumn queryColumn) {
|
|
|
- return compute(SqlConstant.REMAINDER, queryColumn);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryColumn remainder(Number number) {
|
|
|
- return compute(SqlConstant.REMAINDER, number);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryConditionOrder asc() {
|
|
|
- return new QueryConditionOrder(this, SqlConstant.ASC);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryConditionOrder desc() {
|
|
|
- return new QueryConditionOrder(this, SqlConstant.DESC);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition eq(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.EQ, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition ne(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.NE, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition gt(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.GT, object);
|
|
|
}
|
|
|
|
|
|
- public QueryCondition lt(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.LT, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition ge(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.GE, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition le(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.LE, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition between(Object object1, Object object2) {
|
|
|
- if (ObjectUtil.isAllNotEmpty(object1, object2)) {
|
|
|
- return new QueryCondition(this, SqlConstant.BETWEEN, new Object[]{object1, object2});
|
|
|
- }
|
|
|
- return new QueryCondition(this, SqlConstant.BETWEEN, null);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notBetween(Object object1, Object object2) {
|
|
|
- if (ObjectUtil.isAllNotEmpty(object1, object2)) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_BETWEEN, new Object[]{object1, object2});
|
|
|
- }
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_BETWEEN, null);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition like(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.LIKE, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notLike(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_LIKE, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition likeLeft(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.LIKE_LEFT, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notLikeLeft(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_LIKE_LEFT, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition likeRight(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.LIKE_RIGHT, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notLikeRight(Object object) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_LIKE_RIGHT, object);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition isNull() {
|
|
|
- return new QueryCondition(this, SqlConstant.IS_NULL, SqlConstant.IS_NULL);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition isNotNull() {
|
|
|
- return new QueryCondition(this, SqlConstant.IS_NOT_NULL, SqlConstant.IS_NULL);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition in(Collection<?> coll) {
|
|
|
- return new QueryCondition(this, SqlConstant.IN, coll);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition in(Object... obj) {
|
|
|
- if (ObjectUtil.isEmpty(obj)) {
|
|
|
- return new QueryCondition(this, SqlConstant.IN, null);
|
|
|
- }
|
|
|
- return new QueryCondition(this, SqlConstant.IN, Arrays.asList(obj));
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notIn(Collection<?> coll) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_IN, coll);
|
|
|
- }
|
|
|
-
|
|
|
- public QueryCondition notIn(Object... obj) {
|
|
|
- if (ObjectUtil.isEmpty(obj)) {
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_IN, null);
|
|
|
- }
|
|
|
- return new QueryCondition(this, SqlConstant.NOT_IN, Arrays.asList(obj));
|
|
|
- }
|
|
|
-
|
|
|
- private QueryColumn copy() {
|
|
|
- return new QueryColumn(this.table, this.name, new ArrayList<>(this.queryColumnComputeList));
|
|
|
- }
|
|
|
-
|
|
|
- private QueryColumn compute(String symbol, Object obj) {
|
|
|
- QueryColumn newQueryColumn = copy();
|
|
|
- newQueryColumn.queryColumnComputeList.add(new QueryColumnCompute(symbol, obj));
|
|
|
- return newQueryColumn;
|
|
|
+ @Override
|
|
|
+ protected QueryColumn copy() {
|
|
|
+ QueryColumn queryColumn = new QueryColumn(this.table, this.name);
|
|
|
+ queryColumn.queryColumnComputeList.addAll(queryColumnComputeList);
|
|
|
+ return queryColumn;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String toSql(Map<String, String> tableAliasMap, Map<String, Object> paramMap) {
|
|
|
- StringJoiner joiner;
|
|
|
- if (ObjectUtil.isEmpty(queryColumnComputeList)) {
|
|
|
- joiner = new StringJoiner(StringPool.EMPTY);
|
|
|
- } else {
|
|
|
- joiner = new StringJoiner(StringPool.EMPTY, StringPool.LEFT_BRACKET, StringPool.RIGHT_BRACKET);
|
|
|
- }
|
|
|
-
|
|
|
- String tableAlias = SqlUtil.getTableAlias(table, tableAliasMap);
|
|
|
- String queryColumnArithmeticStr = queryColumnComputeList
|
|
|
- .stream()
|
|
|
- .map(item -> item.toSql(tableAliasMap, paramMap))
|
|
|
- .collect(Collectors.joining());
|
|
|
-
|
|
|
- return joiner.add(tableAlias + StringPool.DOT + name).add(queryColumnArithmeticStr).toString();
|
|
|
+ String sql = SqlUtil.getTableAlias(table, tableAliasMap) + StringPool.DOT + name;
|
|
|
+ return spliceCompute(sql, tableAliasMap, paramMap);
|
|
|
}
|
|
|
|
|
|
}
|