diff options
Diffstat (limited to 'src/main/java/com/juick/server/SubscriptionsQueries.java')
-rw-r--r-- | src/main/java/com/juick/server/SubscriptionsQueries.java | 127 |
1 files changed, 26 insertions, 101 deletions
diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index 637c2bcf..8207f499 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -4,12 +4,17 @@ */ package com.juick.server; +import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -48,99 +53,31 @@ public class SubscriptionsQueries { return jids; } - public static List<String> getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { - List<String> jids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - - String tbl = "subscr_jids_" + mid; + public static List<User> getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { + List<Integer> userids = UserQueries.getUserRead(sql, uid); List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); - - try { - stmt = sql.prepareStatement("CREATE TEMPORARY TABLE " + tbl + "(user_id INT UNSIGNED NOT NULL) ENGINE=MEMORY"); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - - try { - String query = "INSERT INTO " + tbl + " SELECT suser_id FROM subscr_users WHERE user_id=" + uid; - if (!tags.isEmpty()) { - query += " UNION DISTINCT SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + ") AND suser_id!=" + uid; - } - stmt = sql.prepareStatement(query); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - - - try { - String query = "SELECT jids.jid FROM " + tbl + " INNER JOIN jids ON (" + tbl + ".user_id=jids.user_id) WHERE jids.active=1 AND " + tbl + ".user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + uid + ")"; - if (!tags.isEmpty()) { - query += " AND " + tbl + ".user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + "))"; - } - stmt = sql.prepareStatement(query); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - jids.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - try { - stmt = sql.prepareStatement("DROP TABLE " + tbl); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - - return jids; + List<Integer> tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + + "WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + ") AND suser_id!=" + uid, Integer.class); + Set<Integer> set = new HashSet<>(); + set.addAll(userids); + set.addAll(tagUsers); + return UserQueries.getUsersByID(sql, new ArrayList<>(set)); } - public static List<String> getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { - List<String> jids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_messages INNER JOIN jids ON (subscr_messages.message_id=? AND subscr_messages.suser_id=jids.user_id) WHERE jids.user_id!=? AND jids.active=1"); - stmt.setInt(1, mid); - stmt.setInt(2, ignore_uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - jids.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List<User> getUsersSubscribedToComments(JdbcTemplate sql, int mid, int ignore_uid) { + List<Integer> userids = sql.queryForList("SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?", + new Object[] {mid, ignore_uid}, Integer.class); + if (userids.size() > 0) { + return UserQueries.getUsersByID(sql, userids); + } else { + return new ArrayList<>(); } - return jids; } - public static List<String> getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { - List<String> jids = new ArrayList<>(); - + public static List<User> getUsersSubscribedToUserRecommendations(JdbcTemplate sql, int uid, int mid, int muid) { List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - String query = "SELECT jid FROM jids WHERE active=1 AND user_id!=" + uid; - query += " AND user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=" + uid + ")"; + String query = "SELECT user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=" + uid + ")"; query += " AND user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + muid + ")"; query += " AND user_id NOT IN (SELECT suser_id FROM subscr_users WHERE user_id=" + muid + ")"; query += " AND user_id NOT IN (SELECT suser_id FROM subscr_messages WHERE message_id=" + mid + ")"; @@ -151,23 +88,11 @@ public class SubscriptionsQueries { query += " AND user_id NOT IN (SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + tagsStr + "))"; query += " AND user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + tagsStr + "))"; } - - stmt = sql.prepareStatement(query); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - jids.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return jids; + List<Integer> userids = sql.queryForList(query, Integer.class); + return UserQueries.getUsersByID(sql, userids); } - public static boolean subscribeMessage(Connection sql, int mid, int vuid) { - return SQLHelpers.execute(sql, "INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; + public static boolean subscribeMessage(JdbcTemplate sql, int mid, int vuid) { + return sql.update("INSERT INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; } } |