mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add ArithmeticQueryColumn and fixed #35
This commit is contained in:
parent
ccf9d38b37
commit
122aefe374
@ -0,0 +1,133 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* 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<ArithmeticInfo> 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<QueryTable> 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<QueryTable> 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<QueryTable> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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) {
|
protected String wrap(IDialect dialect, String table, String column) {
|
||||||
if (StringUtil.isNotBlank(table)) {
|
if (StringUtil.isNotBlank(table)) {
|
||||||
return dialect.wrap(table) + "." + dialect.wrap(column);
|
return dialect.wrap(table) + "." + dialect.wrap(column);
|
||||||
|
|||||||
@ -0,0 +1,135 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* 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`");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user