Skip to content
Closed
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
3 changes: 3 additions & 0 deletions src/main/java/org/apache/ibatis/annotations/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.mapping.StatementType;

Expand All @@ -43,4 +44,6 @@
String keyProperty() default "id";

String keyColumn() default "";

FetchType fetchType() default FetchType.DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.mapping.CacheBuilder;
import org.apache.ibatis.mapping.Discriminator;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMap;
import org.apache.ibatis.mapping.ParameterMapping;
Expand Down Expand Up @@ -287,8 +288,9 @@ public MappedStatement addMappedStatement(
String keyColumn,
String databaseId,
LanguageDriver lang,
String resultSets) {

String resultSets,
FetchType fetchType) {

if (unresolvedCacheRef) throw new IncompleteElementException("Cache-ref not yet resolved");

id = applyCurrentNamespace(id, false);
Expand All @@ -305,6 +307,7 @@ public MappedStatement addMappedStatement(
statementBuilder.lang(lang);
statementBuilder.resultOrdered(resultOrdered);
statementBuilder.resulSets(resultSets);
statementBuilder.fetchType(fetchType);
setStatementTimeout(timeout, statementBuilder);

setStatementParameterMap(parameterMap, parameterType, statementBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Discriminator;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultFlag;
import org.apache.ibatis.mapping.ResultMapping;
Expand Down Expand Up @@ -249,6 +250,7 @@ void parseStatement(Method method) {
ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
SqlCommandType sqlCommandType = getSqlCommandType(method);
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
FetchType fetchType = FetchType.DEFAULT;
boolean flushCache = !isSelect;
boolean useCache = isSelect;

Expand Down Expand Up @@ -281,6 +283,7 @@ void parseStatement(Method method) {
timeout = options.timeout() > -1 ? options.timeout() : null;
statementType = options.statementType();
resultSetType = options.resultSetType();
fetchType = options.fetchType();
}

String resultMapId = null;
Expand Down Expand Up @@ -317,7 +320,8 @@ void parseStatement(Method method) {
keyColumn,
null,
languageDriver,
null);
null,
fetchType);
}
}

Expand Down Expand Up @@ -553,7 +557,7 @@ private KeyGenerator handleSelectKeyAnnotation(SelectKey selectKeyAnnotation, St

assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum,
flushCache, useCache, false,
keyGenerator, keyProperty, null, null, languageDriver, null);
keyGenerator, keyProperty, null, null, languageDriver, null, FetchType.DEFAULT);

