Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query parser to support statement commands with case-insensitive; #512

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class QueryParser {
*/
public Stream<CommunicationEntity> query(String query, String entity, DatabaseManager manager, CommunicationObserverParser observer) {
validation(query, manager, observer);
String command = query.substring(0, 6);
String command = extractQueryCommand(query);
return switch (command) {
case "DELETE" -> delete.query(query, manager, observer);
case "UPDATE" -> update.query(query, manager, observer);
Expand All @@ -60,15 +60,17 @@ public Stream<CommunicationEntity> query(String query, String entity, DatabaseMa
*/
public CommunicationPreparedStatement prepare(String query, String entity, DatabaseManager manager, CommunicationObserverParser observer) {
validation(query, manager, observer);
String command = query.substring(0, 6);

String command = extractQueryCommand(query);
return switch (command) {
case "DELETE" -> delete.prepare(query, manager, observer);
case "UPDATE" -> update.prepare(query, manager, observer);
default -> select.prepare(query, entity, manager, observer);
};
}

private String extractQueryCommand(String query){
return query.substring(0, 6).toUpperCase();
}

private void validation(String query, DatabaseManager manager, CommunicationObserverParser observer) {
Objects.requireNonNull(query, "query is required");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import org.eclipse.jnosql.communication.Condition;
import jakarta.data.exceptions.NonUniqueResultException;
import org.eclipse.jnosql.communication.QueryException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -49,7 +48,7 @@ void shouldReturnErrorWhenHasInvalidQuery() {

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"FROM God"})
void shouldReturnParserQuery(String query) {
void shouldReturnParsedSelectQuery(String query) {
ArgumentCaptor<SelectQuery> captor = ArgumentCaptor.forClass(SelectQuery.class);
parser.query(query, null, manager, CommunicationObserverParser.EMPTY);
Mockito.verify(manager).select(captor.capture());
Expand All @@ -64,10 +63,10 @@ void shouldReturnParserQuery(String query) {

}


@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"DELETE FROM God"})
void shouldReturnParserQuery1(String query) {
@ParameterizedTest(name = "Should parser the query {0} FROM God")
@ValueSource(strings = {"DELETE", "delete", "DeLeTe", "dElEtE", "DElete", "deLETE", "DeleTE", "DELete"})
void shouldReturnParsedDeleteQuery(String queryCommand) {
var query = queryCommand + " FROM God";
ArgumentCaptor<DeleteQuery> captor = ArgumentCaptor.forClass(DeleteQuery.class);
parser.query(query, null, manager, CommunicationObserverParser.EMPTY);
Mockito.verify(manager).delete(captor.capture());
Expand All @@ -93,9 +92,10 @@ void shouldReturnParserQuery3(String query) {
assertEquals(Element.of("name", "Diana"), entity.find("name").get());
}

@ParameterizedTest(name = "Should parser the query {0}")
@ValueSource(strings = {"DELETE FROM God WHERE age = :age"})
void shouldExecutePrepareStatement(String query) {
@ParameterizedTest(name = "Should parser the query {0} FROM God WHERE age = :age")
@ValueSource(strings = {"DELETE", "delete", "DeLeTe", "dElEtE", "DElete", "deLETE", "DeleTE", "DELete"})
void shouldExecutePrepareStatement(String queryCommand) {
var query = queryCommand + " FROM God WHERE age = :age";
ArgumentCaptor<DeleteQuery> captor = ArgumentCaptor.forClass(DeleteQuery.class);

CommunicationPreparedStatement prepare = parser.prepare(query, null, manager, CommunicationObserverParser.EMPTY);
Expand Down