mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
fix: fixed github issues 288, close #288
This commit is contained in:
parent
47e8979166
commit
0e73f391bd
@ -15,14 +15,17 @@
|
||||
*/
|
||||
package com.mybatisflex.core.query;
|
||||
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.constant.SqlConsts;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class DistinctQueryColumn extends QueryColumn {
|
||||
public class DistinctQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
|
||||
private List<QueryColumn> queryColumns;
|
||||
|
||||
@ -62,4 +65,21 @@ public class DistinctQueryColumn extends QueryColumn {
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] getParamValues() {
|
||||
if (CollectionUtil.isEmpty(queryColumns)) {
|
||||
return FlexConsts.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
for (QueryColumn queryColumn : queryColumns) {
|
||||
if (queryColumn instanceof HasParamsColumn) {
|
||||
Object[] paramValues = ((HasParamsColumn) queryColumn).getParamValues();
|
||||
params.addAll(Arrays.asList(paramValues));
|
||||
}
|
||||
}
|
||||
|
||||
return params.toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.github.vertical_blank.sqlformatter.SqlFormatter;
|
||||
import com.mybatisflex.core.query.CPI;
|
||||
import com.mybatisflex.core.query.QueryColumn;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.mybatisflex.core.query.QueryMethods.*;
|
||||
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Lambda 构建 SQL 测试。
|
||||
*
|
||||
* @author 王帅
|
||||
* @since 2023-10-01
|
||||
*/
|
||||
public class QueryWrapperTest {
|
||||
|
||||
public static void printSQL(QueryWrapper queryWrapper) {
|
||||
System.out.println(SqlFormatter.format(queryWrapper.toSQL()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* https://github.com/mybatis-flex/mybatis-flex/issues/288
|
||||
*/
|
||||
@Test
|
||||
public void testIssues288() {
|
||||
final QueryWrapper query = QueryWrapper.create();
|
||||
final QueryColumn storeColumn = column("temp", "store_id");
|
||||
query.select(
|
||||
count(distinct(case_()
|
||||
.when(string("purchasePerson").gt(1)).then(1)
|
||||
.else_(0).end())).as("repurchasePerson")
|
||||
).from(
|
||||
select(ACCOUNT.ID,
|
||||
ACCOUNT.ID,
|
||||
sum(ACCOUNT.ID).as("purchasePerson")
|
||||
).from(ACCOUNT)
|
||||
.where(ACCOUNT.ID.in(5))
|
||||
.groupBy(ACCOUNT.ID,
|
||||
ACCOUNT.ID)).as("temp")
|
||||
.groupBy(storeColumn);
|
||||
|
||||
Object[] valueArray = CPI.getValueArray(query);
|
||||
Assert.assertEquals(valueArray.length, 2);
|
||||
printSQL(query);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user