Skip to content

Commit a3b11b2

Browse files
committed
Support ProviderContext on sql provider method
A ProviderContext instance holds type of mapper interface and mapper method that specified an sql provider annotation. Fixes gh-1013
1 parent 6f9105e commit a3b11b2

File tree

8 files changed

+339
-18
lines changed

8 files changed

+339
-18
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ private SqlSource getSqlSourceFromAnnotations(Method method, Class<?> parameterT
465465
return buildSqlSourceFromStrings(strings, parameterType, languageDriver);
466466
} else if (sqlProviderAnnotationType != null) {
467467
Annotation sqlProviderAnnotation = method.getAnnotation(sqlProviderAnnotationType);
468-
return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation);
468+
return new ProviderSqlSource(assistant.getConfiguration(), sqlProviderAnnotation, type, method);
469469
}
470470
return null;
471471
} catch (Exception e) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2009-2017 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+
* http://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.apache.ibatis.builder.annotation;
17+
18+
import java.lang.reflect.Method;
19+
20+
/**
21+
* The context object for sql provider method.
22+
*
23+
* @author Kazuki Shimizu
24+
* @since 3.4.5
25+
*/
26+
public final class ProviderContext {
27+
28+
private final Class<?> mapperType;
29+
private final Method mapperMethod;
30+
31+
/**
32+
* Constructor.
33+
*
34+
* @param mapperType A mapper interface type that specified provider
35+
* @param mapperMethod A mapper method that specified provider
36+
*/
37+
ProviderContext(Class<?> mapperType, Method mapperMethod) {
38+
this.mapperType = mapperType;
39+
this.mapperMethod = mapperMethod;
40+
}
41+
42+
/**
43+
* Get a mapper interface type that specified provider.
44+
*
45+
* @return A mapper interface type that specified provider
46+
*/
47+
public Class<?> getMapperType() {
48+
return mapperType;
49+
}
50+
51+
/**
52+
* Get a mapper method that specified provider.
53+
*
54+
* @return A mapper method that specified provider
55+
*/
56+
public Method getMapperMethod() {
57+
return mapperMethod;
58+
}
59+
60+
}

src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,22 @@ public class ProviderSqlSource implements SqlSource {
3636
private final Class<?> providerType;
3737
private Method providerMethod;
3838
private String[] providerMethodArgumentNames;
39+
private Class<?>[] providerMethodParameterTypes;
40+
private ProviderContext providerContext;
41+
private Integer providerContextIndex;
3942

43+
/**
44+
* @deprecated Please use the {@link #ProviderSqlSource(Configuration, Object, Class, Method)} instead of this.
45+
*/
46+
@Deprecated
4047
public ProviderSqlSource(Configuration config, Object provider) {
48+
this(config, provider, null, null);
49+
}
50+
51+
/**
52+
* @since 3.4.5
53+
*/
54+
public ProviderSqlSource(Configuration config, Object provider, Class<?> mapperType, Method mapperMethod) {
4155
String providerMethodName;
4256
try {
4357
this.sqlSourceParser = new SqlSourceBuilder(config);
@@ -54,6 +68,7 @@ public ProviderSqlSource(Configuration config, Object provider) {
5468
}
5569
this.providerMethod = m;
5670
this.providerMethodArgumentNames = new ParamNameResolver(config, m).getNames();
71+
this.providerMethodParameterTypes = m.getParameterTypes();
5772
}
5873
}
5974
}
@@ -66,6 +81,18 @@ public ProviderSqlSource(Configuration config, Object provider) {
6681
throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
6782
+ providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");
6883
}
84+
for (int i = 0; i< this.providerMethodParameterTypes.length; i++) {
85+
Class<?> parameterType = this.providerMethodParameterTypes[i];
86+
if (parameterType == ProviderContext.class) {
87+
if (this.providerContext != null){
88+
throw new BuilderException("Error creating SqlSource for SqlProvider. ProviderContext found multiple in SqlProvider method ("
89+
+ this.providerType.getName() + "." + providerMethod.getName()
90+
+ "). ProviderContext can not define multiple in SqlProvider method argument.");
91+
}
92+
this.providerContext = new ProviderContext(mapperType, mapperMethod);
93+
this.providerContextIndex = i;
94+
}
95+
}
6996
}
7097

