diff --git a/src/main/java/io/r2dbc/postgresql/BoundedStatementCache.java b/src/main/java/io/r2dbc/postgresql/BoundedStatementCache.java index c66fc18d..ba0b8fc3 100644 --- a/src/main/java/io/r2dbc/postgresql/BoundedStatementCache.java +++ b/src/main/java/io/r2dbc/postgresql/BoundedStatementCache.java @@ -22,10 +22,9 @@ import io.r2dbc.postgresql.util.Assert; import reactor.core.publisher.Mono; import reactor.util.annotation.Nullable; -import reactor.util.function.Tuple2; -import reactor.util.function.Tuples; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; @@ -38,7 +37,7 @@ */ final class BoundedStatementCache implements StatementCache { - private final Map>, String> cache = new LinkedHashMap<>(16, 0.75f, true); + private final Map cache = new LinkedHashMap<>(16, 0.75f, true); private final Client client; @@ -58,7 +57,7 @@ public BoundedStatementCache(Client client, int limit) { public Mono getName(Binding binding, String sql) { Assert.requireNonNull(binding, "binding must not be null"); Assert.requireNonNull(sql, "sql must not be null"); - Tuple2> key = Tuples.of(sql, binding.getParameterTypes()); + CacheKey key = new CacheKey(sql, binding.getParameterTypes()); String name = get(key); if (name != null) { return Mono.just(name); @@ -76,7 +75,7 @@ public Mono getName(Binding binding, String sql) { .then(); }); - return closeLastStatement.then(this.parse(sql, binding.getParameterTypes())) + return closeLastStatement.then(parse(sql, binding.getParameterTypes())) .doOnNext(preparedName -> put(key, preparedName)); } @@ -101,7 +100,7 @@ Collection getCachedStatementNames() { * @return statement name by key */ @Nullable - private String get(Tuple2> key) { + private String get(CacheKey key) { synchronized (this.cache) { return this.cache.get(key); } @@ -114,7 +113,7 @@ private String get(Tuple2> key) { */ private String getAndRemoveEldest() { synchronized (this.cache) { - Iterator>, String>> iterator = this.cache.entrySet().iterator(); + Iterator> iterator = this.cache.entrySet().iterator(); String entry = iterator.next().getValue(); iterator.remove(); return entry; @@ -124,7 +123,7 @@ private String getAndRemoveEldest() { /** * Synchronized cache access: Store prepared statement. */ - private void put(Tuple2> key, String preparedName) { + private void put(CacheKey key, String preparedName) { synchronized (this.cache) { this.cache.put(key, preparedName); } @@ -151,8 +150,8 @@ public String toString() { '}'; } - private Mono parse(String sql, List types) { - String name = String.format("S_%d", this.counter.getAndIncrement()); + private Mono parse(String sql, int[] types) { + String name = "S_" + this.counter.getAndIncrement(); ExceptionFactory factory = ExceptionFactory.withSql(name); return ExtendedQueryMessageFlow @@ -161,4 +160,40 @@ private Mono parse(String sql, List types) { .then(Mono.just(name)) .cache(); } + + static class CacheKey { + + String sql; + + int[] parameterTypes; + + public CacheKey(String sql, int[] parameterTypes) { + this.sql = sql; + this.parameterTypes = parameterTypes; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof CacheKey)) { + return false; + } + + CacheKey cacheKey = (CacheKey) o; + + if (this.sql != null ? !this.sql.equals(cacheKey.sql) : cacheKey.sql != null) { + return false; + } + return Arrays.equals(this.parameterTypes, cacheKey.parameterTypes); + } + + @Override + public int hashCode() { + int result = this.sql != null ? this.sql.hashCode() : 0; + result = 31 * result + Arrays.hashCode(this.parameterTypes); + return result; + } + } } diff --git a/src/main/java/io/r2dbc/postgresql/DefaultPortalNameSupplier.java b/src/main/java/io/r2dbc/postgresql/DefaultPortalNameSupplier.java index 598d1fe3..9bd8fe27 100644 --- a/src/main/java/io/r2dbc/postgresql/DefaultPortalNameSupplier.java +++ b/src/main/java/io/r2dbc/postgresql/DefaultPortalNameSupplier.java @@ -31,7 +31,7 @@ private DefaultPortalNameSupplier() { @Override public String get() { - return String.format("B_%d", COUNTER.getAndIncrement()); + return "B_%d" + COUNTER.getAndIncrement(); } } diff --git a/src/main/java/io/r2dbc/postgresql/IndefiniteStatementCache.java b/src/main/java/io/r2dbc/postgresql/IndefiniteStatementCache.java index 413f46e0..a23ce4cd 100644 --- a/src/main/java/io/r2dbc/postgresql/IndefiniteStatementCache.java +++ b/src/main/java/io/r2dbc/postgresql/IndefiniteStatementCache.java @@ -21,17 +21,16 @@ import io.r2dbc.postgresql.client.ExtendedQueryMessageFlow; import io.r2dbc.postgresql.util.Assert; import reactor.core.publisher.Mono; -import reactor.util.function.Tuple2; -import reactor.util.function.Tuples; -import java.util.HashMap; -import java.util.List; +import java.util.Arrays; import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; final class IndefiniteStatementCache implements StatementCache { - private final Map>, Mono> cache = new HashMap<>(); + private final Map>> cache = new ConcurrentHashMap<>(); private final Client client; @@ -45,11 +44,35 @@ final class IndefiniteStatementCache implements StatementCache { public Mono getName(Binding binding, String sql) { Assert.requireNonNull(binding, "binding must not be null"); Assert.requireNonNull(sql, "sql must not be null"); + Map> typedMap = this.cache.computeIfAbsent(sql, ignore -> new TreeMap<>((o1, o2) -> { - synchronized (this.cache) { - return this.cache.computeIfAbsent(Tuples.of(sql, binding.getParameterTypes()), - tuple -> this.parse(tuple.getT1(), tuple.getT2())); + if (Arrays.equals(o1, o2)) { + return 0; + } + + if (o1.length != o2.length) { + return o1.length - o2.length; + } + + for (int i = 0; i < o1.length; i++) { + + int cmp = Integer.compare(o1[i], o2[i]); + + if (cmp != 0) { + return cmp; + } + } + + return 0; + })); + + Mono mono = typedMap.get(binding.getParameterTypes()); + if (mono == null) { + mono = parse(sql, binding.getParameterTypes()); + typedMap.put(binding.getParameterTypes(), mono); } + + return mono; } @Override @@ -61,8 +84,8 @@ public String toString() { '}'; } - private Mono parse(String sql, List types) { - String name = String.format("S_%d", this.counter.getAndIncrement()); + private Mono parse(String sql, int[] types) { + String name = "S_" + this.counter.getAndIncrement(); ExceptionFactory factory = ExceptionFactory.withSql(name); return ExtendedQueryMessageFlow @@ -71,5 +94,4 @@ private Mono parse(String sql, List types) { .then(Mono.just(name)) .cache(); } - } diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlColumnMetadata.java b/src/main/java/io/r2dbc/postgresql/PostgresqlColumnMetadata.java index d8989fcd..79e47651 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlColumnMetadata.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlColumnMetadata.java @@ -73,7 +73,7 @@ public boolean equals(Object o) { @Override public Class getJavaType() { - return codecs.preferredType(this.nativeType, this.format); + return this.codecs.preferredType(this.nativeType, this.format); } @Override @@ -106,9 +106,6 @@ public String toString() { } static PostgresqlColumnMetadata toColumnMetadata(Codecs codecs, Field field) { - Assert.requireNonNull(codecs, "codecs must not be null"); - Assert.requireNonNull(field, "field must not be null"); - return new PostgresqlColumnMetadata(codecs, field); } diff --git a/src/main/java/io/r2dbc/postgresql/PostgresqlResult.java b/src/main/java/io/r2dbc/postgresql/PostgresqlResult.java index fed91ab8..81ff5421 100644 --- a/src/main/java/io/r2dbc/postgresql/PostgresqlResult.java +++ b/src/main/java/io/r2dbc/postgresql/PostgresqlResult.java @@ -23,6 +23,7 @@ import io.r2dbc.postgresql.message.backend.CommandComplete; import io.r2dbc.postgresql.message.backend.DataRow; import io.r2dbc.postgresql.message.backend.EmptyQueryResponse; +import io.r2dbc.postgresql.message.backend.ErrorResponse; import io.r2dbc.postgresql.message.backend.PortalSuspended; import io.r2dbc.postgresql.message.backend.RowDescription; import io.r2dbc.postgresql.util.Assert; @@ -31,6 +32,7 @@ import io.r2dbc.spi.RowMetadata; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.core.publisher.SynchronousSink; import java.util.function.BiFunction; import java.util.function.Predicate; @@ -54,39 +56,52 @@ final class PostgresqlResult extends AbstractReferenceCounted implements io.r2db private volatile RowDescription rowDescription; - PostgresqlResult(ConnectionContext context, Flux messages, ExceptionFactory factory) { - this.context = Assert.requireNonNull(context, "context must not be null"); - this.messages = Assert.requireNonNull(messages, "messages must not be null"); - this.factory = Assert.requireNonNull(factory, "factory must not be null"); + private PostgresqlResult(ConnectionContext context, Flux messages, ExceptionFactory factory) { + this.context = context; + this.messages = messages; + this.factory = factory; } @Override + @SuppressWarnings({"rawtypes", "unchecked"}) public Mono getRowsUpdated() { return this.messages - .handle(this.factory::handleErrorResponse) - .doOnNext(ReferenceCountUtil::release) - .ofType(CommandComplete.class) - .singleOrEmpty() - .handle((commandComplete, sink) -> { - Integer rowCount = commandComplete.getRows(); - if (rowCount != null) { - sink.next(rowCount); - } else { - sink.complete(); + .handle((message, sink) -> { + + if (message instanceof ErrorResponse) { + this.factory.handleErrorResponse(message, (SynchronousSink) sink); + return; } - }); + + if (message instanceof DataRow) { + ((DataRow) message).release(); + } + + if (message instanceof CommandComplete) { + + Integer rowCount = ((CommandComplete) message).getRows(); + if (rowCount != null) { + sink.next(rowCount); + } + } + }).singleOrEmpty(); } @Override + @SuppressWarnings({"rawtypes", "unchecked"}) public Flux map(BiFunction f) { Assert.requireNonNull(f, "f must not be null"); return this.messages.takeUntil(TAKE_UNTIL) - .handle(this.factory::handleErrorResponse) .handle((message, sink) -> { try { + if (message instanceof ErrorResponse) { + this.factory.handleErrorResponse(message, (SynchronousSink) sink); + return; + } + if (message instanceof RowDescription) { this.rowDescription = (RowDescription) message; this.metadata = PostgresqlRowMetadata.toRowMetadata(this.context.getCodecs(), (RowDescription) message); @@ -95,8 +110,6 @@ public Flux map(BiFunction f) { if (message instanceof DataRow) { PostgresqlRow row = PostgresqlRow.toRow(this.context, (DataRow) message, this.rowDescription); - - sink.next(f.apply(row, this.metadata)); } diff --git a/src/main/java/io/r2dbc/postgresql/client/Binding.java b/src/main/java/io/r2dbc/postgresql/client/Binding.java index eeb4ccce..41ce2527 100644 --- a/src/main/java/io/r2dbc/postgresql/client/Binding.java +++ b/src/main/java/io/r2dbc/postgresql/client/Binding.java @@ -20,6 +20,7 @@ import io.r2dbc.postgresql.message.Format; import io.r2dbc.postgresql.util.Assert; import org.reactivestreams.Publisher; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.ArrayList; @@ -39,6 +40,8 @@ public final class Binding { private final List parameters; + private final int[] types; + /** * Create a new instance. * @@ -47,6 +50,7 @@ public final class Binding { public Binding(int expectedSize) { this.expectedSize = expectedSize; this.parameters = new ArrayList<>(Collections.nCopies(expectedSize, UNSPECIFIED)); + this.types = new int[expectedSize]; } /** @@ -57,8 +61,7 @@ public Binding(int expectedSize) { * @return this {@link Binding} * @throws IllegalArgumentException if {@code index} or {@code parameter} is {@code null} */ - public Binding add(Integer index, Parameter parameter) { - Assert.requireNonNull(index, "index must not be null"); + public Binding add(int index, Parameter parameter) { Assert.requireNonNull(parameter, "parameter must not be null"); if (index >= this.expectedSize) { @@ -66,6 +69,7 @@ public Binding add(Integer index, Parameter parameter) { } this.parameters.set(index, parameter); + this.types[index] = parameter.getType(); return this; } @@ -97,8 +101,15 @@ public List getParameterFormats() { * * @return the types of the parameters in the binding */ - public List getParameterTypes() { - return getTransformedParameters(Parameter::getType); + public int[] getParameterTypes() { + + for (int i = 0; i < this.parameters.size(); i++) { + Parameter parameter = this.parameters.get(i); + if (parameter == UNSPECIFIED) { + throw new IllegalStateException(String.format("No parameter specified for index %d", i)); + } + } + return this.types; } /** @@ -110,6 +121,10 @@ public List> getParameterValues() { return getTransformedParameters(Parameter::getValue); } + Flux> parameterValues() { + return Flux.fromIterable(this.parameters).map(Parameter::getValue); + } + @Override public int hashCode() { return Objects.hash(this.parameters); @@ -119,6 +134,10 @@ public boolean isEmpty() { return this.parameters.isEmpty(); } + public int size() { + return this.parameters.size(); + } + @Override public String toString() { return "Binding{" + @@ -140,7 +159,12 @@ public void validate() { } private List getTransformedParameters(Function transformer) { - List transformed = new ArrayList<>(this.parameters.size()); + + if (this.parameters.isEmpty()) { + return Collections.emptyList(); + } + + List transformed = null; for (int i = 0; i < this.parameters.size(); i++) { Parameter parameter = this.parameters.get(i); @@ -148,6 +172,15 @@ private List getTransformedParameters(Function transformer) throw new IllegalStateException(String.format("No parameter specified for index %d", i)); } + if (transformed == null) { + if (this.parameters.size() == 1) { + return Collections.singletonList(transformer.apply(parameter)); + } + + transformed = new ArrayList<>(this.parameters.size()); + } + + transformed.add(transformer.apply(parameter)); } diff --git a/src/main/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlow.java b/src/main/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlow.java index d78b2447..51a79807 100644 --- a/src/main/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlow.java +++ b/src/main/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlow.java @@ -39,7 +39,6 @@ import java.util.Collection; import java.util.Collections; -import java.util.List; import java.util.regex.Pattern; import static io.r2dbc.postgresql.message.frontend.Execute.NO_LIMIT; @@ -91,7 +90,7 @@ public static Flux execute(Publisher bindings, Client c * @return the messages received in response to this exchange * @throws IllegalArgumentException if {@code client}, {@code name}, {@code query}, or {@code types} is {@code null} */ - public static Flux parse(Client client, String name, String query, List types) { + public static Flux parse(Client client, String name, String query, int[] types) { Assert.requireNonNull(client, "client must not be null"); Assert.requireNonNull(name, "name must not be null"); Assert.requireNonNull(query, "query must not be null"); @@ -153,7 +152,7 @@ private static Flux toBindFlow(Binding binding, PortalNameSuppl .flatMapMany(values -> { Bind bind = new Bind(portal, binding.getParameterFormats(), values, resultFormat(forceBinary), statementName); - return Flux.just(bind, new Describe(portal, PORTAL), new Execute(portal, NO_LIMIT), new Close(portal, PORTAL)); + return Flux.just(bind, new Describe(portal, PORTAL), new Execute(portal, NO_LIMIT), new Close(portal, PORTAL)); }).doOnSubscribe(ignore -> QueryLogger.logQuery(query)); } diff --git a/src/main/java/io/r2dbc/postgresql/message/frontend/Parse.java b/src/main/java/io/r2dbc/postgresql/message/frontend/Parse.java index 00c5310a..023dbf8e 100644 --- a/src/main/java/io/r2dbc/postgresql/message/frontend/Parse.java +++ b/src/main/java/io/r2dbc/postgresql/message/frontend/Parse.java @@ -22,7 +22,7 @@ import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; -import java.util.List; +import java.util.Arrays; import java.util.Objects; import static io.r2dbc.postgresql.message.frontend.FrontendMessageUtils.writeByte; @@ -49,7 +49,7 @@ public final class Parse implements FrontendMessage { private final String name; - private final List parameters; + private final int[] parameters; private final String query; @@ -63,7 +63,7 @@ public final class Parse implements FrontendMessage { * @see #UNNAMED_STATEMENT * @see #UNSPECIFIED */ - public Parse(String name, List parameters, String query) { + public Parse(String name, int[] parameters, String query) { this.name = Assert.requireNonNull(name, "name must not be null"); this.parameters = Assert.requireNonNull(parameters, "parameters must not be null"); this.query = Assert.requireNonNull(query, "query must not be null"); @@ -81,8 +81,10 @@ public Publisher encode(ByteBufAllocator byteBufAllocator) { writeCStringUTF8(out, this.name); writeCStringUTF8(out, this.query); - writeShort(out, this.parameters.size()); - this.parameters.forEach(parameter -> writeInt(out, parameter)); + writeShort(out, this.parameters.length); + for (int parameter : this.parameters) { + writeInt(out, parameter); + } return writeSize(out); }); @@ -98,7 +100,7 @@ public boolean equals(Object o) { } Parse that = (Parse) o; return Objects.equals(this.name, that.name) && - Objects.equals(this.parameters, that.parameters) && + Arrays.equals(this.parameters, that.parameters) && Objects.equals(this.query, that.query); } @@ -111,7 +113,7 @@ public int hashCode() { public String toString() { return "Parse{" + "name='" + this.name + '\'' + - ", parameters=" + this.parameters + + ", parameters=" + Arrays.toString(this.parameters) + ", query='" + this.query + '\'' + '}'; } diff --git a/src/test/java/io/r2dbc/postgresql/BoundedStatementCacheTest.java b/src/test/java/io/r2dbc/postgresql/BoundedStatementCacheTest.java index 2f2f7f8a..ec183202 100644 --- a/src/test/java/io/r2dbc/postgresql/BoundedStatementCacheTest.java +++ b/src/test/java/io/r2dbc/postgresql/BoundedStatementCacheTest.java @@ -61,17 +61,17 @@ void constructorNoClient() { void getName() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("S_0", Collections.singletonList(100), "test-query-0"), Flush.INSTANCE) + .expectRequest(new Parse("S_0", new int[]{100}, "test-query-0"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("S_1", Collections.singletonList(200), "test-query-1"), Flush.INSTANCE) + .expectRequest(new Parse("S_1", new int[]{200}, "test-query-1"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .expectRequest(new Close("S_0", ExecutionType.STATEMENT), Sync.INSTANCE) .thenRespond(CloseComplete.INSTANCE) - .expectRequest(new Parse("S_2", Collections.singletonList(200), "test-query-2"), Flush.INSTANCE) + .expectRequest(new Parse("S_2", new int[]{200}, "test-query-2"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .expectRequest(new Close("S_2", ExecutionType.STATEMENT), Sync.INSTANCE) .thenRespond(CloseComplete.INSTANCE) - .expectRequest(new Parse("S_3", Collections.singletonList(100), "test-query-0"), Flush.INSTANCE) + .expectRequest(new Parse("S_3", new int[]{100}, "test-query-0"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .build(); // @formatter:on @@ -120,7 +120,7 @@ void getName() { void getNameErrorResponse() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("S_0", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("S_0", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(new ErrorResponse(Collections.emptyList())) .build(); // @formatter:on diff --git a/src/test/java/io/r2dbc/postgresql/DisabledStatementCacheTest.java b/src/test/java/io/r2dbc/postgresql/DisabledStatementCacheTest.java index 428704ca..2de3f583 100644 --- a/src/test/java/io/r2dbc/postgresql/DisabledStatementCacheTest.java +++ b/src/test/java/io/r2dbc/postgresql/DisabledStatementCacheTest.java @@ -48,13 +48,13 @@ void constructorNoClient() { void getName() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("", Collections.singletonList(200), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("", new int[]{200}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("", Collections.singletonList(200), "test-query-2"), Flush.INSTANCE) + .expectRequest(new Parse("", new int[]{200}, "test-query-2"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .build(); // @formatter:on @@ -86,7 +86,7 @@ void getName() { void getNameErrorResponse() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(new ErrorResponse(Collections.emptyList())) .build(); // @formatter:on diff --git a/src/test/java/io/r2dbc/postgresql/IndefiniteStatementCacheTest.java b/src/test/java/io/r2dbc/postgresql/IndefiniteStatementCacheTest.java index 4c338ce1..6db9bad5 100644 --- a/src/test/java/io/r2dbc/postgresql/IndefiniteStatementCacheTest.java +++ b/src/test/java/io/r2dbc/postgresql/IndefiniteStatementCacheTest.java @@ -48,11 +48,11 @@ void constructorNoClient() { void getName() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("S_0", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("S_0", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("S_1", Collections.singletonList(200), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("S_1", new int[]{200}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) - .expectRequest(new Parse("S_2", Collections.singletonList(200), "test-query-2"), Flush.INSTANCE) + .expectRequest(new Parse("S_2", new int[]{200}, "test-query-2"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .build(); // @formatter:on @@ -84,7 +84,7 @@ void getName() { void getNameErrorResponse() { // @formatter:off Client client = TestClient.builder() - .expectRequest(new Parse("S_0", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("S_0", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(new ErrorResponse(Collections.emptyList())) .build(); // @formatter:on diff --git a/src/test/java/io/r2dbc/postgresql/PostgresqlColumnMetadataTest.java b/src/test/java/io/r2dbc/postgresql/PostgresqlColumnMetadataTest.java index fd54d759..b89c5742 100644 --- a/src/test/java/io/r2dbc/postgresql/PostgresqlColumnMetadataTest.java +++ b/src/test/java/io/r2dbc/postgresql/PostgresqlColumnMetadataTest.java @@ -22,22 +22,9 @@ import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; final class PostgresqlColumnMetadataTest { - @Test - void constructorNoName() { - assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlColumnMetadata(null, null)) - .withMessage("codecs must not be null"); - } - - @Test - void constructorNoNativeType() { - assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlColumnMetadata(MockCodecs.empty(), null)) - .withMessage("field must not be null"); - } - @Test void toColumnMetadata() { MockCodecs codecs = MockCodecs.builder() @@ -52,16 +39,4 @@ void toColumnMetadata() { assertThat(columnMetadata.getPrecision()).isEqualTo(400); } - @Test - void toColumnMetadataNoCodecs() { - assertThatIllegalArgumentException().isThrownBy(() -> PostgresqlColumnMetadata.toColumnMetadata(null, new Field((short) 100, 200, 300, (short) 400, FORMAT_TEXT, "test-name", 500))) - .withMessage("codecs must not be null"); - } - - @Test - void toColumnMetadataNoField() { - assertThatIllegalArgumentException().isThrownBy(() -> PostgresqlColumnMetadata.toColumnMetadata(MockCodecs.empty(), null)) - .withMessage("field must not be null"); - } - } diff --git a/src/test/java/io/r2dbc/postgresql/PostgresqlResultTest.java b/src/test/java/io/r2dbc/postgresql/PostgresqlResultTest.java index 8f198a8c..da7bdd16 100644 --- a/src/test/java/io/r2dbc/postgresql/PostgresqlResultTest.java +++ b/src/test/java/io/r2dbc/postgresql/PostgresqlResultTest.java @@ -26,16 +26,8 @@ import java.util.Collections; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; - final class PostgresqlResultTest { - @Test - void constructorNoRowMetadata() { - assertThatIllegalArgumentException().isThrownBy(() -> new PostgresqlResult(MockContext.empty(), null, ExceptionFactory.INSTANCE)) - .withMessage("messages must not be null"); - } - @Test void toResultCommandComplete() { PostgresqlResult result = PostgresqlResult.toResult(MockContext.empty(), Flux.just(new CommandComplete("test", null, 1)), ExceptionFactory.INSTANCE); @@ -63,18 +55,6 @@ void toResultEmptyQueryResponse() { .verifyComplete(); } - @Test - void toResultNoContext() { - assertThatIllegalArgumentException().isThrownBy(() -> PostgresqlResult.toResult(null, Flux.empty(), ExceptionFactory.INSTANCE)) - .withMessage("context must not be null"); - } - - @Test - void toResultNoMessages() { - assertThatIllegalArgumentException().isThrownBy(() -> PostgresqlResult.toResult(MockContext.empty(), null, ExceptionFactory.INSTANCE)) - .withMessage("messages must not be null"); - } - @Test void toResultRowDescriptionRowsUpdated() { PostgresqlResult result = PostgresqlResult.toResult(MockContext.empty(), Flux.just(new RowDescription(Collections.emptyList()), new DataRow(), new CommandComplete diff --git a/src/test/java/io/r2dbc/postgresql/client/BindingTest.java b/src/test/java/io/r2dbc/postgresql/client/BindingTest.java index cb684c45..ebb6e5af 100644 --- a/src/test/java/io/r2dbc/postgresql/client/BindingTest.java +++ b/src/test/java/io/r2dbc/postgresql/client/BindingTest.java @@ -21,7 +21,6 @@ import reactor.core.publisher.Flux; import reactor.test.StepVerifier; -import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR; @@ -33,12 +32,6 @@ final class BindingTest { - @Test - void addNoIndex() { - assertThatIllegalArgumentException().isThrownBy(() -> new Binding(1).add(null, new Parameter(FORMAT_TEXT, 100, NULL_VALUE))) - .withMessage("index must not be null"); - } - @Test void addNoParameter() { assertThatIllegalArgumentException().isThrownBy(() -> new Binding(1).add(1, null)) diff --git a/src/test/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlowTest.java b/src/test/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlowTest.java index 1dbea349..3406ce03 100644 --- a/src/test/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlowTest.java +++ b/src/test/java/io/r2dbc/postgresql/client/ExtendedQueryMessageFlowTest.java @@ -38,6 +38,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; +import java.util.List; import static io.r2dbc.postgresql.client.TestClient.NO_OP; import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; @@ -48,7 +49,7 @@ final class ExtendedQueryMessageFlowTest { @Test void execute() { - Flux bindings = Flux.just( + List bindings = Arrays.asList( new Binding(1).add(0, new Parameter(FORMAT_BINARY, 100, Flux.just(TEST.buffer(4).writeInt(200)))), new Binding(1).add(0, new Parameter(FORMAT_BINARY, 100, Flux.just(TEST.buffer(4).writeInt(300)))) ); @@ -73,7 +74,7 @@ BindComplete.INSTANCE, NoData.INSTANCE, new CommandComplete("test", null, null) PortalNameSupplier portalNameSupplier = new LinkedList<>(Arrays.asList("B_0", "B_1"))::remove; ExtendedQueryMessageFlow - .execute(bindings, client, portalNameSupplier, "test-name", "", false) + .execute(Flux.fromIterable(bindings), client, portalNameSupplier, "test-name", "", false) .as(StepVerifier::create) .expectNext(BindComplete.INSTANCE, NoData.INSTANCE, new CommandComplete("test", null, null)) .expectNext(BindComplete.INSTANCE, NoData.INSTANCE, new CommandComplete("test", null, null)) @@ -107,12 +108,12 @@ void executeNoStatement() { @Test void parse() { Client client = TestClient.builder() - .expectRequest(new Parse("test-name", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("test-name", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(ParseComplete.INSTANCE) .build(); ExtendedQueryMessageFlow - .parse(client, "test-name", "test-query", Collections.singletonList(100)) + .parse(client, "test-name", "test-query", new int[]{100}) .as(StepVerifier::create) .verifyComplete(); } @@ -120,14 +121,14 @@ void parse() { @Test void parseWithError() { Client client = TestClient.builder() - .expectRequest(new Parse("test-name", Collections.singletonList(100), "test-query"), Flush.INSTANCE) + .expectRequest(new Parse("test-name", new int[]{100}, "test-query"), Flush.INSTANCE) .thenRespond(new ErrorResponse(Collections.emptyList())) .expectRequest(Sync.INSTANCE) .thenRespond(new ReadyForQuery(ReadyForQuery.TransactionStatus.IDLE)) .build(); ExtendedQueryMessageFlow - .parse(client, "test-name", "test-query", Collections.singletonList(100)) + .parse(client, "test-name", "test-query", new int[]{100}) .as(StepVerifier::create) .expectNext(new ErrorResponse(Collections.emptyList())) .verifyComplete(); @@ -135,19 +136,19 @@ void parseWithError() { @Test void parseNoClient() { - assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(null, "test-name", "test-query", Collections.emptyList())) + assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(null, "test-name", "test-query", new int[0])) .withMessage("client must not be null"); } @Test void parseNoName() { - assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(NO_OP, null, "test-query", Collections.emptyList())) + assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(NO_OP, null, "test-query", new int[0])) .withMessage("name must not be null"); } @Test void parseNoQuery() { - assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(NO_OP, "test-name", null, Collections.emptyList())) + assertThatIllegalArgumentException().isThrownBy(() -> ExtendedQueryMessageFlow.parse(NO_OP, "test-name", null, new int[0])) .withMessage("query must not be null"); } diff --git a/src/test/java/io/r2dbc/postgresql/client/ReactorNettyClientIntegrationTests.java b/src/test/java/io/r2dbc/postgresql/client/ReactorNettyClientIntegrationTests.java index 732aff7a..5ad7c23a 100644 --- a/src/test/java/io/r2dbc/postgresql/client/ReactorNettyClientIntegrationTests.java +++ b/src/test/java/io/r2dbc/postgresql/client/ReactorNettyClientIntegrationTests.java @@ -284,7 +284,7 @@ void parallelExchange() { @Test void parallelExchangeExtendedFlow() { - ExtendedQueryMessageFlow.parse(this.client, "S_1", "SELECT $1", Arrays.asList(INT4.getObjectId())) + ExtendedQueryMessageFlow.parse(this.client, "S_1", "SELECT $1", new int[]{INT4.getObjectId()}) .as(StepVerifier::create) .verifyComplete(); diff --git a/src/test/java/io/r2dbc/postgresql/message/frontend/ParseTest.java b/src/test/java/io/r2dbc/postgresql/message/frontend/ParseTest.java index 7ee5a1bf..9502fab7 100644 --- a/src/test/java/io/r2dbc/postgresql/message/frontend/ParseTest.java +++ b/src/test/java/io/r2dbc/postgresql/message/frontend/ParseTest.java @@ -18,8 +18,6 @@ import org.junit.jupiter.api.Test; -import java.util.Collections; - import static io.netty.util.CharsetUtil.UTF_8; import static io.r2dbc.postgresql.message.frontend.FrontendMessageAssert.assertThat; import static io.r2dbc.postgresql.message.frontend.Parse.UNSPECIFIED; @@ -29,7 +27,7 @@ final class ParseTest { @Test void constructorNoName() { - assertThatIllegalArgumentException().isThrownBy(() -> new Parse(null, Collections.emptyList(), "test-query")) + assertThatIllegalArgumentException().isThrownBy(() -> new Parse(null, new int[0], "test-query")) .withMessage("name must not be null"); } @@ -41,13 +39,13 @@ void constructorNoParameters() { @Test void constructorNoQuery() { - assertThatIllegalArgumentException().isThrownBy(() -> new Parse("test-name", Collections.emptyList(), null)) + assertThatIllegalArgumentException().isThrownBy(() -> new Parse("test-name", new int[0], null)) .withMessage("query must not be null"); } @Test void encode() { - assertThat(new Parse("test-name", Collections.singletonList(UNSPECIFIED), "test-query")).encoded() + assertThat(new Parse("test-name", new int[]{UNSPECIFIED}, "test-query")).encoded() .isDeferred() .isEncodedAs(buffer -> { buffer