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

com.github.paganini2008.devtools.db4j.SqlRunner Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
/**
* Copyright 2021 Fred Feng ([email protected])

* 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.github.paganini2008.devtools.db4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.github.paganini2008.devtools.ArrayUtils;
import com.github.paganini2008.devtools.Observable;
import com.github.paganini2008.devtools.Observer;
import com.github.paganini2008.devtools.collection.Tuple;
import com.github.paganini2008.devtools.db4j.mapper.BeanPropertyRowMapper;
import com.github.paganini2008.devtools.db4j.mapper.ColumnIndexRowMapper;
import com.github.paganini2008.devtools.db4j.mapper.RowMapper;
import com.github.paganini2008.devtools.db4j.mapper.TupleRowMapper;
import com.github.paganini2008.devtools.jdbc.ConnectionFactory;
import com.github.paganini2008.devtools.jdbc.Cursor;
import com.github.paganini2008.devtools.jdbc.DefaultPageableSql;
import com.github.paganini2008.devtools.jdbc.JdbcUtils;
import com.github.paganini2008.devtools.jdbc.PageableException;
import com.github.paganini2008.devtools.jdbc.PageableQuery;
import com.github.paganini2008.devtools.jdbc.PageableResultSetSlice;
import com.github.paganini2008.devtools.jdbc.PageableSql;
import com.github.paganini2008.devtools.jdbc.PreparedStatementCallback;

/**
 * 
 * SqlRunner
 *
 * @author Fred Feng
 * @version 1.0
 */
public class SqlRunner {

	private TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistryImpl();

	public TypeHandlerRegistry getTypeHandlerRegistry() {
		return typeHandlerRegistry;
	}

