org.jooq.impl.AbstractResultQuery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payment-retries-plugin Show documentation
Show all versions of payment-retries-plugin Show documentation
Kill Bill Payment Retries plugin
The newest version!
/*
* Copyright (c) 2009-2016, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* 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.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
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.SQLDialect.POSTGRES;
// ...
import static org.jooq.impl.Tools.blocking;
import static org.jooq.impl.Tools.consumeResultSets;
import static org.jooq.impl.Tools.DataKey.DATA_LOCK_ROWS_FOR_UPDATE;
import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.stream.Stream;
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.Name;
import org.jooq.Record;
import org.jooq.RecordHandler;
import org.jooq.RecordMapper;
import org.jooq.Result;
import org.jooq.ResultQuery;
import org.jooq.Results;
import org.jooq.Table;
import org.jooq.conf.SettingsTools;
import org.jooq.tools.Convert;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.jdbc.MockResultSet;
/**
* 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 ResultsImpl 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) {
return (ResultQuery) super.bind(param, value);
}
@SuppressWarnings("unchecked")
@Override
public final ResultQuery bind(int index, Object value) {
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... fieldNameStrings) {
intern.internNameStrings = fieldNameStrings;
return this;
}
@Override
public final ResultQuery intern(Name... 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] [#4753] Allow for negative fetch sizes to support some non-standard
// MySQL feature, where Integer.MIN_VALUE is used
int f = SettingsTools.getFetchSize(fetchSize, ctx.settings());
if (f != 0) {
if (log.isDebugEnabled())
log.debug("Setting fetch size", f);
ctx.statement().setFetchSize(f);
}
// [#1854] [#4753] Set the max number of rows for this result query
int m = SettingsTools.getMaxRows(maxRows, ctx.settings());
if (m != 0) {
ctx.statement().setMaxRows(m);
}
}
@Override
protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
listener.executeStart(ctx);
// [#4511] [#4753] PostgreSQL doesn't like fetchSize with autoCommit == true
int f = SettingsTools.getFetchSize(fetchSize, ctx.settings());
if (ctx.family() == POSTGRES && f != 0 && ctx.connection().getAutoCommit())
log.info("Fetch Size", "A fetch size of " + f + " was set on a auto-commit PostgreSQL connection, which is not recommended. See http://jdbc.postgresql.org/documentation/head/query.html#query-with-cursor");
if (ctx.statement().execute()) {
ctx.resultSet(ctx.statement().getResultSet());
}
listener.executeEnd(ctx);
// Fetch a single result set
if (!many) {
// [#5617] This may happen when using plain SQL API or a MockConnection and expecting a result set where
// there is none. The cursor / result is patched into the ctx only for single result sets, where
// access to the cursor / result is possible.
if (ctx.resultSet() == null)
ctx.resultSet(new MockResultSet(new ResultImpl(ctx.configuration())));
Field>[] fields = getFields(ctx.resultSet().getMetaData());
cursor = new CursorImpl(ctx, listener, fields, intern.internIndexes(fields), keepStatement(), keepResultSet(), getRecordType(), SettingsTools.getMaxRows(maxRows, ctx.settings()));
if (!lazy) {
result = cursor.fetch();
cursor = null;
}
}
// Fetch several result sets
else {
results = new ResultsImpl(ctx.configuration());
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 CompletionStage> fetchAsync() {
return fetchAsync(Tools.configuration(this).executorProvider().provide());
}
@Override
public final CompletionStage> fetchAsync(Executor executor) {
return ExecutorProviderCompletionStage.of(CompletableFuture.supplyAsync(blocking(this::fetch), executor), () -> executor);
}
@Override
public final Stream fetchStream() {
return fetchLazy().stream();
}
@Override
public final Stream stream() {
return fetchLazy().stream();
}
@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 Results 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 extends T> type) {
return fetch().getValues(field, type);
}
@Override
public final List fetch(Field field, Converter super T, ? extends U> 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 extends T> type) {
return fetch().getValues(fieldIndex, type);
}
@Override
public final List fetch(int fieldIndex, Converter, ? extends U> 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 extends T> type) {
return fetch().getValues(fieldName, type);
}
@Override
public final List fetch(String fieldName, Converter, ? extends U> converter) {
return fetch().getValues(fieldName, converter);
}
@Override
public final List> fetch(Name fieldName) {
return fetch().getValues(fieldName);
}
@Override
public final List fetch(Name fieldName, Class extends T> type) {
return fetch().getValues(fieldName, type);
}
@Override
public final List fetch(Name fieldName, Converter, ? extends U> converter) {
return fetch().getValues(fieldName, converter);
}
@Override
public final T fetchOne(Field field) {
R record = fetchOne();
return record == null ? null : record.get(field);
}
@Override
public final T fetchOne(Field> field, Class extends T> type) {
return Convert.convert(fetchOne(field), type);
}
@Override
public final U fetchOne(Field field, Converter super T, ? extends U> converter) {
return Convert.convert(fetchOne(field), converter);
}
@Override
public final Object fetchOne(int fieldIndex) {
R record = fetchOne();
return record == null ? null : record.get(fieldIndex);
}
@Override
public final T fetchOne(int fieldIndex, Class extends T> type) {
return Convert.convert(fetchOne(fieldIndex), type);
}
@Override
public final U fetchOne(int fieldIndex, Converter, ? extends U> converter) {
return Convert.convert(fetchOne(fieldIndex), converter);
}
@Override
public final Object fetchOne(String fieldName) {
R record = fetchOne();
return record == null ? null : record.get(fieldName);
}
@Override
public final T fetchOne(String fieldName, Class extends T> type) {
return Convert.convert(fetchOne(fieldName), type);
}
@Override
public final U fetchOne(String fieldName, Converter, ? extends U> converter) {
return Convert.convert(fetchOne(fieldName), converter);
}
@Override
public final Object fetchOne(Name fieldName) {
R record = fetchOne();
return record == null ? null : record.get(fieldName);
}
@Override
public final T fetchOne(Name fieldName, Class extends T> type) {
return Convert.convert(fetchOne(fieldName), type);
}
@Override
public final U fetchOne(Name fieldName, Converter, ? extends U> converter) {
return Convert.convert(fetchOne(fieldName), converter);
}
@Override
public final R fetchOne() {
return Tools.fetchOne(fetchLazy());
}
@Override
public final E fetchOne(RecordMapper super R, E> mapper) {
R record = fetchOne();
return record == null ? null : mapper.map(record);
}
@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 extends E> 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 Optional fetchOptional(Field field) {
return Optional.ofNullable(fetchOne(field));
}
@Override
public final Optional fetchOptional(Field> field, Class extends T> type) {
return Optional.ofNullable(fetchOne(field, type));
}
@Override
public final Optional fetchOptional(Field field, Converter super T, ? extends U> converter) {
return Optional.ofNullable(fetchOne(field, converter));
}
@Override
public final Optional> fetchOptional(int fieldIndex) {
return Optional.ofNullable(fetchOne(fieldIndex));
}
@Override
public final Optional fetchOptional(int fieldIndex, Class extends T> type) {
return Optional.ofNullable(fetchOne(fieldIndex, type));
}
@Override
public final Optional fetchOptional(int fieldIndex, Converter, ? extends U> converter) {
return Optional.ofNullable(fetchOne(fieldIndex, converter));
}
@Override
public final Optional> fetchOptional(String fieldName) {
return Optional.ofNullable(fetchOne(fieldName));
}
@Override
public final Optional fetchOptional(String fieldName, Class extends T> type) {
return Optional.ofNullable(fetchOne(fieldName, type));
}
@Override
public final Optional fetchOptional(String fieldName, Converter, ? extends U> converter) {
return Optional.ofNullable(fetchOne(fieldName, converter));
}
@Override
public final Optional> fetchOptional(Name fieldName) {
return Optional.ofNullable(fetchOne(fieldName));
}
@Override
public final Optional fetchOptional(Name fieldName, Class extends T> type) {
return Optional.ofNullable(fetchOne(fieldName, type));
}
@Override
public final Optional fetchOptional(Name fieldName, Converter, ? extends U> converter) {
return Optional.ofNullable(fetchOne(fieldName, converter));
}
@Override
public final Optional fetchOptional() {
return Optional.ofNullable(fetchOne());
}
@Override
public final Optional fetchOptional(RecordMapper super R, E> mapper) {
return Optional.ofNullable(fetchOne(mapper));
}
@Override
public final Optional