-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from eclipse/count-exception-when-delete-update
Execute an exception when update and delete returns long at CustomRepository
- Loading branch information
Showing
10 changed files
with
280 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...mistructured/src/main/java/org/eclipse/jnosql/communication/semistructured/QueryType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
*/ | ||
package org.eclipse.jnosql.communication.semistructured; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Enum representing the different types of queries supported in Jakarta Data. | ||
* | ||
* <p>The {@code QueryType} enum categorizes queries into three main types: {@code SELECT}, | ||
* {@code DELETE}, and {@code UPDATE}. These types correspond to the standard operations | ||
* typically executed against a database. This enum is used to interpret and classify | ||
* queries within the Jakarta Data API, particularly in implementations like Eclipse JNoSQL.</p> | ||
* | ||
* <ul> | ||
* <li>{@link #SELECT} - Represents a query that retrieves data from the database.</li> | ||
* <li>{@link #DELETE} - Represents a query that removes data from the database.</li> | ||
* <li>{@link #UPDATE} - Represents a query that modifies existing data in the database.</li> | ||
* </ul> | ||
* | ||
* <p>The {@link #parse(String)} method is provided to determine the type of a given query | ||
* string by extracting and evaluating its command keyword. The method returns a corresponding | ||
* {@code QueryType} based on the first six characters of the query, assuming that the query | ||
* begins with a standard SQL-like command.</p> | ||
* | ||
* <p>Note that if the query string does not contain a recognizable command (e.g., if it is | ||
* shorter than six characters or does not match any known command), the method defaults to | ||
* {@code SELECT}.</p> | ||
* | ||
* <p>This enum is particularly relevant for NoSQL implementations like Eclipse JNoSQL, where | ||
* the query language might differ from traditional SQL, yet still, adhere to the concepts | ||
* of selection, deletion, and updating of data.</p> | ||
*/ | ||
public enum QueryType { | ||
|
||
/** | ||
* Represents a query that retrieves data from the database. | ||
* This is the default query type when no specific command is recognized. | ||
*/ | ||
SELECT, | ||
|
||
/** | ||
* Represents a query that removes data from the database. | ||
* Typically used to delete one or more records based on certain conditions. | ||
*/ | ||
DELETE, | ||
|
||
/** | ||
* Represents a query that modifies existing data in the database. | ||
* Typically used to update one or more records based on certain conditions. | ||
*/ | ||
UPDATE; | ||
|
||
/** | ||
* Parses the given query string to determine the type of query. | ||
* | ||
* @param query the query string to parse | ||
* @return the {@code QueryType} corresponding to the query command | ||
*/ | ||
public static QueryType parse(String query) { | ||
Objects.requireNonNull(query, "Query string cannot be null"); | ||
String command = QueryType.extractQueryCommand(query); | ||
return switch (command) { | ||
case "DELETE" -> DELETE; | ||
case "UPDATE" -> UPDATE; | ||
default -> SELECT; | ||
}; | ||
} | ||
|
||
/** | ||
* Checks if the current {@code QueryType} is not a {@code SELECT} operation. | ||
* This method is useful for determining whether the query is intended to modify data | ||
* (i.e., either a {@code DELETE} or {@code UPDATE} operation) rather than retrieve it. | ||
* It can be employed in scenarios where different logic is applied based on whether | ||
* a query modifies data. For example, {@code if (queryType.isNotSelect())} can be used | ||
* to trigger actions specific to non-SELECT queries. This method returns {@code true} | ||
* if the current {@code QueryType} is either {@code DELETE} or {@code UPDATE}, | ||
* and {@code false} if it is {@code SELECT}. | ||
*/ | ||
public boolean isNotSelect() { | ||
return this != SELECT; | ||
} | ||
|
||
/** | ||
* Validates the return type of method based on the type of query being executed. | ||
* <p> | ||
* This method checks whether the specified query is a {@code DELETE} or {@code UPDATE} operation | ||
* and ensures that the return type is {@code Void}. If the query is not a {@code SELECT} operation | ||
* and the return type is not {@code Void}, an {@code UnsupportedOperationException} is thrown. | ||
* <p> | ||
* This validation is necessary because {@code DELETE} and {@code UPDATE} operations typically | ||
* do not return a result set, and as such, they should have a {@code Void} return type. | ||
* | ||
* @param returnType the return type of the method executing the query | ||
* @param query the query being executed | ||
* @throws UnsupportedOperationException if the query is a {@code DELETE} or {@code UPDATE} operation | ||
* and the return type is not {@code Void} | ||
*/ | ||
public void checkValidReturn(Class<?> returnType, String query) { | ||
if (isNotSelect() && !isVoid(returnType)) { | ||
throw new UnsupportedOperationException("The return type must be Void when the query is not a SELECT operation, due to the nature" + | ||
" of DELETE and UPDATE operations. The query: " + query); | ||
} | ||
} | ||
|
||
private boolean isVoid(Class<?> returnType) { | ||
return returnType == Void.class || returnType == Void.TYPE; | ||
} | ||
|
||
private static String extractQueryCommand(String query){ | ||
if(query.length() < 6){ | ||
return ""; | ||
} | ||
return query.substring(0, 6).toUpperCase(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...ructured/src/test/java/org/eclipse/jnosql/communication/semistructured/QueryTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Apache License v2.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html | ||
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php. | ||
* You may elect to redistribute this code under either of these licenses. | ||
* | ||
*/ | ||
package org.eclipse.jnosql.communication.semistructured; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class QueryTypeTest { | ||
|
||
|
||
@Test | ||
void shouldParseSelectQuery() { | ||
String query = "SELECT * FROM table"; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.SELECT); | ||
} | ||
|
||
@Test | ||
void shouldParseDeleteQuery() { | ||
String query = "DELETE FROM table WHERE id = 1"; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.DELETE); | ||
} | ||
|
||
@Test | ||
void shouldParseUpdateQuery() { | ||
String query = "UPDATE table SET name = 'newName' WHERE id = 1"; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.UPDATE); | ||
} | ||
|
||
@Test | ||
void shouldDefaultToSelectForUnknownQuery() { | ||
String query = "INSERT INTO table (id, name) VALUES (1, 'name')"; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.SELECT); | ||
} | ||
|
||
@Test | ||
void shouldDefaultToSelectForShortQuery() { | ||
String query = "DELE"; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.SELECT); | ||
} | ||
|
||
@Test | ||
void shouldDefaultToSelectForEmptyQuery() { | ||
String query = ""; | ||
QueryType result = QueryType.parse(query); | ||
assertThat(result).isEqualTo(QueryType.SELECT); | ||
} | ||
|
||
@Test | ||
void shouldThrowNullPointerExceptionForNullQuery() { | ||
String query = null; | ||
assertThatThrownBy(() -> QueryType.parse(query)) | ||
.isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
void shouldReturnIsNotSelect() { | ||
Assertions.assertThat(QueryType.SELECT.isNotSelect()).isFalse(); | ||
Assertions.assertThat(QueryType.DELETE.isNotSelect()).isTrue(); | ||
Assertions.assertThat(QueryType.UPDATE.isNotSelect()).isTrue(); | ||
} | ||
|
||
@Test | ||
void shouldCheckValidReturn() { | ||
QueryType.SELECT.checkValidReturn(String.class, "SELECT * FROM table"); | ||
QueryType.DELETE.checkValidReturn(Void.class, "DELETE FROM table WHERE id = 1"); | ||
QueryType.UPDATE.checkValidReturn(Void.class, "UPDATE table SET name = 'newName' WHERE id = 1"); | ||
assertThatThrownBy(() -> QueryType.DELETE.checkValidReturn(String.class, "DELETE FROM table WHERE id = 1")) | ||
.isInstanceOf(UnsupportedOperationException.class); | ||
assertThatThrownBy(() -> QueryType.UPDATE.checkValidReturn(String.class, "UPDATE table SET name = 'newName' WHERE id = 1")) | ||
.isInstanceOf(UnsupportedOperationException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.