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.
package com.j256.ormlite.stmt;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DatabaseResultsMapper;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.dao.ObjectCache;
import com.j256.ormlite.dao.RawRowMapper;
import com.j256.ormlite.dao.RawRowObjectMapper;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;
import com.j256.ormlite.misc.IOUtils;
import com.j256.ormlite.misc.SqlExceptionUtil;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.StatementBuilder.StatementType;
import com.j256.ormlite.stmt.mapped.MappedCreate;
import com.j256.ormlite.stmt.mapped.MappedDelete;
import com.j256.ormlite.stmt.mapped.MappedDeleteCollection;
import com.j256.ormlite.stmt.mapped.MappedQueryForFieldEq;
import com.j256.ormlite.stmt.mapped.MappedRefresh;
import com.j256.ormlite.stmt.mapped.MappedUpdate;
import com.j256.ormlite.stmt.mapped.MappedUpdateId;
import com.j256.ormlite.support.CompiledStatement;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.support.DatabaseResults;
import com.j256.ormlite.table.TableInfo;
/**
* Executes SQL statements for a particular table in a particular database. Basically a call through to various mapped
* statement methods.
*
* @param
* The class that the code will be operating on.
* @param
* The class of the ID column associated with the class. The T class does not require an ID field. The class
* needs an ID parameter however so you can use Void or Object to satisfy the compiler.
* @author graywatson
*/
public class StatementExecutor implements GenericRowMapper {
private static Logger logger = LoggerFactory.getLogger(StatementExecutor.class);
private static final FieldType[] noFieldTypes = new FieldType[0];
private final DatabaseType databaseType;
private final TableInfo tableInfo;
private final Dao dao;
private MappedQueryForFieldEq mappedQueryForId;
private PreparedQuery preparedQueryForAll;
private MappedCreate mappedInsert;
private MappedUpdate mappedUpdate;
private MappedUpdateId mappedUpdateId;
private MappedDelete mappedDelete;
private MappedRefresh mappedRefresh;
private String countStarQuery;
private String ifExistsQuery;
private FieldType[] ifExistsFieldTypes;
private RawRowMapper rawRowMapper;
private final ThreadLocal localIsInBatchMode = new ThreadLocal() {
@Override
protected Boolean initialValue() {
return false;
}
};
/**
* Provides statements for various SQL operations.
*/
public StatementExecutor(DatabaseType databaseType, TableInfo tableInfo, Dao dao) {
this.databaseType = databaseType;
this.tableInfo = tableInfo;
this.dao = dao;
}
/**
* Return the object associated with the id or null if none. This does a SQL
* {@code SELECT col1,col2,... FROM ... WHERE ... = id} type query.
*/
public T queryForId(DatabaseConnection databaseConnection, ID id, ObjectCache objectCache) throws SQLException {
if (mappedQueryForId == null) {
mappedQueryForId = MappedQueryForFieldEq.build(dao, tableInfo, null);
}
return mappedQueryForId.execute(databaseConnection, id, objectCache);
}
/**
* Return the first object that matches the {@link PreparedStmt} or null if none.
*/
public T queryForFirst(DatabaseConnection databaseConnection, PreparedStmt preparedStmt, ObjectCache objectCache)
throws SQLException {
CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT);
DatabaseResults results = null;
try {
compiledStatement.setMaxRows(1);
results = compiledStatement.runQuery(objectCache);
if (results.first()) {
logger.debug("query-for-first of '{}' returned at least 1 result", preparedStmt.getStatement());
return preparedStmt.mapRow(results);
} else {
logger.debug("query-for-first of '{}' returned 0 results", preparedStmt.getStatement());
return null;
}
} finally {
IOUtils.closeThrowSqlException(results, "results");
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
}
}
/**
* Return a list of all of the data in the table. Should be used carefully if the table is large. Consider using the
* {@link Dao#iterator} if this is the case.
*/
public List queryForAll(ConnectionSource connectionSource, ObjectCache objectCache) throws SQLException {
prepareQueryForAll();
return query(connectionSource, preparedQueryForAll, objectCache);
}
/**
* Return a long value which is the number of rows in the table.
*/
public long queryForCountStar(DatabaseConnection databaseConnection) throws SQLException {
if (countStarQuery == null) {
StringBuilder sb = new StringBuilder(64);
sb.append("SELECT COUNT(*) FROM ");
if (tableInfo.getSchemaName() != null && tableInfo.getSchemaName().length() > 0){
databaseType.appendEscapedEntityName(sb, tableInfo.getSchemaName());
sb.append('.');
}
databaseType.appendEscapedEntityName(sb, tableInfo.getTableName());
countStarQuery = sb.toString();
}
long count = databaseConnection.queryForLong(countStarQuery);
logger.debug("query of '{}' returned {}", countStarQuery, count);
return count;
}
/**
* Return a long value from a prepared query.
*/
public long queryForLong(DatabaseConnection databaseConnection, PreparedStmt preparedStmt) throws SQLException {
CompiledStatement compiledStatement = preparedStmt.compile(databaseConnection, StatementType.SELECT_LONG);
DatabaseResults results = null;
try {
results = compiledStatement.runQuery(null);
if (results.first()) {
return results.getLong(0);
} else {
throw new SQLException("No result found in queryForLong: " + preparedStmt.getStatement());
}
} finally {
IOUtils.closeThrowSqlException(results, "results");
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
}
}
/**
* Return a long from a raw query with String[] arguments.
*/
public long queryForLong(DatabaseConnection databaseConnection, String query, String[] arguments)
throws SQLException {
logger.debug("executing raw query for long: {}", query);
if (arguments.length > 0) {
// need to do the (Object) cast to force args to be a single object
logger.trace("query arguments: {}", (Object) arguments);
}
CompiledStatement compiledStatement = null;
DatabaseResults results = null;
try {
compiledStatement = databaseConnection.compileStatement(query, StatementType.SELECT, noFieldTypes,
DatabaseConnection.DEFAULT_RESULT_FLAGS, false);
assignStatementArguments(compiledStatement, arguments);
results = compiledStatement.runQuery(null);
if (results.first()) {
return results.getLong(0);
} else {
throw new SQLException("No result found in queryForLong: " + query);
}
} finally {
IOUtils.closeThrowSqlException(results, "results");
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
}
}
/**
* Return a list of all of the data in the table that matches the {@link PreparedStmt}. Should be used carefully if
* the table is large. Consider using the {@link Dao#iterator} if this is the case.
*/
public List query(ConnectionSource connectionSource, PreparedStmt preparedStmt, ObjectCache objectCache)
throws SQLException {
SelectIterator iterator = buildIterator(/* no dao specified because no removes */null, connectionSource,
preparedStmt, objectCache, DatabaseConnection.DEFAULT_RESULT_FLAGS);
try {
List results = new ArrayList();
while (iterator.hasNextThrow()) {
results.add(iterator.nextThrow());
}
logger.debug("query of '{}' returned {} results", preparedStmt.getStatement(), results.size());
return results;
} finally {
IOUtils.closeThrowSqlException(iterator, "iterator");
}
}
/**
* Create and return a SelectIterator for the class using the default mapped query for all statement.
*/
public SelectIterator buildIterator(BaseDaoImpl classDao, ConnectionSource connectionSource,
int resultFlags, ObjectCache objectCache) throws SQLException {
prepareQueryForAll();
return buildIterator(classDao, connectionSource, preparedQueryForAll, objectCache, resultFlags);
}
/**
* Return a row mapper suitable for mapping 'select *' queries.
*/
public GenericRowMapper getSelectStarRowMapper() throws SQLException {
prepareQueryForAll();
return preparedQueryForAll;
}
/**
* Return a raw row mapper suitable for use with {@link Dao#queryRaw(String, RawRowMapper, String...)}.
*/
public RawRowMapper getRawRowMapper() {
if (rawRowMapper == null) {
rawRowMapper = new RawRowMapperImpl(dao);
}
return rawRowMapper;
}
/**
* Create and return an {@link SelectIterator} for the class using a prepared statement.
*/
public SelectIterator buildIterator(BaseDaoImpl classDao, ConnectionSource connectionSource,
PreparedStmt preparedStmt, ObjectCache objectCache, int resultFlags) throws SQLException {
DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName());
CompiledStatement compiledStatement = null;
try {
compiledStatement = preparedStmt.compile(connection, StatementType.SELECT, resultFlags);
SelectIterator iterator = new SelectIterator(tableInfo.getDataClass(), classDao, preparedStmt,
connectionSource, connection, compiledStatement, preparedStmt.getStatement(), objectCache);
connection = null;
compiledStatement = null;
return iterator;
} finally {
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
if (connection != null) {
connectionSource.releaseConnection(connection);
}
}
}
/**
* Return a results object associated with an internal iterator that returns String[] results.
*/
public GenericRawResults queryRaw(ConnectionSource connectionSource, String query, String[] arguments,
ObjectCache objectCache) throws SQLException {
logger.debug("executing raw query for: {}", query);
if (arguments.length > 0) {
// need to do the (Object) cast to force args to be a single object
logger.trace("query arguments: {}", (Object) arguments);
}
DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName());
CompiledStatement compiledStatement = null;
try {
compiledStatement = connection.compileStatement(query, StatementType.SELECT, noFieldTypes,
DatabaseConnection.DEFAULT_RESULT_FLAGS, false);
assignStatementArguments(compiledStatement, arguments);
GenericRawResults rawResults = new RawResultsImpl(connectionSource, connection, query,
String[].class, compiledStatement, this, objectCache);
compiledStatement = null;
connection = null;
return rawResults;
} finally {
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
if (connection != null) {
connectionSource.releaseConnection(connection);
}
}
}
/**
* Return a results object associated with an internal iterator is mapped by the user's rowMapper.
*/
public GenericRawResults queryRaw(ConnectionSource connectionSource, String query,
RawRowMapper rowMapper, String[] arguments, ObjectCache objectCache) throws SQLException {
logger.debug("executing raw query for: {}", query);
if (arguments.length > 0) {
// need to do the (Object) cast to force args to be a single object
logger.trace("query arguments: {}", (Object) arguments);
}
DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName());
CompiledStatement compiledStatement = null;
try {
compiledStatement = connection.compileStatement(query, StatementType.SELECT, noFieldTypes,
DatabaseConnection.DEFAULT_RESULT_FLAGS, false);
assignStatementArguments(compiledStatement, arguments);
RawResultsImpl rawResults = new RawResultsImpl(connectionSource, connection, query, String[].class,
compiledStatement, new UserRawRowMapper(rowMapper, this), objectCache);
compiledStatement = null;
connection = null;
return rawResults;
} finally {
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
if (connection != null) {
connectionSource.releaseConnection(connection);
}
}
}
/**
* Return a results object associated with an internal iterator is mapped by the user's rowMapper.
*/
public GenericRawResults queryRaw(ConnectionSource connectionSource, String query, DataType[] columnTypes,
RawRowObjectMapper rowMapper, String[] arguments, ObjectCache objectCache) throws SQLException {
logger.debug("executing raw query for: {}", query);
if (arguments.length > 0) {
// need to do the (Object) cast to force args to be a single object
logger.trace("query arguments: {}", (Object) arguments);
}
DatabaseConnection connection = connectionSource.getReadOnlyConnection(tableInfo.getTableName());
CompiledStatement compiledStatement = null;
try {
compiledStatement = connection.compileStatement(query, StatementType.SELECT, noFieldTypes,
DatabaseConnection.DEFAULT_RESULT_FLAGS, false);
assignStatementArguments(compiledStatement, arguments);
RawResultsImpl rawResults = new RawResultsImpl(connectionSource, connection, query, String[].class,
compiledStatement, new UserRawRowObjectMapper(rowMapper, columnTypes), objectCache);
compiledStatement = null;
connection = null;
return rawResults;
} finally {
IOUtils.closeThrowSqlException(compiledStatement, "compiled statement");
if (connection != null) {
connectionSource.releaseConnection(connection);
}
}
}
/**
* Return a results object associated with an internal iterator that returns Object[] results.
*/
public GenericRawResults