|
57 | 57 | import org.bson.BsonInt64;
|
58 | 58 | import org.bson.BsonNull;
|
59 | 59 | import org.bson.BsonString;
|
60 |
| -import org.bson.BsonType; |
| 60 | +import org.bson.BsonUndefined; |
61 | 61 | import org.bson.BsonValue;
|
62 | 62 | import org.bson.types.Decimal128;
|
63 | 63 |
|
|
69 | 69 | */
|
70 | 70 | final class MongoPreparedStatement extends MongoStatement implements PreparedStatementAdapter {
|
71 | 71 |
|
| 72 | + private static final BsonUndefined PARAMETER_PLACEHOLDER = new BsonUndefined(); |
| 73 | + |
72 | 74 | private final List<BsonDocument> commandBatch;
|
73 |
| - private final BsonDocument commandPrototype; |
74 | 75 |
|
75 |
| - private BsonDocument command; |
| 76 | + private final BsonDocument command; |
76 | 77 |
|
77 | 78 | private final List<Consumer<BsonValue>> parameterValueSetters;
|
78 | 79 |
|
79 | 80 | MongoPreparedStatement(
|
80 | 81 | MongoClient mongoClient, ClientSession clientSession, MongoConnection mongoConnection, String mql) {
|
81 | 82 | super(mongoClient, clientSession, mongoConnection);
|
82 | 83 | commandBatch = new ArrayList<>();
|
83 |
| - commandPrototype = BsonDocument.parse(mql); |
84 |
| - command = commandPrototype.clone(); |
| 84 | + command = BsonDocument.parse(mql); |
85 | 85 | parameterValueSetters = new ArrayList<>();
|
86 | 86 | parseParameters(command, parameterValueSetters);
|
87 | 87 | }
|
@@ -272,10 +272,8 @@ public void setNull(int parameterIndex, int sqlType, String typeName) throws SQL
|
272 | 272 | @Override
|
273 | 273 | public void addBatch() throws SQLException {
|
274 | 274 | 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)); |
279 | 277 | }
|
280 | 278 |
|
281 | 279 | @Override
|
@@ -403,7 +401,7 @@ private static void parseParameters(BsonValue value, List<Consumer<BsonValue>> p
|
403 | 401 | }
|
404 | 402 |
|
405 | 403 | private static boolean isParameterMarker(BsonValue value) {
|
406 |
| - return value.getBsonType() == BsonType.UNDEFINED; |
| 404 | + return value.equals(PARAMETER_PLACEHOLDER); |
407 | 405 | }
|
408 | 406 |
|
409 | 407 | private void checkParameterIndex(int parameterIndex) throws SQLException {
|
|
0 commit comments