All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.impl.AbstractResultQuery Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/**
 * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
 * All rights reserved.
 *
 * This work is dual-licensed
 * - under the Apache Software License 2.0 (the "ASL")
 * - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
 * =============================================================================
 * You may choose which license applies to you:
 *
 * - If you're using this work with Open Source databases, you may choose
 *   either ASL or jOOQ License.
 * - If you're using this work with at least one commercial database, you must
 *   choose jOOQ License
 *
 * For more information, please visit http://www.jooq.org/licenses
 *
 * Apache Software License 2.0:
 * -----------------------------------------------------------------------------
 * 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.
 *
 * jOOQ License and Maintenance Agreement:
 * -----------------------------------------------------------------------------
 * Data Geekery grants the Customer the non-exclusive, timely limited and
 * non-transferable license to install and use the Software under the terms of
 * the jOOQ License and Maintenance Agreement.
 *
 * This library is distributed with a LIMITED WARRANTY. See the jOOQ License
 * and Maintenance Agreement for more details: http://www.jooq.org/licensing
 */
package org.jooq.impl;

import static java.sql.ResultSet.CONCUR_UPDATABLE;
import static java.sql.ResultSet.TYPE_SCROLL_SENSITIVE;
import static java.util.Arrays.asList;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
// ...
import static org.jooq.SQLDialect.CUBRID;
// ...
import static org.jooq.impl.Utils.DATA_LOCK_ROWS_FOR_UPDATE;
import static org.jooq.impl.Utils.consumeResultSets;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import org.jooq.Configuration;
import org.jooq.Converter;
import org.jooq.Cursor;
import org.jooq.ExecuteContext;
import org.jooq.ExecuteListener;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.RecordHandler;
import org.jooq.RecordMapper;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Table;
import org.jooq.exception.DataTypeException;
import org.jooq.tools.Convert;
import org.jooq.tools.JooqLogger;

/**
 * A query that returns a {@link Result}
 *
 * @author Lukas Eder
 */
abstract class AbstractResultQuery extends AbstractQuery implements ResultQuery {

    /**
     * Generated UID
     */
    private static final long       serialVersionUID = -5588344253566055707L;
    private static final JooqLogger log              = JooqLogger.getLogger(AbstractResultQuery.class);

    private int                     maxRows;
    private int                     fetchSize;
    private int                     resultSetConcurrency;
    private int                     resultSetType;
    private int                     resultSetHoldability;
    private transient boolean       lazy;
    private transient boolean       many;
    private transient Cursor     cursor;
    private Result               result;
    private List>    results;

    // Some temp variables for String interning
    private final Intern            intern = new Intern();

    AbstractResultQuery(Configuration configuration) {
        super(configuration);
    }

    /**
     * Get a list of fields provided a result set.
     */
    protected abstract Field[] getFields(ResultSetMetaData rs) throws SQLException;

    @SuppressWarnings("unchecked")
    @Override
    public final ResultQuery bind(String param, Object value) throws IllegalArgumentException, DataTypeException {
        return (ResultQuery) super.bind(param, value);
    }

    @SuppressWarnings("unchecked")
    @Override
    public final ResultQuery bind(int index, Object value) throws IllegalArgumentException, DataTypeException {
        return (ResultQuery) super.bind(index, value);
    }

    @SuppressWarnings("unchecked")
    @Override
    public final ResultQuery queryTimeout(int timeout) {
        return (ResultQuery) super.queryTimeout(timeout);
    }

    @SuppressWarnings("unchecked")
    @Override
    public final ResultQuery keepStatement(boolean k) {
        return (ResultQuery) super.keepStatement(k);
    }

    @Override
    public final ResultQuery maxRows(int rows) {
        this.maxRows = rows;
        return this;
    }

    @Override
    public final ResultQuery fetchSize(int rows) {
        this.fetchSize = rows;
        return this;
    }

    @Override
    public final ResultQuery resultSetConcurrency(int concurrency) {
        this.resultSetConcurrency = concurrency;
        return this;
    }

