From c12cd592ab1c0d91590189044f2be9398ee12b98 Mon Sep 17 00:00:00 2001 From: AlexBob Date: Fri, 20 Dec 2024 18:06:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(`QueryFragment`):=20Add=20`gro?= =?UTF-8?q?upBy`=20support=20in=20query=20building.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/utils/query/QueryFragment.java | 38 ++++++++++--------- .../boot/commons/utils/query/QueryHelper.java | 2 +- .../commons/utils/query/QueryJsonHelper.java | 4 +- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java index 24f4085..40b9f12 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java @@ -63,7 +63,7 @@ public class QueryFragment extends HashMap { * } * */ - private final StringJoiner querySql = new StringJoiner(" "); + private final StringJoiner select = new StringJoiner(" "); /** * A StringJoiner to accumulate WHERE conditions. @@ -74,7 +74,7 @@ public class QueryFragment extends HashMap { * } * */ - private final StringJoiner whereSql = new StringJoiner(" AND "); + private final StringJoiner where = new StringJoiner(" AND "); /** * A StringJoiner to accumulate ORDER BY clauses. @@ -85,7 +85,9 @@ public class QueryFragment extends HashMap { * } * */ - private final StringJoiner orderSql = new StringJoiner(","); + private final StringJoiner orderBy = new StringJoiner(","); + + private final StringJoiner groupBy = new StringJoiner(","); /** * The maximum number of rows to return (LIMIT clause). @@ -101,7 +103,7 @@ public QueryFragment(int size, long offset, QueryFragment params) { super(16); this.size = size; this.offset = offset; - this.mergeWhere(params.getWhereSql()); + this.mergeWhere(params.getWhere()); this.putAll(params); } @@ -140,15 +142,15 @@ public QueryFragment addColumn(CharSequence... columns) { } public QueryFragment addQuery(CharSequence... queries) { - this.querySql.setEmptyValue(""); + this.select.setEmptyValue(""); for (CharSequence query : queries) { - this.querySql.add(query); + this.select.add(query); } return this; } public QueryFragment addWhere(CharSequence where) { - whereSql.add(where); + this.where.add(where); return this; } @@ -159,7 +161,7 @@ public QueryFragment addWhere(CharSequence where) { * @return this */ public QueryFragment addOrder(CharSequence order) { - orderSql.add(order); + this.orderBy.add(order); return this; } @@ -170,7 +172,7 @@ public QueryFragment addOrder(CharSequence order) { * @return this */ public QueryFragment mergeWhere(StringJoiner where) { - whereSql.merge(where); + this.where.merge(where); return this; } @@ -181,7 +183,7 @@ public QueryFragment mergeWhere(StringJoiner where) { * @return this */ public QueryFragment mergeOrder(StringJoiner order) { - orderSql.merge(order); + this.orderBy.merge(order); return this; } @@ -193,15 +195,15 @@ public QueryFragment mergeOrder(StringJoiner order) { * @return A String forming the WHERE clause of the SQL query, or an empty string if no conditions are present. */ public String whereSql() { - if (this.whereSql.length() > 0) { - return " WHERE " + this.whereSql; + if (this.where.length() > 0) { + return " WHERE " + this.where; } return ""; } public String orderSql() { - if (this.orderSql.length() > 0) { - return " ORDER BY " + this.orderSql; + if (this.orderBy.length() > 0) { + return " ORDER BY " + this.orderBy; } return ""; } @@ -218,9 +220,9 @@ public String orderSql() { * @throws QueryException if the querySql is null, indicating that the query structure is incomplete. */ public String querySql() { - if (this.querySql.length() > 0) { + if (this.select.length() > 0) { return String.format("SELECT %s FROM %s %s %s LIMIT %d OFFSET %d", - this.columns, this.querySql, whereSql(), orderSql(), this.size, this.offset); + this.columns, this.select, whereSql(), orderSql(), this.size, this.offset); } throw QueryException.withError("This querySql is null, please use whereSql() method!", new IllegalArgumentException("This querySql is null, please use whereSql() method")); @@ -238,8 +240,8 @@ public String querySql() { * @throws QueryException if the countSql is null, indicating that the query structure is incomplete. */ public String countSql() { - if (this.querySql.length() > 0) { - return "SELECT COUNT(*) FROM (" + String.format("SELECT %s FROM %s", this.columns, this.querySql) + if (this.select.length() > 0) { + return "SELECT COUNT(*) FROM (" + String.format("SELECT %s FROM %s", this.columns, this.select) + whereSql() + ") t"; } throw QueryException.withError("This countSql is null, please use whereSql() method!", diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java index 05005ec..a489f48 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java @@ -241,7 +241,7 @@ private static void processQueryKey(QueryFragment queryFragment, Map) objectMap.get("query"); var jsonQueryFragment = QueryJsonHelper.queryJson(jsonMap, prefix); - queryFragment.mergeWhere(jsonQueryFragment.getWhereSql()); + queryFragment.mergeWhere(jsonQueryFragment.getWhere()); queryFragment.putAll(jsonQueryFragment); } } diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java index a65639a..8b3f1d1 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryJsonHelper.java @@ -165,7 +165,7 @@ public static QueryFragment queryJson(Map params, String prefix) QueryFragment queryFragment = QueryFragment.withNew(); for (Map.Entry entry : params.entrySet()) { var condition = buildJsonCondition(entry, prefix); - queryFragment.mergeWhere(condition.getWhereSql()); + queryFragment.mergeWhere(condition.getWhere()); queryFragment.putAll(condition); } return queryFragment; @@ -231,7 +231,7 @@ private static QueryFragment buildJsonCondition(Map.Entry entry, } //处理最后键 QueryFragment lastCondition = buildLastCondition(keys, entry.getValue()); - conditionBuilder.append(lastCondition.getWhereSql()); + conditionBuilder.append(lastCondition.getWhere()); return QueryFragment.withMap(lastCondition).addWhere(conditionBuilder.toString()); }