@Test void countQueryOneToManyEquals() throws Exception { String oneToManyAttrName = "oneToManyAttr"; String refIdAttrName = "refEntityId"; Attribute refIdAttr = mock(Attribute.class); when(refIdAttr.getName()).thenReturn(refIdAttrName); when(refIdAttr.getDataType()).thenReturn(INT); String xrefAttrName = "xrefAttr"; Attribute xrefAttr = mock(Attribute.class); when(xrefAttr.getName()).thenReturn(xrefAttrName); EntityType refEntityMeta = mock(EntityType.class); when(refEntityMeta.getId()).thenReturn("refEntityId"); when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr); String idAttrName = "entityId"; Attribute idAttr = mock(Attribute.class); when(idAttr.getName()).thenReturn(idAttrName); Attribute oneToManyAttr = mock(Attribute.class); when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName); when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY); when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta); when(oneToManyAttr.isMappedBy()).thenReturn(true); when(oneToManyAttr.getMappedBy()).thenReturn(xrefAttr); when(entityType.getId()).thenReturn("entityId"); when(entityType.getIdAttribute()).thenReturn(idAttr); doReturn(oneToManyAttr).when(entityType).getAttribute(oneToManyAttrName); int queryValue = 2; QueryRule queryRule = new QueryRule(oneToManyAttrName, EQUALS, queryValue); when(query.getRules()).thenReturn(singletonList(queryRule)); String sql = "SELECT COUNT(DISTINCT this.\"entityId\") FROM \"entityId#fc2928f6\" AS this LEFT JOIN \"refEntityId#07f902bf\" AS \"oneToManyAttr_filter1\" ON (this.\"entityId\" = \"oneToManyAttr_filter1\".\"xrefAttr\") WHERE \"oneToManyAttr_filter1\".\"refEntityId\" = ?"; long count = 123L; when(jdbcTemplate.queryForObject(sql, new Object[] {queryValue}, Long.class)).thenReturn(count); assertEquals(count, postgreSqlRepo.count(query)); }
@Override public long count(Query<Entity> q) { List<Object> parameters = Lists.newArrayList(); String sql = getSqlCount(entityType, q, parameters); if (LOG.isDebugEnabled()) { LOG.debug("Counting [{}] rows for query [{}]", getName(), q); if (LOG.isTraceEnabled()) { LOG.trace("SQL: {}, parameters: {}", sql, parameters); } } return jdbcTemplate.queryForObject( sql, parameters.toArray(new Object[parameters.size()]), Long.class); }
PostgreSqlRepository extends AbstractRepository { @Override public long count(Query<Entity> q) { List<Object> parameters = Lists.newArrayList(); String sql = getSqlCount(entityType, q, parameters); if (LOG.isDebugEnabled()) { LOG.debug("Counting [{}] rows for query [{}]", getName(), q); if (LOG.isTraceEnabled()) { LOG.trace("SQL: {}, parameters: {}", sql, parameters); } } return jdbcTemplate.queryForObject( sql, parameters.toArray(new Object[parameters.size()]), Long.class); } }
PostgreSqlRepository extends AbstractRepository { @Override public long count(Query<Entity> q) { List<Object> parameters = Lists.newArrayList(); String sql = getSqlCount(entityType, q, parameters); if (LOG.isDebugEnabled()) { LOG.debug("Counting [{}] rows for query [{}]", getName(), q); if (LOG.isTraceEnabled()) { LOG.trace("SQL: {}, parameters: {}", sql, parameters); } } return jdbcTemplate.queryForObject( sql, parameters.toArray(new Object[parameters.size()]), Long.class); } PostgreSqlRepository(
      PostgreSqlEntityFactory postgreSqlEntityFactory,
      JdbcTemplate jdbcTemplate,
      DataSource dataSource,
      EntityType entityType); }
PostgreSqlRepository extends AbstractRepository { @Override public long count(Query<Entity> q) { List<Object> parameters = Lists.newArrayList(); String sql = getSqlCount(entityType, q, parameters); if (LOG.isDebugEnabled()) { LOG.debug("Counting [{}] rows for query [{}]", getName(), q); if (LOG.isTraceEnabled()) { LOG.trace("SQL: {}, parameters: {}", sql, parameters); } } return jdbcTemplate.queryForObject( sql, parameters.toArray(new Object[parameters.size()]), Long.class); } PostgreSqlRepository(
      PostgreSqlEntityFactory postgreSqlEntityFactory,
      JdbcTemplate jdbcTemplate,
      DataSource dataSource,
      EntityType entityType); @Override Iterator<Entity> iterator(); @Override Set<RepositoryCapability> getCapabilities(); @Override Set<Operator> getQueryOperators(); @Override EntityType getEntityType(); @Override long count(Query<Entity> q); @Override Stream<Entity> findAll(Query<Entity> q); @Override Entity findOne(Query<Entity> q); @Override Entity findOneById(Object id); @Override Entity findOneById(Object id, Fetch fetch); @Override void update(Entity entity); @Override void update(Stream<Entity> entities); @Override void delete(Entity entity); @Override void delete(Stream<Entity> entities); @Override void deleteById(Object id); @Override void deleteAll(Stream<Object> ids); @Override void deleteAll(); @Override void add(Entity entity); @Override Integer add(Stream<Entity> entities); @Override void forEachBatched(Fetch fetch, Consumer<List<Entity>> consumer, int batchSize); }
PostgreSqlRepository extends AbstractRepository { @Override public long count(Query<Entity> q) { List<Object> parameters = Lists.newArrayList(); String sql = getSqlCount(entityType, q, parameters); if (LOG.isDebugEnabled()) { LOG.debug("Counting [{}] rows for query [{}]", getName(), q); if (LOG.isTraceEnabled()) { LOG.trace("SQL: {}, parameters: {}", sql, parameters); } } return jdbcTemplate.queryForObject( sql, parameters.toArray(new Object[parameters.size()]), Long.class); } PostgreSqlRepository(
      PostgreSqlEntityFactory postgreSqlEntityFactory,
      JdbcTemplate jdbcTemplate,
      DataSource dataSource,
      EntityType entityType); @Override Iterator<Entity> iterator(); @Override Set<RepositoryCapability> getCapabilities(); @Override Set<Operator> getQueryOperators(); @Override EntityType getEntityType(); @Override long count(Query<Entity> q); @Override Stream<Entity> findAll(Query<Entity> q); @Override Entity findOne(Query<Entity> q); @Override Entity findOneById(Object id); @Override Entity findOneById(Object id, Fetch fetch); @Override void update(Entity entity); @Override void update(Stream<Entity> entities); @Override void delete(Entity entity); @Override void delete(Stream<Entity> entities); @Override void deleteById(Object id); @Override void deleteAll(Stream<Object> ids); @Override void deleteAll(); @Override void add(Entity entity); @Override Integer add(Stream<Entity> entities); @Override void forEachBatched(Fetch fetch, Consumer<List<Entity>> consumer, int batchSize);  }