From 73b8c2dc544df207d8c7b9e36d20955ff1d88577 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 3 Feb 2023 04:22:50 +0300 Subject: Make SQL vendor queries private --- src/main/java/com/juick/service/BaseJdbcService.java | 17 +++++++++++------ .../java/com/juick/service/MessagesServiceImpl.java | 6 +++--- src/main/java/com/juick/service/UserServiceImpl.java | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/juick/service/BaseJdbcService.java b/src/main/java/com/juick/service/BaseJdbcService.java index ab2e3fc8..d16dadd2 100644 --- a/src/main/java/com/juick/service/BaseJdbcService.java +++ b/src/main/java/com/juick/service/BaseJdbcService.java @@ -39,18 +39,18 @@ public class BaseJdbcService { @Inject NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlserver')}") - protected boolean omitRecursiveKeyword; + private boolean omitRecursiveKeyword; @Value("#{('${spring.sql.init.platform}' == 'sqlite') or ('${spring.sql.init.platform}' == 'mysql')}") // Added in MariaDB 10.6 - protected boolean haveNoANSIFetch; + private boolean haveNoANSIFetch; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") - protected boolean haveNoDates; + private boolean haveNoDates; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") - protected boolean haveNoGreatest; + private boolean haveNoGreatest; @Value("#{new Boolean('${spring.sql.init.platform}' == 'sqlite')}") - protected boolean haveNoForUpdate; + private boolean haveNoForUpdate; @Value("#{new Boolean('${spring.sql.init.platform}' == 'mysql')}") - protected boolean haveNoOffsetDateTime; + private boolean haveNoOffsetDateTime; public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { return namedParameterJdbcTemplate; @@ -59,6 +59,11 @@ public class BaseJdbcService { public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } + + public String withRecursive() { + return omitRecursiveKeyword ? "WITH" : "WITH RECURSIVE"; + } + protected String limit(int rows) { if (haveNoANSIFetch) { return "LIMIT " + rows; diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java index eb1acd59..c5b29fd5 100644 --- a/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -789,8 +789,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public List getMessages(int uid, final List mids) { if (CollectionUtils.isNotEmpty(mids)) { - var query = (omitRecursiveKeyword ? "WITH " : "WITH RECURSIVE ") + """ - banned(message_id, reply_id) + var query = withRecursive() + """ + banned(message_id, reply_id) AS (SELECT message_id, reply_id FROM replies WHERE replies.message_id IN (:ids) AND (EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) OR EXISTS (SELECT 1 from users u WHERE u.banned = 1 and u.id = replies.user_id and u.id <> :uid)) @@ -882,7 +882,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional @Override public List getReplies(final User user, final int mid) { - List replies = getNamedParameterJdbcTemplate().query((omitRecursiveKeyword ? "WITH " : "WITH RECURSIVE ") + "banned(reply_id, user_id) AS (" + List replies = getNamedParameterJdbcTemplate().query(withRecursive() + " banned(reply_id, user_id) AS (" + "SELECT reply_id, user_id FROM replies " + "WHERE replies.message_id = :mid " + "AND EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) " + "UNION ALL SELECT replies.reply_id, replies.user_id FROM replies " diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index 8ed8fd96..57fcd399 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -391,7 +391,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Transactional(readOnly = true) @Override public boolean isReplyToBL(final User user, final Message reply) { - var replies = getNamedParameterJdbcTemplate().queryForList((omitRecursiveKeyword ? "WITH " : "WITH RECURSIVE ") + "banned(reply_id, user_id) AS (" + + var replies = getNamedParameterJdbcTemplate().queryForList(withRecursive() + " banned(reply_id, user_id) AS (" + "SELECT reply_id, user_id FROM replies " + "WHERE replies.message_id = :mid " + "AND EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) " + -- cgit v1.2.3