    @Override
    public final ResultQuery resultSetType(int type) {
        this.resultSetType = type;
        return this;
    }

    @Override
    public final ResultQuery resultSetHoldability(int holdability) {
        this.resultSetHoldability = holdability;
        return this;
    }

    @Override
    public final ResultQuery intern(Field... fields) {
        intern.internFields = fields;
        return this;
    }

    @Override
    public final ResultQuery intern(int... fieldIndexes) {
        intern.internIndexes = fieldIndexes;
        return this;
    }

    @Override
    public final ResultQuery intern(String... fieldNames) {
        intern.internNames = fieldNames;
        return this;
    }

    @Override
    protected final void prepare(ExecuteContext ctx) throws SQLException {

        // [#1846] [#2265] [#2299] Users may explicitly specify how ResultSets
        // created by jOOQ behave. This will override any other default behaviour
        if (resultSetConcurrency != 0 || resultSetType != 0 || resultSetHoldability != 0) {
            int type = resultSetType != 0 ? resultSetType : ResultSet.TYPE_FORWARD_ONLY;
            int concurrency = resultSetConcurrency != 0 ? resultSetConcurrency : ResultSet.CONCUR_READ_ONLY;

            // Sybase doesn't support holdability. Avoid setting it!
            if (resultSetHoldability == 0) {
                ctx.statement(ctx.connection().prepareStatement(ctx.sql(), type, concurrency));
            }
            else {
                ctx.statement(ctx.connection().prepareStatement(ctx.sql(), type, concurrency, resultSetHoldability));
            }
        }

        // [#1296] These dialects do not implement FOR UPDATE. But the same
        // effect can be achieved using ResultSet.CONCUR_UPDATABLE
        else if (isForUpdate() && asList(CUBRID).contains(ctx.configuration().dialect().family())) {
            ctx.data(DATA_LOCK_ROWS_FOR_UPDATE, true);
            ctx.statement(ctx.connection().prepareStatement(ctx.sql(), TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE));
        }

        // Regular behaviour
        else {
            ctx.statement(ctx.connection().prepareStatement(ctx.sql()));
        }

        // [#1263] Allow for negative fetch sizes to support some non-standard
        // MySQL feature, where Integer.MIN_VALUE is used
        if (fetchSize != 0) {
            if (log.isDebugEnabled())
                log.debug("Setting fetch size", fetchSize);

            ctx.statement().setFetchSize(fetchSize);
        }

        // [#1854] Set the max number of rows for this result query
        if (maxRows != 0) {
            ctx.statement().setMaxRows(maxRows);
        }
    }

    @Override
    protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
        listener.executeStart(ctx);

        /* [pro] xx
        xx xxxx xxxxxxx xxxx xx xxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxx
        xx xxxxxxxxxx xx xxxxx xxx xxx xxxxxxx
        xx xxxxxxxxxxxxx xx xxxx x
            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
        x

        xx xxxxxxx xxxxx xxxxxxxxxxxxxx xx xxxxx xx xxxxxx xxxxxxx xxxx xxx
        xx xxx xxxxxx x xxxxxxxxxx xxxx xxxxxxxx xxxxxx xxxxxxxxxxxxxxxxxxxxxxx
        xxxx xx [/pro] */if (ctx.statement().execute()) {
            ctx.resultSet(ctx.statement().getResultSet());
        }

        listener.executeEnd(ctx);

        // Fetch a single result set
        if (!many) {
            if (ctx.resultSet() != null) {
                Field[] fields = getFields(ctx.resultSet().getMetaData());
                cursor = new CursorImpl(ctx, listener, fields, intern.internIndexes(fields), keepStatement(), keepResultSet(), getRecordType());

                if (!lazy) {
                    result = cursor.fetch();
                    cursor = null;
                }
            }
            else {
                result = new ResultImpl(ctx.configuration());
            }
        }

        // Fetch several result sets
        else {
            results = new ArrayList>();
            consumeResultSets(ctx, listener, results, intern);
        }

