Skip to content

Commit

Permalink
add brackets option
Browse files Browse the repository at this point in the history
  • Loading branch information
yulichang committed Nov 12, 2024
1 parent 6abb21a commit 9173f6e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@
* @since 1.4.5
*/
@SuppressWarnings("DuplicatedCode")
public class WrapperUtils {
public class WrapperUtils implements StringPool {

public static <T> String buildSubSqlByWrapper(Class<T> clazz, MPJLambdaWrapper<T> wrapper, String alias) {
return String.format("%s AS %s", buildUnionSqlByWrapper(clazz, wrapper), alias);
}

public static String buildUnionSqlByWrapper(Class<?> clazz, MPJLambdaWrapper<?> wrapper) {
return buildUnionSqlByWrapper(clazz, true, wrapper);
}

public static String buildUnionSqlByWrapper(Class<?> clazz, boolean brackets, MPJLambdaWrapper<?> wrapper) {
TableInfo tableInfo = TableHelper.getAssert(clazz);
String first = Optional.ofNullable(wrapper.getSqlFirst()).orElse(StringPool.EMPTY);
String first = Optional.ofNullable(wrapper.getSqlFirst()).orElse(EMPTY);
boolean hasWhere = false;
String entityWhere = getEntitySql(tableInfo, wrapper);
if (StrUtils.isNotBlank(entityWhere)) {
Expand All @@ -39,19 +43,31 @@ public static String buildUnionSqlByWrapper(Class<?> clazz, MPJLambdaWrapper<?>
hasWhere = true;
}
String sqlSegment = (wrapper.getSqlSegment() != null && StrUtils.isNotBlank(wrapper.getSqlSegment())) ?
((wrapper.isEmptyOfNormal() ? StringPool.EMPTY : (hasWhere ? " AND " : " WHERE ")) + wrapper.getSqlSegment()) : StringPool.EMPTY;

String sqlComment = Optional.ofNullable(wrapper.getSqlComment()).orElse(StringPool.EMPTY);
return String.format("( %s SELECT %s FROM %s %s %s %s %s %s %s )",
first,
wrapper.getSqlSelect(),
wrapper.getTableName(tableInfo.getTableName()),
wrapper.getAlias(),
wrapper.getFrom(),
mainLogic,
subLogic,
sqlSegment,
sqlComment);
((wrapper.isEmptyOfNormal() ? EMPTY : (hasWhere ? " AND " : " WHERE ")) + wrapper.getSqlSegment()) : EMPTY;
String sqlComment = Optional.ofNullable(wrapper.getSqlComment()).orElse(EMPTY);
StringBuilder sb = new StringBuilder(SPACE)
.append(first)
.append(" SELECT ")
.append(wrapper.getSqlSelect())
.append(" FROM ")
.append(wrapper.getTableName(tableInfo.getTableName()))
.append(SPACE)
.append(wrapper.getAlias())
.append(SPACE)
.append(wrapper.getFrom())
.append(SPACE)
.append(mainLogic)
.append(SPACE)
.append(subLogic)
.append(SPACE)
.append(sqlSegment)
.append(SPACE)
.append(sqlComment)
.append(SPACE);
if (brackets) {
sb.insert(0, "(").append(")");
}
return sb.toString();
}

private static <T> String formatParam(MPJLambdaWrapper<T> wrapper, Object param) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ public <E, F> MPJLambdaWrapper<T> selectSub(Class<E> clazz, String st, Consumer<
return typedThis;
}

public <U> MPJLambdaWrapper<T> union(Class<U> clazz, Consumer<MPJLambdaWrapper<U>> consumer) {
return union(clazz, true, consumer);
}

/**
* union
* <p>
Expand All @@ -267,12 +271,10 @@ public <E, F> MPJLambdaWrapper<T> selectSub(Class<E> clazz, String st, Consumer<
* @param clazz union语句的主表类型
* @since 1.4.8
*/
public <U> MPJLambdaWrapper<T> union(Class<U> clazz, Consumer<MPJLambdaWrapper<U>> consumer) {
public <U> MPJLambdaWrapper<T> union(Class<U> clazz, boolean brackets, Consumer<MPJLambdaWrapper<U>> consumer) {
MPJLambdaWrapper<U> unionWrapper = fromInstance(clazz);
consumer.accept(unionWrapper);

String sb = " UNION " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper);

if (Objects.isNull(unionSql)) {
unionSql = SharedString.emptyString();
}
Expand All @@ -296,6 +298,10 @@ public void doFill(Object obj) {
}
}

public <U> MPJLambdaWrapper<T> unionAll(Class<U> clazz, Consumer<MPJLambdaWrapper<U>> consumer) {
return unionAll(clazz, true, consumer);
}

/**
* union
* <p>
Expand All @@ -304,12 +310,10 @@ public void doFill(Object obj) {
* @param clazz union语句的主表类型
* @since 1.4.8
*/
public <U> MPJLambdaWrapper<T> unionAll(Class<U> clazz, Consumer<MPJLambdaWrapper<U>> consumer) {
public <U> MPJLambdaWrapper<T> unionAll(Class<U> clazz, boolean brackets, Consumer<MPJLambdaWrapper<U>> consumer) {
MPJLambdaWrapper<U> unionWrapper = fromInstance(clazz);
consumer.accept(unionWrapper);

String sb = " UNION ALL " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper);

String sb = " UNION ALL " + WrapperUtils.buildUnionSqlByWrapper(clazz, brackets, unionWrapper);
if (Objects.isNull(unionSql)) {
unionSql = SharedString.emptyString();
}
Expand Down

0 comments on commit 9173f6e

Please sign in to comment.