Skip to content

Commit 6510723

Browse files
simplify the implementation of MongoPreparedStatement#addBatch
1 parent 88f8759 commit 6510723

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import org.bson.BsonInt64;
5858
import org.bson.BsonNull;
5959
import org.bson.BsonString;
60-
import org.bson.BsonType;
60+
import org.bson.BsonUndefined;
6161
import org.bson.BsonValue;
6262
import org.bson.types.Decimal128;
6363

@@ -69,19 +69,19 @@
6969
*/
7070
final class MongoPreparedStatement extends MongoStatement implements PreparedStatementAdapter {
7171

72+
private static final BsonUndefined PARAMETER_PLACEHOLDER = new BsonUndefined();
73+
7274
private final List<BsonDocument> commandBatch;
73-
private final BsonDocument commandPrototype;
7475

75-
private BsonDocument command;
76+
private final BsonDocument command;
7677

7778
private final List<Consumer<BsonValue>> parameterValueSetters;
7879

7980
MongoPreparedStatement(
8081
MongoClient mongoClient, ClientSession clientSession, MongoConnection mongoConnection, String mql) {
8182
super(mongoClient, clientSession, mongoConnection);
8283
commandBatch = new ArrayList<>();
83-
commandPrototype = BsonDocument.parse(mql);
84-
command = commandPrototype.clone();
84+
command = BsonDocument.parse(mql);
8585
parameterValueSetters = new ArrayList<>();
8686
parseParameters(command, parameterValueSetters);
8787
}
@@ -272,10 +272,8 @@ public void setNull(int parameterIndex, int sqlType, String typeName) throws SQL
272272
@Override
273273
public void addBatch() throws SQLException {
274274
checkClosed();
275-
commandBatch.add(command);
276-
command = commandPrototype.clone();
277-
parameterValueSetters.clear();
278-
parseParameters(command, parameterValueSetters);
275+
commandBatch.add(command.clone());
276+
parameterValueSetters.forEach(setter -> setter.accept(PARAMETER_PLACEHOLDER));
279277
}
280278

281279
@Override
@@ -403,7 +401,7 @@ private static void parseParameters(BsonValue value, List<Consumer<BsonValue>> p
403401
}
404402

405403
private static boolean isParameterMarker(BsonValue value) {
406-
return value.getBsonType() == BsonType.UNDEFINED;
404+
return value.equals(PARAMETER_PLACEHOLDER);
407405
}
408406

409407
private void checkParameterIndex(int parameterIndex) throws SQLException {

0 commit comments

Comments
 (0)