Skip to content

Commit

Permalink
♻️ refactor(QueryFragment.java): Simplify query construction and en…
Browse files Browse the repository at this point in the history
…hance method flexibility.

♻️ refactor(`QueryHelper.java`): Optimize query merging logic.
♻️ refactor(`QueryJsonHelper.java`): Improve JSON query handling.
♻️ refactor(`TenantMemberRequest.java`): Refine user code query formatting.
♻️ refactor(`TenantMembersService.java`): Streamline search and pagination queries.
  • Loading branch information
vnobo committed Dec 16, 2024
1 parent 21f577c commit 1eb5c6c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class QueryFragment extends HashMap<String, Object> {

private final StringJoiner columns = new StringJoiner(",");

private final StringJoiner querySql = new StringJoiner(",");
private final StringJoiner querySql = new StringJoiner(" ");

private final StringJoiner whereSql = new StringJoiner(" AND ");

Expand All @@ -43,13 +43,17 @@ public static QueryFragment of(int size, long offset, Map<String, Object> params
return new QueryFragment(size, offset, params);
}

public QueryFragment addColumn(CharSequence column) {
columns.add(column);
public QueryFragment addColumn(CharSequence... columns) {
for (CharSequence column : columns) {
this.columns.add(column);
}
return this;
}

public QueryFragment addQuery(CharSequence query) {
querySql.add(query);
public QueryFragment addQuery(CharSequence... queries) {
for (CharSequence query : queries) {
this.querySql.add(query);
}
return this;
}

Expand All @@ -73,15 +77,6 @@ public QueryFragment mergeOrder(StringJoiner order) {
return this;
}

public QueryFragment merge(QueryFragment fragment) {
this.putAll(fragment);
this.columns.merge(fragment.getColumns());
this.querySql.merge(fragment.getQuerySql());
this.whereSql.merge(fragment.getWhereSql());
this.orderSql.merge(fragment.getOrderSql());
return of(fragment.getSize(), fragment.getOffset(), this);
}

/**
* Generates the WHERE clause part of a SQL query based on the stored conditions.
* If conditions have been accumulated, it prefixes the conditions with the 'WHERE' keyword;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public static QueryFragment query(Object object, Pageable pageable, Collection<S
if (objectMap.containsKey(queryKey)) {
Map<String, Object> jsonMap = (Map<String, Object>) objectMap.get(queryKey);
QueryFragment jsonQueryFragment = QueryJsonHelper.queryJson(jsonMap, prefix);
queryFragment.merge(jsonQueryFragment);
queryFragment.mergeWhere(jsonQueryFragment.getWhereSql());
queryFragment.putAll(jsonQueryFragment);
}

String securityCodeKey = "securityCode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ private static Sort.Order convertSortOrderToCamelCase(Sort.Order order) {
public static QueryFragment queryJson(Map<String, Object> params, String prefix) {
QueryFragment queryFragment = QueryFragment.of(25, 0, Map.of());
for (Map.Entry<String, Object> entry : params.entrySet()) {
queryFragment.merge(buildJsonCondition(entry, prefix));
var condition = buildJsonCondition(entry, prefix);
queryFragment.mergeWhere(condition.getWhereSql());
queryFragment.putAll(condition);
}
return queryFragment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public QueryFragment toParamSql() {
QueryFragment fragment = QueryHelper.query(this, List.of("users", "securityCode", "username"), "a");

if (!ObjectUtils.isEmpty(this.getUsers())) {
fragment.addWhere("a.user_code in :users");
fragment.put("users", this.getUsers());
fragment.addWhere("a.user_code in (:users)");
fragment.put("users", StringUtils.collectionToCommaDelimitedString(this.getUsers()));
}

if (StringUtils.hasLength(this.getSecurityCode())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ select count(*) from se_tenant_members a
private final TenantMembersRepository tenantMembersRepository;

public Flux<TenantMemberResponse> search(TenantMemberRequest request, Pageable pageable) {
QueryFragment queryFragment = QueryHelper.query(request, pageable, "a");
queryFragment = queryFragment.merge(request.toParamSql());
QueryFragment queryFragment = request.toParamSql().addColumn("a.*", "b.name as tenant_name",
"b.extend as tenant_extend", "c.name as login_name", "c.username")
.addQuery("a",
"inner join se_tenants b on a.tenant_code = b.code",
"inner join se_users c on c.code = a.user_code");
QueryHelper.applySort(queryFragment, pageable.getSort(), "a");
return super.queryWithCache(BeanUtils.cacheKey(request, pageable), queryFragment.querySql(),
queryFragment, TenantMemberResponse.class);
}

public Mono<Page<TenantMemberResponse>> page(TenantMemberRequest request, Pageable pageable) {
var searchMono = this.search(request, pageable).collectList();
QueryFragment queryFragment = request.toParamSql();
String query = COUNT_SQL + queryFragment.whereSql();
Mono<Long> countMono = this.countWithCache(BeanUtils.cacheKey(request), query, queryFragment);
Mono<Long> countMono = this.countWithCache(BeanUtils.cacheKey(request), queryFragment.countSql(), queryFragment);
return searchMono.zipWith(countMono)
.map(tuple2 -> new PageImpl<>(tuple2.getT1(), pageable, tuple2.getT2()));
}
Expand Down

0 comments on commit 1eb5c6c

Please sign in to comment.