-
Notifications
You must be signed in to change notification settings - Fork 132
Add support for named parameters #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
[[new-features]] | ||
= New & Noteworthy | ||
|
||
[[new-features.1-0-0-M2]] | ||
== What's New in Spring Data R2DBC 1.0.0 M2 | ||
|
||
* Support for named parameters. | ||
|
||
[[new-features.1-0-0-M1]] | ||
== What's New in Spring Data R2DBC 1.0.0 M1 | ||
|
||
* Initial R2DBC support through `DatabaseClient` | ||
* Initial Transaction support through `TransactionalDatabaseClient` | ||
* Initial R2DBC Repository Support through `R2dbcRepository` | ||
* Initial Dialect support for Postgres and Microsoft SQL Server | ||
* Initial R2DBC support through `DatabaseClient`. | ||
* Initial Transaction support through `TransactionalDatabaseClient`. | ||
* Initial R2DBC Repository Support through `R2dbcRepository`. | ||
* Initial Dialect support for Postgres and Microsoft SQL Server. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/springframework/data/r2dbc/function/BindParameterSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.r2dbc.function; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Interface that defines common functionality for objects that can offer parameter values for named bind parameters, | ||
* serving as argument for {@link NamedParameterExpander} operations. | ||
* <p> | ||
* This interface allows for the specification of the type in addition to parameter values. All parameter values and | ||
* types are identified by specifying the name of the parameter. | ||
* <p> | ||
* Intended to wrap various implementations like a {@link java.util.Map} with a consistent interface. | ||
* | ||
* @author Mark Paluch | ||
* @see MapBindParameterSource | ||
*/ | ||
public interface BindParameterSource { | ||
|
||
/** | ||
* Determine whether there is a value for the specified named parameter. | ||
* | ||
* @param paramName the name of the parameter. | ||
* @return {@literal true} if there is a value defined; {@literal false} otherwise. | ||
*/ | ||
boolean hasValue(String paramName); | ||
|
||
/** | ||
* Return the parameter value for the requested named parameter. | ||
* | ||
* @param paramName the name of the parameter. | ||
* @return the value of the specified parameter, can be {@literal null}. | ||
* @throws IllegalArgumentException if there is no value for the requested parameter. | ||
*/ | ||
@Nullable | ||
Object getValue(String paramName) throws IllegalArgumentException; | ||
|
||
/** | ||
* Determine the type for the specified named parameter. | ||
* | ||
* @param paramName the name of the parameter. | ||
* @return the type of the specified parameter, or {@link Object#getClass()} if not known. | ||
*/ | ||
default Class<?> getType(String paramName) { | ||
return Object.class; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,9 +66,6 @@ | |
*/ | ||
class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { | ||
|
||
/** | ||
* Logger available to subclasses | ||
*/ | ||
private final Log logger = LogFactory.getLog(getClass()); | ||
|
||
private final ConnectionFactory connector; | ||
|
@@ -77,14 +74,18 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor { | |
|
||
private final ReactiveDataAccessStrategy dataAccessStrategy; | ||
|
||
private final NamedParameterExpander namedParameters; | ||
|
||
private final DefaultDatabaseClientBuilder builder; | ||
|
||
DefaultDatabaseClient(ConnectionFactory connector, R2dbcExceptionTranslator exceptionTranslator, | ||
ReactiveDataAccessStrategy dataAccessStrategy, DefaultDatabaseClientBuilder builder) { | ||
ReactiveDataAccessStrategy dataAccessStrategy, NamedParameterExpander namedParameters, | ||
DefaultDatabaseClientBuilder builder) { | ||
|
||
this.connector = connector; | ||
this.exceptionTranslator = exceptionTranslator; | ||
this.dataAccessStrategy = dataAccessStrategy; | ||
this.namedParameters = namedParameters; | ||
this.builder = builder; | ||
} | ||
|
||
|
@@ -253,21 +254,30 @@ protected DefaultGenericExecuteSpec createGenericExecuteSpec(Supplier<String> sq | |
private static void doBind(Statement<?> statement, Map<String, SettableValue> byName, | ||
Map<Integer, SettableValue> byIndex) { | ||
|
||
byIndex.forEach((i, o) -> { | ||
bindByIndex(statement, byIndex); | ||
bindByName(statement, byName); | ||
} | ||
|
||
private static void bindByName(Statement<?> statement, Map<String, SettableValue> byName) { | ||
|
||
byName.forEach((name, o) -> { | ||
|
||
if (o.getValue() != null) { | ||
statement.bind(i.intValue(), o.getValue()); | ||
statement.bind(name, o.getValue()); | ||
} else { | ||
statement.bindNull(i.intValue(), o.getType()); | ||
statement.bindNull(name, o.getType()); | ||
} | ||
}); | ||
} | ||
|
||
byName.forEach((name, o) -> { | ||
private static void bindByIndex(Statement<?> statement, Map<Integer, SettableValue> byIndex) { | ||
|
||
byIndex.forEach((i, o) -> { | ||
|
||
if (o.getValue() != null) { | ||
statement.bind(name, o.getValue()); | ||
statement.bind(i.intValue(), o.getValue()); | ||
} else { | ||
statement.bindNull(name, o.getType()); | ||
statement.bindNull(i.intValue(), o.getType()); | ||
} | ||
}); | ||
} | ||
|
@@ -325,8 +335,21 @@ <T> FetchSpec<T> exchange(String sql, BiFunction<Row, RowMetadata, T> mappingFun | |
logger.debug("Executing SQL statement [" + sql + "]"); | ||
} | ||
|
||
Statement<?> statement = it.createStatement(sql); | ||
doBind(statement, byName, byIndex); | ||
BindableOperation operation = namedParameters.expand(sql, dataAccessStrategy.getBindMarkersFactory(), | ||
new MapBindParameterSource(byName)); | ||
|
||
Statement<?> statement = it.createStatement(operation.toQuery()); | ||
|
||
byName.forEach((name, o) -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing the binding by name inline but calling a method for binding by index looks unpleasant unsymmetrically. |
||
|
||
if (o.getValue() != null) { | ||
operation.bind(statement, name, o.getValue()); | ||
} else { | ||
operation.bindNull(statement, name, o.getType()); | ||
} | ||
}); | ||
|
||
bindByIndex(statement, byIndex); | ||
|
||
return statement; | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
$1
syntax left in there an oversight or an attempt to demonstrate database dependent options?If the latter I think we need an explanatory note to that extent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's intentionally there, to occasionally use native bind markers. See the callout description: