@Override
  public int getTotalNumberOfRecords(Configuration conf) throws HiveJdbcDatabaseAccessException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      initializeDatabaseConnection(conf);
      String tableName = getQualifiedTableName(conf);
      // Always use JDBC_QUERY if available both for correctness and performance. JDBC_QUERY can be set by the user
      // or the CBO including pushdown optimizations. SELECT all query should be used only when JDBC_QUERY is null.
      String sql = firstNonNull(conf.get(Constants.JDBC_QUERY), selectAllFromTable(tableName));
      String countQuery = "SELECT COUNT(*) FROM (" + sql + ") tmptable";
      LOGGER.info("Query to execute is [{}]", countQuery);

      conn = dbcpDataSource.getConnection();
      ps = conn.prepareStatement(countQuery);
      rs = ps.executeQuery();
      if (rs.next()) {
        return rs.getInt(1);
      }
      else {
        LOGGER.warn("The count query {} did not return any results.", countQuery);
        throw new HiveJdbcDatabaseAccessException("Count query did not return any results.");
      }
    }
    catch (HiveJdbcDatabaseAccessException he) {
      throw he;
    }
    catch (Exception e) {
      LOGGER.error("Caught exception while trying to get the number of records: " + e.getMessage(), e);
      throw new HiveJdbcDatabaseAccessException(e);
    }
    finally {
      cleanupResources(conn, ps, rs);
    }
  }
@Test
  public void testGetTotalNumberOfRecords() throws HiveJdbcDatabaseAccessException {
    Configuration conf = buildConfiguration();
    DatabaseAccessor accessor = DatabaseAccessorFactory.getAccessor(conf);
    int numRecords = accessor.getTotalNumberOfRecords(conf);

    assertThat(numRecords, is(equalTo(5)));
  }