Skip to content

Commit

Permalink
Fixes mybatis#273 An extra call to ResultHandler#handleResult is made…
Browse files Browse the repository at this point in the history
… for a nested result map.
  • Loading branch information
harawata committed Sep 29, 2014
1 parent 8638f0e commit fc8c6d2
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap r
throws SQLException {
DefaultResultContext resultContext = new DefaultResultContext();
skipRows(rsw.getResultSet(), rowBounds);
while (shouldProcessMoreRows(rsw.getResultSet(), resultContext, rowBounds)) {
while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
Object rowValue = getRowValue(rsw, discriminatedResultMap);
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
Expand All @@ -309,8 +309,8 @@ private void callResultHandler(ResultHandler resultHandler, DefaultResultContext
resultHandler.handleResult(resultContext);
}

private boolean shouldProcessMoreRows(ResultSet rs, ResultContext context, RowBounds rowBounds) throws SQLException {
return !context.isStopped() && rs.next() && context.getResultCount() < rowBounds.getLimit();
private boolean shouldProcessMoreRows(ResultContext context, RowBounds rowBounds) throws SQLException {
return !context.isStopped() && context.getResultCount() < rowBounds.getLimit();
}

private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
Expand Down Expand Up @@ -755,7 +755,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
final DefaultResultContext resultContext = new DefaultResultContext();
skipRows(rsw.getResultSet(), rowBounds);
Object rowValue = null;
while (shouldProcessMoreRows(rsw.getResultSet(), resultContext, rowBounds)) {
while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
final ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
final CacheKey rowKey = createRowKey(discriminatedResultMap, rsw, null);
Object partialObject = nestedResultObjects.get(rowKey);
Expand All @@ -773,7 +773,7 @@ private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap r
}
}
}
if (rowValue != null && mappedStatement.isResultOrdered()) {
if (rowValue != null && mappedStatement.isResultOrdered() && shouldProcessMoreRows(resultContext, rowBounds)) {
storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*-
* Copyright 2009-2014 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.nestedresulthandler_association;

import java.util.Date;

public class Account {
private String accountUuid;

private String accountName;

private Date birthDate;

private AccountAddress address;

public String getAccountUuid() {
return accountUuid;
}

public void setAccountUuid(String accountUuid) {
this.accountUuid = accountUuid;
}

public String getAccountName() {
return accountName;
}

public void setAccountName(String accountName) {
this.accountName = accountName;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

public AccountAddress getAddress() {
return address;
}

public void setAddress(AccountAddress address) {
this.address = address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-
* Copyright 2009-2014 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.nestedresulthandler_association;

public class AccountAddress {
private String accountUuid;

private String zipCode;

private String address;

public String getAccountUuid() {
return accountUuid;
}

public void setAccountUuid(String accountUuid) {
this.accountUuid = accountUuid;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2014 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.
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.apache.ibatis.submitted.nestedresulthandler_association.AccountRepository">
<resultMap id="resultMapAccount" type="org.apache.ibatis.submitted.nestedresulthandler_association.Account">
<id property="accountUuid" column="account_uuid" />
<result property="accountName" column="account_name" />
<result property="birthDate" column="birth_date" />
<association property="address" javaType="org.apache.ibatis.submitted.nestedresulthandler_association.AccountAddress">
<id property="accountUuid" column="account_uuid" />
<result property="zipCode" column="zip_code" />
<result property="address" column="address" />
</association>
</resultMap>
<select id="collectPageByBirthMonth" resultMap="resultMapAccount" resultOrdered="true">
<![CDATA[
SELECT
a.account_uuid
,a.account_name
,a.birth_date
,aa.zip_code
,aa.address
FROM
t_account a
LEFT OUTER JOIN
t_account_address aa
ON aa.account_uuid = a.account_uuid
WHERE
a.birth_date >= #{birthMonth} AND a.birth_date < ADD_MONTHS(#{birthMonth}, 1)
ORDER BY
a.account_uuid
]]>
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--
-- Copyright 2009-2014 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.
--

DROP TABLE t_account_address if exists;
DROP TABLE t_account if exists;
CREATE TABLE t_account(
account_uuid CHAR(36) PRIMARY KEY
,account_name VARCHAR(256)
,birth_date DATETIME
);
CREATE TABLE t_account_address(
account_uuid CHAR(36) PRIMARY KEY
,zip_code CHAR(7)
,address VARCHAR(256)
);

INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc01', 'Bob1', '2014-01-01 00:00:00.000');
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', 'Bob2', '2014-01-02 00:00:00.000');
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc03', 'Bob3', '2014-01-03 00:00:00.000');
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc04', 'Bob4', '2014-01-04 00:00:00.000');
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc05', 'Bob5', '2014-01-05 00:00:00.000');
INSERT INTO t_account (account_uuid,account_name,birth_date) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc11', 'Mark1', '2014-02-01 00:00:00.000');

INSERT INTO t_account_address (account_uuid,zip_code,address) VALUES ('4d3c8bdd-5379-4aeb-bc56-fcb01eb7cc02', '1710051', 'Tokyo Toshimaku Nagasaki');
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2009-2014 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.nestedresulthandler_association;

import static org.junit.Assert.*;

import java.io.Reader;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
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.BeforeClass;
import org.junit.Test;

public class NestedResultHandlerAssociationTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeClass
public static void setUp() throws Exception {
// create an SqlSessionFactory
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();

// populate in-memory database
SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nestedresulthandler_association/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);
reader.close();
session.close();
}

@Test
public void shouldHandleRowBounds() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date targetMonth = fmt.parse("2014-01-01");
final List<Account> accounts = new ArrayList<Account>();
try {
sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() {
@Override
public void handleResult(ResultContext context) {
Account account = (Account) context.getResultObject();
accounts.add(account);
}
});
} finally {
sqlSession.close();
}
assertEquals(2, accounts.size());
assertEquals("Bob2", accounts.get(0).getAccountName());
assertEquals("Bob3", accounts.get(1).getAccountName());
}

@Test
public void shouldHandleStop() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
final List<Account> accounts = new ArrayList<Account>();
try {
Date targetMonth = fmt.parse("2014-01-01");
sqlSession.select("collectPageByBirthMonth", targetMonth, new ResultHandler() {
@Override
public void handleResult(ResultContext context) {
Account account = (Account) context.getResultObject();
accounts.add(account);
if (accounts.size() > 1)
context.stop();
}
});
} finally {
sqlSession.close();
}
assertEquals(2, accounts.size());
assertEquals("Bob1", accounts.get(0).getAccountName());
assertEquals("Bob2", accounts.get(1).getAccountName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2014 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.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:nestedresulthandler_association" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="org/apache/ibatis/submitted/nestedresulthandler_association/AccountRepository.xml" />
</mappers>

</configuration>

0 comments on commit fc8c6d2

Please sign in to comment.