Skip to content
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

Support to assign single primitive parameter on SQL provider method #1604

Merged
merged 3 commits into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ProviderSqlSource implements SqlSource {
private final Configuration configuration;
private final Class<?> providerType;
private final LanguageDriver languageDriver;
private final Method mapperMethod;
private Method providerMethod;
private String[] providerMethodArgumentNames;
private Class<?>[] providerMethodParameterTypes;
Expand All @@ -59,6 +60,7 @@ public ProviderSqlSource(Configuration configuration, Object provider, Class<?>
String providerMethodName;
try {
this.configuration = configuration;
this.mapperMethod = mapperMethod;
Lang lang = mapperMethod == null ? null : mapperMethod.getAnnotation(Lang.class);
this.languageDriver = configuration.getLanguageDriver(lang == null ? null : lang.value());
this.providerType = getProviderType(provider, mapperMethod);
Expand Down Expand Up @@ -116,33 +118,45 @@ private SqlSource createSqlSource(Object parameterObject) {
try {
int bindParameterCount = providerMethodParameterTypes.length - (providerContext == null ? 0 : 1);
String sql;
if (providerMethodParameterTypes.length == 0) {
if (parameterObject instanceof Map) {
if (bindParameterCount == 1 && providerMethodParameterTypes[0] == Map.class) {
sql = invokeProviderMethod(extractProviderMethodArguments(parameterObject));
} else {
@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) parameterObject;
sql = invokeProviderMethod(extractProviderMethodArguments(params, providerMethodArgumentNames));
}
} else if (providerMethodParameterTypes.length == 0) {
sql = invokeProviderMethod();
} else if (bindParameterCount == 0) {
sql = invokeProviderMethod(providerContext);
} else if (bindParameterCount == 1
&& (parameterObject == null || providerMethodParameterTypes[providerContextIndex == null || providerContextIndex == 1 ? 0 : 1].isAssignableFrom(parameterObject.getClass()))) {
} else if (providerMethodParameterTypes.length == 1) {
if (providerContext == null) {
sql = invokeProviderMethod(parameterObject);
} else {
sql = invokeProviderMethod(providerContext);
}
} else if (providerMethodParameterTypes.length == 2) {
sql = invokeProviderMethod(extractProviderMethodArguments(parameterObject));
} else if (parameterObject instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> params = (Map<String, Object>) parameterObject;
sql = invokeProviderMethod(extractProviderMethodArguments(params, providerMethodArgumentNames));
} else {
throw new BuilderException("Error invoking SqlProvider method ("
+ providerType.getName() + "." + providerMethod.getName()
+ "). Cannot invoke a method that holds "
+ (bindParameterCount == 1 ? "named argument(@Param)" : "multiple arguments")
+ " using a specifying parameterObject. In this case, please specify a 'java.util.Map' object.");
throw new BuilderException("Cannot invoke SqlProvider method '" + providerMethod
+ "' with specify parameter '" + (parameterObject == null ? null : parameterObject.getClass())
+ "' because SqlProvider method arguments for '" + mapperMethod + "' is an invalid combination.");
harawata marked this conversation as resolved.
Show resolved Hide resolved
}
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
return languageDriver.createSqlSource(configuration, sql, parameterType);
} catch (BuilderException e) {
throw e;
} catch (Exception e) {
throw new BuilderException("Error invoking SqlProvider method ("
+ providerType.getName() + "." + providerMethod.getName()
+ "). Cause: " + e, e);
throw new BuilderException("Error invoking SqlProvider method '" + providerMethod
+ "' with specify parameter '" + (parameterObject == null ? null : parameterObject.getClass()) + "'. Cause: " + extractRootCause(e), e);
harawata marked this conversation as resolved.
Show resolved Hide resolved
}
}

private Throwable extractRootCause(Exception e) {
Throwable cause = e;
while(cause.getCause() != null) {
cause = e.getCause();
}
return cause;
}

private Object[] extractProviderMethodArguments(Object parameterObject) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-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.
Expand Down Expand Up @@ -41,6 +41,9 @@ public interface Mapper extends BaseMapper<User> {
@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersByCriteriaMapQuery")
List<User> getUsersByCriteriaMap(Map<String, Object> criteria);

@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersByCriteriaMapWithParamQuery")
List<User> getUsersByCriteriaMapWithParam(Map<String, Object> criteria);

@SelectProvider(type = OurSqlBuilder.class, method = "buildGetUsersByNameQuery")
List<User> getUsersByName(String name, String orderByColumn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ public String buildGetUsersByCriteriaMapQuery(final Map<String, Object> criteria
}}.toString();
}

public String buildGetUsersByCriteriaMapWithParamQuery(@Param("id") Integer id, @Param("name") String name) {
return new SQL() {{
SELECT("*");
FROM("users");
if (id != null) {
WHERE("id = #{id}");
}
if (name != null) {
WHERE("name like #{name} || '%'");
}
}}.toString();
}

public String buildGetUsersByNameQuery(final String name, final String orderByColumn) {
return new SQL(){{
SELECT("*");
Expand Down
Loading