Skip to content

Commit

Permalink
chore: adding support to countAll, NotNull and Null operations …
Browse files Browse the repository at this point in the history
…keywords

Signed-off-by: Maximillian Arruda <dearrudam@gmail.com>
  • Loading branch information
dearrudam committed Jun 5, 2024
1 parent e2e6343 commit 6591533
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String toString() {
}

public static DefaultQueryValue of(String text) {
if(text.startsWith(":")) {
if(text != null && text.startsWith(":")) {
return new DefaultQueryValue(text.substring(1));
}
return new DefaultQueryValue(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jnosql.communication.query.ParamQueryValue;
import org.eclipse.jnosql.communication.query.QueryCondition;
import org.eclipse.jnosql.communication.query.QueryErrorListener;
import org.eclipse.jnosql.communication.query.StringQueryValue;
import org.eclipse.jnosql.communication.query.Where;
import org.eclipse.jnosql.query.grammar.method.MethodBaseListener;
import org.eclipse.jnosql.query.grammar.method.MethodLexer;
Expand All @@ -37,6 +38,7 @@
import java.util.function.Function;
import java.util.stream.Stream;

import static java.util.stream.Collectors.joining;
import static org.eclipse.jnosql.communication.Condition.AND;
import static org.eclipse.jnosql.communication.Condition.BETWEEN;
import static org.eclipse.jnosql.communication.Condition.EQUALS;
Expand All @@ -48,7 +50,6 @@
import static org.eclipse.jnosql.communication.Condition.LIKE;
import static org.eclipse.jnosql.communication.Condition.NOT;
import static org.eclipse.jnosql.communication.Condition.OR;
import static java.util.stream.Collectors.joining;

abstract class AbstractMethodQueryProvider extends MethodBaseListener {

Expand All @@ -59,6 +60,8 @@ abstract class AbstractMethodQueryProvider extends MethodBaseListener {

protected boolean and = true;

protected boolean shouldCount = false;

protected void runQuery(String query) {

CharStream stream = CharStreams.fromString(query);
Expand All @@ -81,6 +84,11 @@ protected void runQuery(String query) {

abstract Function<MethodParser, ParseTree> getParserTree();

@Override
public void exitSelectStart(MethodParser.SelectStartContext ctx) {
this.shouldCount = ctx.getText().startsWith("count");
}

@Override
public void exitEq(MethodParser.EqContext ctx) {
Condition operator = EQUALS;
Expand Down Expand Up @@ -158,6 +166,13 @@ public void exitBetween(MethodParser.BetweenContext ctx) {
checkCondition(new MethodCondition(variable, operator, value), hasNot);
}

@Override
public void exitNullable(MethodParser.NullableContext ctx) {
boolean hasNot = Objects.nonNull(ctx.not());
String variable = getVariable(ctx.variable());
checkCondition(new MethodCondition(variable, EQUALS, StringQueryValue.of(null)), hasNot);
}

@Override
public void exitAnd(MethodParser.AndContext ctx) {
this.and = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
public final class MethodQuery implements Supplier<String> {

private final String value;
private static final Pattern PATTERN = Pattern.compile("findBy|deleteBy|countBy|existsBy|"
private static final Pattern PATTERN = Pattern.compile("findBy|deleteBy|countAll|countBy|existsBy|"
+ "OrderBy|First(?=\\d+By)|(?<=First\\d{1,})By|"
+ "And|Or(?!der)|Not|Equals|GreaterThanEqual|True|False|" +
+ "And|Or(?!der)|Null|Not|Equals|GreaterThanEqual|True|False|" +
"LessThanEqual|GreaterThan|LessThan|Between|In|Like|Asc|Desc");
private static final Map<String, String> CACHE = Collections.synchronizedMap(new WeakHashMap<>());
private MethodQuery(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ final class MethodSelectQuery implements SelectQuery {

private final long limit;

MethodSelectQuery(String entity, List<Sort<?>> sorts, Where where, long limit) {
private final boolean count;

MethodSelectQuery(String entity, List<Sort<?>> sorts, Where where, long limit, boolean count) {
this.entity = entity;
this.sorts = sorts;
this.where = where;
this.limit = limit;
this.count = count;
}


Expand Down Expand Up @@ -70,7 +73,7 @@ public List<Sort<?>> orderBy() {

@Override
public boolean isCount() {
return false;
return count;
}

public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SelectQuery apply(String query, String entity) {
Objects.requireNonNull(query, " query is required");
Objects.requireNonNull(entity, " entity is required");
runQuery(MethodQuery.of(query).get());
return new MethodSelectQuery(entity, sorts, where, limit);
return new MethodSelectQuery(entity, sorts, where, limit, shouldCount);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
*/
package org.eclipse.jnosql.communication.query.method;

import org.eclipse.jnosql.communication.Condition;
import jakarta.data.Sort;
import jakarta.data.Direction;
import jakarta.data.Sort;
import org.eclipse.jnosql.communication.Condition;
import org.eclipse.jnosql.communication.query.BooleanQueryValue;
import org.eclipse.jnosql.communication.query.ConditionQueryValue;
import org.eclipse.jnosql.communication.query.ParamQueryValue;
import org.eclipse.jnosql.communication.query.QueryCondition;
import org.eclipse.jnosql.communication.query.QueryValue;
import org.eclipse.jnosql.communication.query.SelectQuery;
import org.eclipse.jnosql.communication.query.StringQueryValue;
import org.eclipse.jnosql.communication.query.Where;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -32,6 +33,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SelectMethodQueryProviderTest {
Expand All @@ -40,20 +42,38 @@ class SelectMethodQueryProviderTest {


@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"findBy", "countBy", "existsBy"})
@ValueSource(strings = {"findBy", "existsBy"})
void shouldReturnParserQuery(String query) {
String entity = "entity";
SelectQuery selectQuery = queryProvider.apply(query, entity);
assertNotNull(selectQuery);
assertEquals(entity, selectQuery.entity());
assertTrue(selectQuery.fields().isEmpty());
assertTrue(selectQuery.orderBy().isEmpty());
assertFalse(selectQuery.isCount());
assertEquals(0, selectQuery.limit());
assertEquals(0, selectQuery.skip());
Optional<Where> where = selectQuery.where();
assertFalse(where.isPresent());
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"countBy", "countAll"})
void shouldReturnParsedCountableQuery(String query) {
String entity = "entity";
SelectQuery selectQuery = queryProvider.apply(query, entity);
assertNotNull(selectQuery);
assertEquals(entity, selectQuery.entity());
assertTrue(selectQuery.fields().isEmpty());
assertTrue(selectQuery.orderBy().isEmpty());
assertTrue(selectQuery.isCount());
assertEquals(0, selectQuery.limit());
assertEquals(0, selectQuery.skip());
Optional<Where> where = selectQuery.where();
assertFalse(where.isPresent());
}


@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"findFirst10By"})
void shouldFindFirstLimit(String query) {
Expand Down Expand Up @@ -470,6 +490,53 @@ void shouldReturnParserQuery35(String query) {
checkNotCondition(query, Condition.EQUALS, "name");
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"findByNameNotNull", "countByNameNotNull", "existsByNameNotNull"})
void shouldReturnParserQuery36(String query) {
String entity = "entity";
SelectQuery selectQuery = queryProvider.apply(query, entity);
assertNotNull(selectQuery);
assertEquals(entity, selectQuery.entity());
assertTrue(selectQuery.fields().isEmpty());
assertTrue(selectQuery.orderBy().isEmpty());
assertEquals(0, selectQuery.limit());
assertEquals(0, selectQuery.skip());
Optional<Where> where = selectQuery.where();
assertTrue(where.isPresent());
QueryCondition condition = where.get().condition();
QueryValue<?> value = condition.value();
assertEquals(Condition.NOT, condition.condition());


assertEquals("_NOT", condition.name());
assertTrue(value instanceof ConditionQueryValue);
QueryCondition condition1 = ConditionQueryValue.class.cast(value).get().get(0);

assertEquals("name", condition1.name());
assertEquals(Condition.EQUALS, condition1.condition());
var param = condition1.value();
assertNull(StringQueryValue.class.cast(param).get());

}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"findByNameNull", "countByNameNull", "existsByNameNull"})
void shouldReturnParserQuery37(String query) {
String entity = "entity";
SelectQuery selectQuery = queryProvider.apply(query, entity);
assertNotNull(selectQuery);
assertEquals(entity, selectQuery.entity());
assertTrue(selectQuery.fields().isEmpty());
assertTrue(selectQuery.orderBy().isEmpty());
assertEquals(0, selectQuery.limit());
assertEquals(0, selectQuery.skip());
Optional<Where> where = selectQuery.where();
assertTrue(where.isPresent());
QueryCondition condition = where.get().condition();
assertEquals("name", condition.name());
assertEquals(Condition.EQUALS, condition.condition());
assertNull(condition.value().get());
}

private void checkOrderBy(String query, Direction direction, Direction direction2) {
String entity = "entity";
Expand Down

0 comments on commit 6591533

Please sign in to comment.