mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fixed: #I7HVXT
This commit is contained in:
parent
8245e55dce
commit
dffc0eb568
@ -318,20 +318,16 @@ public class CommonsDialectImpl implements IDialect {
|
||||
public String buildSelectSql(QueryWrapper queryWrapper) {
|
||||
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
||||
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
throw FlexExceptions.wrap("QueryWrapper must use the 'FROM' clause to select the table.");
|
||||
}
|
||||
|
||||
List<QueryTable> joinTables = CPI.getJoinTables(queryWrapper);
|
||||
List<QueryTable> allTables = CollectionUtil.merge(queryTables, joinTables);
|
||||
|
||||
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
||||
|
||||
int queryTablesCount = queryTables.size();
|
||||
int queryTablesCount = queryTables == null ? 0 : queryTables.size();
|
||||
int joinTablesCount = joinTables != null ? joinTables.size() : 0;
|
||||
|
||||
//多表查询时,自动映射
|
||||
if (queryTablesCount + joinTablesCount > 1) {
|
||||
if (queryTablesCount > 0 && queryTablesCount + joinTablesCount > 1) {
|
||||
QueryTable firstTable = queryTables.get(0);
|
||||
if (!(firstTable instanceof SelectQueryTable)) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofTableName(firstTable.getName());
|
||||
@ -374,7 +370,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
buildOrderBySql(sqlBuilder, queryWrapper, allTables);
|
||||
|
||||
List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(unions)) {
|
||||
if (!CollectionUtil.isEmpty(unions)) {
|
||||
sqlBuilder.insert(0, BRACKET_LEFT).append(BRACKET_RIGHT);
|
||||
for (UnionWrapper unionWrapper : unions) {
|
||||
unionWrapper.buildSql(sqlBuilder, this);
|
||||
@ -388,7 +384,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
}
|
||||
|
||||
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||
if (!CollectionUtil.isEmpty(endFragments)) {
|
||||
for (String endFragment : endFragments) {
|
||||
sqlBuilder.append(BLANK).append(endFragment);
|
||||
}
|
||||
@ -408,7 +404,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
buildOrderBySql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST);
|
||||
|
||||
List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(unions)) {
|
||||
if (!CollectionUtil.isEmpty(unions)) {
|
||||
if (sqlBuilder.length() > 0) {
|
||||
sqlBuilder.insert(0, BRACKET_LEFT).append(BRACKET_RIGHT);
|
||||
}
|
||||
@ -424,7 +420,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
}
|
||||
|
||||
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||
if (!CollectionUtil.isEmpty(endFragments)) {
|
||||
for (String endFragment : endFragments) {
|
||||
sqlBuilder.append(BLANK).append(endFragment);
|
||||
}
|
||||
@ -476,7 +472,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
//buildLimitSql(sqlBuilder, queryWrapper)
|
||||
|
||||
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||
if (!CollectionUtil.isEmpty(endFragments)) {
|
||||
for (String endFragment : endFragments) {
|
||||
sqlBuilder.append(BLANK).append(endFragment);
|
||||
}
|
||||
@ -833,7 +829,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
sql.append(WHERE).append(whereConditionSql);
|
||||
|
||||
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||
if (!CollectionUtil.isEmpty(endFragments)) {
|
||||
for (String endFragment : endFragments) {
|
||||
sql.append(BLANK).append(endFragment);
|
||||
}
|
||||
@ -863,7 +859,7 @@ public class CommonsDialectImpl implements IDialect {
|
||||
sql.append(WHERE).append(whereConditionSql);
|
||||
|
||||
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||
if (!CollectionUtil.isEmpty(endFragments)) {
|
||||
for (String endFragment : endFragments) {
|
||||
sql.append(BLANK).append(endFragment);
|
||||
}
|
||||
|
||||
@ -364,13 +364,7 @@ public class EntitySqlProvider {
|
||||
public static String selectListByQuery(Map params, ProviderContext context) {
|
||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||
|
||||
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||
for (TableInfo tableInfo : tableInfos) {
|
||||
tableInfo.appendConditions(null, queryWrapper);
|
||||
|
||||
CPI.setSelectColumnsIfNecessary(queryWrapper, tableInfo.getDefaultQueryColumn());
|
||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
|
||||
}
|
||||
appendTableConditions(context, queryWrapper, true);
|
||||
|
||||
//优先构建 sql,再构建参数
|
||||
String sql = DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
||||
@ -381,6 +375,7 @@ public class EntitySqlProvider {
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* selectCountByQuery 的 sql 构建
|
||||
*
|
||||
@ -392,12 +387,7 @@ public class EntitySqlProvider {
|
||||
public static String selectObjectByQuery(Map params, ProviderContext context) {
|
||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||
|
||||
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||
|
||||
for (TableInfo tableInfo : tableInfos) {
|
||||
tableInfo.appendConditions(null, queryWrapper);
|
||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
|
||||
}
|
||||
appendTableConditions(context, queryWrapper, false);
|
||||
|
||||
//优先构建 sql,再构建参数
|
||||
String sql = DialectFactory.getDialect().forSelectByQuery(queryWrapper);
|
||||
@ -409,10 +399,31 @@ public class EntitySqlProvider {
|
||||
}
|
||||
|
||||
|
||||
private static void appendTableConditions(ProviderContext context, QueryWrapper queryWrapper, boolean setSelectColumns) {
|
||||
List<TableInfo> tableInfos = getTableInfos(context, queryWrapper);
|
||||
if (!CollectionUtil.isEmpty(tableInfos)) {
|
||||
for (TableInfo tableInfo : tableInfos) {
|
||||
tableInfo.appendConditions(null, queryWrapper);
|
||||
if (setSelectColumns) {
|
||||
CPI.setSelectColumnsIfNecessary(queryWrapper, tableInfo.getDefaultQueryColumn());
|
||||
}
|
||||
CPI.setFromIfNecessary(queryWrapper, tableInfo.getSchema(), tableInfo.getTableName());
|
||||
}
|
||||
} else {
|
||||
List<QueryWrapper> childQueryWrappers = CPI.getChildSelect(queryWrapper);
|
||||
if (!CollectionUtil.isEmpty(childQueryWrappers)) {
|
||||
for (QueryWrapper childQueryWrapper : childQueryWrappers) {
|
||||
appendTableConditions(context, childQueryWrapper, setSelectColumns);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static List<TableInfo> getTableInfos(ProviderContext context, QueryWrapper queryWrapper) {
|
||||
List<TableInfo> tableInfos;
|
||||
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(queryTables)) {
|
||||
if (!CollectionUtil.isEmpty(queryTables)) {
|
||||
tableInfos = new ArrayList<>();
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
String tableNameWithSchema = queryTable.getNameWithSchema();
|
||||
|
||||
@ -232,7 +232,7 @@ public class CPI {
|
||||
return selectQueryColumn.getQueryWrapper();
|
||||
}
|
||||
|
||||
public static boolean isSameTable(QueryTable queryTable,QueryTable otherTable){
|
||||
public static boolean isSameTable(QueryTable queryTable, QueryTable otherTable) {
|
||||
return queryTable.isSameTable(otherTable);
|
||||
}
|
||||
}
|
||||
|
||||
@ -690,14 +690,31 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
List<QueryWrapper> getChildSelect() {
|
||||
|
||||
List<QueryWrapper> tableChildQuery = null;
|
||||
List<QueryTable> queryTables = getQueryTables();
|
||||
if (CollectionUtil.isNotEmpty(queryTables)) {
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
if (queryTable instanceof SelectQueryTable) {
|
||||
if (tableChildQuery == null) {
|
||||
tableChildQuery = new ArrayList<>();
|
||||
}
|
||||
tableChildQuery.add(((SelectQueryTable) queryTable).getQueryWrapper());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
List<QueryWrapper> whereChildQuery = WrapperUtil.getChildQueryWrapper(whereQueryCondition);
|
||||
List<QueryWrapper> havingChildQuery = WrapperUtil.getChildQueryWrapper(havingQueryCondition);
|
||||
|
||||
if (whereChildQuery.isEmpty() && havingChildQuery.isEmpty()) {
|
||||
if (tableChildQuery == null && whereChildQuery.isEmpty() && havingChildQuery.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<QueryWrapper> childQueryWrappers = new ArrayList<>(whereChildQuery);
|
||||
|
||||
List<QueryWrapper> childQueryWrappers = tableChildQuery == null
|
||||
? new ArrayList<>() : new ArrayList<>(tableChildQuery);
|
||||
childQueryWrappers.addAll(whereChildQuery);
|
||||
childQueryWrappers.addAll(havingChildQuery);
|
||||
|
||||
return childQueryWrappers;
|
||||
|
||||
@ -31,7 +31,6 @@ public class CollectionUtil {
|
||||
return collection == null || collection.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNotEmpty(Collection<?> collection) {
|
||||
return !isEmpty(collection);
|
||||
}
|
||||
@ -41,7 +40,6 @@ public class CollectionUtil {
|
||||
return map == null || map.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNotEmpty(Map<?, ?> map) {
|
||||
return !isEmpty(map);
|
||||
}
|
||||
|
||||
@ -197,7 +197,7 @@ public class MapperUtil {
|
||||
"Expected one result (or null) to be returned by selectOne(), but found: " + size);
|
||||
}
|
||||
|
||||
public static long getLongNumber(List<Object> objects,QueryWrapper queryWrapper){
|
||||
public static long getLongNumber(List<Object> objects, QueryWrapper queryWrapper) {
|
||||
Object object = objects == null || objects.isEmpty() ? null : objects.get(0);
|
||||
if (object == null) {
|
||||
return 0;
|
||||
|
||||
@ -28,6 +28,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
@ -58,6 +59,9 @@ public class EntityTestStarter {
|
||||
|
||||
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||
|
||||
List<Account> accounts = accountMapper.selectAll();
|
||||
System.out.println(accounts);
|
||||
|
||||
// QueryWrapper wrapper = QueryWrapper.create().select(ACCOUNT.ID
|
||||
// , case_().when(ACCOUNT.ID.ge(2)).then("x2")
|
||||
// .when(ACCOUNT.ID.ge(1)).then("x1")
|
||||
|
||||
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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.test;
|
||||
|
||||
import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.util.MapperUtil;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
|
||||
public class JoinTester {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DataSource dataSource = new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("schema.sql")
|
||||
.addScript("data.sql")
|
||||
.build();
|
||||
|
||||
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
||||
.setDataSource(dataSource)
|
||||
.addMapper(AccountMapper.class)
|
||||
.start();
|
||||
|
||||
//开启审计功能
|
||||
AuditManager.setAuditEnable(true);
|
||||
|
||||
//设置 SQL 审计收集器
|
||||
MessageCollector collector = new ConsoleMessageCollector();
|
||||
AuditManager.setMessageCollector(collector);
|
||||
|
||||
|
||||
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
|
||||
|
||||
List<Account> accounts = accountMapper.selectAll();
|
||||
System.out.println(accounts);
|
||||
|
||||
QueryWrapper queryWrapper = MapperUtil.rawCountQueryWrapper(QueryWrapper.create().from("tb_account"));
|
||||
System.out.println(">>>>>>> count: " + accountMapper.selectCountByQuery(queryWrapper));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user