/** * Counts all entities of the specified type matching the given {@link Query} instance. The SQL runs as a * <code>SELECT COUNT(*)</code> to ensure maximum performance. * * @param type The type of the entities which should be counted. * @param query The {@link Query} instance used to determine the result set which will be counted. * @return The number of entities of the given type which match the specified query. */ public <K> int count(Class<? extends RawEntity<K>> type, Query query) throws SQLException { EntityInfo entityInfo = resolveEntityInfo(type); Connection connection = null; PreparedStatement stmt = null; ResultSet res = null; try { connection = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), true); stmt = provider.preparedStatement(connection, sql); provider.setQueryStatementProperties(stmt, query); query.setParameters(this, stmt); res = stmt.executeQuery(); return res.next() ? res.getInt(1) : -1; } finally { closeQuietly(res); closeQuietly(stmt); closeQuietly(connection); } }
/** * Counts all entities of the specified type matching the given {@link Query} instance. The SQL runs as a * <code>SELECT COUNT(*)</code> to ensure maximum performance. * * @param type The type of the entities which should be counted. * @param query The {@link Query} instance used to determine the result set which will be counted. * @return The number of entities of the given type which match the specified query. */ public <K> int count(Class<? extends RawEntity<K>> type, Query query) throws SQLException { EntityInfo entityInfo = resolveEntityInfo(type); Connection connection = null; PreparedStatement stmt = null; ResultSet res = null; try { connection = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), true); stmt = provider.preparedStatement(connection, sql); provider.setQueryStatementProperties(stmt, query); query.setParameters(this, stmt); res = stmt.executeQuery(); return res.next() ? res.getInt(1) : -1; } finally { closeQuietly(res); closeQuietly(stmt); closeQuietly(connection); } }
provider.setQueryStatementProperties(stmt, query);
provider.setQueryStatementProperties(stmt, query);
provider.setQueryStatementProperties(stmt, query);
provider.setQueryStatementProperties(stmt, query);