Skip to content

Commit 8dd9cbb

Browse files
IAM20mhyeon-lee
authored andcommitted
Add saving parameterName prefix feature (#50)
(cherry picked from commit d99fe71)
1 parent 30c2f06 commit 8dd9cbb

File tree

6 files changed

+190
-5
lines changed

6 files changed

+190
-5
lines changed

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/provider/EntityJdbcProvider.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.navercorp.spring.data.jdbc.plus.sql.convert.SqlProvider;
3535
import com.navercorp.spring.data.jdbc.plus.sql.parametersource.SqlParameterSourceFactory;
3636
import com.navercorp.spring.jdbc.plus.support.parametersource.CompositeSqlParameterSource;
37+
import com.navercorp.spring.jdbc.plus.support.parametersource.ConvertibleParameterSourceFactory;
3738

3839
/**
3940
* The type Entity jdbc provider.
@@ -198,6 +199,21 @@ public BeanPropertySqlParameterSource beanParameterSource(Object bean) {
198199
return this.sqlParameterSourceFactory.beanParameterSource(bean);
199200
}
200201

202+
/**
203+
* Bean parameter source bean property sql parameter source.
204+
*
205+
* @param prefix the prefix
206+
* @param bean the bean
207+
* @return the bean property sql parameter source
208+
*/
209+
public BeanPropertySqlParameterSource beanParameterSource(String prefix, Object bean) {
210+
if (this.sqlParameterSourceFactory instanceof ConvertibleParameterSourceFactory) {
211+
return ((ConvertibleParameterSourceFactory) this.sqlParameterSourceFactory).beanParameterSource(prefix, bean);
212+
} else {
213+
throw new UnsupportedOperationException("Prefix saving is not supported as default.");
214+
}
215+
}
216+
201217
/**
202218
* Map parameter source map sql parameter source.
203219
*

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/support/JdbcDaoSupport.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ protected BeanPropertySqlParameterSource beanParameterSource(Object bean) {
125125
return this.entityJdbcProvider.beanParameterSource(bean);
126126
}
127127

128+
/**
129+
* Bean parameter source bean property sql parameter source.
130+
*
131+
* @param prefix the prefix
132+
* @param bean the bean
133+
* @return the bean property sql parameter source
134+
*/
135+
protected BeanPropertySqlParameterSource beanParameterSource(String prefix, Object bean) {
136+
return this.entityJdbcProvider.beanParameterSource(prefix, bean);
137+
}
138+
128139
/**
129140
* Map parameter source map sql parameter source.
130141
*

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/support/JdbcRepositorySupport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ protected BeanPropertySqlParameterSource beanParameterSource(Object bean) {
178178
return this.entityJdbcProvider.beanParameterSource(bean);
179179
}
180180

181+
182+
/**
183+
* Bean parameter source bean property sql parameter source.
184+
*
185+
* @param prefix the prefix
186+
* @param bean the bean
187+
* @return the bean property sql parameter source
188+
*/
189+
protected BeanPropertySqlParameterSource beanParameterSource(String prefix, Object bean) {
190+
return this.entityJdbcProvider.beanParameterSource(prefix, bean);
191+
}
192+
181193
/**
182194
* Map parameter source map sql parameter source.
183195
*

spring-jdbc-plus-support/src/main/java/com/navercorp/spring/jdbc/plus/support/parametersource/ConvertibleBeanPropertySqlParameterSource.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.StringUtils;
2526

2627
import com.navercorp.spring.jdbc.plus.support.parametersource.converter.IterableExpandPadding;
2728
import com.navercorp.spring.jdbc.plus.support.parametersource.converter.JdbcParameterSourceConverter;
@@ -31,10 +32,12 @@
3132
* The type Convertible bean property sql parameter source.
3233
*
3334
* @author Myeonghyeon Lee
35+
* @author IAM20
3436
*/
3537
public class ConvertibleBeanPropertySqlParameterSource extends BeanPropertySqlParameterSource {
3638
private final JdbcParameterSourceConverter converter;
3739
private final FallbackParameterSource fallbackParameterSource;
40+
private final String prefix;
3841

3942
private boolean paddingIterableParams = false;
4043
private int[] paddingIterableBoundaries = null;
@@ -61,28 +64,65 @@ public ConvertibleBeanPropertySqlParameterSource(
6164
JdbcParameterSourceConverter converter,
6265
FallbackParameterSource fallbackParameterSource) {
6366

67+
this(null, bean, converter, fallbackParameterSource);
68+
}
69+
70+
/**
71+
* Instantiates a new Convertible bean property sql parameter source.
72+
*
73+
* @param prefix the prefix
74+
* @param bean the bean
75+
* @param converter the converter
76+
*/
77+
public ConvertibleBeanPropertySqlParameterSource(
78+
String prefix,
79+
Object bean,
80+
JdbcParameterSourceConverter converter
81+
) {
82+
this(prefix, bean, converter, null);
83+
}
84+
85+
/**
86+
* Instantiates a new Convertible bean property sql parameter source.
87+
*
88+
* @param prefix the prefix
89+
* @param bean the bean
90+
* @param converter the converter
91+
* @param fallbackParameterSource the fallback parameter source
92+
*/
93+
public ConvertibleBeanPropertySqlParameterSource(
94+
String prefix,
95+
Object bean,
96+
JdbcParameterSourceConverter converter,
97+
FallbackParameterSource fallbackParameterSource
98+
) {
99+
64100
super(bean);
101+
this.prefix = prefix != null ? prefix.trim() : null;
65102
this.converter = Objects.requireNonNull(converter, "Converter must not be null.");
66103
this.fallbackParameterSource = fallbackParameterSource;
67104
}
68105

69106
@Nullable
70107
@Override
71108
public Object getValue(String paramName) {
109+
String patchedParamName = paramName;
72110
Object value = null;
111+
73112
try {
74-
value = super.getValue(paramName);
113+
patchedParamName = this.patchParamName(paramName);
114+
value = super.getValue(patchedParamName);
75115
} catch (IllegalArgumentException e) {
76-
if (!this.isFallback(paramName)) {
116+
if (!this.isFallback(patchedParamName)) {
77117
throw e;
78118
}
79119
}
80120

81-
if (value == null && this.isFallback(paramName)) {
82-
value = this.fallbackParameterSource.fallback(paramName);
121+
if (value == null && this.isFallback(patchedParamName)) {
122+
value = this.fallbackParameterSource.fallback(patchedParamName);
83123
}
84124

85-
value = this.converter.convert(paramName, value);
125+
value = this.converter.convert(patchedParamName, value);
86126
if (this.paddingIterableParams) {
87127
value = IterableExpandPadding.expandIfIterable(value, this.paddingIterableBoundaries);
88128
}
@@ -111,4 +151,16 @@ public void setPaddingIterableParam(boolean padding) {
111151
private boolean isFallback(String paramName) {
112152
return this.fallbackParameterSource != null && this.fallbackParameterSource.isFallback(paramName);
113153
}
154+
155+
private String patchParamName(String paramName) {
156+
if (!StringUtils.hasText(prefix)) {
157+
return paramName;
158+
}
159+
160+
if (!paramName.startsWith(prefix)) {
161+
throw new IllegalArgumentException("Param name does not starts with " + this.prefix);
162+
}
163+
164+
return paramName.substring(prefix.length());
165+
}
114166
}

spring-jdbc-plus-support/src/main/java/com/navercorp/spring/jdbc/plus/support/parametersource/ConvertibleParameterSourceFactory.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* The type Convertible parameter source factory.
3131
*
3232
* @author Myeonghyeon Lee
33+
* @author IAM20
3334
*/
3435
public class ConvertibleParameterSourceFactory {
3536
private final JdbcParameterSourceConverter converter;
@@ -73,6 +74,22 @@ public BeanPropertySqlParameterSource beanParameterSource(Object bean) {
7374
return paramSource;
7475
}
7576

77+
/**
78+
* Bean parameter source bean property sql parameter source.
79+
*
80+
* @param prefix the prefix
81+
* @param bean the bean
82+
* @return the bean property sql parameter source
83+
*/
84+
public BeanPropertySqlParameterSource beanParameterSource(String prefix, Object bean) {
85+
ConvertibleBeanPropertySqlParameterSource paramSource =
86+
new ConvertibleBeanPropertySqlParameterSource(
87+
prefix, bean, this.converter, this.fallbackParameterSource);
88+
paramSource.setPaddingIterableParam(this.paddingIterableParams);
89+
paramSource.setPaddingIterableBoundaries(this.paddingIterableBoundaries);
90+
return paramSource;
91+
}
92+
7693
/**
7794
* Map parameter source map sql parameter source.
7895
*

spring-jdbc-plus-support/src/test/java/com/navercorp/spring/jdbc/plus/support/parametersource/ConvertibleBeanPropertySqlParameterSourceTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
/**
4141
* @author Myeonghyeon Lee
42+
* @author IAM20
4243
*/
4344
class ConvertibleBeanPropertySqlParameterSourceTest {
4445
private final JdbcParameterSourceConverter converter = new DefaultJdbcParameterSourceConverter(
@@ -132,6 +133,82 @@ void getValueNoParamButFallback() {
132133
assertThat(actual).isEqualTo("fallback");
133134
}
134135

136+
@Test
137+
void getPrefixValue() {
138+
// given
139+
Instant now = Instant.now();
140+
String paramName = "test.occurrenceTime";
141+
Criteria criteria = Criteria.of("sample", now);
142+
ConvertibleBeanPropertySqlParameterSource sut = new ConvertibleBeanPropertySqlParameterSource(
143+
"test.", criteria, this.converter);
144+
145+
// when
146+
Object actual = sut.getValue(paramName);
147+
148+
// then
149+
assertThat(actual).isEqualTo(Date.from(now));
150+
}
151+
152+
@Test
153+
void getWhitespaceIncludedPrefixValue() {
154+
// given
155+
Instant now = Instant.now();
156+
String paramName = "t e s t .occurrenceTime";
157+
Criteria criteria = Criteria.of("sample", now);
158+
ConvertibleBeanPropertySqlParameterSource sut = new ConvertibleBeanPropertySqlParameterSource(
159+
" t e s t . ", criteria, this.converter);
160+
161+
// when
162+
Object actual = sut.getValue(paramName);
163+
164+
// then
165+
assertThat(actual).isEqualTo(Date.from(now));
166+
}
167+
168+
@Test
169+
void getValuePrefixNotMathced() {
170+
String paramName = "occurrenceTime";
171+
Criteria criteria = Criteria.of("sample", Instant.now());
172+
ConvertibleBeanPropertySqlParameterSource sut = new ConvertibleBeanPropertySqlParameterSource(
173+
"test.", criteria, this.converter);
174+
175+
assertThatThrownBy(() -> sut.getValue(paramName))
176+
.isExactlyInstanceOf(IllegalArgumentException.class)
177+
.hasMessage("Param name does not starts with test.");
178+
}
179+
180+
@DisplayName("Prefix 가 없지만 fallback parameter 라면 Exception 이 발생하지 않습니다.")
181+
@Test
182+
void getValuePrefixNotMatchedButFallback() {
183+
// given
184+
String paramName = "none";
185+
Criteria criteria = Criteria.of(null, Instant.now());
186+
ConvertibleBeanPropertySqlParameterSource sut = new ConvertibleBeanPropertySqlParameterSource(
187+
"test.", criteria, this.converter, new TestFallbackParamSource());
188+
189+
// when
190+
Object actual = sut.getValue(paramName);
191+
192+
// then
193+
assertThat(actual).isEqualTo("fallback");
194+
}
195+
196+
@DisplayName("Object의 Field 가 존재하지 않지만, Prefix 가 있는 fallback parameter 라면 Exception 이 발생하지 않습니다.")
197+
@Test
198+
void getValueNoParamButFallbackInPrefixSource() {
199+
// given
200+
String paramName = "test.none";
201+
Criteria criteria = Criteria.of(null, Instant.now());
202+
ConvertibleBeanPropertySqlParameterSource sut = new ConvertibleBeanPropertySqlParameterSource(
203+
"test.", criteria, this.converter, new TestFallbackParamSource());
204+
205+
// when
206+
Object actual = sut.getValue(paramName);
207+
208+
// then
209+
assertThat(actual).isEqualTo("fallback");
210+
}
211+
135212
@DisplayName("Iterable 한 값은, element 를 컨버팅한 후 expand padding 을 수행한다.")
136213
@ParameterizedTest
137214
@AutoSource

0 commit comments

Comments
 (0)