Search in sources :

Example 1 with IPagingPlugin

use of org.jaffa.persistence.engines.jdbcengine.paging.IPagingPlugin in project jaffa-framework by jaffa-projects.

the class JdbcBridge method executeQuery.

/**
 * Executes the query based on the Criteria object.
 * @param criteria The input Criteria.
 * @param dataSource The DataSource against which the query is to be executed.
 * @throws IOException if any error occurs while extracting the String from the criteria.
 * @throws SQLException if any database error occurs.
 * @throws PostLoadFailedException if any error is thrown in the PostLoad trigger of the persistent object.
 * @throws DataSourceCursorRuntimeException if any error occurs while molding the row into the Persistent object.
 * @return a Collection of Persistent objects as a result of the query.
 */
public static Collection executeQuery(Criteria criteria, DataSource dataSource) throws IOException, SQLException, PostLoadFailedException, DataSourceCursorRuntimeException {
    ClassMetaData classMetaData = criteria.getTable() != null ? ConfigurationService.getInstance().getMetaData(criteria.getTable()) : null;
    // The optimization will also be used, if the criteria contains a count(*) for performing an existence-check based on the primary-key
    if (usePreparedStatement(dataSource) && hasPKCriteriaOnly(criteria, classMetaData)) {
        if (criteria.getFunctionEntries() == null || criteria.getFunctionEntries().size() == 0) {
            // Check the cache before executing the query
            IPersistent object = dataSource.lookupObjectCache(criteria);
            if (object != null) {
                if (log.isDebugEnabled())
                    log.debug("Found the cached object: " + object);
                Collection output = new LinkedList();
                output.add(object);
                return output;
            }
            if (log.isDebugEnabled())
                log.debug("Optimized to use the PreparedStatement for querying by primary-key");
            return executeFindByPKWithPreparedStatement(criteria, dataSource, classMetaData);
        } else if (criteria.getFunctionEntries().size() == 1) {
            Criteria.FunctionEntry fe = (Criteria.FunctionEntry) criteria.getFunctionEntries().iterator().next();
            if (fe.getName() == null && fe.getFunction() == Criteria.FUNCTION_COUNT) {
                // Check the cache before executing the query
                IPersistent object = dataSource.lookupObjectCache(criteria);
                if (object != null) {
                    if (log.isDebugEnabled())
                        log.debug("Found the cached object for existence-check by primary-key: " + object);
                    Collection output = new LinkedList();
                    Map map = new HashMap();
                    map.put(fe.getId(), 1);
                    output.add(map);
                    return output;
                }
                if (log.isDebugEnabled())
                    log.debug("Optimized to use the PreparedStatement for existence-check by primary-key");
                return executeExistsWithPreparedStatement(criteria, dataSource, classMetaData);
            }
        }
    }
    // Utilize the pagingPlugin if the Criteria contains values for the firstResult and/or maxResults properties
    IPagingPlugin pagingPlugin = createPagingPlugin(criteria, dataSource.getEngineType());
    if (usePreparedStatement(dataSource)) {
        // Perform a query using a PreparedStatement
        PreparedStatement pstmt = QueryStatementHelper.getPreparedStatement(criteria, dataSource, pagingPlugin);
        return dataSource.executeQuery(pstmt, classMetaData, criteria, (criteria.getLocking() == Criteria.LOCKING_PARANOID ? QUERY_TIMEOUT_FOR_LOCKING : 0), pagingPlugin);
    } else {
        // Perform a query using a regular Statement
        String sql = QueryStatementHelper.getSQL(criteria, dataSource, pagingPlugin);
        return dataSource.executeQuery(sql, classMetaData, criteria, (criteria.getLocking() == Criteria.LOCKING_PARANOID ? QUERY_TIMEOUT_FOR_LOCKING : 0), pagingPlugin);
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) PreparedStatement(java.sql.PreparedStatement) Criteria(org.jaffa.persistence.Criteria) IPagingPlugin(org.jaffa.persistence.engines.jdbcengine.paging.IPagingPlugin) ClassMetaData(org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 Criteria (org.jaffa.persistence.Criteria)1 IPersistent (org.jaffa.persistence.IPersistent)1 ClassMetaData (org.jaffa.persistence.engines.jdbcengine.configservice.ClassMetaData)1 IPagingPlugin (org.jaffa.persistence.engines.jdbcengine.paging.IPagingPlugin)1