Polishing

This commit is contained in:
Sam Brannen 2022-11-10 17:25:20 +01:00
parent 4c2072d20c
commit 1443669091
3 changed files with 27 additions and 29 deletions

View File

@ -37,14 +37,14 @@ import org.springframework.r2dbc.core.binding.BindMarkersFactory;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* A non-blocking, reactive client for performing database calls requests with * A non-blocking, reactive client for performing database calls with
* Reactive Streams back pressure. Provides a higher level, common API over * Reactive Streams back pressure. Provides a higher level, common API over
* R2DBC client libraries. * R2DBC client libraries.
* *
* <p>Use one of the static factory methods {@link #create(ConnectionFactory)} * <p>Use the static factory method {@link #create(ConnectionFactory)} or obtain
* or obtain a {@link DatabaseClient#builder()} to create an instance. * a {@linkplain DatabaseClient#builder() builder} to create an instance.
* *
* Usage example: * <p>Usage example:
* <pre class="code"> * <pre class="code">
* ConnectionFactory factory = * ConnectionFactory factory =
* *
@ -52,8 +52,7 @@ import org.springframework.util.Assert;
* Mono&lt;Actor&gt; actor = client.sql("select first_name, last_name from t_actor") * Mono&lt;Actor&gt; actor = client.sql("select first_name, last_name from t_actor")
* .map(row -&gt; new Actor(row.get("first_name", String.class), * .map(row -&gt; new Actor(row.get("first_name", String.class),
* row.get("last_name", String.class))) * row.get("last_name", String.class)))
* .first(); * .first();</pre>
* </pre>
* *
* @author Mark Paluch * @author Mark Paluch
* @since 5.3 * @since 5.3
@ -61,13 +60,13 @@ import org.springframework.util.Assert;
public interface DatabaseClient extends ConnectionAccessor { public interface DatabaseClient extends ConnectionAccessor {
/** /**
* Return the {@link ConnectionFactory} that this client uses. * Get the {@link ConnectionFactory} that this client uses.
* @return the connection factory * @return the connection factory
*/ */
ConnectionFactory getConnectionFactory(); ConnectionFactory getConnectionFactory();
/** /**
* Specify a static {@code sql} statement to run. Contract for specifying a * Specify a static {@code sql} statement to run. Contract for specifying an
* SQL call along with options leading to the execution. The SQL string can * SQL call along with options leading to the execution. The SQL string can
* contain either native parameter bind markers or named parameters (e.g. * contain either native parameter bind markers or named parameters (e.g.
* {@literal :foo, :bar}) when {@link NamedParameterExpander} is enabled. * {@literal :foo, :bar}) when {@link NamedParameterExpander} is enabled.
@ -79,7 +78,7 @@ public interface DatabaseClient extends ConnectionAccessor {
GenericExecuteSpec sql(String sql); GenericExecuteSpec sql(String sql);
/** /**
* Specify a {@link Supplier SQL supplier} that provides SQL to run. * Specify an {@linkplain Supplier SQL supplier} that provides SQL to run.
* Contract for specifying an SQL call along with options leading to * Contract for specifying an SQL call along with options leading to
* the execution. The SQL string can contain either native parameter * the execution. The SQL string can contain either native parameter
* bind markers or named parameters (e.g. {@literal :foo, :bar}) when * bind markers or named parameters (e.g. {@literal :foo, :bar}) when
@ -99,7 +98,7 @@ public interface DatabaseClient extends ConnectionAccessor {
/** /**
* Create a {@code DatabaseClient} that will use the provided {@link ConnectionFactory}. * Create a {@code DatabaseClient} that will use the provided {@link ConnectionFactory}.
* @param factory the {@code ConnectionFactory} to use for obtaining connections * @param factory the {@code ConnectionFactory} to use for obtaining connections
* @return a new {@code DatabaseClient}. Guaranteed to be not {@code null}. * @return a new {@code DatabaseClient}; never {@code null}
*/ */
static DatabaseClient create(ConnectionFactory factory) { static DatabaseClient create(ConnectionFactory factory) {
return new DefaultDatabaseClientBuilder().connectionFactory(factory).build(); return new DefaultDatabaseClientBuilder().connectionFactory(factory).build();
@ -129,14 +128,14 @@ public interface DatabaseClient extends ConnectionAccessor {
Builder connectionFactory(ConnectionFactory factory); Builder connectionFactory(ConnectionFactory factory);
/** /**
* Configure a {@link ExecuteFunction} to execute {@link Statement} objects. * Configure an {@link ExecuteFunction} to execute {@link Statement} objects.
* @see Statement#execute() * @see Statement#execute()
*/ */
Builder executeFunction(ExecuteFunction executeFunction); Builder executeFunction(ExecuteFunction executeFunction);
/** /**
* Configure whether to use named parameter expansion. * Configure whether to use named parameter expansion.
* Defaults to {@code true}. * <p>Defaults to {@code true}.
* @param enabled {@code true} to use named parameter expansion; * @param enabled {@code true} to use named parameter expansion;
* {@code false} to disable named parameter expansion * {@code false} to disable named parameter expansion
* @see NamedParameterExpander * @see NamedParameterExpander
@ -144,7 +143,7 @@ public interface DatabaseClient extends ConnectionAccessor {
Builder namedParameters(boolean enabled); Builder namedParameters(boolean enabled);
/** /**
* Configures a {@link Consumer} to configure this builder. * Apply a {@link Consumer} to configure this builder.
*/ */
Builder apply(Consumer<Builder> builderConsumer); Builder apply(Consumer<Builder> builderConsumer);
@ -238,7 +237,7 @@ public interface DatabaseClient extends ConnectionAccessor {
* Perform the SQL call and apply {@link BiFunction function} to the {@link Result}. * Perform the SQL call and apply {@link BiFunction function} to the {@link Result}.
* @param mappingFunction a function that maps from {@link Result} into a result publisher * @param mappingFunction a function that maps from {@link Result} into a result publisher
* @param <R> the result type * @param <R> the result type
* @return a {@link Flux} emitting mapped elements * @return a {@link Flux} that emits mapped elements
* @since 6.0 * @since 6.0
* @see Result#filter(Predicate) * @see Result#filter(Predicate)
* @see Result#flatMap(Function) * @see Result#flatMap(Function)

View File

@ -200,16 +200,15 @@ class DefaultDatabaseClient implements DatabaseClient {
} }
/** /**
* Determine SQL from potential provider object. * Get SQL from a potential provider object.
* @param sqlProvider object that's potentially a SqlProvider * @param object an object that is potentially an SqlProvider
* @return the SQL string, or {@code null} * @return the SQL string, or {@code null}
* @see SqlProvider * @see SqlProvider
*/ */
@Nullable @Nullable
private static String getSql(Object sqlProvider) { private static String getSql(Object object) {
if (object instanceof SqlProvider sqlProvider) {
if (sqlProvider instanceof SqlProvider) { return sqlProvider.getSql();
return ((SqlProvider) sqlProvider).getSql();
} }
else { else {
return null; return null;
@ -218,7 +217,7 @@ class DefaultDatabaseClient implements DatabaseClient {
/** /**
* Base class for {@link DatabaseClient.GenericExecuteSpec} implementations. * Default {@link DatabaseClient.GenericExecuteSpec} implementation.
*/ */
class DefaultGenericExecuteSpec implements GenericExecuteSpec { class DefaultGenericExecuteSpec implements GenericExecuteSpec {
@ -352,10 +351,10 @@ class DefaultDatabaseClient implements DatabaseClient {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Executing SQL statement [" + sql + "]"); logger.debug("Executing SQL statement [" + sql + "]");
} }
if (sqlSupplier instanceof PreparedOperation<?>) { if (sqlSupplier instanceof PreparedOperation<?> preparedOperation) {
Statement statement = connection.createStatement(sql); Statement statement = connection.createStatement(sql);
BindTarget bindTarget = new StatementWrapper(statement); BindTarget bindTarget = new StatementWrapper(statement);
((PreparedOperation<?>) sqlSupplier).bindTo(bindTarget); preparedOperation.bindTo(bindTarget);
return statement; return statement;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,25 +23,25 @@ import org.springframework.r2dbc.core.binding.BindTarget;
/** /**
* Extension to {@link QueryOperation} for a prepared SQL query * Extension to {@link QueryOperation} for a prepared SQL query
* {@link Supplier} with bound parameters. Contains parameter * {@link Supplier} with bound parameters. Contains parameter
* bindings that can be {@link #bindTo bound} bound to a {@link BindTarget}. * bindings that can be {@link #bindTo bound} to a {@link BindTarget}.
* *
* <p>Can be executed with {@link org.springframework.r2dbc.core.DatabaseClient}. * <p>Can be executed with {@link org.springframework.r2dbc.core.DatabaseClient}.
* *
* @author Mark Paluch * @author Mark Paluch
* @since 5.3 * @since 5.3
* @param <T> underlying operation source. * @param <T> underlying operation source
* @see org.springframework.r2dbc.core.DatabaseClient#sql(Supplier) * @see org.springframework.r2dbc.core.DatabaseClient#sql(Supplier)
*/ */
public interface PreparedOperation<T> extends QueryOperation { public interface PreparedOperation<T> extends QueryOperation {
/** /**
* Return the underlying query source. * Get the underlying query source.
* @return the query source, such as a statement/criteria object * @return the query source, such as a statement/criteria object
*/ */
T getSource(); T getSource();
/** /**
* Apply bindings to {@link BindTarget}. * Apply bindings to the supplied {@link BindTarget}.
* @param target the target to apply bindings to * @param target the target to apply bindings to
*/ */
void bindTo(BindTarget target); void bindTo(BindTarget target);