|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.r2dbc.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import org.springframework.r2dbc.core.Parameter; |
| 30 | +import org.springframework.r2dbc.core.PreparedOperation; |
| 31 | +import org.springframework.r2dbc.core.binding.BindMarkersFactory; |
| 32 | +import org.springframework.r2dbc.core.binding.BindTarget; |
| 33 | + |
| 34 | +/** |
| 35 | + * Unit tests for {@link NamedParameterUtils}. |
| 36 | + * |
| 37 | + * @author Mark Paluch |
| 38 | + */ |
| 39 | +class NamedParameterUtilsTests { |
| 40 | + |
| 41 | + @Test // GH-1306 |
| 42 | + void inCollectionSameParameterNameShouldBindAllAnonymousParameters() { |
| 43 | + |
| 44 | + ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement("select :names AND :names"); |
| 45 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(parsedSql, |
| 46 | + BindMarkersFactory.anonymous("?"), |
| 47 | + new MapBindParameterSource(Collections.singletonMap("names", Parameter.from(Arrays.asList("1", "2", "3"))))); |
| 48 | + |
| 49 | + List<String> bindings = new ArrayList<>(); |
| 50 | + |
| 51 | + operation.bindTo(new BindingCaptor(bindings)); |
| 52 | + |
| 53 | + assertThat(operation.get()).isEqualTo("select ?, ?, ? AND ?, ?, ?"); |
| 54 | + assertThat(bindings).contains("0: 1", "1: 2", "2: 3", "3: 1", "4: 2", "5: 3"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test // GH-1306 |
| 58 | + void complexInCollectionSameParameterNameShouldBindAllAnonymousParameters() { |
| 59 | + |
| 60 | + Map<String, Parameter> parameterMap = new HashMap<>(); |
| 61 | + parameterMap.put("names", Parameter.from(Arrays.asList("1", "2", "3"))); |
| 62 | + parameterMap.put("hello", Parameter.from("world")); |
| 63 | + |
| 64 | + ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement("select :names AND :hello OR :names"); |
| 65 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(parsedSql, |
| 66 | + BindMarkersFactory.anonymous("?"), new MapBindParameterSource(parameterMap)); |
| 67 | + |
| 68 | + List<String> bindings = new ArrayList<>(); |
| 69 | + |
| 70 | + operation.bindTo(new BindingCaptor(bindings)); |
| 71 | + |
| 72 | + assertThat(operation.get()).isEqualTo("select ?, ?, ? AND ? OR ?, ?, ?"); |
| 73 | + assertThat(bindings).contains("0: 1", "1: 2", "2: 3", "3: world", "4: 1", "5: 2", "6: 3"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test // GH-1306 |
| 77 | + void inCollectionSameParameterNameShouldBindAllNamedParameters() { |
| 78 | + |
| 79 | + ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement("select :names AND :names"); |
| 80 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(parsedSql, |
| 81 | + BindMarkersFactory.indexed("$", 1), |
| 82 | + new MapBindParameterSource(Collections.singletonMap("names", Parameter.from(Arrays.asList("1", "2", "3"))))); |
| 83 | + |
| 84 | + List<String> bindings = new ArrayList<>(); |
| 85 | + |
| 86 | + operation.bindTo(new BindingCaptor(bindings)); |
| 87 | + |
| 88 | + assertThat(operation.get()).isEqualTo("select $1, $2, $3 AND $1, $2, $3"); |
| 89 | + assertThat(bindings).containsOnly("0: 1", "1: 2", "2: 3"); |
| 90 | + } |
| 91 | + |
| 92 | + static class BindingCaptor implements BindTarget { |
| 93 | + |
| 94 | + private final List<String> bindings; |
| 95 | + |
| 96 | + BindingCaptor(List<String> bindings) { |
| 97 | + this.bindings = bindings; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void bind(String identifier, Object value) { |
| 102 | + bindings.add(identifier + ": " + value); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void bind(int index, Object value) { |
| 107 | + bindings.add(index + ": " + value); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void bindNull(String identifier, Class<?> type) { |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void bindNull(int index, Class<?> type) { |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments