diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java new file mode 100644 index 00000000..7d160fed --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/ArithmeticQueryColumn.java @@ -0,0 +1,133 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.core.query; + +import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.util.StringUtil; + +import java.util.ArrayList; +import java.util.List; + +public class ArithmeticQueryColumn extends QueryColumn { + + private List arithmeticInfos; + + public ArithmeticQueryColumn(Object value) { + arithmeticInfos = new ArrayList<>(); + arithmeticInfos.add(new ArithmeticInfo(value)); + } + + @Override + public QueryColumn add(QueryColumn queryColumn) { + arithmeticInfos.add(new ArithmeticInfo(" + ", queryColumn)); + return this; + } + + @Override + public QueryColumn add(Number number) { + arithmeticInfos.add(new ArithmeticInfo(" + ", number)); + return this; + } + + @Override + public QueryColumn subtract(QueryColumn queryColumn) { + arithmeticInfos.add(new ArithmeticInfo(" - ", queryColumn)); + return this; + } + + @Override + public QueryColumn subtract(Number number) { + arithmeticInfos.add(new ArithmeticInfo(" - ", number)); + return this; + } + + @Override + public QueryColumn multiply(QueryColumn queryColumn) { + arithmeticInfos.add(new ArithmeticInfo(" * ", queryColumn)); + return this; + } + + @Override + public QueryColumn multiply(Number number) { + arithmeticInfos.add(new ArithmeticInfo(" * ", number)); + return this; + } + + @Override + public QueryColumn divide(QueryColumn queryColumn) { + arithmeticInfos.add(new ArithmeticInfo(" / ", queryColumn)); + return this; + } + + @Override + public QueryColumn divide(Number number) { + arithmeticInfos.add(new ArithmeticInfo(" / ", number)); + return this; + } + + @Override + public QueryColumn as(String alias) { + this.alias = alias; + return this; + } + + @Override + String toSelectSql(List queryTables, IDialect dialect) { + StringBuilder sql = new StringBuilder(); + for (int i = 0; i < arithmeticInfos.size(); i++) { + sql.append(arithmeticInfos.get(i).toSql(queryTables, dialect, i)); + } + if (StringUtil.isNotBlank(alias)) { + return "(" + sql + ") AS " + dialect.wrap(alias); + } + return sql.toString(); + } + + + @Override + String toConditionSql(List queryTables, IDialect dialect) { + StringBuilder sql = new StringBuilder(); + for (int i = 0; i < arithmeticInfos.size(); i++) { + sql.append(arithmeticInfos.get(i).toSql(queryTables, dialect, i)); + } + return "(" + sql + ")"; + } + + + static class ArithmeticInfo { + private String symbol; + private Object value; + + public ArithmeticInfo(Object value) { + this.value = value; + } + + public ArithmeticInfo(String symbol, Object value) { + this.symbol = symbol; + this.value = value; + } + + private String toSql(List queryTables, IDialect dialect, int index) { + String valueSql; + if (value instanceof QueryColumn) { + valueSql = ((QueryColumn) value).toConditionSql(queryTables, dialect); + } else { + valueSql = String.valueOf(value); + } + return index == 0 ? valueSql : symbol + valueSql; + } + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java index 2d5f10b3..55ba6a23 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java @@ -224,7 +224,7 @@ public class QueryColumn implements Serializable { */ public QueryCondition in(Object... arrays) { //忽略 QueryWrapper.in("name", null) 的情况 - if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { + if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { return QueryCondition.createEmpty(); } return QueryCondition.create(this, QueryCondition.LOGIC_IN, arrays); @@ -262,7 +262,7 @@ public class QueryColumn implements Serializable { */ public QueryCondition notIn(Object... arrays) { //忽略 QueryWrapper.notIn("name", null) 的情况 - if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { + if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) { return QueryCondition.createEmpty(); } return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, arrays); @@ -324,6 +324,40 @@ public class QueryColumn implements Serializable { } + // 运算 加减乘除 + - * / + public QueryColumn add(QueryColumn queryColumn) { + return new ArithmeticQueryColumn(this).add(queryColumn); + } + + public QueryColumn add(Number number) { + return new ArithmeticQueryColumn(this).add(number); + } + + public QueryColumn subtract(QueryColumn queryColumn) { + return new ArithmeticQueryColumn(this).subtract(queryColumn); + } + + public QueryColumn subtract(Number number) { + return new ArithmeticQueryColumn(this).subtract(number); + } + + public QueryColumn multiply(QueryColumn queryColumn) { + return new ArithmeticQueryColumn(this).multiply(queryColumn); + } + + public QueryColumn multiply(Number number) { + return new ArithmeticQueryColumn(this).multiply(number); + } + + public QueryColumn divide(QueryColumn queryColumn) { + return new ArithmeticQueryColumn(this).divide(queryColumn); + } + + public QueryColumn divide(Number number) { + return new ArithmeticQueryColumn(this).divide(number); + } + + protected String wrap(IDialect dialect, String table, String column) { if (StringUtil.isNotBlank(table)) { return dialect.wrap(table) + "." + dialect.wrap(column); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/query/ArithmeticQueryColumnTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/query/ArithmeticQueryColumnTest.java new file mode 100644 index 00000000..d375187b --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/query/ArithmeticQueryColumnTest.java @@ -0,0 +1,135 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.coretest.query; + +import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.dialect.impl.CommonsDialectImpl; +import com.mybatisflex.core.query.QueryWrapper; +import org.junit.Assert; +import org.junit.Test; + +import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; + +public class ArithmeticQueryColumnTest { + + private static String toSql(QueryWrapper queryWrapper){ + IDialect dialect = new CommonsDialectImpl(); + return dialect.forSelectByQuery(queryWrapper); + } + + + @Test + public void testAdd() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.add(100).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` + 100) AS `x100` FROM `tb_account`"); + } + + @Test + public void testAdd1() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.add(100).add(200).add(300).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` + 100 + 200 + 300) AS `x100` FROM `tb_account`"); + } + + @Test + public void testAdd2() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.add(ACCOUNT.ID).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` + `id`) AS `x100` FROM `tb_account`"); + } + + @Test + public void testAdd3() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.add(ACCOUNT.ID.add(100)).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` + (`id` + 100)) AS `x100` FROM `tb_account`"); + } + + @Test + public void testAdd4() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.add(ACCOUNT.ID.add(100)).multiply(100).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` + (`id` + 100) * 100) AS `x100` FROM `tb_account`"); + } + + @Test + public void testSubtract() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.subtract(100).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` - 100) AS `x100` FROM `tb_account`"); + } + + + @Test + public void testMultiply() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.multiply(100).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` * 100) AS `x100` FROM `tb_account`"); + } + + + @Test + public void testDivide() { + QueryWrapper query = new QueryWrapper() + .select(ACCOUNT.ID.divide(100).as("x100")) + .from(ACCOUNT); + + String sql = toSql(query); + System.out.println(sql); + + Assert.assertEquals(sql,"SELECT (`id` / 100) AS `x100` FROM `tb_account`"); + } + + + + +}