Skip to content

Commit 18a18f7

Browse files
vbabaninstIncMale
andauthored
Apply suggestions from code review
Co-authored-by: Valentin Kovalenko <[email protected]>
1 parent afd8853 commit 18a18f7

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/main/java/com/mongodb/hibernate/jdbc/MongoPreparedStatement.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public ResultSet executeQuery() throws SQLException {
7171
checkClosed();
7272
closeLastOpenResultSet();
7373
checkAllParametersSet();
74+
checkSupportedQueryCommand(command);
7475
return executeQuery(command);
7576
}
7677

@@ -211,7 +212,7 @@ public int[] executeBatch() throws SQLException {
211212
}
212213

213214
/** @throws BatchUpdateException if any of the commands in the batch attempts to return a result set. */
214-
private void checkSupportedBatchCommand(BsonDocument command)
215+
private static void checkSupportedBatchCommand(BsonDocument command)
215216
throws SQLFeatureNotSupportedException, BatchUpdateException, SQLSyntaxErrorException {
216217
var commandDescription = getCommandDescription(command);
217218
if (commandDescription.returnsResultSet()) {

src/main/java/com/mongodb/hibernate/jdbc/MongoStatement.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.mongodb.hibernate.jdbc;
1818

19-
import static com.mongodb.assertions.Assertions.assertFalse;
20-
import static com.mongodb.assertions.Assertions.assertNotNull;
21-
import static com.mongodb.assertions.Assertions.assertTrue;
22-
import static com.mongodb.assertions.Assertions.fail;
19+
import static com.mongodb.hibernate.internal.MongoAssertions.assertFalse;
20+
import static com.mongodb.hibernate.internal.MongoAssertions.assertNotNull;
21+
import static com.mongodb.hibernate.internal.MongoAssertions.assertTrue;
22+
import static com.mongodb.hibernate.internal.MongoAssertions.fail;
2323
import static com.mongodb.hibernate.internal.MongoConstants.EXTENDED_JSON_WRITER_SETTINGS;
2424
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME;
2525
import static com.mongodb.hibernate.internal.VisibleForTesting.AccessModifier.PRIVATE;
@@ -75,7 +75,7 @@ class MongoStatement implements StatementAdapter {
7575
static final int NO_ERROR_CODE = 0;
7676
static final int[] EMPTY_BATCH_RESULT = new int[0];
7777

78-
@Nullable static final String NULL_SQL_STATE = null;
78+
static final String @Nullable NULL_SQL_STATE = null;
7979

8080
private final MongoDatabase mongoDatabase;
8181
private final MongoConnection mongoConnection;
@@ -164,15 +164,15 @@ public int executeUpdate(String mql) throws SQLException {
164164

165165
int[] executeBatch(List<? extends BsonDocument> commandBatch) throws SQLException {
166166
var firstCommandInBatch = commandBatch.get(0);
167-
int commandBatchSize = commandBatch.size();
167+
var commandBatchSize = commandBatch.size();
168168
var commandDescription = getCommandDescription(firstCommandInBatch);
169169
var collection = getCollection(commandDescription, firstCommandInBatch);
170170
WriteModelsToCommandMapper writeModelsToCommandMapper = null;
171171
try {
172172
startTransactionIfNeeded();
173173
var writeModels = new ArrayList<WriteModel<BsonDocument>>(commandBatchSize);
174174
writeModelsToCommandMapper = new WriteModelsToCommandMapper(commandBatchSize);
175-
for (BsonDocument command : commandBatch) {
175+
for (var command : commandBatch) {
176176
WriteModelConverter.convertToWriteModels(commandDescription, command, writeModels);
177177
writeModelsToCommandMapper.add(writeModels.size());
178178
}
@@ -444,16 +444,20 @@ private static int[] calculateBatchUpdateCounts(
444444

445445
private static boolean isTimeoutException(MongoException exception) {
446446
// We do not check for `MongoExecutionTimeoutException` and `MongoOperationTimeoutException` here,
447-
// because it is handled via error codes.
447+
// because they are handled via error codes.
448448
return exception instanceof MongoSocketReadTimeoutException
449449
|| exception instanceof MongoSocketWriteTimeoutException
450450
|| exception instanceof MongoTimeoutException;
451451
}
452452

453453
enum CommandDescription {
454+
/** See <a href="https://www.mongodb.com/docs/manual/reference/command/insert/">{@code insert}</a>. */
454455
INSERT("insert", false, true),
456+
/** See <a href="https://www.mongodb.com/docs/manual/reference/command/update/">{@code update}</a>. */
455457
UPDATE("update", false, true),
458+
/** See <a href="https://www.mongodb.com/docs/manual/reference/command/delete/">{@code delete}</a>. */
456459
DELETE("delete", false, true),
460+
/** See <a href="https://www.mongodb.com/docs/manual/reference/command/aggregate/">{@code aggregate}</a>. */
457461
AGGREGATE("aggregate", true, false);
458462

459463
private final String commandName;
@@ -471,9 +475,9 @@ String getCommandName() {
471475
}
472476

473477
/**
474-
* Indicates whether the command is used in {@code executeUpdate(...)} or {@code executeBatch()} methods.
478+
* Indicates whether the command may be used in {@code executeUpdate(...)} or {@code executeBatch()} methods.
475479
*
476-
* @return true if the command is used in update operations, false if it is used in query operations.
480+
* @return true if the command may be used in update operations, false if it is used in query operations.
477481
*/
478482
boolean isUpdate() {
479483
return isUpdate;
@@ -500,8 +504,10 @@ static CommandDescription fromString(String commandName) throws SQLFeatureNotSup
500504
}
501505

502506
private static class WriteModelConverter {
503-
private static final String UNSUPPORTED_MESSAGE_STATEMENT_FIELD = "Unsupported field in %s statement: [%s]";
504-
private static final String UNSUPPORTED_MESSAGE_COMMAND_FIELD = "Unsupported field in %s command: [%s]";
507+
private static final String UNSUPPORTED_MESSAGE_TEMPLATE_STATEMENT_FIELD =
508+
"Unsupported field in [%s] statement: [%s]";
509+
private static final String UNSUPPORTED_MESSAGE_TEMPLATE_COMMAND_FIELD =
510+
"Unsupported field in [%s] command: [%s]";
505511

506512
private static final Set<String> SUPPORTED_INSERT_COMMAND_FIELDS = Set.of("documents");
507513

0 commit comments

Comments
 (0)