        return result != null ? result.size() : 0;
    }

    @Override
    protected final boolean keepResultSet() {
        return lazy;
    }

    /**
     * Subclasses should indicate whether they want an updatable {@link ResultSet}
     */
    abstract boolean isForUpdate();

    @Override
    public final Result fetch() {
        execute();
        return result;
    }

    @Override
    public final ResultSet fetchResultSet() {
        return fetchLazy().resultSet();
    }

    @Override
    public final Iterator iterator() {
        return fetch().iterator();
    }

    @Override
    public final Cursor fetchLazy() {
        return fetchLazy(fetchSize);
    }

    @Override
    @Deprecated
    public final Cursor fetchLazy(int size) {
        final int previousFetchSize = fetchSize;

        // [#3515] TODO: Avoid modifying a Query's per-execution state
        lazy = true;
        fetchSize = size;

        try {
            execute();
        }
        finally {
            lazy = false;
            fetchSize = previousFetchSize;
        }

        return cursor;
    }

    @Override
    public final List> fetchMany() {

        // [#3515] TODO: Avoid modifying a Query's per-execution state
        many = true;

        try {
            execute();
        }
        finally {
            many = false;
        }

        return results;
    }

    @Override
    public final  List fetch(Field field) {
        return fetch().getValues(field);
    }

    @Override
    public final  List fetch(Field field, Class type) {
        return fetch().getValues(field, type);
    }

    @Override
    public final  List fetch(Field field, Converter converter) {
        return fetch().getValues(field, converter);
    }

    @Override
    public final List fetch(int fieldIndex) {
        return fetch().getValues(fieldIndex);
    }

    @Override
    public final  List fetch(int fieldIndex, Class type) {
        return fetch().getValues(fieldIndex, type);
    }

    @Override
    public final  List fetch(int fieldIndex, Converter converter) {
        return fetch().getValues(fieldIndex, converter);
    }

    @Override
    public final List fetch(String fieldName) {
        return fetch().getValues(fieldName);
    }

    @Override
    public final  List fetch(String fieldName, Class type) {
        return fetch().getValues(fieldName, type);
    }

    @Override
    public final  List fetch(String fieldName, Converter converter) {
        return fetch().getValues(fieldName, converter);
    }

    @Override
    public final  T fetchOne(Field field) {
        R record = fetchOne();
        return record == null ? null : record.getValue(field);
    }

    @Override
    public final  T fetchOne(Field field, Class type) {
        return Convert.convert(fetchOne(field), type);
    }

    @Override
    public final  U fetchOne(Field field, Converter converter) {
        return Convert.convert(fetchOne(field), converter);
    }

    @Override
    public final Object fetchOne(int fieldIndex) {
        R record = fetchOne();
        return record == null ? null : record.getValue(fieldIndex);
    }

    @Override
    public final  T fetchOne(int fieldIndex, Class type) {
        return Convert.convert(fetchOne(fieldIndex), type);
    }

    @Override
    public final  U fetchOne(int fieldIndex, Converter converter) {
        return Convert.convert(fetchOne(fieldIndex), converter);
    }

    @Override
    public final Object fetchOne(String fieldName) {
        R record = fetchOne();
        return record == null ? null : record.getValue(fieldName);
    }

    @Override
    public final  T fetchOne(String fieldName, Class type) {
        return Convert.convert(fetchOne(fieldName), type);
    }

    @Override
    public final  U fetchOne(String fieldName, Converter converter) {
        return Convert.convert(fetchOne(fieldName), converter);
    }

    @Override
    public final R fetchOne() {
        return Utils.fetchOne(fetchLazy());
    }

    @Override
    public final Map fetchOneMap() {
        R record = fetchOne();
        return record == null ? null : record.intoMap();
    }

    @Override
    public final Object[] fetchOneArray() {
        R record = fetchOne();
        return record == null ? null : record.intoArray();
    }

    @Override
    public final  E fetchOneInto(Class type) {
        R record = fetchOne();
        return record == null ? null : record.into(type);
    }

    @Override
    public final  Z fetchOneInto(Table table) {
        R record = fetchOne();
        return record == null ? null : record.into(table);
    }

    @Override
    public final  T fetchAny(Field field) {
        R record = fetchAny();
        return record == null ? null : record.getValue(field);
    }

    @Override
    public final  T fetchAny(Field field, Class type) {
        return Convert.convert(fetchAny(field), type);
    }

    @Override
    public final  U fetchAny(Field field, Converter converter) {
        return Convert.convert(fetchAny(field), converter);
    }

    @Override
    public final Object fetchAny(int fieldIndex) {
        R record = fetchAny();
        return record == null ? null : record.getValue(fieldIndex);
    }

    @Override
    public final  T fetchAny(int fieldIndex, Class type) {
        return Convert.convert(fetchAny(fieldIndex), type);
    }

    @Override
    public final  U fetchAny(int fieldIndex, Converter converter) {
        return Convert.convert(fetchAny(fieldIndex), converter);
    }

    @Override
    public final Object fetchAny(String fieldName) {
        R record = fetchAny();
        return record == null ? null : record.getValue(fieldName);
    }

    @Override
    public final  T fetchAny(String fieldName, Class type) {
        return Convert.convert(fetchAny(fieldName), type);
    }

    @Override
    public final  U fetchAny(String fieldName, Converter converter) {
        return Convert.convert(fetchAny(fieldName), converter);
    }

    @Override
    public final R fetchAny() {
        Cursor c = fetchLazy();

        try {
            return c.fetchOne();
        }
        finally {
            c.close();
        }
    }

    @Override
    public final Map fetchAnyMap() {
        R record = fetchAny();
        return record == null ? null : record.intoMap();
    }

    @Override
    public final Object[] fetchAnyArray() {
        R record = fetchAny();
        return record == null ? null : record.intoArray();
    }

    @Override
    public final  E fetchAnyInto(Class type) {
        R record = fetchAny();
        return record == null ? null : record.into(type);
    }

    @Override
    public final  Z fetchAnyInto(Table table) {
        R record = fetchAny();
        return record == null ? null : record.into(table);
    }

    @Override
    public final  Map fetchMap(Field key) {
        return fetch().intoMap(key);
    }

    @Override
    public final  Map fetchMap(Field key, Field value) {
        return fetch().intoMap(key, value);
    }

    @Override
    public final Map fetchMap(Field[] keys) {
        return fetch().intoMap(keys);
    }

    @Override
    public final  Map, E> fetchMap(Field[] keys, Class type) {
        return fetch().intoMap(keys, type);
    }

    @Override
    public final  Map, E> fetchMap(Field[] keys, RecordMapper mapper) {
        return fetch().intoMap(keys, mapper);
    }

    @Override
    public final  Map fetchMap(Table table) {
        return fetch().intoMap(table);
    }

    @Override
    public final  Map fetchMap(Table table, Class type) {
        return fetch().intoMap(table, type);
    }

    @Override
    public final  Map fetchMap(Table table, RecordMapper mapper) {
        return fetch().intoMap(table, mapper);
    }

    @Override
    public final  Map fetchMap(Field key, Class type) {
        return fetch().intoMap(key, type);
    }

    @Override
    public final  Map fetchMap(Field key, RecordMapper mapper) {
        return fetch().intoMap(key, mapper);
    }

    @Override
    public final List> fetchMaps() {
        return fetch().intoMaps();
    }

    @Override
    public final  Map> fetchGroups(Field key) {
        return fetch().intoGroups(key);
    }

    @Override
    public final  Map> fetchGroups(Field key, Field value) {
        return fetch().intoGroups(key, value);
    }

    @Override
    public final Map> fetchGroups(Field[] keys) {
        return fetch().intoGroups(keys);
    }

    @Override
    public final  Map> fetchGroups(Field[] keys, Class type) {
        return fetch().intoGroups(keys, type);
    }

    @Override
    public final  Map> fetchGroups(Field[] keys, RecordMapper mapper) {
        return fetch().intoGroups(keys, mapper);
    }

    @Override
    public final  Map> fetchGroups(Table table) {
        return fetch().intoGroups(table);
    }

    @Override
    public final  Map> fetchGroups(Table table, Class type) {
        return fetch().intoGroups(table, type);
    }

    @Override
    public final  Map> fetchGroups(Table table, RecordMapper mapper) {
        return fetch().intoGroups(table, mapper);
    }

    @Override
    public final  Map> fetchGroups(Field key, Class type) {
        return fetch().intoGroups(key, type);
    }

    @Override
    public final  Map> fetchGroups(Field key, RecordMapper mapper) {
        return fetch().intoGroups(key, mapper);
    }

    @Override
    public final Object[][] fetchArrays() {
        return fetch().intoArray();
    }

    @Override
    public final Object[] fetchArray(int fieldIndex) {
        return fetch().intoArray(fieldIndex);
    }

    @Override
    public final  T[] fetchArray(int fieldIndex, Class type) {
        return fetch().intoArray(fieldIndex, type);
    }

    @Override
    public final  U[] fetchArray(int fieldIndex, Converter converter) {
        return fetch().intoArray(fieldIndex, converter);
    }

    @Override
    public final Object[] fetchArray(String fieldName) {
        return fetch().intoArray(fieldName);
    }

    @Override
    public final  T[] fetchArray(String fieldName, Class type) {
        return fetch().intoArray(fieldName, type);
    }

    @Override
    public final  U[] fetchArray(String fieldName, Converter converter) {
        return fetch().intoArray(fieldName, converter);
    }

    @Override
    public final  T[] fetchArray(Field field) {
        return fetch().intoArray(field);
    }

    @Override
    public final  T[] fetchArray(Field field, Class type) {
        return fetch().intoArray(field, type);
    }

    @Override
    public final  U[] fetchArray(Field field, Converter converter) {
        return fetch().intoArray(field, converter);
    }

    @Override
    public final Set fetchSet(int fieldIndex) {
        return fetch().intoSet(fieldIndex);
    }

    @Override
    public final  Set fetchSet(int fieldIndex, Class type) {
        return fetch().intoSet(fieldIndex, type);
    }

    @Override
    public final  Set fetchSet(int fieldIndex, Converter converter) {
        return fetch().intoSet(fieldIndex, converter);
    }

    @Override
    public final Set fetchSet(String fieldName) {
        return fetch().intoSet(fieldName);
    }

    @Override
    public final  Set fetchSet(String fieldName, Class type) {
        return fetch().intoSet(fieldName, type);
    }

    @Override
    public final  Set fetchSet(String fieldName, Converter converter) {
        return fetch().intoSet(fieldName, converter);
    }

    @Override
    public final  Set fetchSet(Field field) {
        return fetch().intoSet(field);
    }

    @Override
    public final  Set fetchSet(Field field, Class type) {
        return fetch().intoSet(field, type);
    }

    @Override
    public final  Set fetchSet(Field field, Converter converter) {
        return fetch().intoSet(field, converter);
    }

    /**
     * Subclasses may override this method
     * 

* {@inheritDoc} */ @Override public Class getRecordType() { return null; } @Override public final List fetchInto(Class type) { return fetch().into(type); } @Override public final Result fetchInto(Table table) { return fetch().into(table); } @Override public final > H fetchInto(H handler) { return fetch().into(handler); } @Override public final List fetch(RecordMapper mapper) { return fetch().map(mapper); } @Override @Deprecated public final org.jooq.FutureResult fetchLater() { ExecutorService executor = newSingleThreadExecutor(); Future> future = executor.submit(new ResultQueryCallable()); return new FutureResultImpl(future, executor); } @Override @Deprecated public final org.jooq.FutureResult fetchLater(ExecutorService executor) { Future> future = executor.submit(new ResultQueryCallable()); return new FutureResultImpl(future); } @Override public final Result getResult() { return result; } /** * A wrapper for the {@link ResultQuery#fetch()} method */ private final class ResultQueryCallable implements Callable> { @Override public final Result call() throws Exception { return fetch(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy