mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
feat: 新增 with 的 SQL 构建方法; close #I7E19B
This commit is contained in:
parent
6f2d8113c1
commit
e780c14728
@ -58,6 +58,8 @@ public interface IDialect {
|
|||||||
|
|
||||||
String buildSelectSql(QueryWrapper queryWrapper);
|
String buildSelectSql(QueryWrapper queryWrapper);
|
||||||
|
|
||||||
|
String buildNoSelectSql(QueryWrapper queryWrapper);
|
||||||
|
|
||||||
String buildDeleteSql(QueryWrapper queryWrapper);
|
String buildDeleteSql(QueryWrapper queryWrapper);
|
||||||
|
|
||||||
String buildWhereConditionSql(QueryWrapper queryWrapper);
|
String buildWhereConditionSql(QueryWrapper queryWrapper);
|
||||||
|
|||||||
@ -30,10 +30,7 @@ import com.mybatisflex.core.util.StringUtil;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.StringJoiner;
|
|
||||||
|
|
||||||
import static com.mybatisflex.core.constant.SqlConsts.*;
|
import static com.mybatisflex.core.constant.SqlConsts.*;
|
||||||
|
|
||||||
@ -306,7 +303,15 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
|
||||||
|
|
||||||
StringBuilder sqlBuilder = buildSelectColumnSql(allTables, selectColumns, CPI.getHint(queryWrapper));
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
|
With with = CPI.getWith(queryWrapper);
|
||||||
|
if (with != null) {
|
||||||
|
sqlBuilder.append(with.toSql(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
buildSelectColumnSql(sqlBuilder, allTables, selectColumns, CPI.getHint(queryWrapper));
|
||||||
|
|
||||||
|
|
||||||
sqlBuilder.append(FROM).append(StringUtil.join(DELIMITER, queryTables, queryTable -> queryTable.toSql(this)));
|
sqlBuilder.append(FROM).append(StringUtil.join(DELIMITER, queryTables, queryTable -> queryTable.toSql(this)));
|
||||||
|
|
||||||
buildJoinSql(sqlBuilder, queryWrapper, allTables);
|
buildJoinSql(sqlBuilder, queryWrapper, allTables);
|
||||||
@ -339,8 +344,44 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
return sqlBuilder.toString();
|
return sqlBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private StringBuilder buildSelectColumnSql(List<QueryTable> queryTables, List<QueryColumn> selectColumns, String hint) {
|
@Override
|
||||||
StringBuilder sqlBuilder = new StringBuilder(SELECT);
|
public String buildNoSelectSql(QueryWrapper queryWrapper) {
|
||||||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
buildJoinSql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST);
|
||||||
|
buildWhereSql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST, true);
|
||||||
|
buildGroupBySql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST);
|
||||||
|
buildHavingSql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST);
|
||||||
|
buildOrderBySql(sqlBuilder, queryWrapper, Collections.EMPTY_LIST);
|
||||||
|
|
||||||
|
List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(unions)) {
|
||||||
|
if (sqlBuilder.length() > 0) {
|
||||||
|
sqlBuilder.insert(0, BRACKET_LEFT).append(BRACKET_RIGHT);
|
||||||
|
}
|
||||||
|
for (UnionWrapper unionWrapper : unions) {
|
||||||
|
unionWrapper.buildSql(sqlBuilder, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer limitRows = CPI.getLimitRows(queryWrapper);
|
||||||
|
Integer limitOffset = CPI.getLimitOffset(queryWrapper);
|
||||||
|
if (limitRows != null || limitOffset != null) {
|
||||||
|
sqlBuilder = buildLimitOffsetSql(sqlBuilder, queryWrapper, limitRows, limitOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> endFragments = CPI.getEndFragments(queryWrapper);
|
||||||
|
if (CollectionUtil.isNotEmpty(endFragments)) {
|
||||||
|
for (String endFragment : endFragments) {
|
||||||
|
sqlBuilder.append(BLANK).append(endFragment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildSelectColumnSql(StringBuilder sqlBuilder, List<QueryTable> queryTables, List<QueryColumn> selectColumns, String hint) {
|
||||||
|
sqlBuilder.append(SELECT);
|
||||||
sqlBuilder.append(forHint(hint));
|
sqlBuilder.append(forHint(hint));
|
||||||
if (selectColumns == null || selectColumns.isEmpty()) {
|
if (selectColumns == null || selectColumns.isEmpty()) {
|
||||||
sqlBuilder.append(ASTERISK);
|
sqlBuilder.append(ASTERISK);
|
||||||
@ -355,7 +396,6 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sqlBuilder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -800,7 +840,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String forSelectOneEntityById(TableInfo tableInfo) {
|
public String forSelectOneEntityById(TableInfo tableInfo) {
|
||||||
StringBuilder sql = buildSelectColumnSql(null, null, null);
|
StringBuilder sql = new StringBuilder();
|
||||||
|
buildSelectColumnSql(sql, null, null, null);
|
||||||
sql.append(FROM).append(tableInfo.getWrapSchemaAndTableName(this));
|
sql.append(FROM).append(tableInfo.getWrapSchemaAndTableName(this));
|
||||||
sql.append(WHERE);
|
sql.append(WHERE);
|
||||||
String[] pKeys = tableInfo.getPrimaryKeys();
|
String[] pKeys = tableInfo.getPrimaryKeys();
|
||||||
@ -829,7 +870,8 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String forSelectEntityListByIds(TableInfo tableInfo, Object[] primaryValues) {
|
public String forSelectEntityListByIds(TableInfo tableInfo, Object[] primaryValues) {
|
||||||
StringBuilder sql = buildSelectColumnSql(null, tableInfo.getDefaultQueryColumn(), null);
|
StringBuilder sql = new StringBuilder();
|
||||||
|
buildSelectColumnSql(sql, null, tableInfo.getDefaultQueryColumn(), null);
|
||||||
sql.append(FROM).append(tableInfo.getWrapSchemaAndTableName(this));
|
sql.append(FROM).append(tableInfo.getWrapSchemaAndTableName(this));
|
||||||
sql.append(WHERE);
|
sql.append(WHERE);
|
||||||
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
||||||
@ -974,12 +1016,12 @@ public class CommonsDialectImpl implements IDialect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String buildLogicNormalCondition(String logicColumn) {
|
protected String buildLogicNormalCondition(String logicColumn) {
|
||||||
return LogicDeleteManager.getProcessor().buildLogicNormalCondition(logicColumn,this);
|
return LogicDeleteManager.getProcessor().buildLogicNormalCondition(logicColumn, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected String buildLogicDeletedSet(String logicColumn) {
|
protected String buildLogicDeletedSet(String logicColumn) {
|
||||||
return LogicDeleteManager.getProcessor().buildLogicDeletedSet(logicColumn,this);
|
return LogicDeleteManager.getProcessor().buildLogicDeletedSet(logicColumn, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import java.util.*;
|
|||||||
public class BaseQueryWrapper<T extends BaseQueryWrapper<T>> implements CloneSupport<T> {
|
public class BaseQueryWrapper<T extends BaseQueryWrapper<T>> implements CloneSupport<T> {
|
||||||
|
|
||||||
|
|
||||||
|
protected With with;
|
||||||
protected List<QueryTable> queryTables;
|
protected List<QueryTable> queryTables;
|
||||||
protected String dataSource;
|
protected String dataSource;
|
||||||
protected String hint;
|
protected String hint;
|
||||||
|
|||||||
@ -41,6 +41,12 @@ public class CPI {
|
|||||||
return queryWrapper.getChildSelect();
|
return queryWrapper.getChildSelect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static With getWith(QueryWrapper queryWrapper) {
|
||||||
|
return queryWrapper.with;
|
||||||
|
}
|
||||||
|
|
||||||
public static List<QueryTable> getQueryTables(QueryWrapper queryWrapper) {
|
public static List<QueryTable> getQueryTables(QueryWrapper queryWrapper) {
|
||||||
return queryWrapper.getQueryTables();
|
return queryWrapper.getQueryTables();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -199,15 +199,16 @@ public class QueryMethods {
|
|||||||
return QueryCondition.createEmpty();
|
return QueryCondition.createEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static QueryWrapper newWrapper() {
|
|
||||||
return new QueryWrapper();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
// QueryWrapper methods
|
||||||
public static QueryWrapper select(QueryColumn... queryColumns) {
|
public static QueryWrapper select(QueryColumn... queryColumns) {
|
||||||
return newWrapper().select(queryColumns);
|
return newWrapper().select(queryColumns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static QueryWrapper union(QueryWrapper queryWrapper) {
|
||||||
|
return newWrapper().union(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
public static QueryWrapper selectOne() {
|
public static QueryWrapper selectOne() {
|
||||||
return select(column("1"));
|
return select(column("1"));
|
||||||
}
|
}
|
||||||
@ -220,6 +221,12 @@ public class QueryMethods {
|
|||||||
return select(count("1"));
|
return select(count("1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static QueryWrapper newWrapper() {
|
||||||
|
return new QueryWrapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static RawFragment raw(String raw) {
|
public static RawFragment raw(String raw) {
|
||||||
return new RawFragment(raw);
|
return new RawFragment(raw);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,6 +34,35 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public WithBuilder with(String name) {
|
||||||
|
if (with == null) {
|
||||||
|
with = new With();
|
||||||
|
}
|
||||||
|
return new WithBuilder(this, with, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithBuilder with(String name, String... params) {
|
||||||
|
if (with == null) {
|
||||||
|
with = new With();
|
||||||
|
}
|
||||||
|
return new WithBuilder(this, with, name, Arrays.asList(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithBuilder withRecursive(String name) {
|
||||||
|
if (with == null) {
|
||||||
|
with = new With(true);
|
||||||
|
}
|
||||||
|
return new WithBuilder(this, with, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithBuilder withRecursive(String name, String... params) {
|
||||||
|
if (with == null) {
|
||||||
|
with = new With(true);
|
||||||
|
}
|
||||||
|
return new WithBuilder(this, with, name, Arrays.asList(params));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public QueryWrapper select() {
|
public QueryWrapper select() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -556,6 +585,15 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
*/
|
*/
|
||||||
Object[] getValueArray() {
|
Object[] getValueArray() {
|
||||||
|
|
||||||
|
List<Object> withValues = null;
|
||||||
|
if (with != null) {
|
||||||
|
Object[] paramValues = with.getParamValues();
|
||||||
|
if (ArrayUtil.isNotEmpty(paramValues)) {
|
||||||
|
withValues = new ArrayList<>(Arrays.asList(paramValues));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<Object> columnValues = null;
|
List<Object> columnValues = null;
|
||||||
List<QueryColumn> selectColumns = getSelectColumns();
|
List<QueryColumn> selectColumns = getSelectColumns();
|
||||||
if (CollectionUtil.isNotEmpty(selectColumns)) {
|
if (CollectionUtil.isNotEmpty(selectColumns)) {
|
||||||
@ -564,7 +602,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
Object[] paramValues = ((HasParamsColumn) selectColumn).getParamValues();
|
Object[] paramValues = ((HasParamsColumn) selectColumn).getParamValues();
|
||||||
if (ArrayUtil.isNotEmpty(paramValues)) {
|
if (ArrayUtil.isNotEmpty(paramValues)) {
|
||||||
if (columnValues == null) {
|
if (columnValues == null) {
|
||||||
columnValues = new ArrayList<>();
|
columnValues = new ArrayList<>(paramValues.length);
|
||||||
}
|
}
|
||||||
columnValues.addAll(Arrays.asList(paramValues));
|
columnValues.addAll(Arrays.asList(paramValues));
|
||||||
}
|
}
|
||||||
@ -580,7 +618,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
Object[] tableValueArray = queryTable.getValueArray();
|
Object[] tableValueArray = queryTable.getValueArray();
|
||||||
if (tableValueArray.length > 0) {
|
if (tableValueArray.length > 0) {
|
||||||
if (tableValues == null) {
|
if (tableValues == null) {
|
||||||
tableValues = new ArrayList<>();
|
tableValues = new ArrayList<>(tableValueArray.length);
|
||||||
}
|
}
|
||||||
tableValues.addAll(Arrays.asList(tableValueArray));
|
tableValues.addAll(Arrays.asList(tableValueArray));
|
||||||
}
|
}
|
||||||
@ -596,7 +634,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
Object[] valueArray = joinTable.getValueArray();
|
Object[] valueArray = joinTable.getValueArray();
|
||||||
if (valueArray.length > 0) {
|
if (valueArray.length > 0) {
|
||||||
if (joinValues == null) {
|
if (joinValues == null) {
|
||||||
joinValues = new ArrayList<>();
|
joinValues = new ArrayList<>(valueArray.length);
|
||||||
}
|
}
|
||||||
joinValues.addAll(Arrays.asList(valueArray));
|
joinValues.addAll(Arrays.asList(valueArray));
|
||||||
}
|
}
|
||||||
@ -604,7 +642,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
Object[] values = WrapperUtil.getValues(onCondition);
|
Object[] values = WrapperUtil.getValues(onCondition);
|
||||||
if (values.length > 0) {
|
if (values.length > 0) {
|
||||||
if (joinValues == null) {
|
if (joinValues == null) {
|
||||||
joinValues = new ArrayList<>();
|
joinValues = new ArrayList<>(values.length);
|
||||||
}
|
}
|
||||||
joinValues.addAll(Arrays.asList(values));
|
joinValues.addAll(Arrays.asList(values));
|
||||||
}
|
}
|
||||||
@ -627,7 +665,8 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object[] returnValues = columnValues == null ? FlexConsts.EMPTY_ARRAY : columnValues.toArray();
|
Object[] returnValues = withValues == null ? FlexConsts.EMPTY_ARRAY : withValues.toArray();
|
||||||
|
returnValues = columnValues != null ? ArrayUtil.concat(returnValues, columnValues.toArray()) : returnValues;
|
||||||
returnValues = tableValues != null ? ArrayUtil.concat(returnValues, tableValues.toArray()) : returnValues;
|
returnValues = tableValues != null ? ArrayUtil.concat(returnValues, tableValues.toArray()) : returnValues;
|
||||||
returnValues = joinValues != null ? ArrayUtil.concat(returnValues, joinValues.toArray()) : returnValues;
|
returnValues = joinValues != null ? ArrayUtil.concat(returnValues, joinValues.toArray()) : returnValues;
|
||||||
returnValues = ArrayUtil.concat(returnValues, paramValues);
|
returnValues = ArrayUtil.concat(returnValues, paramValues);
|
||||||
|
|||||||
@ -25,14 +25,14 @@ public class UnionWrapper implements CloneSupport<UnionWrapper> {
|
|||||||
private String key;
|
private String key;
|
||||||
private QueryWrapper queryWrapper;
|
private QueryWrapper queryWrapper;
|
||||||
|
|
||||||
public static UnionWrapper union(QueryWrapper queryWrapper) {
|
static UnionWrapper union(QueryWrapper queryWrapper) {
|
||||||
UnionWrapper unionWrapper = new UnionWrapper();
|
UnionWrapper unionWrapper = new UnionWrapper();
|
||||||
unionWrapper.key = SqlConsts.UNION;
|
unionWrapper.key = SqlConsts.UNION;
|
||||||
unionWrapper.queryWrapper = queryWrapper;
|
unionWrapper.queryWrapper = queryWrapper;
|
||||||
return unionWrapper;
|
return unionWrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UnionWrapper unionAll(QueryWrapper queryWrapper) {
|
static UnionWrapper unionAll(QueryWrapper queryWrapper) {
|
||||||
UnionWrapper unionWrapper = new UnionWrapper();
|
UnionWrapper unionWrapper = new UnionWrapper();
|
||||||
unionWrapper.key = SqlConsts.UNION_ALL;
|
unionWrapper.key = SqlConsts.UNION_ALL;
|
||||||
unionWrapper.queryWrapper = queryWrapper;
|
unionWrapper.queryWrapper = queryWrapper;
|
||||||
|
|||||||
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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.FlexConsts;
|
||||||
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
|
import com.mybatisflex.core.util.ArrayUtil;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class With implements Serializable {
|
||||||
|
|
||||||
|
private boolean recursive;
|
||||||
|
private List<WithItem> withItems;
|
||||||
|
|
||||||
|
public With() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public With(boolean recursive) {
|
||||||
|
this.recursive = recursive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRecursive() {
|
||||||
|
return recursive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecursive(boolean recursive) {
|
||||||
|
this.recursive = recursive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<WithItem> getWithItems() {
|
||||||
|
return withItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithItems(List<WithItem> withItems) {
|
||||||
|
this.withItems = withItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWithItem(WithItem withItem) {
|
||||||
|
if (withItems == null) {
|
||||||
|
withItems = new ArrayList<>();
|
||||||
|
}
|
||||||
|
withItems.add(withItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
StringBuilder sql = new StringBuilder("WITH ");
|
||||||
|
if (recursive) {
|
||||||
|
sql.append("RECURSIVE ");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < withItems.size(); i++) {
|
||||||
|
sql.append(withItems.get(i).toSql(dialect));
|
||||||
|
if (i != withItems.size() - 1) {
|
||||||
|
sql.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sql.append(" ").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
Object[] paramValues = FlexConsts.EMPTY_ARRAY;
|
||||||
|
for (WithItem withItem : withItems) {
|
||||||
|
paramValues = ArrayUtil.concat(paramValues, withItem.getParamValues());
|
||||||
|
}
|
||||||
|
return paramValues;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* 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.util.CollectionUtil;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class WithBuilder {
|
||||||
|
|
||||||
|
private QueryWrapper queryWrapper;
|
||||||
|
private With with;
|
||||||
|
|
||||||
|
//withItem
|
||||||
|
private String name;
|
||||||
|
private List<String> params;
|
||||||
|
|
||||||
|
public WithBuilder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithBuilder(QueryWrapper queryWrapper, With with, String name) {
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
this.with = with;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithBuilder(QueryWrapper queryWrapper, With with, String name, List<String> params) {
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
this.with = with;
|
||||||
|
this.name = name;
|
||||||
|
this.params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public QueryWrapper asSelect(QueryWrapper newWrapper) {
|
||||||
|
WithItem withItem = new WithItem(name, params);
|
||||||
|
withItem.setWithDetail(new WithSelectDetail(newWrapper));
|
||||||
|
with.addWithItem(withItem);
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public QueryWrapper asValues(Object[] values, QueryWrapper newWrapper) {
|
||||||
|
WithItem withItem = new WithItem(name, params);
|
||||||
|
withItem.setWithDetail(new WithValuesDetail(Arrays.asList(values), newWrapper));
|
||||||
|
with.addWithItem(withItem);
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public QueryWrapper asValues(Collection values, QueryWrapper newWrapper) {
|
||||||
|
WithItem withItem = new WithItem(name, params);
|
||||||
|
withItem.setWithDetail(new WithValuesDetail(CollectionUtil.toList(values), newWrapper));
|
||||||
|
with.addWithItem(withItem);
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryWrapper asRaw(String rawSQL, Object... params) {
|
||||||
|
WithItem withItem = new WithItem(name, this.params);
|
||||||
|
withItem.setWithDetail(new WithStringDetail(rawSQL, params));
|
||||||
|
with.addWithItem(withItem);
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public interface WithDetail {
|
||||||
|
|
||||||
|
String toSql(IDialect dialect);
|
||||||
|
|
||||||
|
Object[] getParamValues();
|
||||||
|
}
|
||||||
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* 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.CollectionUtil;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class WithItem implements Serializable {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private List<String> params;
|
||||||
|
|
||||||
|
private WithDetail withDetail;
|
||||||
|
|
||||||
|
public WithItem() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithItem(String name, List<String> params) {
|
||||||
|
this.name = name;
|
||||||
|
this.params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getParams() {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParams(List<String> params) {
|
||||||
|
this.params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithDetail getWithDetail() {
|
||||||
|
return withDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithDetail(WithDetail withDetail) {
|
||||||
|
this.withDetail = withDetail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
StringBuilder sql = new StringBuilder(name);
|
||||||
|
if (CollectionUtil.isNotEmpty(params)){
|
||||||
|
sql.append("(").append(StringUtil.join(", ",params)).append(")");
|
||||||
|
}
|
||||||
|
sql.append(" AS (");
|
||||||
|
sql.append(withDetail.toSql(dialect));
|
||||||
|
return sql.append(")").toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
return withDetail.getParamValues();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class WithSelectDetail implements WithDetail{
|
||||||
|
|
||||||
|
private QueryWrapper queryWrapper;
|
||||||
|
|
||||||
|
public WithSelectDetail() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithSelectDetail(QueryWrapper queryWrapper) {
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryWrapper getQueryWrapper() {
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQueryWrapper(QueryWrapper queryWrapper) {
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
return dialect.buildSelectSql(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
return queryWrapper.getValueArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class WithStringDetail implements WithDetail{
|
||||||
|
|
||||||
|
private String rawSQL;
|
||||||
|
private Object[] params;
|
||||||
|
|
||||||
|
public WithStringDetail(String rawSQL, Object[] params) {
|
||||||
|
this.rawSQL = rawSQL;
|
||||||
|
this.params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRawSQL() {
|
||||||
|
return rawSQL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRawSQL(String rawSQL) {
|
||||||
|
this.rawSQL = rawSQL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getParams() {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParams(Object[] params) {
|
||||||
|
this.params = params;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
return rawSQL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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 WithValuesDetail implements WithDetail{
|
||||||
|
|
||||||
|
private List<Object> values;
|
||||||
|
private QueryWrapper queryWrapper;
|
||||||
|
|
||||||
|
public WithValuesDetail() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public WithValuesDetail(List<Object> values, QueryWrapper queryWrapper) {
|
||||||
|
this.values = values;
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public QueryWrapper getQueryWrapper() {
|
||||||
|
return queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setQueryWrapper(QueryWrapper queryWrapper) {
|
||||||
|
this.queryWrapper = queryWrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toSql(IDialect dialect) {
|
||||||
|
List<String> stringValues = new ArrayList<>(values.size());
|
||||||
|
for (Object value : values) {
|
||||||
|
stringValues.add(String.valueOf(value));
|
||||||
|
}
|
||||||
|
StringBuilder sql = new StringBuilder("VALUES (")
|
||||||
|
.append(StringUtil.join(", ",stringValues)).append(") ");
|
||||||
|
return sql.append(dialect.buildNoSelectSql(queryWrapper)).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] getParamValues() {
|
||||||
|
return queryWrapper.getValueArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -229,7 +229,6 @@ public class StringUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 合并字符串,优化 String.join() 方法
|
* 合并字符串,优化 String.join() 方法
|
||||||
*
|
*
|
||||||
@ -248,6 +247,8 @@ public class StringUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 合并字符串,优化 String.join() 方法
|
* 合并字符串,优化 String.join() 方法
|
||||||
*
|
*
|
||||||
|
|||||||
@ -0,0 +1,100 @@
|
|||||||
|
package com.mybatisflex.coretest;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static com.mybatisflex.core.query.QueryMethods.union;
|
||||||
|
import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT;
|
||||||
|
import static com.mybatisflex.coretest.table.ArticleTableDef.ARTICLE;
|
||||||
|
|
||||||
|
public class WithSQLTester {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSql1() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.with("CTE").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(100))
|
||||||
|
)
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
System.out.println(query.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSql2() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.withRecursive("CTE").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(100))
|
||||||
|
)
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
System.out.println(query.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSql3() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.withRecursive("CTE", "id", "value").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(100))
|
||||||
|
)
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
System.out.println(query.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSql4() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.with("CTE").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(100))
|
||||||
|
)
|
||||||
|
.with("xxx").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(200))
|
||||||
|
)
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
System.out.println(query.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithSql5() {
|
||||||
|
QueryWrapper query = new QueryWrapper()
|
||||||
|
.withRecursive("CTE").asSelect(
|
||||||
|
QueryWrapper.create().from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(100))
|
||||||
|
)
|
||||||
|
.with("xxx", "id").asValues(
|
||||||
|
Arrays.asList("a", "b"),
|
||||||
|
union(
|
||||||
|
QueryWrapper.create()
|
||||||
|
.from(ARTICLE)
|
||||||
|
.where(ARTICLE.ID.ge(200))
|
||||||
|
)
|
||||||
|
|
||||||
|
)
|
||||||
|
.select()
|
||||||
|
.from(ACCOUNT)
|
||||||
|
.where(ACCOUNT.SEX.eq(1));
|
||||||
|
|
||||||
|
System.out.println(query.toSQL());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user