org.apache.ibatis.executor.result.DefaultResultContext Java Examples

The following examples show how to use org.apache.ibatis.executor.result.DefaultResultContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RollupResultHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Client code <b>must</b> call this method once the query returns so that the final results
 * can be passed to the inner RowHandler.  If a query is limited by size, then it is
 * possible that the unprocessed results represent an incomplete final object; in this case
 * it would be best to ignore the last results.  If the query is complete (i.e. all results
 * are returned) then this method should be called.
 * <p>
 * If you want X results and each result is made up of N rows (on average), then set the query
 * limit to: <br/>
 *   L = X * (N+1)<br/>
 * and don't call this method.
 */
public void processLastResults()
{
    // Shortcut if we have processed enough results
    if (maxResults > 0 && resultCount >= maxResults)
    {
        return;
    }
    // Handle any outstanding results
    Object resultObject = coalesceResults(configuration, rawResults, collectionProperty);
    if (resultObject != null)
    {
        DefaultResultContext resultContext = new DefaultResultContext();
        resultContext.nextResultObject(resultObject);
        
        resultHandler.handleResult(resultContext);
        resultCount++;
        rawResults.clear();                         // Stop it from being used again
    }
}
 
Example #2
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
  final DefaultResultContext resultContext = new DefaultResultContext();
  skipRows(rsw.getResultSet(), rowBounds);
  Object rowValue = null;
  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);
    // issue #577 && #542
    if (mappedStatement.isResultOrdered()) {
      if (partialObject == null && rowValue != null) {
        nestedResultObjects.clear();
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
      }
      rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
    } else {
      rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
      if (partialObject == null) {
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
      }
    }
  }
  if (rowValue != null && mappedStatement.isResultOrdered() && shouldProcessMoreRows(resultContext, rowBounds)) {
    storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
  }
}
 
Example #3
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
  final DefaultResultContext resultContext = new DefaultResultContext();
  skipRows(rsw.getResultSet(), rowBounds);
  Object rowValue = null;
  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);
    // issue #577 && #542
    if (mappedStatement.isResultOrdered()) {
      if (partialObject == null && rowValue != null) {
        nestedResultObjects.clear();
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
      }
      rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
    } else {
      rowValue = getRowValue(rsw, discriminatedResultMap, rowKey, rowKey, null, partialObject);
      if (partialObject == null) {
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
      }
    }
  }
  if (rowValue != null && mappedStatement.isResultOrdered() && shouldProcessMoreRows(resultContext, rowBounds)) {
    storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
  }
}
 
Example #4
Source File: RollupResultHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void handleResult(ResultContext context)
{
    // Shortcut if we have processed enough results
    if (maxResults > 0 && resultCount >= maxResults)
    {
        return;
    }
    
    Object valueObject = context.getResultObject();
    MetaObject probe = configuration.newMetaObject(valueObject);
    
    // Check if the key has changed
    if (lastKeyValues == null)
    {
        lastKeyValues = getKeyValues(probe);
        resultCount = 0;
    }
    // Check if it has changed
    Object[] currentKeyValues = getKeyValues(probe);
    if (!Arrays.deepEquals(lastKeyValues, currentKeyValues))
    {
        // Key has changed, so handle the results
        Object resultObject = coalesceResults(configuration, rawResults, collectionProperty);
        if (resultObject != null)
        {
            DefaultResultContext resultContext = new DefaultResultContext();
            resultContext.nextResultObject(resultObject);
            
            resultHandler.handleResult(resultContext);
            resultCount++;
        }
        rawResults.clear();
        lastKeyValues = currentKeyValues;
    }
    // Add the new value to the results for next time
    rawResults.add(valueObject);
    // Done
}
 
