diff options
author | Vitaly Takmazov | 2023-10-23 10:19:13 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-10-23 10:19:13 +0300 |
commit | f08abdf936c6ebc303f1ade8d7bbc8dbe4c77708 (patch) | |
tree | 091cc3e383edf62706b3af8f2a8610a3c27c6677 /src/main | |
parent | 36279101e3903126619e658a53c946cbe8bfc808 (diff) |
jdbc: row mapper usage refactoring
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/juick/service/MessagesServiceImpl.java | 6 | ||||
-rw-r--r-- | src/main/java/com/juick/service/TagServiceImpl.java | 15 | ||||
-rw-r--r-- | src/main/java/com/juick/service/UserServiceImpl.java | 29 |
3 files changed, 30 insertions, 20 deletions
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index fc190c97..8b729c60 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -127,6 +127,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ } } + private final MessageMapper messageMapper = new MessageMapper(); + /** * @see <a href= * "https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html">Java, @@ -817,7 +819,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ subscr_messages.last_read_rid, users.premium"""; List<Message> msgs = getNamedParameterJdbcTemplate().query(query, new MapSqlParameterSource("ids", mids) - .addValue("uid", uid), new MessageMapper()); + .addValue("uid", uid), messageMapper); Map<Integer, Set<Reaction>> likes = updateReactionsFor(mids); msgs.forEach(i -> i.setReactions(likes.get(i.getMid()))); msgs.sort(Comparator.comparing(item -> mids.indexOf(item.getMid()))); @@ -887,7 +889,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ .addValue("mid", mid) .addValue("uid", user.getUid()) .addValue("now", toDateTime(Instant.now().atOffset(ZoneOffset.UTC)), dateTimeType()), - new MessageMapper()); + messageMapper); replies.forEach(i -> { i.setEntities(MessageUtils.getEntities(i)); i.getUser().setAvatar(webApp.getAvatarUrl(i.getUser())); diff --git a/src/main/java/com/juick/service/TagServiceImpl.java b/src/main/java/com/juick/service/TagServiceImpl.java index 9b8b4d95..c9384f6a 100644 --- a/src/main/java/com/juick/service/TagServiceImpl.java +++ b/src/main/java/com/juick/service/TagServiceImpl.java @@ -62,6 +62,9 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { return tag; } } + + private final TagMapper tagMapper = new TagMapper(); + @Override public Tag getTag(int tid) { return getTag(0, tid); @@ -78,7 +81,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { new MapSqlParameterSource() .addValue("userId", userId) .addValue("tagId", tid), - new TagMapper()); + tagMapper); return list.isEmpty() ? null : list.get(0); } @@ -102,7 +105,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { new MapSqlParameterSource() .addValue("userId", userId) .addValue("tagName", tagName), - new TagMapper()); + tagMapper); Tag ret = list.isEmpty() ? null : list.get(0); @@ -144,7 +147,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { : holder.getKey().intValue(); } - private static class TagStatsMapper implements RowMapper<TagStats> { + private class TagStatsMapper implements RowMapper<TagStats> { @Override public TagStats mapRow(ResultSet rs, int rowNum) throws SQLException { @@ -156,6 +159,8 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } } + private final TagStatsMapper tagStatsMapper = new TagStatsMapper(); + @Transactional(readOnly = true) @Override public List<TagStats> getUserTagStats(final int uid) { @@ -164,7 +169,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { "FROM (messages INNER JOIN messages_tags ON (messages.user_id=? " + "AND messages.message_id=messages_tags.message_id)) " + "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id, tags.name ORDER BY tags.name ASC", - new TagStatsMapper(), + tagStatsMapper, uid); } @@ -202,7 +207,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { return getNamedParameterJdbcTemplate() .query(sql, new MapSqlParameterSource() .addValue("ts", toDateTime(ts.atOffset(ZoneOffset.UTC)), dateTimeType()), - new TagStatsMapper()); + tagStatsMapper); } @Transactional diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index 7f73e6d1..2f03c5bf 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -71,6 +71,8 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { } } + private final UserMapper userMapper = new UserMapper(); + @Transactional @Override public String getSignUpHashByTelegramID(final Long telegramId, final String username) { @@ -118,7 +120,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium FROM users u LEFT JOIN facebook f ON f.user_id = u.id LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id - LEFT JOIN emails e ON e.user_id = u.id WHERE u.id = ?""", new UserMapper(), uid); + LEFT JOIN emails e ON e.user_id = u.id WHERE u.id = ?""", userMapper, uid); return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); } @@ -133,7 +135,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { FROM users u LEFT JOIN facebook f ON f.user_id = u.id LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id LEFT JOIN emails e ON e.user_id = u.id - WHERE u.nick = ?""", new UserMapper(), username); + WHERE u.nick = ?""", userMapper, username); if (!list.isEmpty()) return list.get(0); } @@ -159,7 +161,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE u.id = (SELECT DISTINCT user_id FROM emails WHERE email = ?)", - new UserMapper(), + userMapper, email); if (!list.isEmpty()) @@ -185,7 +187,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE u.id = (SELECT user_id FROM jids WHERE jid = ?)", - new UserMapper(), + userMapper, jid); if (!list.isEmpty()) @@ -208,7 +210,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE u.nick IN (:unames)", new MapSqlParameterSource("unames", unames), - new UserMapper()); + userMapper); } @Transactional(readOnly = true) @@ -225,7 +227,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE u.id IN (:ids)", new MapSqlParameterSource("ids", uids), - new UserMapper()); + userMapper); } @Transactional(readOnly = true) @@ -260,7 +262,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE logins.hash = ?", - new UserMapper(), + userMapper, hash); if (!list.isEmpty()) { @@ -297,7 +299,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + "WHERE nick = ?", - new UserMapper(), + userMapper, username); if (!list.isEmpty()) { @@ -747,7 +749,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id LEFT JOIN emails e ON e.user_id = u.id WHERE t.tg_id = ?""", - new UserMapper(), id + userMapper, id ); return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); @@ -900,8 +902,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public boolean canDeleteTelegramUser(User user) { return getEmails(user).size() > 0 || getFbCrossPostStatus(user.getUid()).isConnected() || getVkTokens(user.getUid()) != null; } - private static class TokenMapper implements RowMapper<ExternalToken> { - + private class TokenMapper implements RowMapper<ExternalToken> { @Override public ExternalToken mapRow(ResultSet rs, int rowNum) throws SQLException { return new ExternalToken( @@ -913,12 +914,14 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { } } + private final TokenMapper tokenMapper = new TokenMapper(); + @Transactional(readOnly = true) @Override public Collection<ExternalToken> getToken(final int uid, final String serviceType) { return getJdbcTemplate().query( "SELECT regid, service_type FROM user_services WHERE user_id=? AND service_type=?", - new TokenMapper(), + tokenMapper, uid, serviceType); } @@ -932,7 +935,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { SELECT regid, service_type FROM user_services INNER JOIN users ON (users.id = user_services.user_id) WHERE users.id IN (:ids)""", new MapSqlParameterSource("ids", uids), - new TokenMapper()); + tokenMapper); } @Transactional |