|
@@ -0,0 +1,215 @@
|
|
|
+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.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.StringJoiner;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+public class QueryColumn implements IFormat, IQueryColumn {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表
|
|
|
+ */
|
|
|
+ private final Table table;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字段名
|
|
|
+ */
|
|
|
+ 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 tableAs(String tableAs) {
|
|
|
+ Table table = new Table(tableAs, this.table.getName());
|
|
|
+ return new QueryColumn(table, this.name, this.queryColumnComputeList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ===========================================
|
|
|
+ // 运算 加 减 乘 除 取余 + - * / %
|
|
|
+ // ===========================================
|
|
|
+
|
|
|
+ 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, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public QueryCondition isNotNull() {
|
|
|
+ return new QueryCondition(this, SqlConstant.IS_NOT_NULL, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private QueryColumn copy() {
|
|
|
+ return new QueryColumn(this.table, this.name, new ArrayList<>(this.queryColumnComputeList));
|
|
|
+ }
|
|
|
+
|
|
|
+ private QueryColumn compute(String symbol, QueryColumn queryColumn) {
|
|
|
+ QueryColumn newQueryColumn = copy();
|
|
|
+ newQueryColumn.queryColumnComputeList.add(new QueryColumnCompute(symbol, queryColumn));
|
|
|
+ return newQueryColumn;
|
|
|
+ }
|
|
|
+
|
|
|
+ private QueryColumn compute(String symbol, Number number) {
|
|
|
+ QueryColumn newQueryColumn = copy();
|
|
|
+ newQueryColumn.queryColumnComputeList.add(new QueryColumnCompute(symbol, number));
|
|
|
+ return newQueryColumn;
|
|
|
+ }
|
|
|
+
|
|
|
+ @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();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|