id = assistant.applyCurrentNamespace(id, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.mapping.SqlCommandType;
Expand Down Expand Up @@ -77,6 +78,7 @@ public void parseStatementNode() {
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
FetchType fetchType = FetchType.valueOf(context.getStringAttribute("fetchType", FetchType.DEFAULT.toString()));

// Include Fragments before parsing
XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
Expand Down Expand Up @@ -109,7 +111,7 @@ public void parseStatementNode() {
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets, fetchType);
}

public void parseSelectKeyNodes(String parentId, List<XNode> list, Class<?> parameterTypeClass, LanguageDriver langDriver, String skRequiredDatabaseId) {
Expand Down Expand Up @@ -146,7 +148,7 @@ public void parseSelectKeyNode(String id, XNode nodeToHandle, Class<?> parameter
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, null, databaseId, langDriver, null);
keyGenerator, keyProperty, null, databaseId, langDriver, null, FetchType.DEFAULT);

id = builderAssistant.applyCurrentNamespace(id, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ resultMap CDATA #IMPLIED
resultType CDATA #IMPLIED
resultSetType (FORWARD_ONLY | SCROLL_INSENSITIVE | SCROLL_SENSITIVE) #IMPLIED
statementType (STATEMENT|PREPARED|CALLABLE) #IMPLIED
fetchType (DEFAULT|CURSOR|LAZY) #IMPLIED
fetchSize CDATA #IMPLIED
timeout CDATA #IMPLIED
flushCache (true|false) #IMPLIED
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/apache/ibatis/executor/BatchExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
Expand Down Expand Up @@ -82,7 +83,9 @@ public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds
handler.parameterize(stmt);
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
if (ms.getFetchType() == FetchType.DEFAULT) {
closeStatement(stmt);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/apache/ibatis/executor/SimpleExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
Expand Down Expand Up @@ -56,7 +57,9 @@ public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBo
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
if (ms.getFetchType() == FetchType.DEFAULT) {
closeStatement(stmt);
}
}
}

Expand Down
182 changes: 182 additions & 0 deletions src/main/java/org/apache/ibatis/executor/resultset/CursorList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright 2009-2013 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.executor.resultset;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

/**
* @author Guillaume Darmont / guillaume@dropinocean.com
*/
public class CursorList<E> extends AbstractList<E> {

// ResultSetHandler stuff
private final DefaultResultSetHandler resultSetHandler;
private final ResultMap resultMap;
private final ResultSetWrapper rsw;
private final ResultMapping parentMapping;
private final ObjectWrapperResultHandler<E> objectWrapperResultHandler = new ObjectWrapperResultHandler<E>();

private boolean iteratorAlreadyOpened = false;

private boolean fetchStarted = false;

private boolean resultSetExhausted = false;

public CursorList(CursorResultSetHandler resultSetHandler, ResultSetWrapper rsw, ResultMap resultMap, ResultMapping parentMapping) {
this.resultSetHandler = resultSetHandler;
this.rsw = rsw;
this.resultMap = resultMap;
this.parentMapping = parentMapping;
}

@Override
public E get(int index) {
throw new UnsupportedOperationException("Cannot retrieve object at a specific index in CursorList. "
+ "Consider using Iterator to browse the list.");
}

@Override
public int size() {
throw new UnsupportedOperationException("Cannot retrieve size of a CursorList. "
+ "Consider using Iterator to browse the list.");
}

public boolean isResultSetExhausted() {
return resultSetExhausted;
}

public boolean isFetchStarted() {
return fetchStarted;
}

@Override
public Iterator<E> iterator() {
return new CursorIterator();
}

protected E fetchNextObjectFromDatabase() {
if (resultSetExhausted) return null;

try {
fetchStarted = true;
resultSetHandler.handleRowValues(rsw, resultMap, objectWrapperResultHandler, RowBounds.DEFAULT, parentMapping);
} catch (SQLException e) {
throw new RuntimeException(e);
}

E next = objectWrapperResultHandler.result;
if (next == null) {
closeResultSetAndStatement();
resultSetExhausted = true;
}
objectWrapperResultHandler.result = null;

return next;
}

public void closeResultSetAndStatement() {
ResultSet rs = rsw.getResultSet();
try {
if (rs != null) {
Statement statement = rs.getStatement();

rs.close();
if (statement != null) {
statement.close();
}
}
} catch (SQLException e) {
// ignore
}
}

/**
* This toString returns Object's toString default implementation since we don't want AbstractCollection#toString()
* to iterate on collection.
*
* @return a string representation of the object.
*/
@Override
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(this));
}

private static class ObjectWrapperResultHandler<E> implements ResultHandler {

private E result;

@Override
public void handleResult(ResultContext context) {
this.result = (E) context.getResultObject();
}
}

private class CursorIterator implements Iterator<E> {

/**
* Holder for the next objet to be returned
*/
E object;

public CursorIterator() {
if (iteratorAlreadyOpened) {
throw new IllegalStateException("Cannot open more than one iterator on a CursorList. "
+ "Use LazyList if you want to iterate more than one time.");
}
iteratorAlreadyOpened = true;
}

@Override
public boolean hasNext() {
if (object == null) {
object = fetchNextObjectFromDatabase();
}
return object != null;
}

@Override
public E next() {
// Fill next with object fetched from hasNext()
E next = object;

if (next == null) {
next = fetchNextObjectFromDatabase();
}

if (next != null) {
object = null;
return next;
}
throw new NoSuchElementException();
}

@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove element from CursorList");
}
}
}
Loading