Example #5
Source File: DefaultSqlSession.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
  //转而去调用selectList
  final List<?> list = selectList(statement, parameter, rowBounds);
  final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<K, V>(mapKey,
      configuration.getObjectFactory(), configuration.getObjectWrapperFactory());
  final DefaultResultContext context = new DefaultResultContext();
  for (Object o : list) {
    //循环用DefaultMapResultHandler处理每条记录
    context.nextResultObject(o);
    mapResultHandler.handleResult(context);
  }
  //注意这个DefaultMapResultHandler里面存了所有已处理的记录(内部实现可能就是一个Map),最后再返回一个Map
  return mapResultHandler.getMappedResults();
}
 
Example #6
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
    throws SQLException {
  DefaultResultContext resultContext = new DefaultResultContext();
  skipRows(rsw.getResultSet(), 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());
  }
}
 
Example #7
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void storeObject(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue, ResultMapping parentMapping, ResultSet rs) throws SQLException {
  if (parentMapping != null) {
    linkToParents(rs, parentMapping, rowValue);
  } else {
    callResultHandler(resultHandler, resultContext, rowValue);
  }
}
 
Example #8
Source File: DefaultSqlSession.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
  //转而去调用selectList
  final List<?> list = selectList(statement, parameter, rowBounds);
  final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<K, V>(mapKey,
      configuration.getObjectFactory(), configuration.getObjectWrapperFactory());
  final DefaultResultContext context = new DefaultResultContext();
  for (Object o : list) {
    //循环用DefaultMapResultHandler处理每条记录
    context.nextResultObject(o);
    mapResultHandler.handleResult(context);
  }
  //注意这个DefaultMapResultHandler里面存了所有已处理的记录(内部实现可能就是一个Map),最后再返回一个Map
  return mapResultHandler.getMappedResults();
}
 
Example #9
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
    throws SQLException {
  DefaultResultContext resultContext = new DefaultResultContext();
  skipRows(rsw.getResultSet(), 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());
  }
}
 
Example #10
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void storeObject(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue, ResultMapping parentMapping, ResultSet rs) throws SQLException {
  if (parentMapping != null) {
    linkToParents(rs, parentMapping, rowValue);
  } else {
    callResultHandler(resultHandler, resultContext, rowValue);
  }
}
 
Example #11
Source File: NodeDAOImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void selectChildAssocs(
        Long parentNodeId,
        QName assocTypeQName,
        QName assocQName,
        final int maxResults,
        ChildAssocRefQueryCallback resultsCallback)
{
    ChildAssocEntity assoc = new ChildAssocEntity();
    // Parent
    NodeEntity parentNode = new NodeEntity();
    parentNode.setId(parentNodeId);
    assoc.setParentNode(parentNode);

    // Type QName
    if (assocTypeQName != null)
    {
        if (!assoc.setTypeQNameAll(qnameDAO, assocTypeQName, false))
        {
            resultsCallback.done();
            return;                 // Shortcut
        }
    }
    // QName
    if (assocQName != null)
    {
        if (!assoc.setQNameAll(qnameDAO, assocQName, false))
        {
            resultsCallback.done();
            return;                 // Shortcut
        }
    }
    // Order
    assoc.setOrdered(resultsCallback.orderResults());

    ChildAssocResultHandler resultHandler = new ChildAssocResultHandler(resultsCallback);
    
    RowBounds rowBounds = new RowBounds(0, maxResults);
    List<?> entities = template.selectList(SELECT_CHILD_ASSOCS_OF_PARENT_LIMITED, assoc, rowBounds);
    @SuppressWarnings("rawtypes")
    final DefaultResultContext resultContext = new DefaultResultContext();
    for (Object entity : entities)
    {
          resultContext.nextResultObject(entity);
          resultHandler.handleResult(resultContext);
    }
    
    resultsCallback.done();
}
 
Example #12
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 4 votes vote down vote up
private void callResultHandler(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue) {
  resultContext.nextResultObject(rowValue);
  resultHandler.handleResult(resultContext);
}
 
Example #13
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 4 votes vote down vote up
private void callResultHandler(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue) {
  resultContext.nextResultObject(rowValue);
  resultHandler.handleResult(resultContext);
}