|
18 | 18 | import org.springframework.data.domain.Sort;
|
19 | 19 | import org.springframework.data.r2dbc.convert.R2dbcConverter;
|
20 | 20 | import org.springframework.data.r2dbc.core.DatabaseClient;
|
| 21 | +import org.springframework.data.r2dbc.core.DatabaseClient.BindSpec; |
21 | 22 | import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
|
| 23 | +import org.springframework.data.r2dbc.repository.query.ParameterMetadataProvider.ParameterMetadata; |
22 | 24 | import org.springframework.data.relational.repository.query.RelationalEntityMetadata;
|
23 | 25 | import org.springframework.data.relational.repository.query.RelationalParameterAccessor;
|
24 | 26 | import org.springframework.data.relational.repository.query.RelationalParameters;
|
@@ -68,8 +70,56 @@ public PartTreeR2dbcQuery(R2dbcQueryMethod method,
|
68 | 70 | protected BindableQuery createQuery(RelationalParameterAccessor accessor) {
|
69 | 71 | RelationalEntityMetadata<?> entityMetadata = getQueryMethod().getEntityInformation();
|
70 | 72 | ParameterMetadataProvider parameterMetadataProvider = new ParameterMetadataProvider(accessor);
|
71 |
| - return new R2dbcQueryCreator(tree, dataAccessStrategy, entityMetadata, parameterMetadataProvider) |
72 |
| - .createQuery(getDynamicSort(accessor)); |
| 73 | + R2dbcQueryCreator queryCreator = new R2dbcQueryCreator(tree, dataAccessStrategy, entityMetadata, |
| 74 | + parameterMetadataProvider); |
| 75 | + String sql = queryCreator.createQuery(getDynamicSort(accessor)); |
| 76 | + return new BindableQuery() { |
| 77 | + @Override |
| 78 | + public <T extends BindSpec<T>> T bind(T bindSpec) { |
| 79 | + T bindSpecToUse = bindSpec; |
| 80 | + |
| 81 | + int index = 0; |
| 82 | + int bindingIndex = 0; |
| 83 | + |
| 84 | + for (Object value : accessor.getValues()) { |
| 85 | + ParameterMetadata metadata = parameterMetadataProvider.getParameterMetadata(index++); |
| 86 | + String parameterName = metadata.getName(); |
| 87 | + Class<?> parameterType = metadata.getType(); |
| 88 | + |
| 89 | + if (parameterName != null) { |
| 90 | + if (value == null) { |
| 91 | + checkNullIsAllowed(metadata, "Value of parameter with name %s must not be null!", |
| 92 | + parameterName); |
| 93 | + bindSpecToUse = bindSpecToUse.bindNull(parameterName, parameterType); |
| 94 | + } else { |
| 95 | + bindSpecToUse = bindSpecToUse.bind(parameterName, value); |
| 96 | + } |
| 97 | + } else { |
| 98 | + if (value == null) { |
| 99 | + checkNullIsAllowed(metadata, "Value of parameter with index %d must not be null!", |
| 100 | + bindingIndex); |
| 101 | + bindSpecToUse = bindSpecToUse.bindNull(bindingIndex++, parameterType); |
| 102 | + } else { |
| 103 | + bindSpecToUse = bindSpecToUse.bind(bindingIndex++, value); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return bindSpecToUse; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String get() { |
| 113 | + return sql; |
| 114 | + } |
| 115 | + |
| 116 | + private void checkNullIsAllowed(ParameterMetadata metadata, String errorMessage, Object messageArgument) { |
| 117 | + if (!metadata.isIsNullParameter()) { |
| 118 | + String message = String.format(errorMessage, messageArgument); |
| 119 | + throw new IllegalArgumentException(message); |
| 120 | + } |
| 121 | + } |
| 122 | + }; |
73 | 123 | }
|
74 | 124 |
|
75 | 125 | private Sort getDynamicSort(RelationalParameterAccessor accessor) {
|
|
0 commit comments