	// --------------------- Query --------------------------
	public  T query(Connection connection, String sql, Object[] parameters, ResultSetExtractor extractor) throws SQLException {
		return query(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), extractor);
	}

	public  T query(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, ResultSetExtractor extractor)
			throws SQLException {
		return query(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry), extractor);
	}

	public  T query(Connection connection, String sql, PreparedStatementCallback callback, ResultSetExtractor extractor)
			throws SQLException {
		return query(connection, PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY),
				callback, extractor);
	}

	public  T query(Connection connection, PreparedStatementCreator statementCreator, final PreparedStatementCallback callback,
			final ResultSetExtractor extractor) throws SQLException {
		return execute(connection, statementCreator, new PreparedStatementExecutor() {
			public T execute(PreparedStatement ps) throws SQLException {
				if (callback != null) {
					callback.setValues(ps);
				}
				ResultSet rs = null;
				try {
					rs = ps.executeQuery();
					return extractor.extractData(rs);
				} finally {
					JdbcUtils.closeQuietly(rs);
				}
			}

			public void close(PreparedStatement ps) {
				JdbcUtils.closeQuietly(ps);
			}
		});
	}

	public  T queryForObject(Connection connection, String sql, Object[] parameters, Class requiredType) throws SQLException {
		return queryForObject(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), requiredType);
	}

	public  T queryForObject(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, Class requiredType)
			throws SQLException {
		return queryForObject(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				requiredType);
	}

	public  T queryForObject(Connection connection, String sql, PreparedStatementCallback callback, Class requiredType)
			throws SQLException {
		return queryForObject(connection, PreparedStatementCreatorUtils.forDefault(sql), callback, requiredType);
	}

	public  T queryForObject(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback,
			Class requiredType) throws SQLException {
		return queryForObject(connection, statementCreator, callback, new ColumnIndexRowMapper(requiredType));
	}

	public  T queryForObject(Connection connection, String sql, Object[] parameters, RowMapper rowMapper) throws SQLException {
		return queryForObject(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), rowMapper);
	}

	public  T queryForObject(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, RowMapper rowMapper)
			throws SQLException {
		return queryForObject(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				rowMapper);
	}

	public  T queryForObject(Connection connection, String sql, PreparedStatementCallback callback, RowMapper rowMapper)
			throws SQLException {
		return queryForObject(connection, PreparedStatementCreatorUtils.forDefault(sql), callback, rowMapper);
	}

	public  T queryForObject(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback,
			RowMapper rowMapper) throws SQLException {
		return query(connection, statementCreator, callback, new FirstRowResultSetExtractor(rowMapper, typeHandlerRegistry));
	}

	public Tuple queryForTuple(Connection connection, String sql, Object[] parameters) throws SQLException {
		return queryForTuple(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry));
	}

	public Tuple queryForTuple(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes) throws SQLException {
		return queryForTuple(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public Tuple queryForTuple(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return queryForTuple(connection, PreparedStatementCreatorUtils.forDefault(sql), callback);
	}

	public Tuple queryForTuple(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback)
			throws SQLException {
		return queryForObject(connection, statementCreator, callback, new TupleRowMapper());
	}

	public  List queryForList(Connection connection, String sql, Object[] parameters, RowMapper rowMapper) throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), rowMapper);
	}

	public  List queryForList(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, RowMapper rowMapper)
			throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry), rowMapper);
	}

	public  List queryForList(Connection connection, String sql, PreparedStatementCallback callback, RowMapper rowMapper)
			throws SQLException {
		return queryForList(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY), callback, rowMapper);
	}

	public  List queryForList(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback,
			RowMapper rowMapper) throws SQLException {
		return query(connection, statementCreator, callback, new RowMapperResultSetExtractor(rowMapper, typeHandlerRegistry));
	}

	public List queryForList(Connection connection, String sql, Object[] parameters) throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), new TupleRowMapper());
	}

	public List queryForList(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes) throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				new TupleRowMapper());
	}

	public List queryForList(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return queryForList(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY), callback,
				new TupleRowMapper());
	}

	public List queryForList(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback)
			throws SQLException {
		return queryForList(connection, statementCreator, callback, new TupleRowMapper());
	}

	public  List queryForList(Connection connection, String sql, Object[] parameters, Class objectClass) throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), objectClass);
	}

	public  List queryForList(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, Class objectClass)
			throws SQLException {
		return queryForList(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				objectClass);
	}

	public  List queryForList(Connection connection, String sql, PreparedStatementCallback callback, Class objectClass)
			throws SQLException {
		return queryForList(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY), callback,
				objectClass);
	}

	public  List queryForList(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementCallback callback,
			Class objectClass) throws SQLException {
		return queryForList(connection, statementCreator, callback, new BeanPropertyRowMapper(objectClass));
	}

	// ---------------------- Cursor ------------------------

	public Cursor queryForCursor(Connection connection, String sql, Object[] parameters) throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry));
	}

	public Cursor queryForCursor(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes) throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public Cursor queryForCursor(Connection connection, String sql) throws SQLException {
		return queryForCursor(connection, sql, (PreparedStatementCallback) null);
	}

	public Cursor queryForCursor(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return queryForCursor(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY), callback,
				new TupleRowMapper());
	}

	public  Cursor queryForCursor(Connection connection, String sql, Object[] parameters, Class objectClass) throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry),
				new BeanPropertyRowMapper(objectClass));
	}

	public  Cursor queryForCursor(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, Class objectClass)
			throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				new BeanPropertyRowMapper(objectClass));
	}

	public  Cursor queryForCursor(Connection connection, String sql, PreparedStatementCallback callback, Class objectClass)
			throws SQLException {
		return queryForCursor(connection, sql, callback, new BeanPropertyRowMapper(objectClass));
	}

	public  Cursor queryForCursor(Connection connection, String sql, Object[] parameters, RowMapper rowMapper)
			throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), rowMapper);
	}

	public  Cursor queryForCursor(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes,
			RowMapper rowMapper) throws SQLException {
		return queryForCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				rowMapper);
	}

	public  Cursor queryForCursor(Connection connection, String sql, PreparedStatementCallback callback, RowMapper rowMapper)
			throws SQLException {
		return queryForCursor(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY), callback,
				rowMapper);
	}

	public  Cursor queryForCursor(Connection connection, PreparedStatementCreator statementCreator,
			final PreparedStatementCallback callback, final RowMapper rowMapper) throws SQLException {
		final Observable closeable = Observable.unrepeatable();
		final AtomicBoolean success = new AtomicBoolean(true);
		try {
			return execute(connection, statementCreator, new PreparedStatementExecutor>() {

				public Cursor execute(PreparedStatement ps) throws SQLException {
					if (callback != null) {
						callback.setValues(ps);
					}
					ResultSet rs = null;
					try {
						rs = ps.executeQuery();
						ResultSetExtractor> extractor = new CursorResultSetExtractor(rowMapper, typeHandlerRegistry,
								closeable);
						return extractor.extractData(rs);
					} catch (SQLException e) {
						success.set(false);
						throw e;
					} finally {
						if (success.get()) {
							final ResultSet ref = rs;
							closeable.addObserver(new Observer() {
								public void update(Observable o, Object arg) {
									JdbcUtils.closeQuietly(ref);
								}
							});
						} else {
							JdbcUtils.closeQuietly(rs);
						}
					}
				}

				public void close(final PreparedStatement ps) {
					if (success.get()) {
						closeable.addObserver(new Observer() {
							public void update(Observable o, Object arg) {
								JdbcUtils.closeQuietly(ps);
							}
						});
					} else {
						JdbcUtils.closeQuietly(ps);
					}
				}

			});
		} finally {
			if (success.get()) {
				closeable.addObserver(new Observer() {
					public void update(Observable o, Object arg) {
						JdbcUtils.closeQuietly(connection);
					}
				});
			} else {
				JdbcUtils.closeQuietly(connection);
			}
		}
	}

	public Cursor queryForCachedCursor(Connection connection, String sql, Object[] parameters) throws SQLException {
		return queryForCachedCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry));
	}

	public Cursor queryForCachedCursor(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes)
			throws SQLException {
		return queryForCachedCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public Cursor queryForCachedCursor(Connection connection, String sql) throws SQLException {
		return queryForCachedCursor(connection, sql, (PreparedStatementCallback) null);
	}

	public Cursor queryForCachedCursor(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return queryForCachedCursor(connection, sql, callback, new TupleRowMapper());
	}

	public  Cursor queryForCachedCursor(Connection connection, String sql, Object[] parameters, RowMapper rowMapper)
			throws SQLException {
		return queryForCachedCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), rowMapper);
	}

	public  Cursor queryForCachedCursor(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes,
			RowMapper rowMapper) throws SQLException {
		return queryForCachedCursor(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry),
				rowMapper);
	}

	public  Cursor queryForCachedCursor(Connection connection, String sql, final PreparedStatementCallback callback,
			final RowMapper rowMapper) throws SQLException {
		return queryForCachedCursor(connection,
				PreparedStatementCreatorUtils.forQuery(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY), callback,
				rowMapper);
	}

	public  Cursor queryForCachedCursor(Connection connection, PreparedStatementCreator statementCreator,
			final PreparedStatementCallback callback, final RowMapper rowMapper) throws SQLException {
		try {
			return execute(connection, statementCreator, new PreparedStatementExecutor>() {

				public Cursor execute(PreparedStatement ps) throws SQLException {
					if (callback != null) {
						callback.setValues(ps);
					}
					ResultSet rs = null;
					try {
						rs = ps.executeQuery();
						ResultSetExtractor> extractor = new CachedCursorResultSetExtractor(rowMapper, typeHandlerRegistry);
						return extractor.extractData(rs);
					} finally {
						JdbcUtils.closeQuietly(rs);
					}
				}

				public void close(PreparedStatement ps) {
					JdbcUtils.closeQuietly(ps);
				}

			});
		} finally {
			JdbcUtils.closeQuietly(connection);
		}
	}

	// ------------------------- Pageable Query -------------------

	public  PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, Object[] parameters, RowMapper rowMapper) {
		return new PageableQueryImpl(connectionFactory, new DefaultPageableSql(sql),
				PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), rowMapper, this);
	}

	public  PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, Object[] parameters, JdbcType[] jdbcTypes,
			RowMapper rowMapper) {
		return new PageableQueryImpl(connectionFactory, new DefaultPageableSql(sql),
				PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry), rowMapper, this);
	}

	public  PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, PreparedStatementCallback callback,
			RowMapper rowMapper) {
		return new PageableQueryImpl(connectionFactory, new DefaultPageableSql(sql), callback, rowMapper, this);
	}

	public  PageableQuery queryForPage(ConnectionFactory connectionFactory, PageableSql pageableSql,
			PreparedStatementCallback callback, RowMapper rowMapper) {
		return new PageableQueryImpl(connectionFactory, pageableSql, callback, rowMapper, this);
	}

	public PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, Object[] parameters, JdbcType[] jdbcTypes) {
		return queryForPage(connectionFactory, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, Object[] parameters) {
		return queryForPage(connectionFactory, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry));
	}

	public PageableQuery queryForPage(ConnectionFactory connectionFactory, String sql, PreparedStatementCallback callback) {
		return queryForPage(connectionFactory, new DefaultPageableSql(sql), callback);
	}

	public PageableQuery queryForPage(ConnectionFactory connectionFactory, PageableSql pageableSql,
			PreparedStatementCallback callback) {
		return queryForPage(connectionFactory, pageableSql, callback, new TupleRowMapper());
	}

	// ------------------------- Update ---------------------------

	public int update(Connection connection, String sql, Object[] parameters, GeneratedKey generatedKey) throws SQLException {
		return update(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry), generatedKey);
	}

	public int update(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes, GeneratedKey generatedKey)
			throws SQLException {
		return update(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry), generatedKey);
	}

	public int update(Connection connection, String sql, PreparedStatementCallback callback, GeneratedKey generatedKey)
			throws SQLException {
		return update(connection,
				ArrayUtils.isNotEmpty(generatedKey.getKeyNames())
						? PreparedStatementCreatorUtils.forColumnNames(sql, generatedKey.getKeyNames())
						: PreparedStatementCreatorUtils.forGeneratedKey(sql),
				callback, generatedKey);
	}

	public int update(Connection connection, PreparedStatementCreator statementCreator, final PreparedStatementCallback callback,
			final GeneratedKey generatedKey) throws SQLException {
		return execute(connection, statementCreator, new PreparedStatementExecutor() {
			@SuppressWarnings("unchecked")
			public Integer execute(PreparedStatement ps) throws SQLException {
				if (callback != null) {
					callback.setValues(ps);
				}
				int effected = ps.executeUpdate();
				if (effected > 0) {
					ResultSet rs = ps.getGeneratedKeys();
					if (rs != null) {
						try {
							ResultSetExtractor extractor = new FirstRowResultSetExtractor(new TupleRowMapper(),
									typeHandlerRegistry);
							Tuple keys = extractor.extractData(rs);
							if (keys != null) {
								generatedKey.setKeys((Map) keys);
							}
						} finally {
							JdbcUtils.closeQuietly(rs);
						}
					}
				}
				return effected;
			}

			public void close(PreparedStatement ps) {
				JdbcUtils.closeQuietly(ps);
			}
		});
	}

	public int update(Connection connection, String sql, Object[] parameters) throws SQLException {
		return update(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, typeHandlerRegistry));
	}

	public int update(Connection connection, String sql, Object[] parameters, JdbcType[] jdbcTypes) throws SQLException {
		return update(connection, sql, PreparedStatementCallbackUtils.prepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public int update(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return update(connection, PreparedStatementCreatorUtils.forDefault(sql), callback);
	}

	public int update(Connection connection, PreparedStatementCreator statementCreator, final PreparedStatementCallback callback)
			throws SQLException {
		return execute(connection, statementCreator, new PreparedStatementExecutor() {
			public Integer execute(PreparedStatement ps) throws SQLException {
				if (callback != null) {
					callback.setValues(ps);
				}
				return ps.executeUpdate();
			}

			public void close(PreparedStatement ps) {
				JdbcUtils.closeQuietly(ps);
			}
		});
	}

	// ---------------------- Batch Update------------------------------

	public int[] batchUpdate(Connection connection, String sql, List parameters, JdbcType[] jdbcTypes) throws SQLException {
		return batchUpdate(connection, sql, PreparedStatementCallbackUtils.batchPrepare(parameters, jdbcTypes, typeHandlerRegistry));
	}

	public int[] batchUpdate(Connection connection, String sql, List parameters) throws SQLException {
		return batchUpdate(connection, sql, PreparedStatementCallbackUtils.batchPrepare(parameters, typeHandlerRegistry));
	}

	public int[] batchUpdate(Connection connection, String sql, PreparedStatementCallback callback) throws SQLException {
		return batchUpdate(connection, PreparedStatementCreatorUtils.forDefault(sql), callback);
	}

	public int[] batchUpdate(Connection connection, PreparedStatementCreator statementCreator, final PreparedStatementCallback callback)
			throws SQLException {
		return execute(connection, statementCreator, new PreparedStatementExecutor() {
			public int[] execute(PreparedStatement ps) throws SQLException {
				if (callback != null) {
					callback.setValues(ps);
				}
				return ps.executeBatch();
			}

			public void close(PreparedStatement ps) {
				JdbcUtils.closeQuietly(ps);
			}
		});
	}

	public  T execute(Connection connection, PreparedStatementCreator statementCreator, PreparedStatementExecutor statementExecutor)
			throws SQLException {
		PreparedStatement ps = null;
		try {
			ps = statementCreator.createPreparedStatement(connection);
			return statementExecutor.execute(ps);
		} finally {
			statementExecutor.close(ps);
		}
	}

	static boolean useCachedRowSet = true;

	public static void setUseCachedRowSet(boolean useCachedRowSet) {
		SqlRunner.useCachedRowSet = useCachedRowSet;
	}

	/**
	 * 
	 * PageableQueryImpl
	 *
	 * @author Fred Feng
	 * @version 1.0
	 */
	private static class PageableQueryImpl extends PageableResultSetSlice implements PageableQuery {

		private final ConnectionFactory connectionFactory;
		private final PageableSql pageableSql;
		private final PreparedStatementCallback callback;
		private final RowMapper rowMapper;
		private final SqlRunner sqlRunner;

		private PageableQueryImpl(ConnectionFactory connectionFactory, PageableSql pageableSql, PreparedStatementCallback callback,
				RowMapper rowMapper, SqlRunner sqlRunner) {
			this.connectionFactory = connectionFactory;
			this.pageableSql = pageableSql;
			this.callback = callback;
			this.rowMapper = rowMapper;
			this.sqlRunner = sqlRunner;
		}

		@Override
		public int rowCount() {
			final String sql = pageableSql.countableSql();
			Connection connection = null;
			try {
				connection = connectionFactory.getConnection();
				return sqlRunner.queryForObject(connection, sql, callback, Integer.class);
			} catch (SQLException e) {
				throw new PageableException(e.getMessage(), e);
			} finally {
				JdbcUtils.closeQuietly(connection);
			}
		}

		@Override
		public Cursor cursor(int maxResults, int firstResult) {
			final String sql = pageableSql.pageableSql(maxResults, firstResult);
			Connection connection = null;
			try {
				connection = connectionFactory.getConnection();
				if (useCachedRowSet) {
					return sqlRunner.queryForCachedCursor(connection, sql, callback, rowMapper);
				}
				return sqlRunner.queryForCursor(connection, sql, callback, rowMapper);
			} catch (SQLException e) {
				throw new PageableException(e.getMessage(), e);
			}
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy