Skip to content

Commit

Permalink
✨ feat(QueryFragment): Add groupBy support in query building.
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Dec 20, 2024
1 parent aeb9399 commit c12cd59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class QueryFragment extends HashMap<String, Object> {
* }
* </pre>
*/
private final StringJoiner querySql = new StringJoiner(" ");
private final StringJoiner select = new StringJoiner(" ");

/**
* A StringJoiner to accumulate WHERE conditions.
Expand All @@ -74,7 +74,7 @@ public class QueryFragment extends HashMap<String, Object> {
* }
* </pre>
*/
private final StringJoiner whereSql = new StringJoiner(" AND ");
private final StringJoiner where = new StringJoiner(" AND ");

/**
* A StringJoiner to accumulate ORDER BY clauses.
Expand All @@ -85,7 +85,9 @@ public class QueryFragment extends HashMap<String, Object> {
* }
* </pre>
*/
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).
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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 "";
}
Expand All @@ -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"));
Expand All @@ -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!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static void processQueryKey(QueryFragment queryFragment, Map<String, Obj
if (objectMap.containsKey("query")) {
var jsonMap = (Map<String, Object>) objectMap.get("query");
var jsonQueryFragment = QueryJsonHelper.queryJson(jsonMap, prefix);
queryFragment.mergeWhere(jsonQueryFragment.getWhereSql());
queryFragment.mergeWhere(jsonQueryFragment.getWhere());
queryFragment.putAll(jsonQueryFragment);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static QueryFragment queryJson(Map<String, Object> params, String prefix)
QueryFragment queryFragment = QueryFragment.withNew();
for (Map.Entry<String, Object> entry : params.entrySet()) {
var condition = buildJsonCondition(entry, prefix);
queryFragment.mergeWhere(condition.getWhereSql());
queryFragment.mergeWhere(condition.getWhere());
queryFragment.putAll(condition);
}
return queryFragment;
Expand Down Expand Up @@ -231,7 +231,7 @@ private static QueryFragment buildJsonCondition(Map.Entry<String, Object> entry,
}
//处理最后键
QueryFragment lastCondition = buildLastCondition(keys, entry.getValue());
conditionBuilder.append(lastCondition.getWhereSql());
conditionBuilder.append(lastCondition.getWhere());
return QueryFragment.withMap(lastCondition).addWhere(conditionBuilder.toString());
}

Expand Down

0 comments on commit c12cd59

Please sign in to comment.