mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
commit
2db7019e41
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* 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.ArrayUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IF 函数查询列。
|
||||||
|
*
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-07-07
|
||||||
|
*/
|
||||||
|
public class IfFunctionQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||||
|
|
||||||
|
private QueryCondition condition;
|
||||||
|
private QueryColumn trueValue;
|
||||||
|
private QueryColumn falseValue;
|
||||||
|
|
||||||
|
public IfFunctionQueryColumn(QueryCondition condition, QueryColumn trueValue, QueryColumn falseValue) {
|
||||||
|
this.condition = condition;
|
||||||
|
this.trueValue = trueValue;
|
||||||
|
this.falseValue = falseValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||||
|
return "IF(" + condition.toSql(queryTables, dialect) + ", " +
|
||||||
|
trueValue.toConditionSql(queryTables, dialect) + ", " +
|
||||||
|
falseValue.toConditionSql(queryTables, dialect) + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
Object[] params = WrapperUtil.getValues(condition);
|
||||||
|
// IF 函数嵌套
|
||||||
|
if (trueValue instanceof HasParamsColumn) {
|
||||||
|
Object[] paramValues = ((HasParamsColumn) trueValue).getParamValues();
|
||||||
|
params = ArrayUtil.concat(params, paramValues);
|
||||||
|
}
|
||||||
|
if (falseValue instanceof HasParamsColumn) {
|
||||||
|
Object[] paramValues = ((HasParamsColumn) falseValue).getParamValues();
|
||||||
|
params = ArrayUtil.concat(params, paramValues);
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IfFunctionQueryColumn clone() {
|
||||||
|
IfFunctionQueryColumn clone = (IfFunctionQueryColumn) super.clone();
|
||||||
|
// deep clone ...
|
||||||
|
clone.condition = this.condition.clone();
|
||||||
|
clone.trueValue = this.trueValue.clone();
|
||||||
|
clone.falseValue = this.falseValue.clone();
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -2387,6 +2387,13 @@ public class QueryMethods {
|
|||||||
|
|
||||||
// === 构建 column 列 ===
|
// === 构建 column 列 ===
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建 NULL 常量。
|
||||||
|
*/
|
||||||
|
public static QueryColumn null_() {
|
||||||
|
return new StringQueryColumn("NULL");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建数字常量。
|
* 构建数字常量。
|
||||||
*/
|
*/
|
||||||
@ -2436,6 +2443,50 @@ public class QueryMethods {
|
|||||||
return new SelectQueryColumn(queryWrapper);
|
return new SelectQueryColumn(queryWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === IF 函数 ===
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IF 函数。
|
||||||
|
*/
|
||||||
|
public static QueryColumn if_(QueryCondition condition, String trueColumn, String falseColumn) {
|
||||||
|
return new IfFunctionQueryColumn(condition, new QueryColumn(trueColumn), new QueryColumn(falseColumn));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IF 函数。
|
||||||
|
*/
|
||||||
|
public static QueryColumn if_(QueryCondition condition, QueryColumn trueColumn, QueryColumn falseColumn) {
|
||||||
|
return new IfFunctionQueryColumn(condition, trueColumn, falseColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IF 函数。
|
||||||
|
*/
|
||||||
|
public static <T, F> QueryColumn if_(QueryCondition condition, LambdaGetter<T> trueColumn, LambdaGetter<F> falseColumn) {
|
||||||
|
return new IfFunctionQueryColumn(condition, LambdaUtil.getQueryColumn(trueColumn), LambdaUtil.getQueryColumn(falseColumn));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IFNULL 函数。
|
||||||
|
*/
|
||||||
|
public static QueryColumn ifNull(String nullColumn, String elseColumn) {
|
||||||
|
return new FunctionQueryColumn("IFNULL", new QueryColumn(nullColumn), new QueryColumn(elseColumn));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IFNULL 函数。
|
||||||
|
*/
|
||||||
|
public static QueryColumn ifNull(QueryColumn nullColumn, QueryColumn elseColumn) {
|
||||||
|
return new FunctionQueryColumn("IFNULL", nullColumn, elseColumn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IFNULL 函数。
|
||||||
|
*/
|
||||||
|
public static <N, E> QueryColumn ifNull(LambdaGetter<N> nullColumn, LambdaGetter<E> elseColumn) {
|
||||||
|
return new FunctionQueryColumn("IFNULL", LambdaUtil.getQueryColumn(nullColumn), LambdaUtil.getQueryColumn(elseColumn));
|
||||||
|
}
|
||||||
|
|
||||||
// === 构建 QueryCondition 查询条件 ===
|
// === 构建 QueryCondition 查询条件 ===
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.*;
|
||||||
|
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 王帅
|
||||||
|
* @since 2023-07-07
|
||||||
|
*/
|
||||||
|
public class IfFunctionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test01() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(if_(ACCOUNT.AGE.ge(6), ACCOUNT.IS_NORMAL, ACCOUNT.IS_DELETE).as("type"))
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.ID.eq(1));
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test02() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(if_(ACCOUNT.AGE.ge(18), string("成年人"),
|
||||||
|
if_(ACCOUNT.AGE.le(8), string("未上学"), string("已上学"))).as("type"))
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.ID.eq(1));
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test03() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(ifNull(ACCOUNT.ID, number(0)))
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.ID.eq(1));
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test04() {
|
||||||
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
|
.select(ifNull(null_(), number(0)))
|
||||||
|
.from(ACCOUNT);
|
||||||
|
System.out.println(queryWrapper.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user