@@ -36,14 +36,18 @@ final class N1QLBuilder implements Supplier<N1QLQuery> {
3636
3737 private final String scope ;
3838
39- private N1QLBuilder (SelectQuery query , String database , String scope ) {
39+ private final boolean shouldCount ;
40+
41+ private N1QLBuilder (SelectQuery query , String database , String scope , boolean shouldCount ) {
4042 this .query = query ;
4143 this .database = database ;
4244 this .scope = scope ;
45+ this .shouldCount = shouldCount ;
4346 }
4447
4548 @ Override
4649 public N1QLQuery get () {
50+
4751 StringBuilder n1ql = new StringBuilder ();
4852 JsonObject params = JsonObject .create ();
4953 List <String > ids = new ArrayList <>();
@@ -60,6 +64,9 @@ public N1QLQuery get() {
6064 condition (c , n1ql , params , ids );
6165 });
6266
67+ if (shouldCount ) {
68+ return N1QLQuery .of (n1ql , params , ids );
69+ }
6370
6471 if (!query .sorts ().isEmpty ()) {
6572 n1ql .append (" ORDER BY " );
@@ -80,19 +87,18 @@ public N1QLQuery get() {
8087 return N1QLQuery .of (n1ql , params , ids );
8188 }
8289
83-
8490 private void condition (CriteriaCondition condition , StringBuilder n1ql , JsonObject params , List <String > ids ) {
8591 Element document = condition .element ();
8692 switch (condition .condition ()) {
8793 case EQUALS :
88- if (document .name ().equals (EntityConverter .ID_FIELD )) {
94+ if (document .name ().equals (EntityConverter .ID_FIELD ) && ! shouldCount ) {
8995 ids .add (document .get (String .class ));
9096 } else {
9197 predicate (n1ql , " = " , document , params );
9298 }
9399 return ;
94100 case IN :
95- if (document .name ().equals (EntityConverter .ID_FIELD )) {
101+ if (document .name ().equals (EntityConverter .ID_FIELD ) && ! shouldCount ) {
96102 ids .addAll (document .get (new TypeReference <List <String >>() {
97103 }));
98104 } else {
@@ -165,10 +171,10 @@ private void appendCondition(StringBuilder n1ql, JsonObject params,
165171 for (CriteriaCondition documentCondition : conditions ) {
166172 StringBuilder query = new StringBuilder ();
167173 condition (documentCondition , query , params , ids );
168- if (index == 0 ){
169- n1ql .append (" " ).append (query );
170- } else if (!query .isEmpty ()) {
171- n1ql .append (condition ).append (query );
174+ if (index == 0 ) {
175+ n1ql .append (" " ).append (query );
176+ } else if (!query .isEmpty ()) {
177+ n1ql .append (condition ).append (query );
172178 }
173179 index ++;
174180 }
@@ -191,6 +197,9 @@ private String identifierOf(String name) {
191197 }
192198
193199 private String select () {
200+ if (shouldCount ) {
201+ return "COUNT(*)" ;
202+ }
194203 String documents = String .join (", " , query .columns ());
195204 if (documents .isBlank ()) {
196205 return "*" ;
@@ -199,6 +208,10 @@ private String select() {
199208 }
200209
201210 public static N1QLBuilder of (SelectQuery query , String database , String scope ) {
202- return new N1QLBuilder (query , database , scope );
211+ return new N1QLBuilder (query , database , scope , false );
212+ }
213+
214+ public static N1QLBuilder countOf (SelectQuery query , String database , String scope ) {
215+ return new N1QLBuilder (query , database , scope , true );
203216 }
204217}
0 commit comments