Skip to content

Commit

Permalink
✨ feat(QueryFragment and QueryHelper): Refactor QueryFragment c…
Browse files Browse the repository at this point in the history
…onstructor and static methods, and update usage in `QueryHelper` and `QueryJsonHelper`.
  • Loading branch information
vnobo committed Dec 16, 2024
1 parent 1eb5c6c commit b88bca9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,38 @@ public class QueryFragment extends HashMap<String, Object> {

private final long offset;

public QueryFragment(int size, long offset, QueryFragment params) {
super(16);
this.size = size;
this.offset = offset;
this.mergeWhere(params.getWhereSql());
this.putAll(params);
}

public QueryFragment(int size, long offset, Map<String, Object> params) {
super(16);
this.size = size;
this.offset = offset;
this.putAll(params);
}

public static QueryFragment of(Map<String, Object> params) {
public static QueryFragment withNew() {
return withMap(25, 0, Map.of());
}

public static QueryFragment withMap(Map<String, Object> params) {
return new QueryFragment(25, 0, params);
}

public static QueryFragment withMap(int size, long offset, Map<String, Object> params) {
return new QueryFragment(size, offset, params);
}

public static QueryFragment of(QueryFragment params) {
return of(25, 0, params);
}

public static QueryFragment of(int size, long offset, Map<String, Object> params) {
public static QueryFragment of(int size, long offset, QueryFragment params) {
return new QueryFragment(size, offset, params);
}

Expand All @@ -51,6 +71,7 @@ public QueryFragment addColumn(CharSequence... columns) {
}

public QueryFragment addQuery(CharSequence... queries) {
this.querySql.setEmptyValue("");
for (CharSequence query : queries) {
this.querySql.add(query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static QueryFragment query(Object object, Pageable pageable, Collection<S
Map<String, Object> filterMap = ObjectUtils.isEmpty(objectMap) ? Map.of() :
Maps.filterKeys(objectMap, key -> !SKIP_CRITERIA_KEYS.contains(key) && !skipKeys.contains(key));

QueryFragment queryFragment = QueryFragment.of(pageable.getPageSize(), pageable.getOffset(), filterMap);
QueryFragment queryFragment = QueryFragment.withMap(pageable.getPageSize(), pageable.getOffset(), filterMap);
QueryHelper.applySort(queryFragment, pageable.getSort(), prefix);
QueryHelper.applyWhere(queryFragment, prefix);
QueryHelper.applyQuerySql(queryFragment, object);
Expand Down Expand Up @@ -80,7 +80,7 @@ public static QueryFragment query(Object object, Pageable pageable, Collection<S
if (objectMap.containsKey(searchKey) && !ObjectUtils.isEmpty(objectMap.get(searchKey))) {
String textSearch = (String) objectMap.get(searchKey);
queryFragment.addColumn("TS_RANK_CD(text_search, queryTextSearch) AS rank");
queryFragment.addQuery("TO_TSQUERY('chinese',:textSearch) queryTextSearch");
queryFragment.addQuery(",TO_TSQUERY('chinese',:textSearch) queryTextSearch");
queryFragment.addWhere("text_search @@ TO_TSQUERY('chinese',:textSearch)");
queryFragment.put("textSearch", textSearch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static Sort.Order convertSortOrderToCamelCase(Sort.Order order) {
* @throws IllegalArgumentException If any processing error occurs due to invalid input structure or content.
*/
public static QueryFragment queryJson(Map<String, Object> params, String prefix) {
QueryFragment queryFragment = QueryFragment.of(25, 0, Map.of());
QueryFragment queryFragment = QueryFragment.withNew();
for (Map.Entry<String, Object> entry : params.entrySet()) {
var condition = buildJsonCondition(entry, prefix);
queryFragment.mergeWhere(condition.getWhereSql());
Expand Down Expand Up @@ -173,7 +173,7 @@ private static QueryFragment buildJsonCondition(Map.Entry<String, Object> entry,
//处理最后键
QueryFragment lastCondition = buildLastCondition(keys, entry.getValue());
conditionBuilder.append(lastCondition.getWhereSql());
return QueryFragment.of(lastCondition).addWhere(conditionBuilder.toString());
return QueryFragment.withMap(lastCondition).addWhere(conditionBuilder.toString());
}

/**
Expand Down Expand Up @@ -201,7 +201,7 @@ private static QueryFragment buildLastCondition(String[] keys, Object value) {
Map.Entry<String, String> exps = queryKeywordMapper(lastKey);
if (exps == null) {
conditionSql.append(lastKey).append("' = :").append(paramName);
return QueryFragment.of(Map.of(paramName, value)).addWhere(conditionSql.toString());
return QueryFragment.withMap(Map.of(paramName, value)).addWhere(conditionSql.toString());
}

String key = lastKey.substring(0, lastKey.length() - exps.getKey().length());
Expand All @@ -221,7 +221,7 @@ private static QueryFragment buildLastCondition(String[] keys, Object value) {
conditionSql.append(exps.getValue()).append(" :").append(paramName);
params = Map.of(paramName, value);
}
return QueryFragment.of(params).addWhere(conditionSql.toString());
return QueryFragment.withMap(params).addWhere(conditionSql.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,15 @@
@Service
@RequiredArgsConstructor
public class TenantMembersService extends AbstractDatabase {
private final static String QUERY_SQL = """
select a.*, b.name as tenant_name, b.extend as tenant_extend,c.name as login_name,c.username
from se_tenant_members a
inner join se_tenants b on a.tenant_code = b.code
inner join se_users c on c.code = a.user_code
""";
private final static String COUNT_SQL = """
select count(*) from se_tenant_members a
inner join se_tenants b on a.tenant_code = b.code
inner join se_users c on c.code = a.user_code
""";

private final TenantMembersRepository tenantMembersRepository;

public Flux<TenantMemberResponse> search(TenantMemberRequest request, Pageable pageable) {
QueryFragment queryFragment = request.toParamSql().addColumn("a.*", "b.name as tenant_name",
QueryFragment fragment = request.toParamSql();
QueryFragment queryFragment = QueryFragment.of(pageable.getPageSize(), pageable.getOffset(), fragment)
.addColumn("a.*", "b.name as tenant_name",
"b.extend as tenant_extend", "c.name as login_name", "c.username")
.addQuery("a",
.addQuery("se_tenant_members 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");
Expand Down

0 comments on commit b88bca9

Please sign in to comment.