7198
@Override
@@ -77,12 +104,15 @@ public BoundSql getBoundSql(Object parameterObject) {
77104
private SqlSource createSqlSource(Object parameterObject) {
78105
try {
79106
Class<?>[] parameterTypes = providerMethod.getParameterTypes();
107+
int bindParameterCount = parameterTypes.length - (providerContext == null ? 0 : 1);
80108
String sql;
81109
if (parameterTypes.length == 0) {
82110
sql = (String) providerMethod.invoke(providerType.newInstance());
83-
} else if (parameterTypes.length == 1 &&
84-
(parameterObject == null || parameterTypes[0].isAssignableFrom(parameterObject.getClass()))) {
85-
sql = (String) providerMethod.invoke(providerType.newInstance(), parameterObject);
111+
} else if (bindParameterCount == 0) {
112+
sql = (String) providerMethod.invoke(providerType.newInstance(), providerContext);
113+
} else if (bindParameterCount == 1 &&
114+
(parameterObject == null || parameterTypes[(providerContextIndex == null || providerContextIndex == 1) ? 0 : 1].isAssignableFrom(parameterObject.getClass()))) {
115+
sql = (String) providerMethod.invoke(providerType.newInstance(), extractProviderMethodArguments(parameterObject));
86116
} else if (parameterObject instanceof Map) {
87117
@SuppressWarnings("unchecked")
88118
Map<String, Object> params = (Map<String, Object>) parameterObject;
@@ -91,7 +121,7 @@ private SqlSource createSqlSource(Object parameterObject) {
91121
throw new BuilderException("Error invoking SqlProvider method ("
92122
+ providerType.getName() + "." + providerMethod.getName()
93123
+ "). Cannot invoke a method that holds "
94-
+ (parameterTypes.length == 1 ? "named argument(@Param)": "multiple arguments")
124+
+ (bindParameterCount == 1 ? "named argument(@Param)": "multiple arguments")
95125
+ " using a specifying parameterObject. In this case, please specify a 'java.util.Map' object.");
96126
}
97127
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
@@ -105,10 +135,25 @@ private SqlSource createSqlSource(Object parameterObject) {
105135
}
106136
}
107137

138+
private Object[] extractProviderMethodArguments(Object parameterObject) {
139+
if (providerContext != null) {
140+
Object[] args = new Object[2];
141+
args[providerContextIndex == 0 ? 1 : 0] = parameterObject;
142+
args[providerContextIndex] = providerContext;
143+
return args;
144+
} else {
145+
return new Object[] { parameterObject };
146+
}
147+
}
148+
108149
private Object[] extractProviderMethodArguments(Map<String, Object> params, String[] argumentNames) {
109150
Object[] args = new Object[argumentNames.length];
110151
for (int i = 0; i < args.length; i++) {
111-
args[i] = params.get(argumentNames[i]);
152+
if (providerContextIndex != null && providerContextIndex == i) {
153+
args[i] = providerContext;
154+
} else {
155+
args[i] = params.get(argumentNames[i]);
156+
}
112157
}
113158
return args;
114159
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2009-2017 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+
* http://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.apache.ibatis.submitted.sqlprovider;
17+
18+
import org.apache.ibatis.annotations.Param;
19+
import org.apache.ibatis.annotations.SelectProvider;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
import java.util.List;
26+
27+
public interface BaseMapper<T> {
28+
29+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdProviderContextOnly")
30+
@ContainsLogicalDelete
31+
T selectById(Integer id);
32+
33+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdProviderContextOnly")
34+
T selectActiveById(Integer id);
35+
36+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByNameOneParamAndProviderContext")
37+
@ContainsLogicalDelete
38+
List<T> selectByName(String name);
39+
40+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByNameOneParamAndProviderContext")
41+
List<T> selectActiveByName(String name);
42+
43+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdAndNameMultipleParamAndProviderContextWithAtParam")
44+
@ContainsLogicalDelete
45+
List<T> selectByIdAndNameWithAtParam(@Param("id") Integer id, @Param("name") String name);
46+
47+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdAndNameMultipleParamAndProviderContextWithAtParam")
48+
List<T> selectActiveByIdAndNameWithAtParam(@Param("id") Integer id, @Param("name") String name);
49+
50+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdAndNameMultipleParamAndProviderContext")
51+
@ContainsLogicalDelete
52+
List<T> selectByIdAndName(Integer id, String name);
53+
54+
@SelectProvider(type= OurSqlBuilder.class, method= "buildSelectByIdAndNameMultipleParamAndProviderContext")
55+
List<T> selectActiveByIdAndName(Integer id, String name);
56+
57+
@Retention(RetentionPolicy.RUNTIME)
58+
@Target(ElementType.METHOD)
59+
@interface ContainsLogicalDelete {
60+
boolean value() default false;
61+
}
62+
63+
@Retention(RetentionPolicy.RUNTIME)
64+
@Target(ElementType.TYPE)
65+
@interface Meta {
66+
String tableName();
67+
}
68+
69+
}

src/test/java/org/apache/ibatis/submitted/sqlprovider/CreateDB.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--
2-
-- Copyright 2009-2016 the original author or authors.
2+
-- Copyright 2009-2017 the original author or authors.
33
--
44
-- Licensed under the Apache License, Version 2.0 (the "License");
55
-- you may not use this file except in compliance with the License.
@@ -18,11 +18,12 @@ drop table users if exists;
1818

1919
create table users (
2020
id int,
21-
name varchar(20)
21+
name varchar(20),
22+
logical_delete boolean default false
2223
);
2324

2425
insert into users (id, name) values(1, 'User1');
2526
insert into users (id, name) values(2, 'User2');
2627
insert into users (id, name) values(3, 'User3');
27-
insert into users (id, name) values(4, 'User4');
28+
insert into users (id, name, logical_delete) values(4, 'User4', true);
2829

src/test/java/org/apache/ibatis/submitted/sqlprovider/Mapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,8 @@
2424
import org.apache.ibatis.annotations.SelectProvider;
2525
import org.apache.ibatis.annotations.UpdateProvider;
2626

27-
public interface Mapper {
27+
@BaseMapper.Meta(tableName = "users")
28+
public interface Mapper extends BaseMapper<User> {
2829
@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersQuery")
2930
List<User> getUsers(List<Integer> allFilterIds);
3031

src/test/java/org/apache/ibatis/submitted/sqlprovider/OurSqlBuilder.java

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.apache.ibatis.submitted.sqlprovider;
1717

1818
import org.apache.ibatis.annotations.Param;
19+
import org.apache.ibatis.builder.annotation.ProviderContext;
1920
import org.apache.ibatis.jdbc.SQL;
2021

2122
import java.util.List;
@@ -148,4 +149,68 @@ public String buildDelete() {
148149
return "delete from users where id = #{id}";
149150
}
150151

152+
public String buildSelectByIdProviderContextOnly(ProviderContext context) {
153+
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
154+
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
155+
return new SQL(){{
156+
SELECT("*");
157+
FROM(tableName);
158+
WHERE("id = #{id}");
159+
if (!containsLogicalDelete){
160+
WHERE("logical_delete = false");
161+
}
162+
}}.toString();
163+
}
164+
165+
public String buildSelectByNameOneParamAndProviderContext(ProviderContext context, final String name) {
166+
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
167+
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
168+
return new SQL(){{
169+
SELECT("*");
170+
FROM(tableName);
171+
if (name != null) {
172+
WHERE("name like #{name} || '%'");
173+
}
174+
if (!containsLogicalDelete){
175+
WHERE("logical_delete = false");
176+
}
177+
}}.toString();
178+
}
179+
180+
public String buildSelectByIdAndNameMultipleParamAndProviderContextWithAtParam(@Param("id") final Integer id, ProviderContext context, @Param("name") final String name) {
181+
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
182+
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
183+
return new SQL(){{
184+
SELECT("*");
185+
FROM(tableName);
186+
if (id != null) {
187+
WHERE("id = #{id}");
188+
}
189+
if (name != null) {
190+
WHERE("name like #{name} || '%'");
191+
}
192+
if (!containsLogicalDelete){
193+
WHERE("logical_delete = false");
194+
}
195+
}}.toString();
196+
}
197+
198+
public String buildSelectByIdAndNameMultipleParamAndProviderContext(final Integer id, final String name, ProviderContext context) {
199+
final boolean containsLogicalDelete = context.getMapperMethod().getAnnotation(BaseMapper.ContainsLogicalDelete.class) != null;
200+
final String tableName = context.getMapperType().getAnnotation(BaseMapper.Meta.class).tableName();
201+
return new SQL(){{
202+
SELECT("*");
203+
FROM(tableName);
204+
if (id != null) {
205+
WHERE("id = #{param1}");
206+
}
207+
if (name != null) {
208+
WHERE("name like #{param2} || '%'");
209+
}
210+
if (!containsLogicalDelete){
211+
WHERE("logical_delete = false");
212+
}
213+
}}.toString();
214+
}
215+
151216
}

0 commit comments

Comments
 (0)