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

Add the @Options#timeoutString #2886

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Options.java
Expand Up @@ -96,9 +96,26 @@ enum FlushCachePolicy {
* Returns the statement timeout.
*
* @return the statement timeout
*
* @see #timeoutString()
*/
int timeout() default -1;

/**
* Returns the statement timeout string.
* <p>
* Can specify configuration's variables such as {@code ${timeout.select}}. If not resolve variable value, fallback
* the {@link #timeout()} value.
* </p>
*
* @return the statement timeout string
*
* @see #timeout()
*
* @since 3.5.14
*/
String timeoutString() default "";

/**
* Returns whether use the generated keys feature supported by JDBC 3.0
*
Expand Down
Expand Up @@ -33,6 +33,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -80,6 +81,7 @@
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.mapping.StatementType;
import org.apache.ibatis.parsing.GenericTokenParser;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.reflection.TypeParameterResolver;
import org.apache.ibatis.scripting.LanguageDriver;
Expand All @@ -101,6 +103,8 @@ public class MapperAnnotationBuilder {
InsertProvider.class, DeleteProvider.class)
.collect(Collectors.toSet());

private static final GenericTokenParser TOKEN_PARSER = new GenericTokenParser("${", "}", t -> t);

private final Configuration configuration;
private final MapperBuilderAssistant assistant;
private final Class<?> type;
Expand Down Expand Up @@ -345,7 +349,8 @@ void parseStatement(Method method) {
useCache = options.useCache();
// issue #348
fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null;
timeout = options.timeout() > -1 ? options.timeout() : null;
Integer fallbackTimeout = options.timeout() > -1 ? options.timeout() : null;
timeout = parseStringValue(options.timeoutString(), fallbackTimeout, Integer::parseInt);
statementType = options.statementType();
if (options.resultSetType() != ResultSetType.DEFAULT) {
resultSetType = options.resultSetType();
Expand All @@ -372,6 +377,19 @@ null, parameterTypeClass, resultMapId, getReturnType(method, type), resultSetTyp
});
}

private <T> T parseStringValue(String valueString, T fallbackValue, Function<String, T> valueTypeConverter) {
if (valueString.isEmpty()) {
return fallbackValue;
} else {
Properties variables = new Properties();
Optional.ofNullable(fallbackValue).map(String::valueOf)
.ifPresent(x -> variables.setProperty(TOKEN_PARSER.parse(valueString), x));
variables.putAll(configuration.getVariables());
return Optional.ofNullable(PropertyParser.parse(valueString, variables)).map(valueTypeConverter)
.orElse(fallbackValue);
}
}

private LanguageDriver getLanguageDriver(Method method) {
Lang lang = method.getAnnotation(Lang.class);
Class<? extends LanguageDriver> langClass = null;
Expand Down
2 changes: 1 addition & 1 deletion src/site/es/xdoc/java-api.xml
Expand Up @@ -418,7 +418,7 @@ void rollback(boolean force)</source>
<td>
Esta anotación proporciona acceso a un gran conjunto de opciones de configuración que normalmente aparecen como atributos en los mapped statements.
En lugar de complicar cada anotación existente la anotación Options proporciona una forma sencilla y concisa de acceder a estas opciones.
Atributos: useCache=true, flushCache=FlushCachePolicy.DEFAULT, resultSetType=DEFAULT, statementType=PREPARED, fetchSize=-1, timeout=-1, useGeneratedKeys=false, keyProperty=“”, keyColumn=“”, resultSets=“”, databaseId="".
Atributos: useCache=true, flushCache=FlushCachePolicy.DEFAULT, resultSetType=DEFAULT, statementType=PREPARED, fetchSize=-1, timeout=-1, timeoutString="", useGeneratedKeys=false, keyProperty=“”, keyColumn=“”, resultSets=“”, databaseId="".
Es importante comprender que las anotaciones en Java no permiten indicar un valor nulo.
Por lo tanto, cuando usas la anotación Options el statement usará todos los valores por defecto.
Presta atención a estos valores pro defecto para evitar comportamientos inesperados.
Expand Down
2 changes: 1 addition & 1 deletion src/site/ja/xdoc/java-api.xml
Expand Up @@ -430,7 +430,7 @@ void rollback(boolean force)</source>
<td>マップドステートメントの属性</td>
<td>このアノテーションを使うと、通常マップドステートメントの属性として指定される多様なスイッチや設定オプションにアクセスすることができます。<code>Options</code> アノテーションによって、各ステートメントのアノテーションを複雑化することなく、一貫したクリーンな方法で設定にアクセスできるよう工夫されています。キー: Attributes:
<code>useCache=true</code>, <code>flushCache=FlushCachePolicy.DEFAULT</code>, <code>resultSetType=DEFAULT</code>,
<code>statementType=PREPARED</code>, <code>fetchSize=-1</code>, <code>timeout=-1</code>,
<code>statementType=PREPARED</code>, <code>fetchSize=-1</code>, <code>timeout=-1</code>, <code>timeoutString=""</code>,
<code>useGeneratedKeys=false</code>, <code>keyProperty=""</code>, <code>keyColumn=""</code>, <code>resultSets=""</code>,
<code>databaseId=""</code>.
Java アノテーションを使う場合、値として <code>null</code> を指定することはできないという制限があります。これはどういうことかというと、<code>Options</code> アノテーションを付加したステートメントにはデフォルトのオプションが適用されるということです。予期しない動作を防ぐため、各オプションのデフォルト値を把握しておくようにしてください。
Expand Down
1 change: 1 addition & 0 deletions src/site/ko/xdoc/java-api.xml
Expand Up @@ -541,6 +541,7 @@ void rollback(boolean force)</source>
statementType=PREPARED,
fetchSize=-1,
timeout=-1,
timeoutString="",
useGeneratedKeys=false,
keyProperty=“”,
keyColumn=“”,
Expand Down