From fdf7a9ed402f0f4626510bece72d6c795d5789fa Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Wed, 5 Sep 2018 09:27:25 +0900 Subject: [PATCH 1/3] Change the default resultSetType to DEFAULT(same as unset) at annotation option Fixes #1334 --- .../apache/ibatis/annotations/Options.java | 2 +- .../annotation/MapperAnnotationBuilder.java | 2 +- .../ibatis/builder/xml/mybatis-3-mapper.dtd | 2 +- .../statement/CallableStatementHandler.java | 9 +- .../statement/PreparedStatementHandler.java | 5 +- .../statement/SimpleStatementHandler.java | 5 +- .../apache/ibatis/mapping/ResultSetType.java | 8 +- .../apache/ibatis/reflection/MetaObject.java | 2 +- src/site/es/xdoc/java-api.xml | 2 +- src/site/es/xdoc/sqlmap-xml.xml | 4 +- src/site/ja/xdoc/java-api.xml | 2 +- src/site/ja/xdoc/sqlmap-xml.xml | 4 +- src/site/ko/xdoc/java-api.xml | 2 +- src/site/ko/xdoc/sqlmap-xml.xml | 2 +- src/site/xdoc/java-api.xml | 2 +- src/site/xdoc/sqlmap-xml.xml | 2 +- src/site/zh/xdoc/java-api.xml | 2 +- src/site/zh/xdoc/sqlmap-xml.xml | 4 +- .../submitted/result_set_type/CreateDB.sql | 57 ++++++++++++ .../submitted/result_set_type/Mapper.java | 42 +++++++++ .../submitted/result_set_type/Mapper.xml | 88 +++++++++++++++++++ .../result_set_type/ResultSetTypeTest.java | 87 ++++++++++++++++++ .../submitted/result_set_type/User.java | 38 ++++++++ .../result_set_type/mybatis-config.xml | 42 +++++++++ 24 files changed, 387 insertions(+), 28 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/CreateDB.sql create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.java create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.xml create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/ResultSetTypeTest.java create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/User.java create mode 100644 src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml diff --git a/src/main/java/org/apache/ibatis/annotations/Options.java b/src/main/java/org/apache/ibatis/annotations/Options.java index 053dbc6c33d..f10f8572f5a 100644 --- a/src/main/java/org/apache/ibatis/annotations/Options.java +++ b/src/main/java/org/apache/ibatis/annotations/Options.java @@ -48,7 +48,7 @@ public enum FlushCachePolicy { FlushCachePolicy flushCache() default FlushCachePolicy.DEFAULT; - ResultSetType resultSetType() default ResultSetType.FORWARD_ONLY; + ResultSetType resultSetType() default ResultSetType.DEFAULT; StatementType statementType() default StatementType.PREPARED; diff --git a/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java b/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java index a9a1d24c350..39731047e71 100644 --- a/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java @@ -297,7 +297,7 @@ void parseStatement(Method method) { Integer fetchSize = null; Integer timeout = null; StatementType statementType = StatementType.PREPARED; - ResultSetType resultSetType = ResultSetType.FORWARD_ONLY; + ResultSetType resultSetType = ResultSetType.DEFAULT; SqlCommandType sqlCommandType = getSqlCommandType(method); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; boolean flushCache = !isSelect; diff --git a/src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd b/src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd index 0f9894bd0b7..242d4f30d35 100755 --- a/src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd +++ b/src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd @@ -174,7 +174,7 @@ parameterMap CDATA #IMPLIED parameterType CDATA #IMPLIED resultMap CDATA #IMPLIED resultType CDATA #IMPLIED -resultSetType (FORWARD_ONLY | SCROLL_INSENSITIVE | SCROLL_SENSITIVE) #IMPLIED +resultSetType (FORWARD_ONLY | SCROLL_INSENSITIVE | SCROLL_SENSITIVE | DEFAULT) #IMPLIED statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED fetchSize CDATA #IMPLIED timeout CDATA #IMPLIED diff --git a/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java index 5a70aada5ff..318b974d875 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -26,10 +26,7 @@ import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.executor.keygen.KeyGenerator; -import org.apache.ibatis.mapping.BoundSql; -import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.ParameterMapping; -import org.apache.ibatis.mapping.ParameterMode; +import org.apache.ibatis.mapping.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.JdbcType; @@ -82,7 +79,7 @@ public Cursor queryCursor(Statement statement) throws SQLException { @Override protected Statement instantiateStatement(Connection connection) throws SQLException { String sql = boundSql.getSql(); - if (mappedStatement.getResultSetType() != null) { + if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { return connection.prepareCall(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } else { return connection.prepareCall(sql); diff --git a/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java index de3e29bd9db..1d7eaa10281 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -28,6 +28,7 @@ import org.apache.ibatis.executor.keygen.KeyGenerator; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -81,7 +82,7 @@ protected Statement instantiateStatement(Connection connection) throws SQLExcept } else { return connection.prepareStatement(sql, keyColumnNames); } - } else if (mappedStatement.getResultSetType() != null) { + } else if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } else { return connection.prepareStatement(sql); diff --git a/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java index fdadba8b410..bbb6fe16589 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2015 the original author or authors. + * Copyright 2009-2018 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. @@ -28,6 +28,7 @@ import org.apache.ibatis.executor.keygen.SelectKeyGenerator; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -83,7 +84,7 @@ public Cursor queryCursor(Statement statement) throws SQLException { @Override protected Statement instantiateStatement(Connection connection) throws SQLException { - if (mappedStatement.getResultSetType() != null) { + if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } else { return connection.createStatement(); diff --git a/src/main/java/org/apache/ibatis/mapping/ResultSetType.java b/src/main/java/org/apache/ibatis/mapping/ResultSetType.java index 7d60b88bf0b..001344edd29 100644 --- a/src/main/java/org/apache/ibatis/mapping/ResultSetType.java +++ b/src/main/java/org/apache/ibatis/mapping/ResultSetType.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. @@ -21,6 +21,12 @@ * @author Clinton Begin */ public enum ResultSetType { + /** + * behavior with same as unset (driver dependent). + * + * @since 3.5.0 + */ + DEFAULT(-1), FORWARD_ONLY(ResultSet.TYPE_FORWARD_ONLY), SCROLL_INSENSITIVE(ResultSet.TYPE_SCROLL_INSENSITIVE), SCROLL_SENSITIVE(ResultSet.TYPE_SCROLL_SENSITIVE); diff --git a/src/main/java/org/apache/ibatis/reflection/MetaObject.java b/src/main/java/org/apache/ibatis/reflection/MetaObject.java index 4f5ae2e39e3..d38c3052914 100644 --- a/src/main/java/org/apache/ibatis/reflection/MetaObject.java +++ b/src/main/java/org/apache/ibatis/reflection/MetaObject.java @@ -1,5 +1,5 @@ /** - * Copyright 2009-2017 the original author or authors. + * Copyright 2009-2018 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. diff --git a/src/site/es/xdoc/java-api.xml b/src/site/es/xdoc/java-api.xml index 7910b51da5e..3e4d31432a9 100644 --- a/src/site/es/xdoc/java-api.xml +++ b/src/site/es/xdoc/java-api.xml @@ -422,7 +422,7 @@ try (SqlSession session = sqlSessionFactory.openSession()) { @Options Method Attributes of mapped statements. - 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=FORWARD_ONLY, statementType=PREPARED, fetchSize=-1, timeout=-1, useGeneratedKeys=false, keyProperty=“”, keyColumn=“”, resultSets=“”. 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. La keyColumn solo se requiere para algunas bases de datos (como PostgreSQL) cuando la columna no es la primera columna de la tabla. + 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=“”. 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. La keyColumn solo se requiere para algunas bases de datos (como PostgreSQL) cuando la columna no es la primera columna de la tabla. diff --git a/src/site/es/xdoc/sqlmap-xml.xml b/src/site/es/xdoc/sqlmap-xml.xml index 56ae5ae279b..09299c6f06c 100644 --- a/src/site/es/xdoc/sqlmap-xml.xml +++ b/src/site/es/xdoc/sqlmap-xml.xml @@ -1,7 +1,7 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/ResultSetTypeTest.java b/src/test/java/org/apache/ibatis/submitted/result_set_type/ResultSetTypeTest.java new file mode 100644 index 00000000000..d3fc237903d --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/ResultSetTypeTest.java @@ -0,0 +1,87 @@ +/** + * Copyright 2009-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.result_set_type; + +import org.apache.ibatis.BaseDataTest; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.jdbc.ScriptRunner; +import org.apache.ibatis.session.RowBounds; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.Reader; +import java.util.List; +import java.util.function.Function; + +public class ResultSetTypeTest { + + private static SqlSessionFactory sqlSessionFactory; + + @BeforeClass + public static void setUp() throws Exception { + // create an SqlSessionFactory + try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/result_set_type/mybatis-config.xml")) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); + } + + // populate in-memory database + ScriptRunner runner = new ScriptRunner(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection()); + runner.setDelimiter("go"); + runner.setLogWriter(null); + runner.setErrorLogWriter(null); + BaseDataTest.runScript(runner, "org/apache/ibatis/submitted/result_set_type/CreateDB.sql"); + } + + @Test + public void testWithStatement() { + test(mapper -> mapper.getUserWithStatementAndUnset(new RowBounds(5, 3)), 0); + test(mapper -> mapper.getUserWithStatementAndDefault(new RowBounds(4, 3)), 1); + test(mapper -> mapper.getUserWithStatementAndForwardOnly(new RowBounds(3, 3)), 2); + test(mapper -> mapper.getUserWithStatementAndScrollInsensitive(new RowBounds(2, 2)), 2); + test(mapper -> mapper.getUserWithStatementAndScrollSensitive(new RowBounds(1, 1)), 1); + } + + @Test + public void testWithPrepared() { + test(mapper -> mapper.getUserWithPreparedAndUnset(new RowBounds(5, 3)), 0); + test(mapper -> mapper.getUserWithPreparedAndDefault(new RowBounds(4, 3)), 1); + test(mapper -> mapper.getUserWithPreparedAndForwardOnly(new RowBounds(3, 3)), 2); + test(mapper -> mapper.getUserWithPreparedAndScrollInsensitive(new RowBounds(2, 2)), 2); + test(mapper -> mapper.getUserWithPreparedAndScrollSensitive(new RowBounds(1, 1)), 1); + } + + @Test + public void testWithCallable() { + test(mapper -> mapper.getUserWithCallableAndUnset(new RowBounds(5, 3)), 0); + test(mapper -> mapper.getUserWithCallableAndDefault(new RowBounds(4, 3)), 1); + test(mapper -> mapper.getUserWithCallableAndForwardOnly(new RowBounds(3, 3)), 2); + test(mapper -> mapper.getUserWithCallableAndScrollInsensitive(new RowBounds(2, 2)), 2); + test(mapper -> mapper.getUserWithCallableAndScrollSensitive(new RowBounds(1, 1)), 1); + } + + private void test(Function> usersSupplier, int expectedSize) { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Mapper mapper = sqlSession.getMapper(Mapper.class); + List users = usersSupplier.apply(mapper); + Assert.assertEquals(expectedSize, users.size()); + } + } + +} diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/User.java b/src/test/java/org/apache/ibatis/submitted/result_set_type/User.java new file mode 100644 index 00000000000..dfcc0526aa2 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/User.java @@ -0,0 +1,38 @@ +/** + * Copyright 2009-2018 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ibatis.submitted.result_set_type; + +public class User { + + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml new file mode 100644 index 00000000000..59727ab3cb8 --- /dev/null +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + From 788236f177fc9c6fbbe14810d6e546ceec8760d4 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Thu, 6 Sep 2018 15:04:49 +0900 Subject: [PATCH 2/3] Polishing #1344 --- .../builder/annotation/MapperAnnotationBuilder.java | 2 +- .../executor/statement/CallableStatementHandler.java | 12 ++++++++---- .../executor/statement/PreparedStatementHandler.java | 6 +++--- .../executor/statement/SimpleStatementHandler.java | 6 +++--- .../org/apache/ibatis/mapping/MappedStatement.java | 3 ++- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java b/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java index 39731047e71..94b6806ab2f 100644 --- a/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java +++ b/src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java @@ -297,7 +297,7 @@ void parseStatement(Method method) { Integer fetchSize = null; Integer timeout = null; StatementType statementType = StatementType.PREPARED; - ResultSetType resultSetType = ResultSetType.DEFAULT; + ResultSetType resultSetType = null; SqlCommandType sqlCommandType = getSqlCommandType(method); boolean isSelect = sqlCommandType == SqlCommandType.SELECT; boolean flushCache = !isSelect; diff --git a/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java index 318b974d875..859ed7e2342 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/CallableStatementHandler.java @@ -26,7 +26,11 @@ import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.executor.keygen.KeyGenerator; -import org.apache.ibatis.mapping.*; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ParameterMapping; +import org.apache.ibatis.mapping.ParameterMode; +import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.JdbcType; @@ -79,10 +83,10 @@ public Cursor queryCursor(Statement statement) throws SQLException { @Override protected Statement instantiateStatement(Connection connection) throws SQLException { String sql = boundSql.getSql(); - if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { - return connection.prepareCall(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); - } else { + if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { return connection.prepareCall(sql); + } else { + return connection.prepareCall(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } } diff --git a/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java index 1d7eaa10281..f7ce1b9b685 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/PreparedStatementHandler.java @@ -82,10 +82,10 @@ protected Statement instantiateStatement(Connection connection) throws SQLExcept } else { return connection.prepareStatement(sql, keyColumnNames); } - } else if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { - return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); - } else { + } else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { return connection.prepareStatement(sql); + } else { + return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } } diff --git a/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java b/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java index bbb6fe16589..6cecec4838a 100644 --- a/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java +++ b/src/main/java/org/apache/ibatis/executor/statement/SimpleStatementHandler.java @@ -84,10 +84,10 @@ public Cursor queryCursor(Statement statement) throws SQLException { @Override protected Statement instantiateStatement(Connection connection) throws SQLException { - if (mappedStatement.getResultSetType() != null && mappedStatement.getResultSetType() != ResultSetType.DEFAULT) { - return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); - } else { + if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) { return connection.createStatement(); + } else { + return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY); } } diff --git a/src/main/java/org/apache/ibatis/mapping/MappedStatement.java b/src/main/java/org/apache/ibatis/mapping/MappedStatement.java index b999f8962db..05e6a4b258a 100644 --- a/src/main/java/org/apache/ibatis/mapping/MappedStatement.java +++ b/src/main/java/org/apache/ibatis/mapping/MappedStatement.java @@ -69,6 +69,7 @@ public Builder(Configuration configuration, String id, SqlSource sqlSource, SqlC mappedStatement.id = id; mappedStatement.sqlSource = sqlSource; mappedStatement.statementType = StatementType.PREPARED; + mappedStatement.resultSetType = ResultSetType.DEFAULT; mappedStatement.parameterMap = new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<>()).build(); mappedStatement.resultMaps = new ArrayList<>(); mappedStatement.sqlCommandType = sqlCommandType; @@ -119,7 +120,7 @@ public Builder statementType(StatementType statementType) { } public Builder resultSetType(ResultSetType resultSetType) { - mappedStatement.resultSetType = resultSetType; + mappedStatement.resultSetType = resultSetType == null ? ResultSetType.DEFAULT : resultSetType; return this; } From af0016aa06592317003ce65f0c4b2f43d466dd86 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Thu, 6 Sep 2018 22:32:23 +0900 Subject: [PATCH 3/3] Polishing #1334 --- .../submitted/result_set_type/CreateDB.sql | 23 +++++++------- .../submitted/result_set_type/Mapper.xml | 30 +++++++++---------- .../result_set_type/mybatis-config.xml | 2 +- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/CreateDB.sql b/src/test/java/org/apache/ibatis/submitted/result_set_type/CreateDB.sql index 7e31194e01a..eccf9e12f2a 100644 --- a/src/test/java/org/apache/ibatis/submitted/result_set_type/CreateDB.sql +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/CreateDB.sql @@ -14,43 +14,40 @@ -- limitations under the License. -- -drop table resultsettype.users if exists +drop table users if exists go -drop procedure resultsettype.getusers if exists +drop procedure getusers if exists go -create schema resultsettype -go - -create table resultsettype.users ( +create table users ( id int, name varchar(20) ) go -create procedure resultsettype.getusers() +create procedure getusers() modifies sql data dynamic result sets 1 BEGIN ATOMIC - declare cur cursor for select * from resultsettype.users order by id; + declare cur cursor for select * from users order by id; open cur; END go -insert into resultsettype.users (id, name) values(1, 'User1') +insert into users (id, name) values(1, 'User1') go -insert into resultsettype.users (id, name) values(2, 'User1') +insert into users (id, name) values(2, 'User1') go -insert into resultsettype.users (id, name) values(3, 'User1') +insert into users (id, name) values(3, 'User1') go -insert into resultsettype.users (id, name) values(4, 'User1') +insert into users (id, name) values(4, 'User1') go -insert into resultsettype.users (id, name) values(5, 'User1') +insert into users (id, name) values(5, 'User1') go diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.xml b/src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.xml index 58ddfe75992..841356a95d4 100644 --- a/src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.xml +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/Mapper.xml @@ -24,65 +24,65 @@ diff --git a/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml b/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml index 59727ab3cb8..b1570f1ba47 100644 --- a/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml +++ b/src/test/java/org/apache/ibatis/submitted/result_set_type/mybatis-config.xml @@ -29,7 +29,7 @@ - +