Skip to content

Commit 36976bc

Browse files
committed
Ported counting of users' collections from JPA to SQL.
No functional changes.
1 parent 37cdc1e commit 36976bc

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20-
import org.springframework.data.jpa.repository.Query;
2120
import org.springframework.data.repository.CrudRepository;
2221

2322
import ru.mystamps.web.entity.Collection;
2423

2524
public interface CollectionDao extends CrudRepository<Collection, Integer> {
26-
@Query("SELECT COUNT(*) FROM Collection c WHERE c.series IS NOT EMPTY")
27-
int countCollectionsOfUsers();
2825
}

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

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

2222
public interface JdbcCollectionDao {
2323
Iterable<LinkEntityDto> findLastCreated(int quantity);
24+
long countCollectionsOfUsers();
2425
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class JdbcCollectionDaoImpl implements JdbcCollectionDao {
3838
@Value("${collection.find_last_created_sql}")
3939
private String findLastCreatedCollectionsSql;
4040

41+
@Value("${collection.count_collections_of_users}")
42+
private String countCollectionsOfUsersSql;
43+
4144
public JdbcCollectionDaoImpl(DataSource dataSource) {
4245
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
4346
}
@@ -51,4 +54,13 @@ public Iterable<LinkEntityDto> findLastCreated(int quantity) {
5154
);
5255
}
5356

57+
@Override
58+
public long countCollectionsOfUsers() {
59+
return jdbcTemplate.queryForObject(
60+
countCollectionsOfUsersSql,
61+
Collections.<String, Object>emptyMap(),
62+
Long.class
63+
);
64+
}
65+
5466
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public boolean isSeriesInCollection(User user, Series series) {
135135
@Override
136136
@Transactional(readOnly = true)
137137
public long countCollectionsOfUsers() {
138-
return collectionDao.countCollectionsOfUsers();
138+
return jdbcCollectionDao.countCollectionsOfUsers();
139139
}
140140

141141
@Override

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ collection.find_last_created_sql = \
99
ON u.id = c.user_id \
1010
ORDER BY c.id DESC \
1111
LIMIT :quantity
12+
13+
collection.count_collections_of_users = \
14+
SELECT COUNT(DISTINCT c.id) \
15+
FROM collections c \
16+
LEFT JOIN collections_series cs \
17+
ON cs.collection_id = c.id \
18+
LEFT JOIN series s \
19+
ON s.id = cs.series_id \
20+
WHERE s.id IS NOT NULL

0 commit comments

Comments
 (0)