Skip to content

Commit 176327d

Browse files
committed
feat: enhance condition handling for count queries and add SelectCountBuilder for OracleNoSQL count queries support
Updated the condition method to support a new parameter indicating whether the query is for a count operation, allowing for optimized handling of ID conditions. Introduced SelectCountBuilder class to facilitate building SQL count queries, enhancing query capabilities for the Oracle database integration. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 1ccd781 commit 176327d

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

jnosql-oracle-nosql/src/main/java/org/eclipse/jnosql/databases/oracle/communication/AbstractQueryBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ abstract class AbstractQueryBuilder implements Supplier<OracleQuery> {
3434
this.table = table;
3535
}
3636

37-
protected void condition(CriteriaCondition condition, StringBuilder query, List<FieldValue> params, List<String> ids) {
37+
protected void condition(CriteriaCondition condition, StringBuilder query, List<FieldValue> params, List<String> ids, boolean forCount) {
3838
var document = condition.element();
3939
switch (condition.condition()) {
4040
case EQUALS:
41-
if (document.name().equals(DefaultOracleNoSQLDocumentManager.ID)) {
41+
if (!forCount && document.name().equals(DefaultOracleNoSQLDocumentManager.ID)) {
4242
ids.add(document.get(String.class));
4343
} else {
4444
predicate(query, " = ", document, params);
4545
}
4646
return;
4747
case IN:
48-
if (document.name().equals(DefaultOracleNoSQLDocumentManager.ID)) {
48+
if (!forCount && document.name().equals(DefaultOracleNoSQLDocumentManager.ID)) {
4949
ids.addAll(document.get(new TypeReference<List<String>>() {
5050
}));
5151
} else {
@@ -78,15 +78,15 @@ protected void condition(CriteriaCondition condition, StringBuilder query, List<
7878
return;
7979
case NOT:
8080
query.append(" NOT ");
81-
condition(document.get(CriteriaCondition.class), query, params, ids);
81+
condition(document.get(CriteriaCondition.class), query, params, ids, forCount);
8282
return;
8383
case OR:
8484
appendCondition(query, params, document.get(new TypeReference<>() {
85-
}), " OR ", ids);
85+
}), " OR ", ids, forCount);
8686
return;
8787
case AND:
8888
appendCondition(query, params, document.get(new TypeReference<>() {
89-
}), " AND ", ids);
89+
}), " AND ", ids, forCount);
9090
return;
9191
case BETWEEN:
9292
predicateBetween(query, params, document);
@@ -111,11 +111,11 @@ protected void predicateBetween(StringBuilder query,List<FieldValue> params, Ele
111111

112112
protected void appendCondition(StringBuilder query, List<FieldValue> params,
113113
List<CriteriaCondition> conditions,
114-
String condition, List<String> ids) {
114+
String condition, List<String> ids, boolean forCount) {
115115
int index = ORIGIN;
116116
for (CriteriaCondition documentCondition : conditions) {
117117
StringBuilder appendQuery = new StringBuilder();
118-
condition(documentCondition, appendQuery, params, ids);
118+
condition(documentCondition, appendQuery, params, ids, forCount);
119119
if(index == ORIGIN && !appendQuery.isEmpty()){
120120
query.append(appendQuery);
121121
} else if(!appendQuery.isEmpty()) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
package org.eclipse.jnosql.databases.oracle.communication;
16+
17+
import oracle.nosql.driver.values.FieldValue;
18+
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
final class SelectCountBuilder extends AbstractQueryBuilder {
24+
25+
public static final String COUNT = "count";
26+
private final SelectQuery documentQuery;
27+
28+
private final String table;
29+
30+
SelectCountBuilder(SelectQuery documentQuery, String table) {
31+
super(table);
32+
this.documentQuery = documentQuery;
33+
this.table = table;
34+
}
35+
36+
@Override
37+
public OracleQuery get() {
38+
List<String> ids = new ArrayList<>();
39+
List<FieldValue> params = new ArrayList<>();
40+
StringBuilder query = new StringBuilder();
41+
query.append("select ");
42+
query.append("count(*) as ").append(COUNT).append(' ');
43+
query.append("from ").append(table);
44+
entityCondition(query, documentQuery.name());
45+
this.documentQuery.condition().ifPresent(c -> {
46+
query.append(" AND ");
47+
condition(c, query, params, ids, true);
48+
});
49+
return new OracleQuery(query.toString(), params, ids);
50+
}
51+
}

0 commit comments

Comments
 (0)