Skip to content

Commit

Permalink
Merge 6dd9c12 into c060395
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Jun 11, 2023
2 parents c060395 + 6dd9c12 commit a860fb8
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 25 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Options.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ void rollback(boolean force)</source>
statementType=PREPARED,
fetchSize=-1,
timeout=-1,
timeoutString="",
useGeneratedKeys=false,
keyProperty=“”,
keyColumn=“”,
Expand Down
Loading

0 comments on commit a860fb8

Please sign in to comment.