public List<Long> selectPostIds(final EnumSet<Post.Type> types,
                                   final Post.Status status,
                                   final Collection<TaxonomyTerm> terms,
                                   final Post.Sort sort,
                                   final Paging paging) throws SQLException {

      if(paging.limit < 1 || paging.start < 0) {
         return ImmutableList.of();
      }

      List<Long> ids = Lists.newArrayListWithExpectedSize(paging.limit < 1024 ? paging.limit : 1024);

      StringBuilder sql = new StringBuilder("SELECT ID FROM ");
      sql.append(postsTableName);
      if(terms != null && terms.size() > 0) {
         sql.append(",").append(termRelationshipsTableName);
         sql.append(" WHERE post_status=? AND object_id=ID AND ");
         if(terms.size() == 1) {
            sql.append("term_taxonomy_id=").append(terms.iterator().next().id);
         } else {
            sql.append("term_taxonomy_id IN (");
            Iterator<TaxonomyTerm> iter = terms.iterator();
            sql.append(iter.next().id);
            while(iter.hasNext()) {
               sql.append(",").append(iter.next().id);
            }
            sql.append(")");
         }
      } else {
         sql.append(" WHERE post_status=?");
      }

      appendPostTypes(types, sql);
      appendPagingSortSQL(sql, sort, paging);

      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      Timer.Context ctx = metrics.selectPostIdsTimer.time();
      try {
         conn = connectionSupplier.getConnection();
         stmt = conn.prepareStatement(sql.toString());
         stmt.setString(1, status.toString().toLowerCase());
         if(paging.interval != null) {
            stmt.setTimestamp(2, new Timestamp(paging.interval.getStartMillis()));
            stmt.setTimestamp(3, new Timestamp(paging.interval.getEndMillis()));
            stmt.setInt(4, paging.start);
            stmt.setInt(5, paging.limit);
         } else {
            stmt.setInt(2, paging.start);
            stmt.setInt(3, paging.limit);
         }
         rs = stmt.executeQuery();
         while(rs.next()) {
            ids.add(rs.getLong(1));
         }
      } finally {
         ctx.stop();
         SQLUtil.closeQuietly(conn, stmt, rs);
      }
      return ids;
   }
public List < Long > selectPostIds ( final EnumSet < Post . Type > types , final Post . Status status , final Collection < TaxonomyTerm > terms , final Post . Sort sort , final Paging paging ) throws SQLException { if ( paging . limit < 1 || paging . start < 0 ) { return ImmutableList . of ( ) ; } List < Long > ids = Lists . newArrayListWithExpectedSize ( paging . limit < 1024 ? paging . limit : 1024 ) ; StringBuilder sql = new StringBuilder ( "SELECT ID FROM " ) ; sql . append ( postsTableName ) ; if ( terms != null && terms . size ( ) > 0 ) { sql . append ( "," ) . append ( termRelationshipsTableName ) ; sql . append ( " WHERE post_status=? AND object_id=ID AND " ) ; if ( terms . size ( ) == 1 ) { sql . append ( "term_taxonomy_id=" ) . append ( terms . iterator ( ) . next ( ) . id ) ; } else { sql . append ( "term_taxonomy_id IN (" ) ; Iterator < TaxonomyTerm > iter = terms . iterator ( ) ; sql . append ( iter . next ( ) . id ) ; while ( iter . hasNext ( ) ) { sql . append ( "," ) . append ( iter . next ( ) . id ) ; } sql . append ( ")" ) ; } } else { sql . append ( " WHERE post_status=?" ) ; } appendPostTypes ( types , sql ) ; appendPagingSortSQL ( sql , sort , paging ) ; Connection conn = null ; PreparedStatement stmt = null ; ResultSet rs = null ; Timer . Context ctx = metrics . selectPostIdsTimer . time ( ) ; try { conn = connectionSupplier . getConnection ( ) ; stmt = conn . prepareStatement ( sql . toString ( ) ) ; stmt . setString ( 1 , status . toString ( ) . toLowerCase ( ) ) ; if ( paging . interval != null ) { stmt . setTimestamp ( 2 , new Timestamp ( paging . interval . getStartMillis ( ) ) ) ; stmt . setTimestamp ( 3 , new Timestamp ( paging . interval . getEndMillis ( ) ) ) ; stmt . setInt ( 4 , paging . start ) ; stmt . setInt ( 5 , paging . limit ) ; } else { stmt . setInt ( 2 , paging . start ) ; stmt . setInt ( 3 , paging . limit ) ; } rs = stmt . executeQuery ( ) ; while ( rs . next ( ) ) { ids . add ( rs . getLong ( 1 ) ) ; } } finally { ctx . stop ( ) ; SQLUtil . closeQuietly ( conn , stmt , rs ) ; } return ids ; }
Selects a page of posts with associated terms and a set of types.
@param types The post types. May be {@code null} or empty fo any type.
@param status The required post status.
@param terms A collection of terms attached to the posts.
@param sort The page sort.
@param paging The page range and interval.
@return The list of posts.
@throws SQLException on database error.