Skip to content

Commit 95eceec

Browse files
committed
Ported counting of stamps from JPA to SQL.
No functional changes.
1 parent 36976bc commit 95eceec

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

src/main/java/ru/mystamps/web/dao/JdbcSeriesDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
public interface JdbcSeriesDao {
2323
Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang);
24+
long countAllStamps();
2425
long countSeriesOfCollection(Integer collectionId);
2526
long countStampsOfCollection(Integer collectionId);
2627
}

src/main/java/ru/mystamps/web/dao/SeriesDao.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public interface SeriesDao extends CrudRepository<Series, Integer> {
4040
@Query("SELECT COUNT(*) FROM Series s INNER JOIN s.gibbons m WHERE m.code = :gibbonsCode")
4141
int countByGibbonsNumberCode(@Param("gibbonsCode") String gibbonsNumberCode);
4242

43-
@Query("SELECT COALESCE(SUM(quantity), 0) FROM Series")
44-
long countAllStamps();
45-
4643
@Query(
4744
"SELECT NEW ru.mystamps.web.service.dto.SeriesInfoDto("
4845
+ "s.id, "

src/main/java/ru/mystamps/web/dao/impl/JdbcSeriesDaoImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class JdbcSeriesDaoImpl implements JdbcSeriesDao {
4040
@Value("${series.find_last_added_sql}")
4141
private String findLastAddedSeriesSql;
4242

43+
@Value("${series.count_all_stamps}")
44+
private String countAllStampsSql;
45+
4346
@Value("${series.count_series_of_collection}")
4447
private String countSeriesOfCollectionSql;
4548

@@ -59,6 +62,15 @@ public Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang) {
5962
return jdbcTemplate.query(findLastAddedSeriesSql, params, SERIES_INFO_DTO_ROW_MAPPER);
6063
}
6164

65+
@Override
66+
public long countAllStamps() {
67+
return jdbcTemplate.queryForObject(
68+
countAllStampsSql,
69+
Collections.<String, Object>emptyMap(),
70+
Long.class
71+
);
72+
}
73+
6274
@Override
6375
public long countSeriesOfCollection(Integer collectionId) {
6476
return jdbcTemplate.queryForObject(

src/main/java/ru/mystamps/web/service/SeriesServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public long countAll() {
130130
@Override
131131
@Transactional(readOnly = true)
132132
public long countAllStamps() {
133-
return seriesDao.countAllStamps();
133+
return jdbcSeriesDao.countAllStamps();
134134
}
135135

136136
@Override

src/main/resources/sql/series_dao_queries.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ LEFT JOIN countries count \
1919
ORDER BY s.id DESC \
2020
LIMIT :quantity
2121

22+
series.count_all_stamps = \
23+
SELECT COALESCE(SUM(s.quantity), 0) \
24+
FROM series s
25+
2226
series.count_series_of_collection = \
2327
SELECT COUNT(*) AS counter \
2428
FROM collections_series cs \

src/test/groovy/ru/mystamps/web/service/SeriesServiceImplTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ class SeriesServiceImplTest extends Specification {
615615
when:
616616
long result = service.countAllStamps()
617617
then:
618-
1 * seriesDao.countAllStamps() >> expectedResult
618+
1 * jdbcSeriesDao.countAllStamps() >> expectedResult
619619
and:
620620
result == expectedResult
621621
}

0 commit comments

Comments
 (0)