Skip to content
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
18 changes: 6 additions & 12 deletions src/main/java/io/asyncer/r2dbc/mysql/MySqlBatchingBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.asyncer.r2dbc.mysql.codec.Codecs;
import reactor.core.publisher.Flux;

import java.util.StringJoiner;

import static io.asyncer.r2dbc.mysql.internal.util.AssertUtils.requireNonNull;

/**
Expand All @@ -34,7 +36,7 @@ final class MySqlBatchingBatch extends MySqlBatch {

private final ConnectionContext context;

private StringBuilder builder;
private final StringJoiner queries = new StringJoiner(";");

MySqlBatchingBatch(Client client, Codecs codecs, ConnectionContext context) {
this.client = requireNonNull(client, "client must not be null");
Expand All @@ -50,9 +52,9 @@ public MySqlBatch add(String sql) {

if (index >= 0 && sql.charAt(index) == ';') {
// Skip last ';' and whitespaces that following last ';'.
requireBuilder().append(sql, 0, index);
queries.add(sql.substring(0, index));
} else {
requireBuilder().append(sql);
queries.add(sql);
}

return this;
Expand All @@ -75,15 +77,7 @@ public String toString() {
* @return current batching SQL statement
*/
String getSql() {
return builder.toString();
}

private StringBuilder requireBuilder() {
if (builder == null) {
return (builder = new StringBuilder());
}

return builder.append(';');
return queries.toString();
}

private static int lastNonWhitespace(String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ void add() {
void badAdd() {
assertThrows(IllegalArgumentException.class, () -> batch.add(null));
}

@Test
void addNothing() {
final MySqlBatchingBatch batch = new MySqlBatchingBatch(mock(Client.class), mock(Codecs.class),
ConnectionContextTest.mock());
assertEquals(batch.getSql(), "");
}
}