Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.jdbc.AbstractQuery Maven / Gradle / Ivy
/*
* Copyright (c) 2019, Haiyang Li.
*
* 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 com.landawn.abacus.jdbc;
import java.io.Closeable;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLType;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Predicate;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.LazyEvaluation;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.jdbc.Jdbc.BiResultExtractor;
import com.landawn.abacus.jdbc.Jdbc.ResultExtractor;
import com.landawn.abacus.jdbc.Jdbc.RowConsumer;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.type.TypeFactory;
import com.landawn.abacus.util.CheckedStream;
import com.landawn.abacus.util.CheckedStream.CheckedIterator;
import com.landawn.abacus.util.ClassUtil;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.DataSet;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableObjArray;
import com.landawn.abacus.util.Numbers;
import com.landawn.abacus.util.Strings;
import com.landawn.abacus.util.Throwables;
import com.landawn.abacus.util.Tuple;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalBoolean;
import com.landawn.abacus.util.u.OptionalByte;
import com.landawn.abacus.util.u.OptionalChar;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalFloat;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.u.OptionalShort;
/**
* Performance Tips:
* Avoid unnecessary/repeated database calls.
* Only fetch the columns you need or update the columns you want.
* Index is the key point in a lot of database performance issues.
*
*
*
* The backed {@code PreparedStatement/CallableStatement} will be closed by default
* after any execution methods(which will trigger the backed {@code PreparedStatement/CallableStatement} to be executed, for example: get/query/queryForInt/Long/../findFirst/findOnlyOne/list/execute/...).
* except the {@code 'closeAfterExecution'} flag is set to {@code false} by calling {@code #closeAfterExecution(false)}.
*
*
* Generally, don't cache or reuse the instance of this class,
* except the {@code 'closeAfterExecution'} flag is set to {@code false} by calling {@code #closeAfterExecution(false)}.
*
*
* Remember: parameter/column index in {@code PreparedStatement/ResultSet} starts from 1, not 0.
*
* @author haiyangl
* @param
* @param
*/
@SuppressWarnings("java:S1192")
public abstract class AbstractQuery> implements Closeable {
static final Logger logger = LoggerFactory.getLogger(AbstractQuery.class);
static final Set> stmtParameterClasses = new HashSet<>();
static {
final Method[] methods = PreparedStatement.class.getDeclaredMethods();
for (Method m : methods) {
if (Modifier.isPublic(m.getModifiers()) && m.getName().startsWith("set") && m.getParameterCount() == 2
&& m.getParameterTypes()[0].equals(int.class)) {
stmtParameterClasses.add(m.getParameterTypes()[1]);
}
}
final List> primitiveTypes = N.asList(boolean.class, char.class, byte.class, short.class, int.class, long.class, float.class, double.class);
for (Class> cls : primitiveTypes) {
stmtParameterClasses.add(cls);
stmtParameterClasses.add(ClassUtil.wrap(cls));
stmtParameterClasses.add(N.newArray(cls, 0).getClass());
stmtParameterClasses.add(N.newArray(ClassUtil.wrap(cls), 0).getClass());
}
stmtParameterClasses.remove(Object.class);
}
@SuppressWarnings("rawtypes")
static final Throwables.BiConsumer defaultAddBatchAction = (q, s) -> s.addBatch();
Throwables.BiConsumer super This, ? super Stmt, SQLException> addBatchAction = defaultAddBatchAction;
final Stmt stmt;
boolean isFetchDirectionSet = false;
boolean isBatch = false;
boolean isCloseAfterExecution = true;
boolean isClosed = false;
Runnable closeHandler;
AbstractQuery(Stmt stmt) {
this.stmt = stmt;
}
// /**
// * It's designed to void try-catch.
// * This method should be called immediately after {@code JdbcUtil#prepareCallableQuery/SQLExecutor#prepareQuery}.
// *
// * @return
// */
// public Try tried() {
// assertNotClosed();
//
// return Try.of((Q) this);
// }
/**
*
* @param closeAfterExecution default is {@code true}.
* @return
*/
public This closeAfterExecution(boolean closeAfterExecution) {
assertNotClosed();
this.isCloseAfterExecution = closeAfterExecution;
return (This) this;
}
boolean isCloseAfterExecution() {
return isCloseAfterExecution;
}
/**
*
* @param closeHandler A task to execute after this {@code Query} is closed
* @return
*/
@SuppressWarnings("hiding")
public This onClose(final Runnable closeHandler) {
checkArgNotNull(closeHandler, "closeHandler");
assertNotClosed();
if (this.closeHandler == null) {
this.closeHandler = closeHandler;
} else {
final Runnable tmp = this.closeHandler;
this.closeHandler = () -> {
try {
tmp.run();
} finally {
closeHandler.run();
}
};
}
return (This) this;
}
/**
* Sets the null.
*
* @param parameterIndex starts from 1, not 0.
* @param sqlType
* @return
* @throws SQLException
* @see java.sql.Types
*/
public This setNull(int parameterIndex, int sqlType) throws SQLException {
stmt.setNull(parameterIndex, sqlType);
return (This) this;
}
/**
* Sets the null.
*
* @param parameterIndex starts from 1, not 0.
* @param sqlType
* @param typeName
* @return
* @throws SQLException
* @see java.sql.Types
*/
public This setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
stmt.setNull(parameterIndex, sqlType, typeName);
return (This) this;
}
/**
* Sets the boolean.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setBoolean(int parameterIndex, boolean x) throws SQLException {
stmt.setBoolean(parameterIndex, x);
return (This) this;
}
/**
* Sets the boolean.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setBoolean(int parameterIndex, Boolean x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.BOOLEAN);
} else {
stmt.setBoolean(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the byte.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setByte(int parameterIndex, byte x) throws SQLException {
stmt.setByte(parameterIndex, x);
return (This) this;
}
/**
* Sets the byte.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setByte(int parameterIndex, Byte x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.TINYINT);
} else {
stmt.setByte(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the short.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setShort(int parameterIndex, short x) throws SQLException {
stmt.setShort(parameterIndex, x);
return (This) this;
}
/**
* Sets the short.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setShort(int parameterIndex, Short x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.SMALLINT);
} else {
stmt.setShort(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the int.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setInt(int parameterIndex, int x) throws SQLException {
stmt.setInt(parameterIndex, x);
return (This) this;
}
/**
* Sets the int.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setInt(int parameterIndex, Integer x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.INTEGER);
} else {
stmt.setInt(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex
* @param x
* @param defaultValueForNull
* @return
* @throws SQLException
*/
@Beta
public This setInt(int parameterIndex, Integer x, int defaultValueForNull) throws SQLException {
if (x == null) {
stmt.setInt(parameterIndex, defaultValueForNull);
} else {
stmt.setInt(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the int.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
@Beta
public This setInt(int parameterIndex, String x) throws SQLException {
if (Strings.isEmpty(x)) {
stmt.setNull(parameterIndex, java.sql.Types.INTEGER);
} else {
stmt.setInt(parameterIndex, Numbers.toInt(x));
}
return (This) this;
}
/**
*
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
* @see #setString(int, char)
* @deprecated generally {@code char} should be saved as {@code String} in db.
*/
@Deprecated
@Beta
public This setInt(int parameterIndex, char x) throws SQLException {
stmt.setInt(parameterIndex, x);
return (This) this;
}
/**
*
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
* @see #setString(int, Character)
* @deprecated generally {@code char} should be saved as {@code String} in db.
*/
@Deprecated
@Beta
public This setInt(int parameterIndex, Character x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.INTEGER);
} else {
stmt.setInt(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setLong(int parameterIndex, long x) throws SQLException {
stmt.setLong(parameterIndex, x);
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setLong(int parameterIndex, Long x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.BIGINT);
} else {
stmt.setLong(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex
* @param x
* @param defaultValueForNull
* @return
* @throws SQLException
*/
@Beta
public This setLong(int parameterIndex, Long x, long defaultValueForNull) throws SQLException {
if (x == null) {
stmt.setLong(parameterIndex, defaultValueForNull);
} else {
stmt.setLong(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
@Beta
public This setLong(int parameterIndex, String x) throws SQLException {
if (Strings.isEmpty(x)) {
stmt.setNull(parameterIndex, java.sql.Types.BIGINT);
} else {
stmt.setLong(parameterIndex, Numbers.toLong(x));
}
return (This) this;
}
/**
* Sets the long.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setLong(int parameterIndex, BigInteger x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.BIGINT);
} else {
stmt.setLong(parameterIndex, x.longValueExact());
}
return (This) this;
}
/**
* Sets the float.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setFloat(int parameterIndex, float x) throws SQLException {
stmt.setFloat(parameterIndex, x);
return (This) this;
}
/**
* Sets the float.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setFloat(int parameterIndex, Float x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.FLOAT);
} else {
stmt.setFloat(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the double.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setDouble(int parameterIndex, double x) throws SQLException {
stmt.setDouble(parameterIndex, N.defaultIfNull(x));
return (This) this;
}
/**
* Sets the double.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setDouble(int parameterIndex, Double x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, java.sql.Types.DOUBLE);
} else {
stmt.setDouble(parameterIndex, x);
}
return (This) this;
}
/**
* Sets the big decimal.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
stmt.setBigDecimal(parameterIndex, x);
return (This) this;
}
/**
* Sets the BigInteger.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setBigDecimal(int parameterIndex, BigInteger x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, Types.DECIMAL);
} else {
stmt.setBigDecimal(parameterIndex, new BigDecimal(x));
}
return (This) this;
}
/**
* Sets the BigInteger.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
* @see {@link #setString(int, BigInteger)}
* @see {@link #setBigDecimal(int, BigInteger)}
* @see {@link #setLong(int, BigInteger)}
*/
@Beta
public This setBigIntegerAsString(int parameterIndex, BigInteger x) throws SQLException {
return setString(parameterIndex, x);
}
/**
* Sets the string.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setString(int parameterIndex, String x) throws SQLException {
stmt.setString(parameterIndex, x);
return (This) this;
}
/**
* Sets the string.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setString(int parameterIndex, CharSequence x) throws SQLException {
stmt.setString(parameterIndex, x == null ? null : x.toString());
return (This) this;
}
/**
*
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setString(int parameterIndex, char x) throws SQLException {
stmt.setString(parameterIndex, String.valueOf(x));
return (This) this;
}
/**
* Sets the String.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setString(int parameterIndex, Character x) throws SQLException {
stmt.setString(parameterIndex, x == null ? null : String.valueOf(x));
return (This) this;
}
/**
* Sets the BigInteger.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setString(int parameterIndex, BigInteger x) throws SQLException {
if (x == null) {
stmt.setNull(parameterIndex, Types.VARCHAR);
} else {
stmt.setString(parameterIndex, x.toString(10));
}
return (This) this;
}
/**
* Sets the String.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setNString(int parameterIndex, String x) throws SQLException {
stmt.setNString(parameterIndex, x);
return (This) this;
}
/**
* Sets the String.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setNString(int parameterIndex, CharSequence x) throws SQLException {
stmt.setNString(parameterIndex, x == null ? null : x.toString());
return (This) this;
}
/**
* Sets the date.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setDate(int parameterIndex, java.sql.Date x) throws SQLException {
stmt.setDate(parameterIndex, x);
return (This) this;
}
/**
* Sets the date.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setDate(int parameterIndex, java.util.Date x) throws SQLException {
stmt.setDate(parameterIndex, x == null ? null : x instanceof java.sql.Date ? (java.sql.Date) x : new java.sql.Date(x.getTime()));
return (This) this;
}
/**
* Sets the date.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @param cal
* @return
* @throws SQLException
*/
public This setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException {
stmt.setDate(parameterIndex, x, cal);
return (This) this;
}
/**
* Sets the time.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setTime(int parameterIndex, java.sql.Time x) throws SQLException {
stmt.setTime(parameterIndex, x);
return (This) this;
}
/**
* Sets the time.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setTime(int parameterIndex, java.util.Date x) throws SQLException {
stmt.setTime(parameterIndex, x == null ? null : x instanceof java.sql.Time ? (java.sql.Time) x : new java.sql.Time(x.getTime()));
return (This) this;
}
/**
* Sets the time.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @param cal
* @return
* @throws SQLException
*/
public This setTime(int parameterIndex, java.sql.Time x, Calendar cal) throws SQLException {
stmt.setTime(parameterIndex, x, cal);
return (This) this;
}
/**
* Sets the timestamp.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException {
stmt.setTimestamp(parameterIndex, x);
return (This) this;
}
/**
* Sets the timestamp.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setTimestamp(int parameterIndex, java.util.Date x) throws SQLException {
stmt.setTimestamp(parameterIndex, x == null ? null : x instanceof java.sql.Timestamp ? (java.sql.Timestamp) x : new java.sql.Timestamp(x.getTime()));
return (This) this;
}
/**
* Sets the timestamp.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @param cal
* @return
* @throws SQLException
*/
public This setTimestamp(int parameterIndex, java.sql.Timestamp x, Calendar cal) throws SQLException {
stmt.setTimestamp(parameterIndex, x, cal);
return (This) this;
}
/**
* Sets the bytes.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setBytes(int parameterIndex, byte[] x) throws SQLException {
stmt.setBytes(parameterIndex, x);
return (This) this;
}
/**
* Sets the ascii stream.
*
* @param parameterIndex
* @param inputStream
* @return
* @throws SQLException
*/
public This setAsciiStream(int parameterIndex, InputStream inputStream) throws SQLException {
stmt.setAsciiStream(parameterIndex, inputStream);
return (This) this;
}
/**
* Sets the ascii stream.
*
* @param parameterIndex
* @param inputStream
* @param length
* @return
* @throws SQLException
*/
public This setAsciiStream(int parameterIndex, InputStream inputStream, int length) throws SQLException {
stmt.setAsciiStream(parameterIndex, inputStream, length);
return (This) this;
}
/**
* Sets the ascii stream.
*
* @param parameterIndex
* @param inputStream
* @param length
* @return
* @throws SQLException
*/
public This setAsciiStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
stmt.setAsciiStream(parameterIndex, inputStream, length);
return (This) this;
}
/**
* Sets the binary stream.
*
* @param parameterIndex
* @param inputStream
* @return
* @throws SQLException
*/
public This setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
stmt.setBinaryStream(parameterIndex, inputStream);
return (This) this;
}
/**
* Sets the binary stream.
*
* @param parameterIndex
* @param inputStream
* @param length
* @return
* @throws SQLException
*/
public This setBinaryStream(int parameterIndex, InputStream inputStream, int length) throws SQLException {
stmt.setBinaryStream(parameterIndex, inputStream, length);
return (This) this;
}
/**
* Sets the binary stream.
*
* @param parameterIndex
* @param inputStream
* @param length
* @return
* @throws SQLException
*/
public This setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
stmt.setBinaryStream(parameterIndex, inputStream, length);
return (This) this;
}
/**
* Sets the character stream.
*
* @param parameterIndex
* @param reader
* @return
* @throws SQLException
*/
public This setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
stmt.setCharacterStream(parameterIndex, reader);
return (This) this;
}
/**
* Sets the character stream.
*
* @param parameterIndex
* @param reader
* @param length
* @return
* @throws SQLException
*/
public This setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
stmt.setCharacterStream(parameterIndex, reader, length);
return (This) this;
}
/**
* Sets the character stream.
*
* @param parameterIndex
* @param reader
* @param length
* @return
* @throws SQLException
*/
public This setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
stmt.setCharacterStream(parameterIndex, reader, length);
return (This) this;
}
/**
* Sets the N character stream.
*
* @param parameterIndex
* @param reader
* @return
* @throws SQLException
*/
public This setNCharacterStream(int parameterIndex, Reader reader) throws SQLException {
stmt.setNCharacterStream(parameterIndex, reader);
return (This) this;
}
/**
* Sets the N character stream.
*
* @param parameterIndex
* @param reader
* @param length
* @return
* @throws SQLException
*/
public This setNCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
stmt.setNCharacterStream(parameterIndex, reader, length);
return (This) this;
}
/**
* Sets the blob.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setBlob(int parameterIndex, java.sql.Blob x) throws SQLException {
stmt.setBlob(parameterIndex, x);
return (This) this;
}
/**
* Sets the blob.
*
* @param parameterIndex
* @param inputStream
* @return
* @throws SQLException
*/
public This setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
stmt.setBlob(parameterIndex, inputStream);
return (This) this;
}
/**
* Sets the blob.
*
* @param parameterIndex
* @param inputStream
* @param length
* @return
* @throws SQLException
*/
public This setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
stmt.setBlob(parameterIndex, inputStream, length);
return (This) this;
}
/**
* Sets the clob.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setClob(int parameterIndex, java.sql.Clob x) throws SQLException {
stmt.setClob(parameterIndex, x);
return (This) this;
}
/**
* Sets the clob.
*
* @param parameterIndex
* @param reader
* @return
* @throws SQLException
*/
public This setClob(int parameterIndex, Reader reader) throws SQLException {
stmt.setClob(parameterIndex, reader);
return (This) this;
}
/**
* Sets the clob.
*
* @param parameterIndex
* @param reader
* @param length
* @return
* @throws SQLException
*/
public This setClob(int parameterIndex, Reader reader, long length) throws SQLException {
stmt.setClob(parameterIndex, reader, length);
return (This) this;
}
/**
* Sets the N clob.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setNClob(int parameterIndex, java.sql.NClob x) throws SQLException {
stmt.setNClob(parameterIndex, x);
return (This) this;
}
/**
* Sets the N clob.
*
* @param parameterIndex
* @param reader
* @return
* @throws SQLException
*/
public This setNClob(int parameterIndex, Reader reader) throws SQLException {
stmt.setNClob(parameterIndex, reader);
return (This) this;
}
/**
* Sets the N clob.
*
* @param parameterIndex
* @param reader
* @param length
* @return
* @throws SQLException
*/
public This setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
stmt.setNClob(parameterIndex, reader, length);
return (This) this;
}
/**
* Sets the URL.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setURL(int parameterIndex, URL x) throws SQLException {
stmt.setURL(parameterIndex, x);
return (This) this;
}
/**
* Sets the array.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setArray(int parameterIndex, java.sql.Array x) throws SQLException {
stmt.setArray(parameterIndex, x);
return (This) this;
}
/**
* Sets the SQLXML.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setSQLXML(int parameterIndex, java.sql.SQLXML x) throws SQLException {
stmt.setSQLXML(parameterIndex, x);
return (This) this;
}
/**
* Sets the ref.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setRef(int parameterIndex, java.sql.Ref x) throws SQLException {
stmt.setRef(parameterIndex, x);
return (This) this;
}
/**
* Sets the row id.
*
* @param parameterIndex
* @param x
* @return
* @throws SQLException
*/
public This setRowId(int parameterIndex, java.sql.RowId x) throws SQLException {
stmt.setRowId(parameterIndex, x);
return (This) this;
}
/**
* Sets the object.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @return
* @throws SQLException
*/
public This setObject(int parameterIndex, Object x) throws SQLException {
if (x == null) {
stmt.setObject(parameterIndex, x);
} else {
N.typeOf(x.getClass()).set(stmt, parameterIndex, x);
}
return (This) this;
}
/**
* Sets the object.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @param sqlType
* @return
* @throws SQLException
* @see java.sql.Types
*/
public This setObject(int parameterIndex, Object x, int sqlType) throws SQLException {
stmt.setObject(parameterIndex, x, sqlType);
return (This) this;
}
/**
* Sets the object.
*
* @param parameterIndex starts from 1, not 0.
* @param x
* @param sqlType
* @param scaleOrLength
* @return
* @throws SQLException
* @see java.sql.Types
*/
public This setObject(int parameterIndex, Object x, int sqlType, int scaleOrLength) throws SQLException {
stmt.setObject(parameterIndex, x, sqlType, scaleOrLength);
return (This) this;
}
/**
* Sets the object.
*
* @param parameterIndex
* @param x
* @param sqlType
* @return
* @throws SQLException
*/
public This setObject(int parameterIndex, Object x, SQLType sqlType) throws SQLException {
stmt.setObject(parameterIndex, x, sqlType);
return (This) this;
}
/**
* Sets the object.
*
* @param parameterIndex
* @param x
* @param sqlType
* @param scaleOrLength
* @return
* @throws SQLException
*/
public This setObject(int parameterIndex, Object x, SQLType sqlType, int scaleOrLength) throws SQLException {
stmt.setObject(parameterIndex, x, sqlType, scaleOrLength);
return (This) this;
}
/**
*
*
* @param parameterIndex
* @param x
* @param type
* @return
* @throws SQLException
*/
public This setObject(int parameterIndex, Object x, Type type) throws SQLException {
type.set(stmt, parameterIndex, x);
return (This) this;
}
// public This setParameter(final Jdbc.ParametersSetter super Stmt> paramSetter) throws SQLException {
// checkArgNotNull(paramSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramSetter.accept(stmt);
//
// noException = true;
// } finally {
// if (!noException) {
// close();
// }
// }
//
// return (This) this;
// }
//
// public This setParameter(final T parameter, final Jdbc.BiParametersSetter super Stmt, ? super T> paramSetter) throws SQLException {
// checkArgNotNull(paramSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramSetter.accept(stmt, parameter);
//
// noException = true;
// } finally {
// if (!noException) {
// close();
// }
// }
//
// return (This) this;
// }
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2) throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2, final String param3) throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
stmt.setString(3, param3);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2, final String param3, final String param4) throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
stmt.setString(3, param3);
stmt.setString(4, param4);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2, final String param3, final String param4, final String param5) throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
stmt.setString(3, param3);
stmt.setString(4, param4);
stmt.setString(5, param5);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2, final String param3, final String param4, final String param5, final String param6)
throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
stmt.setString(3, param3);
stmt.setString(4, param4);
stmt.setString(5, param5);
stmt.setString(6, param6);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @param param7
* @return
* @throws SQLException
*/
public This setParameters(final String param1, final String param2, final String param3, final String param4, final String param5, final String param6,
final String param7) throws SQLException {
stmt.setString(1, param1);
stmt.setString(2, param2);
stmt.setString(3, param3);
stmt.setString(4, param4);
stmt.setString(5, param5);
stmt.setString(6, param6);
stmt.setString(7, param7);
return (This) this;
}
// /**
// * Sets the parameters.
// *
// * @param param1
// * @param param2
// * @return
// * @throws SQLException
// * @deprecated to void error: java ambiguous method call with other {@code setParameters} methods
// */
// @Deprecated
// public This setParameters(final Object param1, final Object param2) throws SQLException {
// setObject(1, param1);
// setObject(2, param2);
//
// return (This) this;
// }
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4, final Object param5) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
setObject(5, param5);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4, final Object param5, final Object param6)
throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
setObject(5, param5);
setObject(6, param6);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @param param7
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4, final Object param5, final Object param6,
final Object param7) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
setObject(5, param5);
setObject(6, param6);
setObject(7, param7);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @param param7
* @param param8
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4, final Object param5, final Object param6,
final Object param7, final Object param8) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
setObject(5, param5);
setObject(6, param6);
setObject(7, param7);
setObject(8, param8);
return (This) this;
}
/**
* Sets the parameters.
*
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
* @param param6
* @param param7
* @param param8
* @param param9
* @return
* @throws SQLException
*/
public This setParameters(final Object param1, final Object param2, final Object param3, final Object param4, final Object param5, final Object param6,
final Object param7, final Object param8, final Object param9) throws SQLException {
setObject(1, param1);
setObject(2, param2);
setObject(3, param3);
setObject(4, param4);
setObject(5, param5);
setObject(6, param6);
setObject(7, param7);
setObject(8, param8);
setObject(9, param9);
return (This) this;
}
// public This setParameters(final T parameters, final Throwables.TriConsumer super This, ? super Stmt, ? super T, ? extends SQLException> paramsSetter)
// throws SQLException {
// checkArgNotNull(paramsSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramsSetter.accept((This) this, stmt, parameters);
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (This) this;
// }
/**
* Sets the parameters.
*
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This setParameters(final int[] parameters) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters);
}
/**
* Sets the parameters.
*
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This setParameters(final long[] parameters) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters);
}
/**
* Sets the parameters.
*
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This setParameters(final String[] parameters) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters);
}
/**
* Sets the parameters.
*
* @param
* @param parameters it should be an array of concrete types. For example: {@code String[]}, {@code Date[]}.
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This setParameters(final T[] parameters) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters);
}
/**
* Sets the parameters.
*
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters}.
* @throws SQLException
*/
public This setParameters(final Collection> parameters) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters);
}
/**
* Sets the parameters.
*
* @param
* @param parameters
* @param type
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This setParameters(final Collection extends T> parameters, final Class type) throws IllegalArgumentException, SQLException {
return settParameters(1, parameters, type);
}
/**
* Sets the parameters.
*
* @param paramsSetter
* @return
* @throws SQLException
*/
public This setParameters(final Jdbc.ParametersSetter super Stmt> paramsSetter) throws SQLException {
checkArgNotNull(paramsSetter, "paramsSetter");
boolean noException = false;
try {
paramsSetter.accept(stmt);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
// /**
// *
// * @param paramsSetter
// * @return
// * @throws SQLException
// */
// public This setParameters(final BiParametersSetter super This, ? super Stmt> paramsSetter) throws SQLException {
// checkArgNotNull(paramsSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramsSetter.accept((This) this, stmt);
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (This) this;
// }
/**
* Sets the parameters.
*
* @param
* @param parameters
* @param paramsSetter
* @return
* @throws SQLException
*/
public This setParameters(final T parameters, final Jdbc.BiParametersSetter super Stmt, ? super T> paramsSetter) throws SQLException {
checkArgNotNull(paramsSetter, "paramsSetter");
boolean noException = false;
try {
paramsSetter.accept(stmt, parameters);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param startParameterIndex
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final int[] parameters) throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
for (int param : parameters) {
stmt.setInt(startParameterIndex++, param);
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param startParameterIndex
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final long[] parameters) throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
for (long param : parameters) {
stmt.setLong(startParameterIndex++, param);
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param startParameterIndex
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final String[] parameters) throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
for (String param : parameters) {
stmt.setString(startParameterIndex++, param);
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param
* @param startParameterIndex
* @param parameters it should be an array of concrete types. For example: {@code String[]}, {@code Date[]}.
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final T[] parameters) throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
final Class> componentType = parameters.getClass().getComponentType();
if (stmtParameterClasses.contains(componentType)) {
final Type eleType = N.typeOf(componentType);
for (T param : parameters) {
eleType.set(stmt, startParameterIndex++, param);
}
} else {
for (Object param : parameters) {
setObject(startParameterIndex++, param);
}
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param startParameterIndex
* @param parameters
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final Collection> parameters) throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
for (Object param : parameters) {
setObject(startParameterIndex++, param);
}
return (This) this;
}
/**
* Sets the parameters.
*
* @param
* @param startParameterIndex
* @param parameters
* @param type
* @return
* @throws IllegalArgumentException if specified {@code parameters} or {@code type} is null.
* @throws SQLException
*/
public This settParameters(int startParameterIndex, final Collection extends T> parameters, final Class type)
throws IllegalArgumentException, SQLException {
checkArgNotNull(parameters, "parameters");
checkArgNotNull(type, "type");
final Type eleType = N.typeOf(type);
for (T param : parameters) {
eleType.set(stmt, startParameterIndex++, param);
}
return (This) this;
}
// /**
// *
// * @param paramsSetter
// * @return
// * @throws SQLException
// */
// public This setParameters(final BiParametersSetter super This, ? super Stmt> paramsSetter) throws SQLException {
// checkArgNotNull(paramsSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramsSetter.accept((This) this, stmt);
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (This) this;
// }
// public This setParameters(final T parameters, final Throwables.TriConsumer super This, ? super Stmt, ? super T, ? extends SQLException> paramsSetter)
// throws SQLException {
// checkArgNotNull(paramsSetter, "paramsSetter");
//
// boolean noException = false;
//
// try {
// paramsSetter.accept((This) this, stmt, parameters);
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (This) this;
// }
/**
*
* @param paramsSetter
* @return
* @throws SQLException
*/
@Beta
public This settParameters(Jdbc.ParametersSetter super This> paramsSetter) throws SQLException {
checkArgNotNull(paramsSetter, "paramsSetter");
boolean noException = false;
try {
paramsSetter.accept((This) this);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
/**
*
* @param
* @param parameters
* @param paramsSetter
* @return
* @throws SQLException
*/
@Beta
public This settParameters(final T parameters, Jdbc.BiParametersSetter super This, ? super T> paramsSetter) throws SQLException {
checkArgNotNull(paramsSetter, "paramsSetter");
boolean noException = false;
try {
paramsSetter.accept((This) this, parameters);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
/**
*
*
* @param sqlType
* @param parameterIndices
* @return
* @throws SQLException
* @see java.sql.Types
*/
@Beta
public This setNullForMultiPositions(final int sqlType, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setNull(parameterIndex, sqlType);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setBooleanForMultiPositions(final Boolean parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setBoolean(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setIntForMultiPositions(final Integer parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setInt(parameterIndex, parameterValue);
}
return (This) this;
}
// /**
// * Note: The reason for giving name: {@code setIntegerForMultiPositions}, not {@code setIntForMultiPositions} is because of error: The method setIntForMultiPositions(int, int[]) is ambiguous for the type .
// *
// * @param parameterValue
// * @param parameterIndices
// * @return
// * @throws SQLException
// */
// @Beta
// public This setIntegerForMultiPositions(final Integer parameterValue, final int... parameterIndices) throws SQLException {
// checkParameterIndices(parameterIndices);
//
// for (int parameterIndex : parameterIndices) {
// setInt(parameterIndex, parameterValue);
// }
//
// return (This) this;
// }
// ambiguous
// /**
// *
// *
// * @param parameterValue
// * @param parameterIndices
// * @return
// * @throws SQLException
// */
// @Beta
// public This setLongForMultiPositions(final long parameterValue, final int... parameterIndices) throws SQLException {
// checkParameterIndices(parameterIndices);
//
// for (int parameterIndex : parameterIndices) {
// setLong(parameterIndex, parameterValue);
// }
//
// return (This) this;
// }
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setLongForMultiPositions(final Long parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setLong(parameterIndex, parameterValue);
}
return (This) this;
}
// /**
// *
// *
// * @param parameterValue
// * @param parameterIndices
// * @return
// * @throws SQLException
// */
// @Beta
// public This setDoubleForMultiPositions(final double parameterValue, final int... parameterIndices) throws SQLException {
// checkParameterIndices(parameterIndices);
//
// for (int parameterIndex : parameterIndices) {
// setDouble(parameterIndex, parameterValue);
// }
//
// return (This) this;
// }
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setDoubleForMultiPositions(final Double parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setDouble(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setStringForMultiPositions(final String parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setString(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setDateForMultiPositions(final java.sql.Date parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setDate(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setDateForMultiPositions(final java.util.Date parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setDate(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setTimeForMultiPositions(final java.sql.Time parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setTime(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setTimeForMultiPositions(final java.util.Date parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setTime(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setTimestampForMultiPositions(final java.sql.Timestamp parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setTimestamp(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setTimestampForMultiPositions(final java.util.Date parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setTimestamp(parameterIndex, parameterValue);
}
return (This) this;
}
/**
*
*
* @param parameterValue
* @param parameterIndices
* @return
* @throws SQLException
*/
@Beta
public This setObjectForMultiPositions(final Object parameterValue, final int... parameterIndices) throws SQLException {
checkParameterIndices(parameterIndices);
for (int parameterIndex : parameterIndices) {
setObject(parameterIndex, parameterValue);
}
return (This) this;
}
private void checkParameterIndices(final int... parameterIndices) {
checkArg(N.notEmpty(parameterIndices), "'parameterIndices' can't be null or empty");
for (int parameterIndex : parameterIndices) {
if (parameterIndex <= 0) {
checkArg(parameterIndex > 0, "'parameterIndices' must all be positive. It can't be: " + N.toString(parameterIndices));
}
}
}
// /**
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// */
// Q setBatchParameters(final Collection batchParameters, BiParametersSetter super Q, ? super T> parametersSetter) throws SQLException {
// return setBatchParameters(batchParameters.iterator(), parametersSetter);
// }
//
// /**
// *
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// */
// Q setBatchParameters(final Iterator batchParameters, BiParametersSetter super Q, ? super T> parametersSetter) throws SQLException {
// checkArgNotNull(batchParameters, "batchParameters");
// checkArgNotNull(parametersSetter, "parametersSetter");
//
// boolean noException = false;
//
// try {
// if (isBatch) {
// stmt.clearBatch();
// }
//
// final Iterator iter = batchParameters;
//
// while (iter.hasNext()) {
// parametersSetter.accept((Q) this, iter.next());
// addBatch();
// isBatch = true;
// }
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (Q) this;
// }
/**
*
*
* @param batchParameters
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Collection> batchParameters) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
if (N.isEmpty(batchParameters)) {
return (This) this;
}
return addBatchParameters(batchParameters.iterator());
}
/**
*
* @param
* @param batchParameters single batch parameters.
* @param type
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Collection extends T> batchParameters, final Class type) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(type, "type");
if (N.isEmpty(batchParameters)) {
return (This) this;
}
return addBatchParameters(batchParameters.iterator(), type);
}
/**
*
*
* @param batchParameters
* @return
* @throws SQLException
*/
@Beta
@SuppressWarnings("rawtypes")
public This addBatchParameters(final Iterator> batchParameters) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
final Iterator> iter = batchParameters;
boolean noException = false;
try {
if (!iter.hasNext()) {
return (This) this;
}
final Object first = iter.next();
if (first instanceof Collection) {
setParameters((Collection) first);
addBatch();
while (iter.hasNext()) {
setParameters((Collection) iter.next());
addBatch();
}
} else if (first instanceof Object[]) {
setParameters((Object[]) first);
addBatch();
while (iter.hasNext()) {
setParameters((Object[]) iter.next());
addBatch();
}
} else {
stmt.setObject(1, first);
addBatch();
while (iter.hasNext()) {
stmt.setObject(1, iter.next());
addBatch();
}
}
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
/**
*
* @param
* @param batchParameters single batch parameters.
* @param type
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Iterator extends T> batchParameters, final Class type) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(type, "type");
final Iterator extends T> iter = batchParameters;
final Type setter = N.typeOf(type);
boolean noException = false;
try {
if (!iter.hasNext()) {
return (This) this;
}
while (iter.hasNext()) {
setter.set(stmt, 1, iter.next());
addBatch();
}
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
// /**
// *
// * @param batchParameters
// * @return
// * @throws SQLException
// */
// public Q addSingleBatchParameters(final Collection> batchParameters) throws SQLException {
// checkArgNotNull(batchParameters, "batchParameters");
//
// if (N.isEmpty(batchParameters)) {
// return (Q) this;
// }
//
// boolean noException = false;
//
// try {
// for (Object obj : batchParameters) {
// setObject(1, obj);
//
// addBatch();
// }
//
// isBatch = batchParameters.size() > 0;
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (Q) this;
// }
//
// /**
// *
// * @param batchParameters
// * @return
// * @throws SQLException
// */
// public Q addSingleBatchParameters(final Iterator> batchParameters) throws SQLException {
// checkArgNotNull(batchParameters, "batchParameters");
//
// return addSingleBatchParameters(Iterators.toList(batchParameters));
// }
/**
* @param
* @param batchParameters
* @param parametersSetter
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Collection extends T> batchParameters, Jdbc.BiParametersSetter super This, ? super T> parametersSetter)
throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(parametersSetter, "parametersSetter");
return addBatchParameters(batchParameters.iterator(), parametersSetter);
}
/**
*
* @param
* @param batchParameters
* @param parametersSetter
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Iterator extends T> batchParameters, Jdbc.BiParametersSetter super This, ? super T> parametersSetter)
throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(parametersSetter, "parametersSetter");
final This it = (This) this;
boolean noException = false;
try {
final Iterator extends T> iter = batchParameters;
while (iter.hasNext()) {
parametersSetter.accept(it, iter.next());
addBatch();
}
noException = true;
} finally {
if (!noException) {
close();
}
}
return it;
}
// /**
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// */
// @Beta
// public This addBatchParametters(final Collection batchParameters, BiParametersSetter super Stmt, ? super T> parametersSetter) throws SQLException {
// checkArgNotNull(batchParameters, "batchParameters");
// checkArgNotNull(parametersSetter, "parametersSetter");
//
// return addBatchParametters(batchParameters.iterator(), parametersSetter);
// }
//
// /**
// *
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// */
// @Beta
// public This addBatchParametters(final Iterator batchParameters, BiParametersSetter super Stmt, ? super T> parametersSetter) throws SQLException {
// checkArgNotNull(batchParameters, "batchParameters");
// checkArgNotNull(parametersSetter, "parametersSetter");
//
// boolean noException = false;
//
// try {
// final Iterator iter = batchParameters;
//
// while (iter.hasNext()) {
// parametersSetter.accept(stmt, iter.next());
// addBatch();
// isBatch = true;
// }
//
// noException = true;
// } finally {
// if (noException == false) {
// close();
// }
// }
//
// return (This) this;
// }
/**
* @param
* @param batchParameters
* @param parametersSetter
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Collection extends T> batchParameters,
Throwables.TriConsumer super This, ? super Stmt, ? super T, ? extends SQLException> parametersSetter) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(parametersSetter, "parametersSetter");
return addBatchParameters(batchParameters.iterator(), parametersSetter);
}
/**
*
* @param
* @param batchParameters
* @param parametersSetter
* @return
* @throws SQLException
*/
@Beta
public This addBatchParameters(final Iterator extends T> batchParameters,
Throwables.TriConsumer super This, ? super Stmt, ? super T, ? extends SQLException> parametersSetter) throws SQLException {
checkArgNotNull(batchParameters, "batchParameters");
checkArgNotNull(parametersSetter, "parametersSetter");
final This it = (This) this;
boolean noException = false;
try {
final Iterator extends T> iter = batchParameters;
while (iter.hasNext()) {
parametersSetter.accept(it, stmt, iter.next());
addBatch();
}
noException = true;
} finally {
if (!noException) {
close();
}
}
return it;
}
// /**
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// * @deprecated replaced by {@code addBatchParametters}
// */
// @Deprecated
// @Beta
// public Q addBatchParameters2(final Collection batchParameters, BiParametersSetter super S, ? super T> parametersSetter) throws SQLException {
// return addBatchParametters(batchParameters, parametersSetter);
// }
//
// /**
// *
// * @param
// * @param batchParameters
// * @param parametersSetter
// * @return
// * @throws SQLException
// * @deprecated replaced by {@code addBatchParametters}
// */
// @Deprecated
// @Beta
// public Q addBatchParameters2(final Iterator batchParameters, BiParametersSetter super S, ? super T> parametersSetter) throws SQLException {
// return addBatchParametters(batchParameters, parametersSetter);
// }
/**
* Adds the batch.
*
* @return
* @throws SQLException
*/
public This addBatch() throws SQLException {
addBatchAction.accept((This) this, stmt);
isBatch = true;
return (This) this;
}
This configAddBatchAction(final Throwables.BiConsumer super This, ? super Stmt, SQLException> addBatchAction) {
this.addBatchAction = addBatchAction;
return (This) this;
}
int defaultFetchDirection = -1;
int defaultFetchSize = -1;
int defaultQueryTimeout = -1;
int defaultMaxFieldSize = -1;
/**
* Sets the fetch direction.
*
* @param direction one of ResultSet.FETCH_FORWARD
,
* ResultSet.FETCH_REVERSE
, or ResultSet.FETCH_UNKNOWN
* @return
* @throws SQLException
* @see {@link java.sql.Statement#setFetchDirection(int)}
*/
public This setFetchDirection(FetchDirection direction) throws SQLException {
defaultFetchDirection = stmt.getFetchDirection();
stmt.setFetchDirection(direction.intValue);
isFetchDirectionSet = true;
return (This) this;
}
/**
*
* @return
* @throws SQLException
* @see {@link #setFetchDirection(FetchDirection)}
*/
public This setFetchDirectionToForward() throws SQLException {
return setFetchDirection(FetchDirection.FORWARD);
}
/**
* Sets the fetch size.
*
* @param fetchSize the number of rows to fetch
* @return
* @throws SQLException
*/
public This setFetchSize(int fetchSize) throws SQLException {
defaultFetchSize = stmt.getFetchSize();
stmt.setFetchSize(fetchSize);
return (This) this;
}
/**
* Sets the max field size.
*
* @param max
* @return
* @throws SQLException
*/
public This setMaxFieldSize(int max) throws SQLException {
defaultMaxFieldSize = stmt.getMaxFieldSize();
stmt.setMaxFieldSize(max);
return (This) this;
}
/**
* Sets the max rows.
*
* @param max
* @return
* @throws SQLException
*/
public This setMaxRows(int max) throws SQLException {
stmt.setMaxRows(max);
return (This) this;
}
/**
* Sets the large max rows.
*
* @param max
* @return
* @throws SQLException
*/
public This setLargeMaxRows(long max) throws SQLException {
stmt.setLargeMaxRows(max);
return (This) this;
}
/**
* Sets the query timeout.
*
* @param seconds
* @return
* @throws SQLException
*/
public This setQueryTimeout(int seconds) throws SQLException {
defaultQueryTimeout = stmt.getQueryTimeout();
stmt.setQueryTimeout(seconds);
return (This) this;
}
/**
* Configure this {@code PreparedQuery/Statement} by {@code stmtSetter}.
*
*
*
* final Throwables.Consumer commonStmtConfig = q -> q.setFetchSize(100).setQueryTimeout(60000);
*
* JdbcUtil.prepareQuery(sql).configStmt(commonStmtConfig).setParameters(parameters).list...
*
*
*
* @param stmtSetter
* @return
* @throws SQLException
*/
@Beta
public This configStmt(final Throwables.Consumer super Stmt, ? extends SQLException> stmtSetter) throws SQLException {
checkArgNotNull(stmtSetter, "stmtSetter");
boolean noException = false;
try {
stmtSetter.accept(stmt);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
/**
* Configure this {@code PreparedQuery/Statement} by {@code stmtSetter}.
*
*
*
* final Throwables.Consumer commonStmtConfig = (q, stmt) -> q.setFetchSize(100).setQueryTimeout(60000);
*
* JdbcUtil.prepareQuery(sql).configStmt(commonStmtConfig).setParameters(parameters).list...
*
*
*
* @param stmtSetter
* @return
* @throws SQLException
*/
@Beta
public This configStmt(final Throwables.BiConsumer super This, ? super Stmt, ? extends SQLException> stmtSetter) throws SQLException {
checkArgNotNull(stmtSetter, "stmtSetter");
boolean noException = false;
try {
stmtSetter.accept((This) this, stmt);
noException = true;
} finally {
if (!noException) {
close();
}
}
return (This) this;
}
int getFetchSize() throws SQLException {
return stmt.getFetchSize();
}
int getFetchDirection() throws SQLException {
return stmt.getFetchDirection();
}
/**
* Returns an {@code OptionalBoolean} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalBoolean}.
*
* @return
* @throws SQLException
*/
public OptionalBoolean queryForBoolean() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalBoolean.of(rs.getBoolean(1)) : OptionalBoolean.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
static final Type charType = TypeFactory.getType(char.class);
/**
* Returns an {@code OptionalChar} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalChar}.
*
* @return
* @throws SQLException
*/
public OptionalChar queryForChar() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
return OptionalChar.of(charType.get(rs, 1));
} else {
return OptionalChar.empty();
}
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalByte} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalByte}.
*
* @return
* @throws SQLException
*/
public OptionalByte queryForByte() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalByte.of(rs.getByte(1)) : OptionalByte.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalShort} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalShort}.
*
* @return
* @throws SQLException
*/
public OptionalShort queryForShort() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalShort.of(rs.getShort(1)) : OptionalShort.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalInt} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalInt}.
*
* @return
* @throws SQLException
*/
public OptionalInt queryForInt() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalInt.of(rs.getInt(1)) : OptionalInt.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalLong} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalLong}.
*
* @return
* @throws SQLException
*/
public OptionalLong queryForLong() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalLong.of(rs.getLong(1)) : OptionalLong.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalFloat} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalFloat}.
*
* @return
* @throws SQLException
*/
public OptionalFloat queryForFloat() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalFloat.of(rs.getFloat(1)) : OptionalFloat.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code OptionalDouble} describing the value in the first row/column if it exists, otherwise return an empty {@code OptionalDouble}.
*
* @return
* @throws SQLException
*/
public OptionalDouble queryForDouble() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? OptionalDouble.of(rs.getDouble(1)) : OptionalDouble.empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
public Nullable queryForString() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getString(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
private static final Type BIG_INTEGER_TYPE = Type.of(BigInteger.class);
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
@Beta
public Nullable queryForBigInteger() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(BIG_INTEGER_TYPE.get(rs, 1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
@Beta
public Nullable queryForBigDecimal() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getBigDecimal(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
public Nullable queryForDate() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getDate(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
public Nullable queryForTime() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getTime(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
public Nullable queryForTimestamp() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getTimestamp(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @return
* @throws SQLException
*/
public Nullable queryForBytes() throws SQLException {
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(rs.getBytes(1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @param the value type
* @param targetType
* @return
* @throws SQLException
*/
public Nullable queryForSingleResult(final Class extends V> targetType) throws SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
return queryForSingleResult(Type.of(targetType));
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @param
* @param targetType
* @return
* @throws SQLException
*/
public Nullable queryForSingleResult(final Type extends V> targetType) throws SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Nullable.of(targetType.get(rs, 1)) : Nullable. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code Optional} describing the value in the first row/column if it exists, otherwise return an empty {@code Optional}.
*
* @param the value type
* @param targetType
* @return
* @throws SQLException
*/
public Optional queryForSingleNonNull(final Class extends V> targetType) throws SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
return queryForSingleNonNull(Type.of(targetType));
}
/**
* Returns an {@code Optional} describing the value in the first row/column if it exists, otherwise return an empty {@code Optional}.
*
* @param
* @param targetType
* @return
* @throws SQLException
*/
public Optional queryForSingleNonNull(final Type extends V> targetType) throws SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return rs.next() ? Optional.of(targetType.get(rs, 1)) : Optional. empty();
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @param the value type
* @param targetType
* @return
* @throws DuplicatedResultException if more than one record found by the specified {@code id} (or {@code condition}).
* @throws SQLException
*/
public Nullable queryForUniqueResult(final Class extends V> targetType) throws DuplicatedResultException, SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
return queryForUniqueResult(Type.of(targetType));
}
/**
* Returns a {@code Nullable} describing the value in the first row/column if it exists, otherwise return an empty {@code Nullable}.
*
* @param
* @param targetType
* @return
* @throws DuplicatedResultException if more than one record found by the specified {@code id} (or {@code condition}).
* @throws SQLException
*/
public Nullable queryForUniqueResult(final Type extends V> targetType) throws DuplicatedResultException, SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
final Nullable result = rs.next() ? Nullable.of(targetType.get(rs, 1)) : Nullable. empty();
if (result.isPresent() && rs.next()) {
throw new DuplicatedResultException(
"At least two results found: " + Strings.concat(result.get(), ", ", N.convert(JdbcUtil.getColumnValue(rs, 1), targetType)));
}
return result;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Returns an {@code Optional} describing the value in the first row/column if it exists, otherwise return an empty {@code Optional}.
*
* @param the value type
* @param targetType
* @return
* @throws DuplicatedResultException if more than one record found by the specified {@code id} (or {@code condition}).
* @throws SQLException
*/
public Optional queryForUniqueNonNull(final Class extends V> targetType) throws DuplicatedResultException, SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
return queryForUniqueNonNull(Type.of(targetType));
}
/**
* Returns an {@code Optional} describing the value in the first row/column if it exists, otherwise return an empty {@code Optional}.
*
* @param the value type
* @param targetType
* @return
* @throws DuplicatedResultException if more than one record found by the specified {@code id} (or {@code condition}).
* @throws SQLException
*/
public Optional queryForUniqueNonNull(final Type extends V> targetType) throws DuplicatedResultException, SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
final Optional result = rs.next() ? Optional.of(targetType.get(rs, 1)) : Optional. empty();
if (result.isPresent() && rs.next()) {
throw new DuplicatedResultException(
"At least two results found: " + Strings.concat(result.get(), ", ", N.convert(JdbcUtil.getColumnValue(rs, 1), targetType)));
}
return result;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param targetType
* @param rs
* @return
* @throws SQLException
*/
private static T getRow(final Class extends T> targetType, ResultSet rs) throws SQLException {
final List columnLabels = JdbcUtil.getColumnLabelList(rs);
return Jdbc.BiRowMapper.to(targetType).apply(rs, columnLabels);
}
/**
* Retrieves the first {@code ResultSet}.
*
* @return
* @throws SQLException
*/
public DataSet query() throws SQLException {
return query(Jdbc.ResultExtractor.TO_DATA_SET);
}
/**
* Retrieves the first {@code ResultSet}.
*
* @param entityClass used to fetch fields from columns
* @return
* @throws SQLException
* @see {@link Jdbc.ResultExtractor#toDataSet(Class)}
*/
public DataSet query(final Class> entityClass) throws SQLException {
return query(Jdbc.ResultExtractor.toDataSet(entityClass));
}
/**
* Retrieves the first {@code ResultSet}.
*
* @param
* @param resultExtractor Don't save/return {@code ResultSet}. It will be closed after this call.
* @return
* @throws SQLException
*/
public R query(final Jdbc.ResultExtractor extends R> resultExtractor) throws SQLException {
checkArgNotNull(resultExtractor, "resultExtractor");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return JdbcUtil.checkNotResultSet(resultExtractor.apply(rs));
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Retrieves the first {@code ResultSet}.
*
* @param
* @param resultExtractor Don't save/return {@code ResultSet}. It will be closed after this call.
* @return
* @throws SQLException
*/
public R query(final Jdbc.BiResultExtractor extends R> resultExtractor) throws SQLException {
checkArgNotNull(resultExtractor, "resultExtractor");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
return JdbcUtil.checkNotResultSet(resultExtractor.apply(rs, JdbcUtil.getColumnLabelList(rs)));
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Retrieves at most two {@code ResultSets}.
*
* @param
* @param
* @param resultExtractor1 Don't save/return {@code ResultSet}. It will be closed after this call.
* @param resultExtractor2 Don't save/return {@code ResultSet}. It will be closed after this call.
* @return {@code R1/R2} extracted from the first two {@code ResultSets} returned by the executed procedure.
* @throws SQLException
*/
@Beta
public Tuple2 query2Resultsets(final Jdbc.BiResultExtractor extends R1> resultExtractor1,
final Jdbc.BiResultExtractor extends R2> resultExtractor2) throws SQLException {
checkArgNotNull(resultExtractor1, "resultExtractor1");
checkArgNotNull(resultExtractor2, "resultExtractor2");
assertNotClosed();
CheckedIterator iter = null;
try {
JdbcUtil.execute(stmt);
iter = JdbcUtil.iterateAllResultSets(stmt);
R1 result1 = null;
R2 result2 = null;
if (iter.hasNext()) {
result1 = JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor1);
}
if (iter.hasNext()) {
result2 = JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor2);
}
return Tuple.of(result1, result2);
} finally {
try {
if (iter != null) {
iter.close();
}
} finally {
closeAfterExecutionIfAllowed();
}
}
}
/**
* Retrieves at most three {@code ResultSets}.
*
* @param
* @param
* @param
* @param resultExtractor1 Don't save/return {@code ResultSet}. It will be closed after this call.
* @param resultExtractor2 Don't save/return {@code ResultSet}. It will be closed after this call.
* @param resultExtractor3 Don't save/return {@code ResultSet}. It will be closed after this call.
* @return {@code R1/R2/R3} extracted from the first three {@code ResultSets} returned by the executed procedure.
* @throws SQLException
*/
@Beta
public Tuple3 query3Resultsets(final Jdbc.BiResultExtractor extends R1> resultExtractor1,
final Jdbc.BiResultExtractor extends R2> resultExtractor2, final Jdbc.BiResultExtractor extends R3> resultExtractor3) throws SQLException {
checkArgNotNull(resultExtractor1, "resultExtractor1");
checkArgNotNull(resultExtractor2, "resultExtractor2");
checkArgNotNull(resultExtractor3, "resultExtractor3");
assertNotClosed();
CheckedIterator iter = null;
try {
JdbcUtil.execute(stmt);
iter = JdbcUtil.iterateAllResultSets(stmt);
R1 result1 = null;
R2 result2 = null;
R3 result3 = null;
if (iter.hasNext()) {
result1 = JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor1);
}
if (iter.hasNext()) {
result2 = JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor2);
}
if (iter.hasNext()) {
result3 = JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor3);
}
return Tuple.of(result1, result2, result3);
} finally {
try {
if (iter != null) {
iter.close();
}
} finally {
closeAfterExecutionIfAllowed();
}
}
}
/**
* Retrieves all the {@code ResultSets}.
*
* @return a list of {@code DataSet} extracted from all {@code ResultSets} returned by the executed procedure and a list of {@code Out Parameters}.
* @throws SQLException
*/
public List queryMultiResultsets() throws SQLException {
return queryMultiResultsets(ResultExtractor.TO_DATA_SET);
}
/**
* Retrieves all the {@code ResultSets}.
*
* @param
* @param resultExtractor Don't save/return {@code ResultSet}. It will be closed after this call.
* @return a list of {@code R} extracted from all {@code ResultSets} returned by the executed procedure and a list of {@code Out Parameters}.
* @throws SQLException
*/
public List queryMultiResultsets(final Jdbc.ResultExtractor extends R> resultExtractor) throws SQLException {
checkArgNotNull(resultExtractor, "resultExtractor");
assertNotClosed();
CheckedIterator iter = null;
try {
JdbcUtil.execute(stmt);
iter = JdbcUtil.iterateAllResultSets(stmt);
final List result = new ArrayList<>();
while (iter.hasNext()) {
result.add(JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor));
}
return result;
} finally {
try {
if (iter != null) {
iter.close();
}
} finally {
closeAfterExecutionIfAllowed();
}
}
}
/**
* Retrieves all the {@code ResultSets}.
*
* @param
* @param resultExtractor Don't save/return {@code ResultSet}. It will be closed after this call.
* @return a list of {@code R} extracted from all {@code ResultSets} returned by the executed procedure and a list of {@code Out Parameters}.
* @throws SQLException
*/
public List queryMultiResultsets(final Jdbc.BiResultExtractor extends R> resultExtractor) throws SQLException {
checkArgNotNull(resultExtractor, "resultExtractor");
assertNotClosed();
CheckedIterator iter = null;
try {
JdbcUtil.execute(stmt);
iter = JdbcUtil.iterateAllResultSets(stmt);
final List result = new ArrayList<>();
while (iter.hasNext()) {
result.add(JdbcUtil.extractAndCloseResultSet(iter.next(), resultExtractor));
}
return result;
} finally {
try {
if (iter != null) {
iter.close();
}
} finally {
closeAfterExecutionIfAllowed();
}
}
}
/**
*
* @param
* @param
* @param func
* @return
* @throws SQLException
* @throws E
*/
@Beta
public R queryThenApply(final Throwables.Function super DataSet, ? extends R, E> func) throws SQLException, E {
return func.apply(query());
}
/**
*
* @param
* @param
* @param entityClass used to fetch fields from columns
* @param func
* @return
* @throws SQLException
* @throws E
* @see {@link Jdbc.ResultExtractor#toDataSet(Class)}
*/
@Beta
public R queryThenApply(final Class> entityClass, final Throwables.Function super DataSet, ? extends R, E> func)
throws SQLException, E {
return func.apply(query(entityClass));
}
/**
*
*
* @param
* @param action
* @throws SQLException
* @throws E
*/
@Beta
public void queryThenAccept(final Throwables.Consumer super DataSet, E> action) throws SQLException, E {
action.accept(query());
}
/**
*
*
* @param
* @param entityClass used to fetch fields from columns
* @param action
* @throws SQLException
* @throws E
* @see {@link Jdbc.ResultExtractor#toDataSet(Class)}
*/
@Beta
public void queryThenAccept(final Class> entityClass, final Throwables.Consumer super DataSet, E> action) throws SQLException, E {
action.accept(query(entityClass));
}
// /**
// *
// * @param
// * @param targetType
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @deprecated replaced by {@code findOnlyOne}.
// */
// @Deprecated
// public Optional get(final Class extends T> targetType) throws DuplicatedResultException, SQLException {
// return Optional.ofNullable(gett(targetType));
// }
//
// /**
// *
// * @param
// * @param rowMapper
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
// * @deprecated replaced by {@code findOnlyOne}.
// */
// @Deprecated
// public Optional get(Jdbc.RowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
// return Optional.ofNullable(gett(rowMapper));
// }
//
// /**
// *
// * @param
// * @param rowMapper
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
// * @deprecated replaced by {@code findOnlyOne}.
// */
// @Deprecated
// public Optional get(Jdbc.BiRowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
// return Optional.ofNullable(gett(rowMapper));
// }
//
// /**
// *
// * @param
// * @param targetType
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @deprecated replaced by {@code findOnlyOneOrNull}.
// */
// @Deprecated
// public T gett(final Class extends T> targetType) throws DuplicatedResultException, SQLException {
// return findOnlyOneOrNull(targetType);
// }
//
// /**
// *
// * @param
// * @param rowMapper
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
// * @deprecated replaced by {@code findOnlyOneOrNull}.
// */
// @Deprecated
// public T gett(Jdbc.RowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
// return findOnlyOneOrNull(rowMapper);
// }
//
// /**
// *
// * @param
// * @param rowMapper
// * @return
// * @throws DuplicatedResultException If More than one record found by the query
// * @throws SQLException
// * @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
// * @deprecated replaced by {@code findOnlyOneOrNull}.
// */
// @Deprecated
// public T gett(Jdbc.BiRowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
// return findOnlyOneOrNull(rowMapper);
// }
/**
*
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
*/
public Optional> findOnlyOne() throws DuplicatedResultException, SQLException {
return findOnlyOne(Jdbc.BiRowMapper.TO_MAP);
}
/**
*
*
* @param
* @param targetType
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public Optional findOnlyOne(final Class extends T> targetType) throws DuplicatedResultException, SQLException {
return Optional.ofNullable(findOnlyOneOrNull(targetType));
}
/**
*
* @param
* @param rowMapper
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public Optional findOnlyOne(Jdbc.RowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
return Optional.ofNullable(findOnlyOneOrNull(rowMapper));
}
/**
*
* @param
* @param rowMapper
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public Optional findOnlyOne(Jdbc.BiRowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
return Optional.ofNullable(findOnlyOneOrNull(rowMapper));
}
/**
*
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
*/
public Map findOnlyOneOrNull() throws DuplicatedResultException, SQLException {
return findOnlyOneOrNull(Jdbc.BiRowMapper.TO_MAP);
}
/**
*
* @param
* @param targetType
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
*/
public T findOnlyOneOrNull(final Class extends T> targetType) throws DuplicatedResultException, SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
final T result = Objects.requireNonNull(getRow(targetType, rs));
if (rs.next()) {
throw new DuplicatedResultException("More than one record found by the query");
}
return result;
} else {
return null;
}
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowMapper
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public T findOnlyOneOrNull(Jdbc.RowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
final T result = Objects.requireNonNull(rowMapper.apply(rs));
if (rs.next()) {
throw new DuplicatedResultException("More than one record found by the query");
}
return result;
} else {
return null;
}
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowMapper
* @return
* @throws DuplicatedResultException If More than one record found by the query
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public T findOnlyOneOrNull(Jdbc.BiRowMapper extends T> rowMapper) throws DuplicatedResultException, SQLException, IllegalArgumentException {
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
final T result = Objects.requireNonNull(rowMapper.apply(rs, JdbcUtil.getColumnLabelList(rs)));
if (rs.next()) {
throw new DuplicatedResultException("More than one record found by the query");
}
return result;
} else {
return null;
}
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @return
* @throws SQLException
*/
public Optional> findFirst() throws SQLException {
return findFirst(Jdbc.BiRowMapper.TO_MAP);
}
/**
*
* @param
* @param targetType
* @return
* @throws SQLException
*/
public Optional findFirst(final Class extends T> targetType) throws SQLException {
return Optional.ofNullable(findFirstOrNull(targetType));
}
/**
*
* @param
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public Optional findFirst(Jdbc.RowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
return Optional.ofNullable(findFirstOrNull(rowMapper));
}
/**
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
* @deprecated Use {@code stream(RowFilter, RowMapper).first()} instead.
*/
@Deprecated
public Optional findFirst(final Jdbc.RowFilter rowFilter, Jdbc.RowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
return Optional.ofNullable(findFirstOrNull(rowFilter, rowMapper));
}
/**
*
* @param
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public Optional findFirst(Jdbc.BiRowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
return Optional.ofNullable(findFirstOrNull(rowMapper));
}
/**
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
* @deprecated Use {@code stream(BiRowFilter, BiRowMapper).first()} instead.
*/
@Deprecated
public Optional findFirst(final Jdbc.BiRowFilter rowFilter, Jdbc.BiRowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
return Optional.ofNullable(findFirstOrNull(rowFilter, rowMapper));
}
/**
*
* @return
* @throws SQLException
*/
public Map findFirstOrNull() throws SQLException {
return findFirstOrNull(Jdbc.BiRowMapper.TO_MAP);
}
/**
*
* @param
* @param targetType
* @return
* @throws SQLException
*/
public T findFirstOrNull(final Class extends T> targetType) throws SQLException {
checkArgNotNull(targetType, "targetType");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
return Objects.requireNonNull(getRow(targetType, rs));
}
return null;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public T findFirstOrNull(Jdbc.RowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
return Objects.requireNonNull(rowMapper.apply(rs));
}
return null;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
* @deprecated Use {@code stream(RowFilter, RowMapper).first()} instead.
*/
@Deprecated
public T findFirstOrNull(final Jdbc.RowFilter rowFilter, Jdbc.RowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
checkArgNotNull(rowFilter, "rowFilter");
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
while (rs.next()) {
if (rowFilter.test(rs)) {
return Objects.requireNonNull(rowMapper.apply(rs));
}
}
return null;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
*/
public T findFirstOrNull(Jdbc.BiRowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
if (rs.next()) {
return Objects.requireNonNull(rowMapper.apply(rs, JdbcUtil.getColumnLabelList(rs)));
} else {
return null;
}
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
* @throws IllegalArgumentException if {@code rowMapper} returns {@code null} for the found record.
* @deprecated Use {@code stream(BiRowFilter, BiRowMapper).first()} instead.
*/
@Deprecated
public T findFirstOrNull(final Jdbc.BiRowFilter rowFilter, Jdbc.BiRowMapper extends T> rowMapper) throws SQLException, IllegalArgumentException {
checkArgNotNull(rowFilter, "rowFilter");
checkArgNotNull(rowMapper, "rowMapper");
assertNotClosed();
try (ResultSet rs = executeQuery()) {
final List columnLabels = JdbcUtil.getColumnLabelList(rs);
while (rs.next()) {
if (rowFilter.test(rs, columnLabels)) {
return Objects.requireNonNull(rowMapper.apply(rs, columnLabels));
}
}
return null;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @return
* @throws SQLException
*/
public List> list() throws SQLException {
return list(Jdbc.BiRowMapper.TO_MAP);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param targetType
* @return
* @throws SQLException
*/
public List list(final Class extends T> targetType) throws SQLException {
return list(Jdbc.BiRowMapper.to(targetType));
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param targetType
* @param maxResult
* @return
* @throws SQLException
* @deprecated the result size should be limited in database server side by sql scripts.
*/
@Deprecated
public List list(final Class extends T> targetType, int maxResult) throws SQLException {
return list(Jdbc.BiRowMapper.to(targetType), maxResult);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowMapper
* @return
* @throws SQLException
*/
public List list(Jdbc.RowMapper extends T> rowMapper) throws SQLException {
return list(rowMapper, Integer.MAX_VALUE);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowMapper
* @param maxResult
* @return
* @throws SQLException
* @deprecated the result size should be limited in database server side by sql scripts.
*/
@Deprecated
public List list(Jdbc.RowMapper extends T> rowMapper, int maxResult) throws SQLException {
return list(Jdbc.RowFilter.ALWAYS_TRUE, rowMapper, maxResult);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
*/
public List list(final Jdbc.RowFilter rowFilter, Jdbc.RowMapper extends T> rowMapper) throws SQLException {
return list(rowFilter, rowMapper, Integer.MAX_VALUE);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowFilter
* @param rowMapper
* @param maxResult
* @return
* @throws SQLException
*/
public List list(final Jdbc.RowFilter rowFilter, Jdbc.RowMapper extends T> rowMapper, int maxResult) throws SQLException {
checkArgNotNull(rowFilter, "rowFilter");
checkArgNotNull(rowMapper, "rowMapper");
checkArg(maxResult >= 0, "'maxResult' can' be negative: " + maxResult);
assertNotClosed();
try (ResultSet rs = executeQuery()) {
final List result = new ArrayList<>();
while (maxResult > 0 && rs.next()) {
if (rowFilter.test(rs)) {
result.add(rowMapper.apply(rs));
maxResult--;
}
}
return result;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowMapper
* @return
* @throws SQLException
*/
public List list(Jdbc.BiRowMapper extends T> rowMapper) throws SQLException {
return list(rowMapper, Integer.MAX_VALUE);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowMapper
* @param maxResult
* @return
* @throws SQLException
* @deprecated the result size should be limited in database server side by sql scripts.
*/
@Deprecated
public List list(Jdbc.BiRowMapper extends T> rowMapper, int maxResult) throws SQLException {
return list(Jdbc.BiRowFilter.ALWAYS_TRUE, rowMapper, maxResult);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowFilter
* @param rowMapper
* @return
* @throws SQLException
*/
public List list(final Jdbc.BiRowFilter rowFilter, Jdbc.BiRowMapper extends T> rowMapper) throws SQLException {
return list(rowFilter, rowMapper, Integer.MAX_VALUE);
}
/**
* Lists the rows in the first {@code ResultSet}.
*
* @param
* @param rowFilter
* @param rowMapper
* @param maxResult
* @return
* @throws SQLException
*/
public List list(final Jdbc.BiRowFilter rowFilter, Jdbc.BiRowMapper extends T> rowMapper, int maxResult) throws SQLException {
checkArgNotNull(rowFilter, "rowFilter");
checkArgNotNull(rowMapper, "rowMapper");
checkArg(maxResult >= 0, "'maxResult' can' be negative: " + maxResult);
assertNotClosed();
try (ResultSet rs = executeQuery()) {
final List columnLabels = JdbcUtil.getColumnLabelList(rs);
final List result = new ArrayList<>();
while (maxResult > 0 && rs.next()) {
if (rowFilter.test(rs, columnLabels)) {
result.add(rowMapper.apply(rs, columnLabels));
maxResult--;
}
}
return result;
} finally {
closeAfterExecutionIfAllowed();
}
}
/**
* Lists all the {@code ResultSets}.
*
* @param
* @param targetType
* @return the {@code List} extracted from all {@code ResultSets} returned by the executed procedure.
* @throws SQLException
*/
public