Search in sources :

Example 1 with ResultSetMapper

use of jodd.db.oom.mapper.ResultSetMapper in project jodd by oblac.

the class DbOomQuery method find.

protected <T> T find(Class[] types, boolean close, ResultSet resultSet) {
    if (resultSet == null) {
        resultSet = execute();
    }
    ResultSetMapper rsm = createResultSetMapper(resultSet);
    Iterator<T> iterator = new DbListIterator<>(this, types, rsm, false);
    T result = null;
    if (iterator.hasNext()) {
        result = iterator.next();
    }
    close(rsm, close);
    return result;
}
Also used : ResultSetMapper(jodd.db.oom.mapper.ResultSetMapper) DefaultResultSetMapper(jodd.db.oom.mapper.DefaultResultSetMapper)

Example 2 with ResultSetMapper

use of jodd.db.oom.mapper.ResultSetMapper in project jodd by oblac.

the class DbOomQuery method list.

/**
	 * Iterates result set, maps rows to classes and populates resulting array list.
	 * @param types mapping types
	 * @param max max number of rows to collect, <code>-1</code> for all
	 * @param close <code>true</code> if query is closed at the end, otherwise <code>false</code>.
	 * @return list of mapped entities or array of entities
	 */
@SuppressWarnings({ "unchecked" })
protected <T> List<T> list(Class[] types, int max, boolean close) {
    List<T> result = new ArrayList<>(initialCollectionSize(max));
    ResultSetMapper rsm = executeAndBuildResultSetMapper();
    if (types == null) {
        types = rsm.resolveTables();
    }
    Object previousElement = null;
    while (rsm.next()) {
        Object[] objects = rsm.parseObjects(types);
        Object row = resolveRowResults(objects);
        int size = result.size();
        T newElement = (T) row;
        if (entityAwareMode && size > 0) {
            if (previousElement != null && newElement != null) {
                boolean equals;
                if (newElement.getClass().isArray()) {
                    equals = Arrays.equals((Object[]) previousElement, (Object[]) newElement);
                } else {
                    equals = previousElement.equals(newElement);
                }
                if (equals) {
                    continue;
                }
            }
        }
        if (size == max) {
            break;
        }
        result.add(newElement);
        previousElement = newElement;
    }
    close(rsm, close);
    return result;
}
Also used : ResultSetMapper(jodd.db.oom.mapper.ResultSetMapper) DefaultResultSetMapper(jodd.db.oom.mapper.DefaultResultSetMapper) ArrayList(java.util.ArrayList)

Example 3 with ResultSetMapper

use of jodd.db.oom.mapper.ResultSetMapper in project jodd by oblac.

the class DbOomQuery method listSet.

@SuppressWarnings({ "unchecked" })
protected <T> Set<T> listSet(Class[] types, int max, boolean close) {
    Set<T> result = new LinkedHashSet<>(initialCollectionSize(max));
    ResultSetMapper rsm = executeAndBuildResultSetMapper();
    if (types == null) {
        types = rsm.resolveTables();
    }
    Object previousElement = null;
    while (rsm.next()) {
        Object[] objects = rsm.parseObjects(types);
        Object row = resolveRowResults(objects);
        int size = result.size();
        T newElement = (T) row;
        if (entityAwareMode && size > 0) {
            if (previousElement != null && newElement != null) {
                boolean equals;
                if (newElement.getClass().isArray()) {
                    equals = Arrays.equals((Object[]) previousElement, (Object[]) newElement);
                } else {
                    equals = previousElement.equals(newElement);
                }
                if (equals) {
                    continue;
                }
            }
        }
        if (size == max) {
            break;
        }
        result.add(newElement);
        previousElement = newElement;
    }
    close(rsm, close);
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResultSetMapper(jodd.db.oom.mapper.ResultSetMapper) DefaultResultSetMapper(jodd.db.oom.mapper.DefaultResultSetMapper)

Aggregations

DefaultResultSetMapper (jodd.db.oom.mapper.DefaultResultSetMapper)3 ResultSetMapper (jodd.db.oom.mapper.ResultSetMapper)3 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1