From d00fd7705cb1a4b085eed7f34df2f1c6d9a69f76 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 24 Oct 2015 18:09:15 +0300 Subject: moving to Gradle --- src/main/java/com/juick/server/AdsQueries.java | 51 + .../java/com/juick/server/CrosspostQueries.java | 80 ++ .../java/com/juick/server/MessagesQueries.java | 1065 ++++++++++++++++++++ src/main/java/com/juick/server/PMQueries.java | 303 ++++++ src/main/java/com/juick/server/PushQueries.java | 68 ++ src/main/java/com/juick/server/SQLHelpers.java | 163 +++ .../com/juick/server/SubscriptionsQueries.java | 168 +++ src/main/java/com/juick/server/TagQueries.java | 189 ++++ src/main/java/com/juick/server/UserQueries.java | 592 +++++++++++ src/main/java/com/juick/server/Utils.java | 86 ++ 10 files changed, 2765 insertions(+) create mode 100644 src/main/java/com/juick/server/AdsQueries.java create mode 100644 src/main/java/com/juick/server/CrosspostQueries.java create mode 100644 src/main/java/com/juick/server/MessagesQueries.java create mode 100644 src/main/java/com/juick/server/PMQueries.java create mode 100644 src/main/java/com/juick/server/PushQueries.java create mode 100644 src/main/java/com/juick/server/SQLHelpers.java create mode 100644 src/main/java/com/juick/server/SubscriptionsQueries.java create mode 100644 src/main/java/com/juick/server/TagQueries.java create mode 100644 src/main/java/com/juick/server/UserQueries.java create mode 100644 src/main/java/com/juick/server/Utils.java (limited to 'src/main') diff --git a/src/main/java/com/juick/server/AdsQueries.java b/src/main/java/com/juick/server/AdsQueries.java new file mode 100644 index 00000000..0fb4c0a6 --- /dev/null +++ b/src/main/java/com/juick/server/AdsQueries.java @@ -0,0 +1,51 @@ +/* + * Juick + * Copyright (C) 2008-2011, ugnich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * + * @author ugnich + */ +public class AdsQueries { + + public static int getAdMID(Connection sql, int uid) { + if (uid > 0) { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); + } else { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", 0); + } + } + + public static void logAdMID(Connection sql, int uid, int mid) { + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())"); + stmt.setInt(1, uid); + stmt.setInt(2, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } +} diff --git a/src/main/java/com/juick/server/CrosspostQueries.java b/src/main/java/com/juick/server/CrosspostQueries.java new file mode 100644 index 00000000..fd677048 --- /dev/null +++ b/src/main/java/com/juick/server/CrosspostQueries.java @@ -0,0 +1,80 @@ +/* + * Juick + * Copyright (C) 2008-2013, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * + * @author ugnich + */ +public class CrosspostQueries { + + public static String[] getTwitterTokens(Connection sql, int uid) { + String tokens[] = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + tokens = new String[2]; + tokens[0] = rs.getString(1); + tokens[1] = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tokens; + } + + public static String getFacebookToken(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL AND crosspost=1", uid); + } + + public static String[] getVKTokens(Connection sql, int uid) { + String tokens[] = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + tokens = new String[2]; + tokens[0] = rs.getString(1); + tokens[1] = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tokens; + } +} diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java new file mode 100644 index 00000000..8bef9018 --- /dev/null +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -0,0 +1,1065 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.ArrayList; +import java.util.Collections; + +/** + * + * @author Ugnich Anton + */ +public class MessagesQueries { + + public static int createMessage(Connection sql, int uid, String txt, String attachment, ArrayList tags) { + int mid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO messages(user_id,attach) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setInt(1, uid); + if (attachment != null) { + stmt.setString(2, attachment); + } else { + stmt.setNull(2, Types.VARCHAR); + } + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + mid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (mid > 0) { + String tagsNames = ""; + String tagsIDs = ""; + + for (int i = 0; i < tags.size(); i++) { + if (i > 0) { + tagsNames += " "; + tagsIDs += ","; + } + tagsNames += tags.get(i).Name; + tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); + stmt.setInt(1, mid); + stmt.setString(2, tagsNames); + stmt.setString(3, txt); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return mid; + } + + public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { + int ridnew = getReplyIDIncrement(sql, mid); + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO replies(message_id,reply_id,user_id,replyto,attach,txt) VALUES (?,?,?,?,?,?)"); + stmt.setInt(1, mid); + stmt.setInt(2, ridnew); + stmt.setInt(3, uid); + stmt.setInt(4, rid); + if (attachment != null) { + stmt.setString(5, attachment); + } else { + stmt.setNull(5, Types.VARCHAR); + } + stmt.setString(6, txt); + if (stmt.executeUpdate() == 0) { + ridnew = 0; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ridnew > 0) { + SQLHelpers.executeInt(sql, "UPDATE messages SET replies=replies+1 WHERE message_id=?", mid); + } + + return ridnew; + } + + public static int getReplyIDIncrement(Connection sql, int mid) { + int rid = 0; + + SQLHelpers.execute(sql, "LOCK TABLES messages WRITE"); + if (SQLHelpers.executeInt(sql, "UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid) == 1) { + rid = SQLHelpers.getInt(sql, "SELECT maxreplyid FROM messages WHERE message_id=?", mid, 0); + } + SQLHelpers.execute(sql, "UNLOCK TABLES"); + + return rid; + } + + public static boolean recommendMessage(Connection sql, int mid, int vuid) { + boolean res = SQLHelpers.execute(sql, "INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; + if (res) { + SQLHelpers.executeInt(sql, "UPDATE messages SET likes=likes+1 WHERE message_id=?", mid); + } + return res; + } + + public static boolean canViewThread(Connection sql, int mid, int uid) { + + int privacy = 0; + int owner_uid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,privacy FROM messages WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + owner_uid = rs.getInt(1); + privacy = rs.getInt(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return privacy >= 0 + || uid == owner_uid + || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)); + } + + public static boolean isReadOnly(Connection sql, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT readonly FROM messages WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isSubscribed(Connection sql, int uid, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getMessagePrivacy(Connection sql, int mid) { + return SQLHelpers.getInt(sql, "SELECT privacy FROM messages WHERE message_id=?", mid, -4); + } + + public static com.juick.Message getMessage(Connection sql, int mid) { + com.juick.Message msg = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.readonly,messages.privacy,messages.replies,messages.attach,messages.place_id,messages.lat,messages.lon,messages.likes,messages.hidden FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + msg = new com.juick.Message(); + msg.MID = mid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.TimeAgo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.ReadOnly = rs.getBoolean(5); + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Replies = rs.getInt(7); + msg.AttachmentType = rs.getString(8); + //if (rs.getInt(9) > 0) { + // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); + //} + if (rs.getDouble(10) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(10); + msg.Place.lon = rs.getDouble(11); + } + msg.Likes = rs.getInt(12); + msg.Hidden = rs.getBoolean(13); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (msg == null) { + return null; + } + + try { + stmt = sql.prepareStatement("SELECT tags,repliesby,txt FROM messages_txt WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + if (rs.getString(1) != null) { + msg.parseTags(rs.getString(1)); + } + msg.RepliesBy = rs.getString(2); + msg.Text = rs.getString(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msg; + } + + public static com.juick.Message getReply(Connection sql, int mid, int rid) { + com.juick.Message msg = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT replies.user_id,users.nick,replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach,replies.txt FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? AND replies.reply_id=?"); + stmt.setInt(1, mid); + stmt.setInt(2, rid); + rs = stmt.executeQuery(); + if (rs.first()) { + msg = new com.juick.Message(); + msg.MID = mid; + msg.RID = rid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.ReplyTo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.AttachmentType = rs.getString(5); + msg.Text = rs.getString(6); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msg; + } + + public static com.juick.User getMessageAuthor(Connection sql, int mid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT messages.user_id,users.nick FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static ArrayList getMessageTags(Connection sql, int mid) { + ArrayList tags = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = rs.getInt(1); + t.SynonymID = rs.getInt(2); + t.Name = rs.getString(3); + t.UsageCnt = rs.getInt(4); + tags.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tags; + } + + public static ArrayList getMessageTagsIDs(Connection sql, int mid) { + ArrayList tids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id FROM messages_tags WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + tids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return tids; + } + + public static ArrayList getMessageRecommendations(Connection sql, int mid) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.nick FROM favorites INNER JOIN users ON (favorites.message_id=? AND favorites.user_id=users.id)"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + users.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static ArrayList getAll(Connection sql, int visitor_uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (visitor_uid > 1) { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + stmt.setInt(3, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND (privacy>0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); + stmt.setInt(2, visitor_uid); + } + } else { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + } + + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { +// stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.message_id0 OR messages.user_id=?) ORDER BY message_id DESC LIMIT ?"); + stmt.setInt(1, tid); + stmt.setInt(2, tid); + stmt.setInt(3, before); + stmt.setInt(4, visitor_uid); + stmt.setInt(5, cnt); + } else { + //stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.privacy>0 OR messages.user_id=? ORDER BY message_id DESC LIMIT ?"); + stmt.setInt(1, tid); + stmt.setInt(2, tid); + stmt.setInt(3, visitor_uid); + stmt.setInt(4, cnt); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getTags(Connection sql, String tids, int visitor_uid, int before, int cnt) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + stmt.setInt(3, cnt); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + stmt.setInt(1, visitor_uid); + stmt.setInt(2, cnt); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getPlace(Connection sql, int place_id, int visitor_uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND message_id0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + stmt.setInt(2, before); + stmt.setInt(3, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + stmt.setInt(2, visitor_uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getMyFeed(Connection sql, int uid, int before) { + ArrayList mids = new ArrayList(40); + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE message_id=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, before); + stmt.setInt(3, uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id getPrivate(Connection sql, int uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 AND message_id getDiscussions(Connection sql, int uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id getRecommended(Connection sql, int uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id getPopular(Connection sql, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getPhotos(Connection sql, int visitor_uid, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getSearch(Connection sql, Connection sqlSearch, String search, int before) { + ArrayList mids0 = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id mids = new ArrayList(20); + if (mids0.size() > 0) { + try { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return mids; + } + + public static ArrayList getUserBlog(Connection sql, int UID, int privacy, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + privacy + " ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getUserTag(Connection sql, int UID, int TID, int privacy, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.message_id=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, TID); + stmt.setInt(3, before); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, TID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getUserRecommendations(Connection sql, int UID, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id=? AND message_id getUserPhotos(Connection sql, int UID, int privacy, int before) { + ArrayList mids = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, UID); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return mids; + } + + public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { + ArrayList mids0 = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (before > 0) { + stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND id mids = new ArrayList(20); + if (mids0.size() > 0) { + try { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + mids.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return mids; + } + + public static ArrayList getMessages(Connection sql, ArrayList mids) { + ArrayList msgs = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon,messages.likes FROM (messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + + msg.MID = rs.getInt(1); + msg.User.UID = rs.getInt(2); + msg.User.UName = rs.getString(3); + if (rs.getString(4) != null) { + msg.parseTags(rs.getString(4)); + } + msg.ReadOnly = rs.getInt(5) == 1; + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Text = rs.getString(7); + msg.TimeAgo = rs.getInt(8); + msg.TimestampString = rs.getString(9); + msg.Replies = rs.getInt(10); + msg.RepliesBy = rs.getString(11); + msg.AttachmentType = rs.getString(12); + if (rs.getDouble(13) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(13); + msg.Place.lon = rs.getDouble(14); + } + msg.Likes = rs.getInt(15); + + msgs.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } + + public static ArrayList getReplies(Connection sql, int mid) { + ArrayList replies = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.MID = mid; + msg.RID = rs.getInt(1); + msg.ReplyTo = rs.getInt(2); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(3); + msg.User.UName = rs.getString(4); + msg.User.Banned = rs.getBoolean(5); + msg.Text = rs.getString(6); + msg.TimeAgo = rs.getInt(7); + msg.TimestampString = rs.getString(8); + msg.AttachmentType = rs.getString(9); + + replies.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return replies; + } + + public static boolean setMessagePopular(Connection sql, int mid, int popular) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + if (popular == -2) { + stmt = sql.prepareStatement("UPDATE messages SET hidden=1 WHERE message_id=?"); + stmt.setInt(1, mid); + } else if (popular == -1) { + stmt = sql.prepareStatement("UPDATE messages SET popular=0 WHERE message_id=?"); + stmt.setInt(1, mid); + } else { + stmt = sql.prepareStatement("UPDATE messages SET popular=? WHERE message_id=?"); + stmt.setInt(1, popular); + stmt.setInt(2, mid); + } + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (popular == -1) { + try { + stmt = sql.prepareStatement("INSERT INTO top_ignore_messages VALUES (?)"); + stmt.setInt(1, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return ret; + } + + public static boolean setMessagePrivacy(Connection sql, int mid) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE messages SET privacy=1 WHERE message_id=?"); + stmt.setInt(1, mid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } +} diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java new file mode 100644 index 00000000..56b91abf --- /dev/null +++ b/src/main/java/com/juick/server/PMQueries.java @@ -0,0 +1,303 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class PMQueries { + + public static boolean createPM(Connection sql, int uid_from, int uid_to, String body) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)"); + stmt.setInt(1, uid_from); + stmt.setInt(2, uid_to); + stmt.setString(3, body); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ret) { + PreparedStatement stmt2 = null; + try { + stmt2 = sql.prepareStatement("INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) VALUES (?,?,NOW(),1) ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1"); + stmt2.setInt(1, uid_from); + stmt2.setInt(2, uid_to); + stmt2.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt2); + } + } + + return ret; + } + + public static boolean addPMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static boolean removePMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("DELETE FROM pm_inroster WHERE user_id=? AND jid=?"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static boolean havePMinRoster(Connection sql, int uid, String jid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM pm_inroster WHERE user_id=? AND jid=?"); + stmt.setInt(1, uid); + stmt.setString(2, jid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getLastView(Connection sql, int uid_from, int uid_to) { + String ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?"); + stmt.setInt(1, uid_from); + stmt.setInt(2, uid_to); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList getPMLastConversationsUsers(Connection sql, int uid, int cnt) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,unread FROM pm_streams WHERE user_id_to=? AND unread>0 ORDER BY lastmessage DESC LIMIT " + cnt); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.MessagesCount = rs.getInt(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (users.size() < cnt) { + try { + stmt = sql.prepareStatement("SELECT user_id,user_id_to FROM pm_streams WHERE (user_id=? OR user_id_to=?) AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) ORDER BY lastmessage DESC LIMIT ?"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + stmt.setInt(3, cnt * 2); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int uuid = rs.getInt(1) + rs.getInt(2) - uid; + if (!haveUserInArray(users, uuid)) { + com.juick.User u = new com.juick.User(); + u.UID = uuid; + users.add(u); + if (users.size() >= cnt) { + break; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + if (!users.isEmpty()) { + UserQueries.fillUsersByID(sql, users); + } + + return users; + } + + public static boolean haveUserInArray(ArrayList arr, int uid) { + int s = arr.size(); + for (int i = 0; i < s; i++) { + if (arr.get(i).UID == uid) { + return true; + } + } + return false; + } + + public static ArrayList getPMMessages(Connection sql, int uid, int uid_to) { + ArrayList msgs = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id,txt,ts FROM pm WHERE (user_id=? AND user_id_to=?) OR (user_id_to=? AND user_id=?) ORDER BY ts DESC LIMIT 20"); + stmt.setInt(1, uid); + stmt.setInt(2, uid_to); + stmt.setInt(3, uid); + stmt.setInt(4, uid_to); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + int uuid = rs.getInt(1); + msg.User = new com.juick.User(); + msg.User.UID = uuid; + msg.Text = rs.getString(2); + msg.TimestampString = rs.getString(3); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + PreparedStatement stmt2 = null; + try { + stmt2 = sql.prepareStatement("UPDATE pm_streams SET lastview=NOW(),unread=0 WHERE user_id_to=? AND user_id=?"); + stmt2.setInt(1, uid); + stmt2.setInt(2, uid_to); + stmt2.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt2); + } + + return msgs; + } + + public static ArrayList getLastPMInbox(Connection sql, int uid) { + ArrayList msgs = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.Text = rs.getString(3); + msg.TimeAgo = rs.getInt(4); + msg.TimestampString = rs.getString(5); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } + + public static ArrayList getLastPMSent(Connection sql, int uid) { + ArrayList msgs = new ArrayList(20); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id_to=users.id WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.Text = rs.getString(3); + msg.TimeAgo = rs.getInt(4); + msg.TimestampString = rs.getString(5); + msgs.add(0, msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } +} diff --git a/src/main/java/com/juick/server/PushQueries.java b/src/main/java/com/juick/server/PushQueries.java new file mode 100644 index 00000000..84f42722 --- /dev/null +++ b/src/main/java/com/juick/server/PushQueries.java @@ -0,0 +1,68 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class PushQueries { + + public static String getAndroidRegID(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT regid FROM android WHERE user_id=?", uid); + } + + public static ArrayList getAndroidSubscribers(Connection sql, int uid) { + ArrayList regids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT regid FROM android INNER JOIN subscr_users ON (subscr_users.user_id=? AND android.user_id=subscr_users.suser_id)"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + regids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return regids; + } + + public static String getWinPhoneURL(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT url FROM winphone WHERE user_id=?", uid); + } + + public static ArrayList getWinPhoneSubscribers(Connection sql, int uid) { + ArrayList urls = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT url FROM winphone INNER JOIN subscr_users ON (subscr_users.user_id=? AND winphone.user_id=subscr_users.suser_id)"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + urls.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return urls; + } +} diff --git a/src/main/java/com/juick/server/SQLHelpers.java b/src/main/java/com/juick/server/SQLHelpers.java new file mode 100644 index 00000000..f5569993 --- /dev/null +++ b/src/main/java/com/juick/server/SQLHelpers.java @@ -0,0 +1,163 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class SQLHelpers { + + public static int execute(Connection sql, String query) { + int ret = -1; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement(query); + ret = stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static int executeInt(Connection sql, String query, int param) { + int ret = -1; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + ret = stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, int param, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static int getInt(Connection sql, String query, String param, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setString(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getString(Connection sql, String query, int param) { + String ret = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static String getString(Connection sql, String query, String param) { + String ret = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setString(1, param); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList getArrayInteger(Connection sql, String query, int param) { + ArrayList ret = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + stmt.setInt(1, param); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } +} diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java new file mode 100644 index 00000000..d0f5f308 --- /dev/null +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -0,0 +1,168 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +/** + * + * @author ugnich + */ +public class SubscriptionsQueries { + + public static ArrayList getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) { + ArrayList jids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + if (friendsonly == false) { + stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1"); + stmt.setInt(1, uid); + } else { + stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)"); + stmt.setInt(1, uid); + stmt.setInt(2, uid); + } + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + jids.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return jids; + } + + public static ArrayList getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { + ArrayList jids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + + String tbl = "subscr_jids_" + mid; + ArrayList 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) { + System.err.println(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) { + System.err.println(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) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + try { + stmt = sql.prepareStatement("DROP TABLE " + tbl); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return jids; + } + + public static ArrayList getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { + ArrayList 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) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return jids; + } + + public static ArrayList getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { + ArrayList jids = new ArrayList(); + + ArrayList 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 + ")"; + 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 + ")"; + query += " AND user_id NOT IN (SELECT user_id FROM favorites WHERE message_id=" + mid + ")"; + query += " AND user_id NOT IN (SELECT subscr_users.suser_id FROM subscr_users INNER JOIN favorites ON (favorites.message_id=" + mid + " AND subscr_users.user_id=favorites.user_id AND favorites.user_id!=" + uid + "))"; + if (!tags.isEmpty()) { + String tagsStr = Utils.convertArrayInt2String(tags); + 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) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return jids; + } + + 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; + } +} diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java new file mode 100644 index 00000000..493e8675 --- /dev/null +++ b/src/main/java/com/juick/server/TagQueries.java @@ -0,0 +1,189 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class TagQueries { + + public static com.juick.Tag getTag(Connection sql, int tid) { + com.juick.Tag ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT synonym_id,name FROM tags WHERE tag_id=?"); + stmt.setInt(1, tid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = new com.juick.Tag(); + ret.TID = tid; + ret.SynonymID = rs.getInt(1); + ret.Name = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { + com.juick.Tag ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id,synonym_id,name FROM tags WHERE name=?"); + stmt.setString(1, tag); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.SynonymID = rs.getInt(2); + ret.Name = rs.getString(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (ret == null && autoCreate) { + ret = new com.juick.Tag(); + ret.TID = createTag(sql, tag); + ret.Name = tag; + } + + return ret; + } + + public static ArrayList getTags(Connection sql, String[] tags, boolean autoCreate) { + ArrayList ret = new ArrayList(); + + for (int i = 0; i < tags.length; i++) { + if (!tags[i].isEmpty()) { + com.juick.Tag t = getTag(sql, tags[i], autoCreate); + if (t != null) { + ret.add(t); + } + } + } + + return ret; + } + + public static boolean getTagNoIndex(Connection sql, int tag_id) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT noindex FROM tags WHERE tag_id=?"); + stmt.setInt(1, tag_id); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static int createTag(Connection sql, String name) { + int ret = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, name); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static ArrayList getUserTagsAll(Connection sql, int uid) { + ArrayList tags = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.name,COUNT(messages.message_id) 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 ORDER BY tags.name ASC"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.Name = rs.getString(1); + t.UsageCnt = rs.getInt(2); + tags.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return tags; + } + + public static ArrayList getUserBLTags(Connection sql, int uid) { + ArrayList tags = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tags.name FROM tags INNER JOIN bl_tags ON (bl_tags.user_id=? AND bl_tags.tag_id=tags.tag_id) ORDER BY tags.name"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + tags.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return tags; + } +} diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java new file mode 100644 index 00000000..d1313715 --- /dev/null +++ b/src/main/java/com/juick/server/UserQueries.java @@ -0,0 +1,592 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Random; +import java.util.UUID; + +/** + * + * @author Ugnich Anton + */ +public class UserQueries { + + static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static String getSignUpHashByJID(Connection sql, String jid) { + String hash = SQLHelpers.getString(sql, "SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", jid); + + if (hash == null) { + hash = UUID.randomUUID().toString(); + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO jids(jid,loginhash) VALUES (?,?)"); + stmt.setString(1, jid); + stmt.setString(2, hash); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return hash; + } + + public static int createUser(Connection sql, String username, String password) { + int uid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO users(nick,passw) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, username); + stmt.setString(2, password); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + uid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + SQLHelpers.executeInt(sql, "INSERT INTO useroptions(user_id) VALUES (?)", uid); + SQLHelpers.executeInt(sql, "INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); + + return uid; + } + + public static com.juick.User getUserByUID(Connection sql, int uid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT nick,banned FROM users WHERE id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = uid; + user.UName = rs.getString(1); + user.Banned = rs.getBoolean(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static com.juick.User getUserByName(Connection sql, String username) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick,banned FROM users WHERE nick=?"); + stmt.setString(1, username); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.Banned = rs.getBoolean(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static com.juick.User getUserByJID(Connection sql, String jid) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)"); + stmt.setString(1, jid); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.JID = jid; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static ArrayList getUsersByName(Connection sql, ArrayList unames) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + users.add(user); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return users; + } + + public static ArrayList getUsersByID(Connection sql, ArrayList uids) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static boolean fillUsersByID(Connection sql, ArrayList users) { + boolean ret = false; + + String uids = ""; + final int usersSize = users.size(); + for (int i = 0; i < usersSize; i++) { + if (i > 0) { + uids += ","; + } + uids += users.get(i).UID; + } + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + uids + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int uid = rs.getInt(1); + for (int i = 0; i < usersSize; i++) { + if (users.get(i).UID == uid) { + users.get(i).UName = rs.getString(2); + ret = true; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static ArrayList getUsersByJID(Connection sql, ArrayList jids) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick,jids.jid FROM users INNER JOIN jids ON jids.user_id=users.id WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")"); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.JID = rs.getString(3); + users.add(user); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return users; + } + + public static String getJIDbyUID(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT jid FROM jids WHERE user_id=? AND active=1", uid); + } + + public static int getUIDbyJID(Connection sql, String jid) { + return SQLHelpers.getInt(sql, "SELECT user_id FROM jids WHERE jid=?", jid, 0); + } + + public static int getUIDbyName(Connection sql, String uname) { + return SQLHelpers.getInt(sql, "SELECT id FROM users WHERE nick=?", uname, 0); + } + + public static int getUIDbyHash(Connection sql, String hash) { + return SQLHelpers.getInt(sql, "SELECT user_id FROM logins WHERE hash=?", hash, 0); + } + + public static com.juick.User getUserByHash(Connection sql, String hash) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT logins.user_id,users.nick FROM logins INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?"); + stmt.setString(1, hash); + rs = stmt.executeQuery(); + if (rs.first()) { + user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.AuthHash = hash; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static String getHashByUID(Connection sql, int uid) { + String hash = SQLHelpers.getString(sql, "SELECT hash FROM logins WHERE user_id=?", uid); + + if (hash == null) { + hash = generateHash(16); + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); + stmt.setInt(1, uid); + stmt.setString(2, hash); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return hash; + } + + public static String generateHash(int len) { + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); + } + return sb.toString(); + } + + public static boolean checkUserNameValid(String uname) { + return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); + } + + public static int checkPassword(Connection sql, String username, String password) { + int uid = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,passw FROM users WHERE nick=?"); + stmt.setString(1, username); + rs = stmt.executeQuery(); + if (rs.first()) { + if (password.equals(rs.getString(2))) { + uid = rs.getInt(1); + } else { + uid = -1; + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return uid; + } + + public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { + int ret = defaultValue; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT " + option + " FROM useroptions WHERE user_id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static void setUserOptionInt(Connection sql, int uid, String option, int value) { + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE useroptions SET " + option + "=? WHERE user_id=?"); + stmt.setInt(1, value); + stmt.setInt(2, uid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + public static boolean getCanMedia(Connection sql, int uid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) < 3600; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInWL(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInBL(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static boolean isInBLAny(Connection sql, int uid, int uid2) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE (user_id=? AND bl_user_id=?) OR (user_id=? AND bl_user_id=?)"); + stmt.setInt(1, uid); + stmt.setInt(2, uid2); + stmt.setInt(3, uid2); + stmt.setInt(4, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList checkBL(Connection sql, int visitor, ArrayList uids) { + ArrayList ret = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id FROM bl_users WHERE bl_user_id=? and user_id IN (" + Utils.convertArrayInt2String(uids) + ")"); + stmt.setInt(1, visitor); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret.add(rs.getInt(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + public static boolean isSubscribed(Connection sql, int uid, int check) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?"); + stmt.setInt(1, uid); + stmt.setInt(2, check); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1) == 1; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + + public static ArrayList getUserRead(Connection sql, int uid) { + return SQLHelpers.getArrayInteger(sql, "SELECT user_id FROM subscr_users WHERE suser_id=?", uid); + } + + public static ArrayList getUserReadLeastPopular(Connection sql, int uid, int cnt) { + ArrayList users = new ArrayList(cnt); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM (subscr_users INNER JOIN users_subscr ON (subscr_users.suser_id=? AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?"); + stmt.setInt(1, uid); + stmt.setInt(2, cnt); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static ArrayList getUserReaders(Connection sql, int uid) { + return SQLHelpers.getArrayInteger(sql, "SELECT suser_id FROM subscr_users WHERE user_id=?", uid); + } + + public static ArrayList getUserBLUsers(Connection sql, int uid) { + ArrayList users = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT users.id,users.nick FROM users INNER JOIN bl_users ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.UName = rs.getString(2); + users.add(u); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return users; + } + + public static int getStatsIRead(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); + } + + public static int getStatsMyReaders(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE user_id=?", uid, 0); + } + + public static int getStatsMessages(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM messages WHERE user_id=?", uid, 0); + } + + public static int getStatsReplies(Connection sql, int uid) { + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); + } +} diff --git a/src/main/java/com/juick/server/Utils.java b/src/main/java/com/juick/server/Utils.java new file mode 100644 index 00000000..f97797d7 --- /dev/null +++ b/src/main/java/com/juick/server/Utils.java @@ -0,0 +1,86 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class Utils { + + public static String convertArrayInt2String(ArrayList mids) { + String q = ""; + for (int i = 0; i < mids.size(); i++) { + if (i > 0) { + q += ","; + } + q += mids.get(i); + } + return q; + } + + public static String convertArrayString2String(ArrayList unames) { + String q = ""; + for (int i = 0; i < unames.size(); i++) { + if (i > 0) { + q += ","; + } + q += "\"" + unames.get(i) + "\""; + } + return q; + } + + public static String buildQueryArray(String query1, int length, String query2) { + String ret = query1; + for (int i = 0; i < length; i++) { + if (i > 0) { + ret += ","; + } + ret += "?"; + } + ret += query2; + return ret; + } + + public static void stmtSetStringArray(PreparedStatement stmt, int offset, String strs[]) throws SQLException { + for (int i = 0; i < strs.length; i++) { + stmt.setString(offset + i, strs[i]); + } + } + + public static void finishSQL(ResultSet rs, Statement stmt) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + } + } + } +} -- cgit v1.2.3 From 5f981ff945d7bad508d0fb56d037bfe3dccd0cf3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 31 Oct 2015 00:36:58 +0300 Subject: Do not show banned users in feed --- src/main/java/com/juick/server/MessagesQueries.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 17e9f832..d8d4887d 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -416,21 +416,29 @@ public class MessagesQueries { try { if (visitor_uid > 1) { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); stmt.setInt(2, visitor_uid); stmt.setInt(3, visitor_uid); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND (privacy>0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0" + + " AND (privacy>0 OR user_id=?)" + + " AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, visitor_uid); stmt.setInt(2, visitor_uid); } } else { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND user_id NOT IN (SELECT id from users WHERE banned=1) " + + " ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); } } -- cgit v1.2.3 From caf7d95837e0dae5056b2b297a47928f0adc5900 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 26 Nov 2015 02:37:01 +0300 Subject: APNS --- src/main/java/com/juick/server/PushQueries.java | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PushQueries.java b/src/main/java/com/juick/server/PushQueries.java index 84f42722..bf7505ca 100644 --- a/src/main/java/com/juick/server/PushQueries.java +++ b/src/main/java/com/juick/server/PushQueries.java @@ -9,6 +9,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; /** * @@ -65,4 +66,29 @@ public class PushQueries { } return urls; } + + public static String getAPNSToken(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT token from ios WHERE user_id=?", uid); + } + + public static List getAPNSSubscribers(Connection sql, int uid) { + List urls = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT token FROM ios INNER JOIN subscr_users ON (subscr_users.user_id=? AND ios.user_id=subscr_users.suser_id)"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + urls.add(rs.getString(1)); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return urls; + } } -- cgit v1.2.3 From c49596ab050db2f30d97d6f7129b8239bb4f33e3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 30 Nov 2015 20:26:20 +0300 Subject: do not show bl_users in photos --- src/main/java/com/juick/server/MessagesQueries.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 8bef9018..99ab1ce4 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -707,12 +707,14 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); stmt.setInt(2, visitor_uid); + stmt.setInt(3, visitor_uid); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, visitor_uid); + stmt.setInt(2, visitor_uid); } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From 0c451fd02654ffbd56b6422affe8da38ed810644 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 30 Nov 2015 20:31:53 +0300 Subject: fix previous query --- src/main/java/com/juick/server/MessagesQueries.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 7501e869..82fef672 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -719,14 +719,12 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); stmt.setInt(2, visitor_uid); - stmt.setInt(3, visitor_uid); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, visitor_uid); - stmt.setInt(2, visitor_uid); } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From 23666546ca2c0f1ee492b1bea8c0ea83c8c04a2b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 30 Nov 2015 20:41:01 +0300 Subject: do not show banned users in search --- src/main/java/com/juick/server/MessagesQueries.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 7501e869..b6e722f3 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -749,11 +749,11 @@ public class MessagesQueries { try { if (before > 0) { - stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { -- cgit v1.2.3 From 0a00a5e5625ea0241c0effb3ed6f715fd737a04d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 1 Dec 2015 12:34:32 +0300 Subject: Revert "do not show banned users in search" This reverts commit 23666546ca2c0f1ee492b1bea8c0ea83c8c04a2b. --- src/main/java/com/juick/server/MessagesQueries.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index c13aec2b..82fef672 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -747,11 +747,11 @@ public class MessagesQueries { try { if (before > 0) { - stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { -- cgit v1.2.3 From 79e48598873f6a2b4ec1aa850e310a689d862afa Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 1 Dec 2015 16:27:32 +0300 Subject: link twitter account query --- src/main/java/com/juick/server/UserQueries.java | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index d1313715..470f3c8e 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -17,6 +17,8 @@ */ package com.juick.server; +import com.juick.User; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -574,6 +576,33 @@ public class UserQueries { return users; } + public static boolean linkTwitterAccount(Connection sql, User user, String accessToken, + String accessTokenSecret, String screenName) { + try { + PreparedStatement stmt = + sql.prepareStatement("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + + "VALUES (?,?,?,?)" + + " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?;"); + stmt.setInt(1, user.UID); + stmt.setString(2, accessToken); + stmt.setString(3, accessTokenSecret); + stmt.setString(4, screenName); + stmt.setString(5, accessToken); + stmt.setString(6, accessTokenSecret); + stmt.setString(7, screenName); + if (stmt.execute()) { + PreparedStatement stmt2 = + sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id,jid) " + + "VALUES (?,1741,'juick\\@twitter.juick.com')"); + stmt2.setInt(1, user.UID); + return stmt2.execute(); + } + } catch (SQLException e) { + + } + return false; + } + public static int getStatsIRead(Connection sql, int uid) { return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); } -- cgit v1.2.3 From 512d0b353f28e241937be3fa146617ba8ff97c9c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 16 Dec 2015 13:24:29 +0300 Subject: getUsersByHash: fetch users.banned field --- src/main/java/com/juick/server/UserQueries.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 470f3c8e..95944ce0 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -281,13 +281,14 @@ public class UserQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT logins.user_id,users.nick FROM logins INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?"); + stmt = sql.prepareStatement("SELECT logins.user_id,users.nick, users.banned FROM logins INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?"); stmt.setString(1, hash); rs = stmt.executeQuery(); if (rs.first()) { user = new com.juick.User(); user.UID = rs.getInt(1); user.UName = rs.getString(2); + user.Banned = rs.getBoolean(3); user.AuthHash = hash; } } catch (SQLException e) { -- cgit v1.2.3 From 03316e2af6989e026318e6860f27d26e6e805d4d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 22 Dec 2015 16:40:00 +0300 Subject: initial logging --- src/main/java/com/juick/server/UserQueries.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 95944ce0..7e3a212b 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -27,6 +27,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Random; import java.util.UUID; +import java.util.logging.Logger; /** * @@ -36,6 +37,8 @@ public class UserQueries { static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static Logger logger = Logger.getLogger(UserQueries.class.getName()); + public static String getSignUpHashByJID(Connection sql, String jid) { String hash = SQLHelpers.getString(sql, "SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", jid); @@ -583,7 +586,7 @@ public class UserQueries { PreparedStatement stmt = sql.prepareStatement("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + "VALUES (?,?,?,?)" + - " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?;"); + " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?"); stmt.setInt(1, user.UID); stmt.setString(2, accessToken); stmt.setString(3, accessTokenSecret); @@ -599,7 +602,7 @@ public class UserQueries { return stmt2.execute(); } } catch (SQLException e) { - + logger.throwing(UserQueries.class.getName(), "linkTwitterAccount", e); } return false; } -- cgit v1.2.3 From 98b91289a497989e2c776c7e4e333ce6df719211 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 10 Jan 2016 15:10:16 +0300 Subject: ON/OFF --- src/main/java/com/juick/server/UserQueries.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index d1313715..9140e8ea 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -589,4 +589,29 @@ public class UserQueries { public static int getStatsReplies(Connection sql, int uid) { return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); } + + public enum ActiveStatus { + Inactive, + Active + } + + public static boolean setActiveStatusForJID(Connection sql, String JID, ActiveStatus jidStatus) { + User user = getUserByJID(sql, JID); + if (user != null) { + PreparedStatement preparedStatement = null; + try { + preparedStatement = sql.prepareStatement( + "UPDATE jids SET active=? WHERE user_id=? AND jid=?"); + int newStatus = jidStatus == ActiveStatus.Active ? 1 : 0; + preparedStatement.setInt(1, newStatus); + preparedStatement.setInt(2, user.UID); + preparedStatement.setString(3, JID); + return preparedStatement.executeUpdate() >= 0; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + Utils.finishSQL(null, preparedStatement); + } + } + } } -- cgit v1.2.3 From 2713251c8503f6a0b5ba7cb45f91cf71fa8f81b2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 10 Jan 2016 15:36:45 +0300 Subject: fix ON/OFF --- src/main/java/com/juick/server/UserQueries.java | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index ff819629..cb192b73 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -646,5 +646,6 @@ public class UserQueries { Utils.finishSQL(null, preparedStatement); } } + return false; } } -- cgit v1.2.3 From 439d42ef6d60f0a535fdfa2457da8417b70d966a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 10 Jan 2016 16:19:30 +0300 Subject: ShowQueries --- src/main/java/com/juick/server/ShowQueries.java | 72 +++++++++++++++++++++++++ src/main/java/com/juick/server/UserQueries.java | 3 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/juick/server/ShowQueries.java (limited to 'src/main') diff --git a/src/main/java/com/juick/server/ShowQueries.java b/src/main/java/com/juick/server/ShowQueries.java new file mode 100644 index 00000000..06aafb2d --- /dev/null +++ b/src/main/java/com/juick/server/ShowQueries.java @@ -0,0 +1,72 @@ +package com.juick.server; + +import com.juick.User; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Created by vt on 10/01/16. + */ +public class ShowQueries { + + private static final Logger logger = Logger.getLogger(ShowQueries.class.getName()); + + public static List getRecommendedUsers(Connection sql, User forUser) { + List result = new ArrayList<>(); + PreparedStatement preparedStatement = null; + ResultSet rs = null; + try { + preparedStatement = sql.prepareStatement("SELECT users.nick FROM subscr_users INNER JOIN users " + + "ON subscr_users.user_id=users.id " + + "WHERE subscr_users.user_id NOT IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " + + "AND subscr_users.suser_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " + + "AND subscr_users.user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + + "AND subscr_users.user_id!=? AND users.lastmessage>UNIX_TIMESTAMP()-259200 " + + "GROUP BY subscr_users.user_id ORDER BY count(*) DESC LIMIT 10"); + preparedStatement.setInt(1, forUser.UID); + preparedStatement.setInt(2, forUser.UID); + preparedStatement.setInt(3, forUser.UID); + preparedStatement.setInt(4, forUser.UID); + rs = preparedStatement.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + result.add(rs.getString(1)); + } + } catch (SQLException e) { + logger.log(Level.SEVERE, "sql error", e); + } finally { + Utils.finishSQL(rs, preparedStatement); + } + return result; + } + + public static List getTopUsers(Connection sql) { + List result = new ArrayList<>(); + PreparedStatement preparedStatement = null; + ResultSet rs = null; + try { + preparedStatement = sql.prepareStatement("SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " + + "FROM (subscr_users INNER JOIN users ON subscr_users.user_id=users.id) " + + "INNER JOIN useroptions ON users.id=useroptions.user_id " + + "WHERE useroptions.privacy_view>0 AND users.lastmessage>UNIX_TIMESTAMP()-259200 " + + "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10"); + rs = preparedStatement.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + result.add(rs.getString(1)); + } + } catch (SQLException e) { + logger.log(Level.SEVERE, "sql error", e); + } finally { + Utils.finishSQL(rs, preparedStatement); + } + return result; + } +} diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index cb192b73..767a68d2 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -27,6 +27,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Random; import java.util.UUID; +import java.util.logging.Level; import java.util.logging.Logger; /** @@ -641,7 +642,7 @@ public class UserQueries { preparedStatement.setString(3, JID); return preparedStatement.executeUpdate() >= 0; } catch (SQLException e) { - e.printStackTrace(); + logger.log(Level.SEVERE, "sql error", e); } finally { Utils.finishSQL(null, preparedStatement); } -- cgit v1.2.3 From c37ed56f884661666b964e47efd23a36be9b24d4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 10 Jan 2016 16:56:51 +0300 Subject: logger, diamond and arraylist refactoring --- src/main/java/com/juick/server/AdsQueries.java | 6 +- .../java/com/juick/server/CrosspostQueries.java | 8 +- .../java/com/juick/server/MessagesQueries.java | 173 +++++++++++---------- src/main/java/com/juick/server/PMQueries.java | 54 ++++--- src/main/java/com/juick/server/PushQueries.java | 20 ++- src/main/java/com/juick/server/SQLHelpers.java | 25 +-- .../com/juick/server/SubscriptionsQueries.java | 39 +++-- src/main/java/com/juick/server/TagQueries.java | 37 +++-- src/main/java/com/juick/server/UserQueries.java | 79 +++++----- src/main/java/com/juick/server/Utils.java | 6 +- 10 files changed, 244 insertions(+), 203 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/AdsQueries.java b/src/main/java/com/juick/server/AdsQueries.java index 0fb4c0a6..ad9b7149 100644 --- a/src/main/java/com/juick/server/AdsQueries.java +++ b/src/main/java/com/juick/server/AdsQueries.java @@ -20,6 +20,8 @@ package com.juick.server; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -27,6 +29,8 @@ import java.sql.SQLException; */ public class AdsQueries { + private static final Logger logger = Logger.getLogger(AdsQueries.class.getName()); + public static int getAdMID(Connection sql, int uid) { if (uid > 0) { return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); @@ -43,7 +47,7 @@ public class AdsQueries { stmt.setInt(2, mid); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } diff --git a/src/main/java/com/juick/server/CrosspostQueries.java b/src/main/java/com/juick/server/CrosspostQueries.java index fd677048..b44b6b59 100644 --- a/src/main/java/com/juick/server/CrosspostQueries.java +++ b/src/main/java/com/juick/server/CrosspostQueries.java @@ -21,6 +21,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -28,6 +30,8 @@ import java.sql.SQLException; */ public class CrosspostQueries { + private static final Logger logger = Logger.getLogger(CrosspostQueries.class.getName()); + public static String[] getTwitterTokens(Connection sql, int uid) { String tokens[] = null; @@ -43,7 +47,7 @@ public class CrosspostQueries { tokens[1] = rs.getString(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -70,7 +74,7 @@ public class CrosspostQueries { tokens[1] = rs.getString(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 82fef672..4d5683bd 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -25,6 +25,9 @@ import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -32,11 +35,13 @@ import java.util.Collections; */ public class MessagesQueries { - public static int createMessage(Connection sql, int uid, String txt, String attachment, ArrayList tags) { + private static final Logger logger = Logger.getLogger(MessagesQueries.class.getName()); + + public static int createMessage(Connection sql, int uid, String txt, String attachment, List tags) { int mid = 0; PreparedStatement stmt = null; - ResultSet rs = null; + ResultSet rs; try { stmt = sql.prepareStatement("INSERT INTO messages(user_id,attach) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, uid); @@ -51,7 +56,7 @@ public class MessagesQueries { mid = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -73,7 +78,7 @@ public class MessagesQueries { stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -81,7 +86,7 @@ public class MessagesQueries { try { stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); stmt.setInt(1, mid); - if (tagsNames == "") { + if (tagsNames.isEmpty()) { stmt.setNull(2, Types.VARCHAR); } else { stmt.setString(2, tagsNames); @@ -89,7 +94,7 @@ public class MessagesQueries { stmt.setString(3, txt); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -118,7 +123,7 @@ public class MessagesQueries { ridnew = 0; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -166,7 +171,7 @@ public class MessagesQueries { privacy = rs.getInt(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -189,7 +194,7 @@ public class MessagesQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -210,7 +215,7 @@ public class MessagesQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -255,7 +260,7 @@ public class MessagesQueries { msg.Hidden = rs.getBoolean(13); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -276,7 +281,7 @@ public class MessagesQueries { msg.Text = rs.getString(3); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -307,7 +312,7 @@ public class MessagesQueries { msg.Text = rs.getString(6); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -330,15 +335,15 @@ public class MessagesQueries { user.UName = rs.getString(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return user; } - public static ArrayList getMessageTags(Connection sql, int mid) { - ArrayList tags = new ArrayList(); + public static List getMessageTags(Connection sql, int mid) { + List tags = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -356,7 +361,7 @@ public class MessagesQueries { tags.add(t); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -364,8 +369,8 @@ public class MessagesQueries { return tags; } - public static ArrayList getMessageTagsIDs(Connection sql, int mid) { - ArrayList tids = new ArrayList(); + public static List getMessageTagsIDs(Connection sql, int mid) { + List tids = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -378,7 +383,7 @@ public class MessagesQueries { tids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -387,7 +392,7 @@ public class MessagesQueries { } public static ArrayList getMessageRecommendations(Connection sql, int mid) { - ArrayList users = new ArrayList(); + ArrayList users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -400,7 +405,7 @@ public class MessagesQueries { users.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -408,8 +413,8 @@ public class MessagesQueries { return users; } - public static ArrayList getAll(Connection sql, int visitor_uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getAll(Connection sql, int visitor_uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -448,15 +453,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { - ArrayList mids = new ArrayList(20); + public static List getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -483,15 +488,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getTags(Connection sql, String tids, int visitor_uid, int before, int cnt) { - ArrayList mids = new ArrayList(20); + public static List getTags(Connection sql, String tids, int visitor_uid, int before, int cnt) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -512,15 +517,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getPlace(Connection sql, int place_id, int visitor_uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getPlace(Connection sql, int place_id, int visitor_uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -541,15 +546,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getMyFeed(Connection sql, int uid, int before) { - ArrayList mids = new ArrayList(40); + public static List getMyFeed(Connection sql, int uid, int before) { + List mids = new ArrayList<>(40); PreparedStatement stmt = null; ResultSet rs = null; try { @@ -569,7 +574,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -589,7 +594,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -603,8 +608,8 @@ public class MessagesQueries { return mids; } - public static ArrayList getPrivate(Connection sql, int uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getPrivate(Connection sql, int uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -623,15 +628,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getDiscussions(Connection sql, int uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getDiscussions(Connection sql, int uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -650,7 +655,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -658,8 +663,8 @@ public class MessagesQueries { return mids; } - public static ArrayList getRecommended(Connection sql, int uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getRecommended(Connection sql, int uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -678,7 +683,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -687,8 +692,8 @@ public class MessagesQueries { return mids; } - public static ArrayList getPopular(Connection sql, int before) { - ArrayList mids = new ArrayList(20); + public static List getPopular(Connection sql, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -705,15 +710,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getPhotos(Connection sql, int visitor_uid, int before) { - ArrayList mids = new ArrayList(20); + public static List getPhotos(Connection sql, int visitor_uid, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -732,15 +737,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getSearch(Connection sql, Connection sqlSearch, String search, int before) { - ArrayList mids0 = new ArrayList(20); + public static List getSearch(Connection sql, Connection sqlSearch, String search, int before) { + List mids0 = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -760,12 +765,12 @@ public class MessagesQueries { mids0.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } - ArrayList mids = new ArrayList(20); + List mids = new ArrayList<>(20); if (mids0.size() > 0) { try { stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); @@ -775,7 +780,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -784,8 +789,8 @@ public class MessagesQueries { return mids; } - public static ArrayList getUserBlog(Connection sql, int UID, int privacy, int before) { - ArrayList mids = new ArrayList(20); + public static List getUserBlog(Connection sql, int UID, int privacy, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -804,15 +809,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getUserTag(Connection sql, int UID, int TID, int privacy, int before) { - ArrayList mids = new ArrayList(20); + public static List getUserTag(Connection sql, int UID, int TID, int privacy, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -833,15 +838,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getUserRecommendations(Connection sql, int UID, int before) { - ArrayList mids = new ArrayList(20); + public static List getUserRecommendations(Connection sql, int UID, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -860,15 +865,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getUserPhotos(Connection sql, int UID, int privacy, int before) { - ArrayList mids = new ArrayList(20); + public static List getUserPhotos(Connection sql, int UID, int privacy, int before) { + List mids = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -887,15 +892,15 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return mids; } - public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { - ArrayList mids0 = new ArrayList(20); + public static List getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { + List mids0 = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -916,12 +921,12 @@ public class MessagesQueries { mids0.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } - ArrayList mids = new ArrayList(20); + List mids = new ArrayList<>(20); if (mids0.size() > 0) { try { stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); @@ -931,7 +936,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -940,8 +945,8 @@ public class MessagesQueries { return mids; } - public static ArrayList getMessages(Connection sql, ArrayList mids) { - ArrayList msgs = new ArrayList(20); + public static List getMessages(Connection sql, List mids) { + List msgs = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -978,7 +983,7 @@ public class MessagesQueries { msgs.add(msg); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -986,8 +991,8 @@ public class MessagesQueries { return msgs; } - public static ArrayList getReplies(Connection sql, int mid) { - ArrayList replies = new ArrayList(); + public static List getReplies(Connection sql, int mid) { + List replies = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -1013,7 +1018,7 @@ public class MessagesQueries { replies.add(msg); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -1039,7 +1044,7 @@ public class MessagesQueries { } ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -1050,7 +1055,7 @@ public class MessagesQueries { stmt.setInt(1, mid); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -1068,7 +1073,7 @@ public class MessagesQueries { stmt.setInt(1, mid); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 56b91abf..c4b4fa2d 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -17,11 +17,16 @@ */ package com.juick.server; +import com.juick.User; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -29,6 +34,8 @@ import java.util.ArrayList; */ public class PMQueries { + private static final Logger logger = Logger.getLogger(PMQueries.class.getName()); + public static boolean createPM(Connection sql, int uid_from, int uid_to, String body) { boolean ret = false; PreparedStatement stmt = null; @@ -39,7 +46,7 @@ public class PMQueries { stmt.setString(3, body); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -52,7 +59,7 @@ public class PMQueries { stmt2.setInt(2, uid_to); stmt2.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt2); } @@ -70,7 +77,7 @@ public class PMQueries { stmt.setString(2, jid); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -86,7 +93,7 @@ public class PMQueries { stmt.setString(2, jid); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -107,7 +114,7 @@ public class PMQueries { ret = true; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -128,15 +135,15 @@ public class PMQueries { ret = rs.getString(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static ArrayList getPMLastConversationsUsers(Connection sql, int uid, int cnt) { - ArrayList users = new ArrayList(); + public static List getPMLastConversationsUsers(Connection sql, int uid, int cnt) { + List users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -152,7 +159,7 @@ public class PMQueries { users.add(u); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -177,7 +184,7 @@ public class PMQueries { } } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -190,18 +197,17 @@ public class PMQueries { return users; } - public static boolean haveUserInArray(ArrayList arr, int uid) { - int s = arr.size(); - for (int i = 0; i < s; i++) { - if (arr.get(i).UID == uid) { + public static boolean haveUserInArray(List arr, int uid) { + for (User user : arr) { + if (user.UID == uid) { return true; } } return false; } - public static ArrayList getPMMessages(Connection sql, int uid, int uid_to) { - ArrayList msgs = new ArrayList(20); + public static List getPMMessages(Connection sql, int uid, int uid_to) { + List msgs = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -223,7 +229,7 @@ public class PMQueries { msgs.add(0, msg); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -235,7 +241,7 @@ public class PMQueries { stmt2.setInt(2, uid_to); stmt2.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt2); } @@ -243,8 +249,8 @@ public class PMQueries { return msgs; } - public static ArrayList getLastPMInbox(Connection sql, int uid) { - ArrayList msgs = new ArrayList(20); + public static List getLastPMInbox(Connection sql, int uid) { + List msgs = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -264,7 +270,7 @@ public class PMQueries { msgs.add(0, msg); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -272,8 +278,8 @@ public class PMQueries { return msgs; } - public static ArrayList getLastPMSent(Connection sql, int uid) { - ArrayList msgs = new ArrayList(20); + public static List getLastPMSent(Connection sql, int uid) { + List msgs = new ArrayList<>(20); PreparedStatement stmt = null; ResultSet rs = null; @@ -293,7 +299,7 @@ public class PMQueries { msgs.add(0, msg); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/PushQueries.java b/src/main/java/com/juick/server/PushQueries.java index bf7505ca..c17972b0 100644 --- a/src/main/java/com/juick/server/PushQueries.java +++ b/src/main/java/com/juick/server/PushQueries.java @@ -10,6 +10,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -17,12 +19,14 @@ import java.util.List; */ public class PushQueries { + private static final Logger logger = Logger.getLogger(PushQueries.class.getName()); + public static String getAndroidRegID(Connection sql, int uid) { return SQLHelpers.getString(sql, "SELECT regid FROM android WHERE user_id=?", uid); } - public static ArrayList getAndroidSubscribers(Connection sql, int uid) { - ArrayList regids = new ArrayList(); + public static List getAndroidSubscribers(Connection sql, int uid) { + List regids = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -35,7 +39,7 @@ public class PushQueries { regids.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -46,8 +50,8 @@ public class PushQueries { return SQLHelpers.getString(sql, "SELECT url FROM winphone WHERE user_id=?", uid); } - public static ArrayList getWinPhoneSubscribers(Connection sql, int uid) { - ArrayList urls = new ArrayList(); + public static List getWinPhoneSubscribers(Connection sql, int uid) { + List urls = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -60,7 +64,7 @@ public class PushQueries { urls.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -72,7 +76,7 @@ public class PushQueries { } public static List getAPNSSubscribers(Connection sql, int uid) { - List urls = new ArrayList(); + List urls = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -85,7 +89,7 @@ public class PushQueries { urls.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/SQLHelpers.java b/src/main/java/com/juick/server/SQLHelpers.java index f5569993..13ebe98a 100644 --- a/src/main/java/com/juick/server/SQLHelpers.java +++ b/src/main/java/com/juick/server/SQLHelpers.java @@ -9,6 +9,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -16,6 +19,8 @@ import java.util.ArrayList; */ public class SQLHelpers { + private static final Logger logger = Logger.getLogger(SQLHelpers.class.getName()); + public static int execute(Connection sql, String query) { int ret = -1; PreparedStatement stmt = null; @@ -23,7 +28,7 @@ public class SQLHelpers { stmt = sql.prepareStatement(query); ret = stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -38,7 +43,7 @@ public class SQLHelpers { stmt.setInt(1, param); ret = stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -56,7 +61,7 @@ public class SQLHelpers { ret = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -75,7 +80,7 @@ public class SQLHelpers { ret = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -94,7 +99,7 @@ public class SQLHelpers { ret = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -113,7 +118,7 @@ public class SQLHelpers { ret = rs.getString(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -132,15 +137,15 @@ public class SQLHelpers { ret = rs.getString(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static ArrayList getArrayInteger(Connection sql, String query, int param) { - ArrayList ret = new ArrayList(); + public static List getArrayInteger(Connection sql, String query, int param) { + List ret = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -153,7 +158,7 @@ public class SQLHelpers { ret.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index d0f5f308..637c2bcf 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -9,6 +9,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -16,8 +19,10 @@ import java.util.ArrayList; */ public class SubscriptionsQueries { - public static ArrayList getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) { - ArrayList jids = new ArrayList(); + private static final Logger logger = Logger.getLogger(SubscriptionsQueries.class.getName()); + + public static List getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) { + List jids = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -36,27 +41,27 @@ public class SubscriptionsQueries { jids.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return jids; } - public static ArrayList getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { - ArrayList jids = new ArrayList(); + public static List getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { + List jids = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; String tbl = "subscr_jids_" + mid; - ArrayList tags = MessagesQueries.getMessageTagsIDs(sql, mid); + List 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) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -69,7 +74,7 @@ public class SubscriptionsQueries { stmt = sql.prepareStatement(query); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -87,7 +92,7 @@ public class SubscriptionsQueries { jids.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -96,7 +101,7 @@ public class SubscriptionsQueries { stmt = sql.prepareStatement("DROP TABLE " + tbl); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -104,8 +109,8 @@ public class SubscriptionsQueries { return jids; } - public static ArrayList getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { - ArrayList jids = new ArrayList(); + public static List getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { + List jids = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -119,17 +124,17 @@ public class SubscriptionsQueries { jids.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return jids; } - public static ArrayList getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { - ArrayList jids = new ArrayList(); + public static List getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { + List jids = new ArrayList<>(); - ArrayList tags = MessagesQueries.getMessageTagsIDs(sql, mid); + List tags = MessagesQueries.getMessageTagsIDs(sql, mid); PreparedStatement stmt = null; ResultSet rs = null; @@ -154,7 +159,7 @@ public class SubscriptionsQueries { jids.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 493e8675..1c5e27d1 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -17,12 +17,17 @@ */ package com.juick.server; +import com.juick.Tag; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -30,6 +35,8 @@ import java.util.ArrayList; */ public class TagQueries { + private static final Logger logger = Logger.getLogger(TagQueries.class.getName()); + public static com.juick.Tag getTag(Connection sql, int tid) { com.juick.Tag ret = null; @@ -46,7 +53,7 @@ public class TagQueries { ret.Name = rs.getString(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -70,7 +77,7 @@ public class TagQueries { ret.Name = rs.getString(3); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -84,12 +91,12 @@ public class TagQueries { return ret; } - public static ArrayList getTags(Connection sql, String[] tags, boolean autoCreate) { - ArrayList ret = new ArrayList(); + public static List getTags(Connection sql, String[] tags, boolean autoCreate) { + List ret = new ArrayList<>(); - for (int i = 0; i < tags.length; i++) { - if (!tags[i].isEmpty()) { - com.juick.Tag t = getTag(sql, tags[i], autoCreate); + for (String tag : tags) { + if (!tag.isEmpty()) { + Tag t = getTag(sql, tag, autoCreate); if (t != null) { ret.add(t); } @@ -112,7 +119,7 @@ public class TagQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -134,7 +141,7 @@ public class TagQueries { ret = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -142,8 +149,8 @@ public class TagQueries { return ret; } - public static ArrayList getUserTagsAll(Connection sql, int uid) { - ArrayList tags = new ArrayList(); + public static List getUserTagsAll(Connection sql, int uid) { + List tags = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -159,15 +166,15 @@ public class TagQueries { tags.add(t); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return tags; } - public static ArrayList getUserBLTags(Connection sql, int uid) { - ArrayList tags = new ArrayList(); + public static List getUserBLTags(Connection sql, int uid) { + List tags = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -180,7 +187,7 @@ public class TagQueries { tags.add(rs.getString(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 767a68d2..98573bfb 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -25,6 +25,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.List; import java.util.Random; import java.util.UUID; import java.util.logging.Level; @@ -52,7 +53,7 @@ public class UserQueries { stmt.setString(2, hash); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -76,7 +77,7 @@ public class UserQueries { uid = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -103,7 +104,7 @@ public class UserQueries { user.Banned = rs.getBoolean(2); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -126,7 +127,7 @@ public class UserQueries { user.Banned = rs.getBoolean(3); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -149,15 +150,15 @@ public class UserQueries { user.JID = jid; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return user; } - public static ArrayList getUsersByName(Connection sql, ArrayList unames) { - ArrayList users = new ArrayList(); + public static List getUsersByName(Connection sql, List unames) { + List users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -172,15 +173,15 @@ public class UserQueries { users.add(user); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return users; } - public static ArrayList getUsersByID(Connection sql, ArrayList uids) { - ArrayList users = new ArrayList(); + public static List getUsersByID(Connection sql, List uids) { + List users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -195,7 +196,7 @@ public class UserQueries { users.add(u); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -203,7 +204,7 @@ public class UserQueries { return users; } - public static boolean fillUsersByID(Connection sql, ArrayList users) { + public static boolean fillUsersByID(Connection sql, List users) { boolean ret = false; String uids = ""; @@ -231,7 +232,7 @@ public class UserQueries { } } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -239,8 +240,8 @@ public class UserQueries { return ret; } - public static ArrayList getUsersByJID(Connection sql, ArrayList jids) { - ArrayList users = new ArrayList(); + public static List getUsersByJID(Connection sql, List jids) { + List users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -256,7 +257,7 @@ public class UserQueries { users.add(user); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -296,7 +297,7 @@ public class UserQueries { user.AuthHash = hash; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -315,7 +316,7 @@ public class UserQueries { stmt.setString(2, hash); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -353,7 +354,7 @@ public class UserQueries { } } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -373,7 +374,7 @@ public class UserQueries { ret = rs.getInt(1); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -388,7 +389,7 @@ public class UserQueries { stmt.setInt(2, uid); stmt.executeUpdate(); } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -407,7 +408,7 @@ public class UserQueries { ret = rs.getInt(1) < 3600; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -428,7 +429,7 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -449,7 +450,7 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -472,15 +473,15 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static ArrayList checkBL(Connection sql, int visitor, ArrayList uids) { - ArrayList ret = new ArrayList(); + public static List checkBL(Connection sql, int visitor, List uids) { + List ret = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -493,7 +494,7 @@ public class UserQueries { ret.add(rs.getInt(1)); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -515,19 +516,19 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static ArrayList getUserRead(Connection sql, int uid) { + public static List getUserRead(Connection sql, int uid) { return SQLHelpers.getArrayInteger(sql, "SELECT user_id FROM subscr_users WHERE suser_id=?", uid); } - public static ArrayList getUserReadLeastPopular(Connection sql, int uid, int cnt) { - ArrayList users = new ArrayList(cnt); + public static List getUserReadLeastPopular(Connection sql, int uid, int cnt) { + List users = new ArrayList<>(cnt); PreparedStatement stmt = null; ResultSet rs = null; @@ -544,7 +545,7 @@ public class UserQueries { users.add(u); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -552,12 +553,12 @@ public class UserQueries { return users; } - public static ArrayList getUserReaders(Connection sql, int uid) { + public static List getUserReaders(Connection sql, int uid) { return SQLHelpers.getArrayInteger(sql, "SELECT suser_id FROM subscr_users WHERE user_id=?", uid); } - public static ArrayList getUserBLUsers(Connection sql, int uid) { - ArrayList users = new ArrayList(); + public static List getUserBLUsers(Connection sql, int uid) { + List users = new ArrayList<>(); PreparedStatement stmt = null; ResultSet rs = null; @@ -573,7 +574,7 @@ public class UserQueries { users.add(u); } } catch (SQLException e) { - System.err.println(e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -603,7 +604,7 @@ public class UserQueries { return stmt2.execute(); } } catch (SQLException e) { - logger.throwing(UserQueries.class.getName(), "linkTwitterAccount", e); + logger.log(Level.SEVERE, "sql exception", e); } return false; } @@ -642,7 +643,7 @@ public class UserQueries { preparedStatement.setString(3, JID); return preparedStatement.executeUpdate() >= 0; } catch (SQLException e) { - logger.log(Level.SEVERE, "sql error", e); + logger.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, preparedStatement); } diff --git a/src/main/java/com/juick/server/Utils.java b/src/main/java/com/juick/server/Utils.java index f97797d7..1968150e 100644 --- a/src/main/java/com/juick/server/Utils.java +++ b/src/main/java/com/juick/server/Utils.java @@ -21,7 +21,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; +import java.util.List; /** * @@ -29,7 +29,7 @@ import java.util.ArrayList; */ public class Utils { - public static String convertArrayInt2String(ArrayList mids) { + public static String convertArrayInt2String(List mids) { String q = ""; for (int i = 0; i < mids.size(); i++) { if (i > 0) { @@ -40,7 +40,7 @@ public class Utils { return q; } - public static String convertArrayString2String(ArrayList unames) { + public static String convertArrayString2String(List unames) { String q = ""; for (int i = 0; i < unames.size(); i++) { if (i > 0) { -- cgit v1.2.3 From 7248c94dfd3fecda1c854e09731592a03a79ca31 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 14 Jan 2016 17:01:28 +0300 Subject: spring-jdbc WIP --- build.gradle | 8 + src/main/java/com/juick/server/UserQueries.java | 274 +++++++----------------- 2 files changed, 89 insertions(+), 193 deletions(-) (limited to 'src/main') diff --git a/build.gradle b/build.gradle index 405a81af..b82c3d5c 100644 --- a/build.gradle +++ b/build.gradle @@ -1 +1,9 @@ apply plugin: "java" + +repositories { + mavenCentral() +} + +dependencies { + compile "org.springframework:spring-jdbc:4.2.4.RELEASE" +} \ No newline at end of file diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 98573bfb..bf56e974 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -18,6 +18,11 @@ package com.juick.server; import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.PreparedStatementCreator; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; import java.sql.Connection; import java.sql.PreparedStatement; @@ -41,167 +46,73 @@ public class UserQueries { private static Logger logger = Logger.getLogger(UserQueries.class.getName()); - public static String getSignUpHashByJID(Connection sql, String jid) { - String hash = SQLHelpers.getString(sql, "SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", jid); + static class UserMapper implements RowMapper { + @Override + public User mapRow(ResultSet rs, int rowNum) throws SQLException { + User user = new User(); + user.UID = rs.getInt("id"); + user.UName = rs.getString("nick"); + user.Banned = rs.getBoolean("banned"); + return user; + } + } + public static String getSignUpHashByJID(JdbcTemplate sql, String jid) { + String hash = sql.queryForObject("SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", + String.class, jid); if (hash == null) { hash = UUID.randomUUID().toString(); - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO jids(jid,loginhash) VALUES (?,?)"); - stmt.setString(1, jid); - stmt.setString(2, hash); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + sql.update("INSERT INTO jids(jid,loginhash) VALUES (?,?)", jid, hash); } - return hash; } - public static int createUser(Connection sql, String username, String password) { - int uid = 0; + public static int createUser(JdbcTemplate sql, String username, String password) { + KeyHolder holder = new GeneratedKeyHolder(); + sql.update(con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO users(nick,passw) VALUES (?,?)", + Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, username); + stmt.setString(2, password); + return stmt; + }, holder); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("INSERT INTO users(nick,passw) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); - stmt.setString(1, username); - stmt.setString(2, password); - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.first()) { - uid = rs.getInt(1); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + int uid = holder.getKey().intValue(); - SQLHelpers.executeInt(sql, "INSERT INTO useroptions(user_id) VALUES (?)", uid); - SQLHelpers.executeInt(sql, "INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); + sql.update("INSERT INTO useroptions(user_id) VALUES (?)", uid); + sql.update("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); return uid; } - public static com.juick.User getUserByUID(Connection sql, int uid) { - com.juick.User user = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT nick,banned FROM users WHERE id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - user = new com.juick.User(); - user.UID = uid; - user.UName = rs.getString(1); - user.Banned = rs.getBoolean(2); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return user; + public static User getUserByUID(JdbcTemplate sql, int uid) { + return sql.queryForObject("SELECT id, nick,banned FROM users WHERE id=?", + new Object[] {uid}, + new UserMapper()); } - public static com.juick.User getUserByName(Connection sql, String username) { - com.juick.User user = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,nick,banned FROM users WHERE nick=?"); - stmt.setString(1, username); - rs = stmt.executeQuery(); - if (rs.first()) { - user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - user.Banned = rs.getBoolean(3); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return user; + public static User getUserByName(JdbcTemplate sql, String username) { + return sql.queryForObject("SELECT id,nick,banned FROM users WHERE nick=?", + new Object[] {username}, + new UserMapper()); } - public static com.juick.User getUserByJID(Connection sql, String jid) { - com.juick.User user = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)"); - stmt.setString(1, jid); - rs = stmt.executeQuery(); - if (rs.first()) { - user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - user.JID = jid; - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return user; + public static User getUserByJID(JdbcTemplate sql, String jid) { + return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", + new Object[] {jid}, + new UserMapper()); } - public static List getUsersByName(Connection sql, List unames) { - List users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - users.add(user); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return users; + public static List getUsersByName(JdbcTemplate sql, List unames) { + return sql.query("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", + new UserMapper(), + unames.toArray()); } - public static List getUsersByID(Connection sql, List uids) { - List users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User u = new com.juick.User(); - u.UID = rs.getInt(1); - u.UName = rs.getString(2); - users.add(u); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return users; + public static List getUsersByID(JdbcTemplate sql, List uids) { + return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")", + new UserMapper(), + uids.toArray()); } public static boolean fillUsersByID(Connection sql, List users) { @@ -246,7 +157,9 @@ public class UserQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT users.id,users.nick,jids.jid FROM users INNER JOIN jids ON jids.user_id=users.id WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")"); + stmt = sql.prepareStatement("SELECT users.id,users.nick,jids.jid FROM users " + + "INNER JOIN jids ON jids.user_id=users.id " + + "WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { @@ -264,64 +177,43 @@ public class UserQueries { return users; } - public static String getJIDbyUID(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT jid FROM jids WHERE user_id=? AND active=1", uid); + public static String getJIDbyUID(JdbcTemplate sql, int uid) { + return sql.queryForObject("SELECT jid FROM jids WHERE user_id=? AND active=1", String.class, uid); } - public static int getUIDbyJID(Connection sql, String jid) { - return SQLHelpers.getInt(sql, "SELECT user_id FROM jids WHERE jid=?", jid, 0); + public static int getUIDbyJID(JdbcTemplate sql, String jid) { + return sql.queryForObject("SELECT user_id FROM jids WHERE jid=?", Integer.class, jid); } - public static int getUIDbyName(Connection sql, String uname) { - return SQLHelpers.getInt(sql, "SELECT id FROM users WHERE nick=?", uname, 0); + public static int getUIDbyName(JdbcTemplate sql, String uname) { + return sql.queryForObject("SELECT id FROM users WHERE nick=?", Integer.class, uname); } - public static int getUIDbyHash(Connection sql, String hash) { - return SQLHelpers.getInt(sql, "SELECT user_id FROM logins WHERE hash=?", hash, 0); + public static int getUIDbyHash(JdbcTemplate sql, String hash) { + return sql.queryForObject("SELECT user_id FROM logins WHERE hash=?", Integer.class, hash); } - public static com.juick.User getUserByHash(Connection sql, String hash) { - com.juick.User user = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT logins.user_id,users.nick, users.banned FROM logins INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - user.Banned = rs.getBoolean(3); - user.AuthHash = hash; - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + public static com.juick.User getUserByHash(JdbcTemplate sql, String hash) { + User user = sql.queryForObject("SELECT logins.user_id,users.nick, users.banned FROM logins " + + "INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?", + new UserMapper(), hash); + user.AuthHash = hash; return user; } - public static String getHashByUID(Connection sql, int uid) { - String hash = SQLHelpers.getString(sql, "SELECT hash FROM logins WHERE user_id=?", uid); + public static String getHashByUID(JdbcTemplate sql, int uid) { + String hash = sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); if (hash == null) { hash = generateHash(16); - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); + final String finalHash = hash; + sql.update(con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); stmt.setInt(1, uid); - stmt.setString(2, hash); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + stmt.setString(2, finalHash); + return stmt; + }); } - return hash; } @@ -630,23 +522,19 @@ public class UserQueries { Active } - public static boolean setActiveStatusForJID(Connection sql, String JID, ActiveStatus jidStatus) { + public static boolean setActiveStatusForJID(JdbcTemplate sql, String JID, ActiveStatus jidStatus) { User user = getUserByJID(sql, JID); if (user != null) { - PreparedStatement preparedStatement = null; - try { - preparedStatement = sql.prepareStatement( + return sql.update(con -> { + PreparedStatement preparedStatement = con.prepareStatement( "UPDATE jids SET active=? WHERE user_id=? AND jid=?"); int newStatus = jidStatus == ActiveStatus.Active ? 1 : 0; preparedStatement.setInt(1, newStatus); preparedStatement.setInt(2, user.UID); preparedStatement.setString(3, JID); - return preparedStatement.executeUpdate() >= 0; - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, preparedStatement); - } + return preparedStatement; + + }) >= 0; } return false; } -- cgit v1.2.3 From 9bd8a41c0db5a1027a184facfca8d0152945c078 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 12:50:52 +0300 Subject: spring-jdbc WIP --- .../java/com/juick/server/MessagesQueries.java | 151 +++++++-------- src/main/java/com/juick/server/PMQueries.java | 204 ++++++--------------- src/main/java/com/juick/server/TagQueries.java | 63 +++---- src/main/java/com/juick/server/UserQueries.java | 129 +++++-------- 4 files changed, 191 insertions(+), 356 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 4d5683bd..a8605d3a 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -17,6 +17,9 @@ */ package com.juick.server; +import com.juick.Message; +import org.springframework.jdbc.core.JdbcTemplate; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -28,6 +31,7 @@ import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.springframework.jdbc.core.RowMapper; /** * @@ -553,50 +557,32 @@ public class MessagesQueries { return mids; } - public static List getMyFeed(Connection sql, int uid, int before) { + public static List getMyFeed(JdbcTemplate sql, int uid, int before) { List mids = new ArrayList<>(40); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE message_id=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, uid); - stmt.setInt(2, before); - stmt.setInt(3, uid); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, uid); - stmt.setInt(2, uid); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + if (before > 0) { + mids = sql.queryForList("SELECT message_id FROM messages " + + "INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) " + + "WHERE message_id=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", + Integer.class, new Object[]{uid, before, uid}); + } else { + mids = sql.queryForList("SELECT message_id FROM messages " + + "INNER JOIN subscr_users ON (subscr_users.suser_id=? " + + "AND subscr_users.user_id=messages.user_id) " + + "WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", + Integer.class, new Object[]{uid, uid}); } - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id 0) { + mids.addAll(sql.queryForList("SELECT message_id FROM messages " + + "WHERE user_id=? AND message_id getMessages(Connection sql, List mids) { + public static List getMessages(JdbcTemplate sql, List mids) { List msgs = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon,messages.likes FROM (messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - msg.User = new com.juick.User(); - - msg.MID = rs.getInt(1); - msg.User.UID = rs.getInt(2); - msg.User.UName = rs.getString(3); - if (rs.getString(4) != null) { - msg.parseTags(rs.getString(4)); - } - msg.ReadOnly = rs.getInt(5) == 1; - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.Text = rs.getString(7); - msg.TimeAgo = rs.getInt(8); - msg.TimestampString = rs.getString(9); - msg.Replies = rs.getInt(10); - msg.RepliesBy = rs.getString(11); - msg.AttachmentType = rs.getString(12); - if (rs.getDouble(13) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(13); - msg.Place.lon = rs.getDouble(14); - } - msg.Likes = rs.getInt(15); - - msgs.add(msg); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msgs; + + return sql.query("SELECT messages.message_id,messages.user_id,users.nick," + + "messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt," + + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + + "DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies," + + "messages_txt.repliesby,messages.attach,messages.lat," + + "messages.lon,messages.likes " + + "FROM (messages INNER JOIN messages_txt " + + "ON messages.message_id=messages_txt.message_id) " + + "INNER JOIN users ON messages.user_id=users.id " + + "WHERE messages.message_id " + + "IN (" + Utils.convertArrayInt2String(mids) + ") " + + "ORDER BY messages.message_id DESC", (rs, rowNum) -> { + com.juick.Message msg = new com.juick.Message(); + msg.User = new com.juick.User(); + + msg.MID = rs.getInt(1); + msg.User.UID = rs.getInt(2); + msg.User.UName = rs.getString(3); + if (rs.getString(4) != null) { + msg.parseTags(rs.getString(4)); + } + msg.ReadOnly = rs.getInt(5) == 1; + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Text = rs.getString(7); + msg.TimeAgo = rs.getInt(8); + msg.TimestampString = rs.getString(9); + msg.Replies = rs.getInt(10); + msg.RepliesBy = rs.getString(11); + msg.AttachmentType = rs.getString(12); + if (rs.getDouble(13) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(13); + msg.Place.lon = rs.getDouble(14); + } + msg.Likes = rs.getInt(15); + return msg; + }); } public static List getReplies(Connection sql, int mid) { diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index c4b4fa2d..a2d4545c 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -27,6 +27,8 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; /** * @@ -34,54 +36,24 @@ import java.util.logging.Logger; */ public class PMQueries { - private static final Logger logger = Logger.getLogger(PMQueries.class.getName()); + private static final Logger LOGGER = Logger.getLogger(PMQueries.class.getName()); - public static boolean createPM(Connection sql, int uid_from, int uid_to, String body) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)"); - stmt.setInt(1, uid_from); - stmt.setInt(2, uid_to); - stmt.setString(3, body); - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + public static boolean createPM(JdbcTemplate sql, int uid_from, int uid_to, String body) { + boolean ret = sql.update("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)", + new Object[] {uid_from, uid_to, body}) > 0; if (ret) { - PreparedStatement stmt2 = null; - try { - stmt2 = sql.prepareStatement("INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) VALUES (?,?,NOW(),1) ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1"); - stmt2.setInt(1, uid_from); - stmt2.setInt(2, uid_to); - stmt2.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt2); - } + sql.update("INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) " + + "VALUES (?,?,NOW(),1) " + + "ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1", + new Object[] {uid_from, uid_to}); } - return ret; } - public static boolean addPMinRoster(Connection sql, int uid, String jid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)"); - stmt.setInt(1, uid); - stmt.setString(2, jid); - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + public static boolean addPMinRoster(JdbcTemplate sql, int uid, String jid) { + return sql.update("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)", + new Object[] {uid, jid}) > 0; } public static boolean removePMinRoster(Connection sql, int uid, String jid) { @@ -93,32 +65,18 @@ public class PMQueries { stmt.setString(2, jid); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } return ret; } - public static boolean havePMinRoster(Connection sql, int uid, String jid) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT 1 FROM pm_inroster WHERE user_id=? AND jid=?"); - stmt.setInt(1, uid); - stmt.setString(2, jid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = true; - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + public static boolean havePMinRoster(JdbcTemplate sql, int uid, String jid) { + List res = sql.queryForList("SELECT 1 FROM pm_inroster " + + "WHERE user_id=? AND jid=?", Integer.class, + new Object[] {uid, jid}); + return res.size() > 0; } public static String getLastView(Connection sql, int uid_from, int uid_to) { @@ -135,61 +93,42 @@ public class PMQueries { ret = rs.getString(1); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static List getPMLastConversationsUsers(Connection sql, int uid, int cnt) { - List users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT user_id,unread FROM pm_streams WHERE user_id_to=? AND unread>0 ORDER BY lastmessage DESC LIMIT " + cnt); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User u = new com.juick.User(); - u.UID = rs.getInt(1); - u.MessagesCount = rs.getInt(2); - users.add(u); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + public static List getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { + List users = sql.query("SELECT user_id,unread FROM pm_streams " + + "WHERE user_id_to=? AND unread>0 " + + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { + com.juick.User u = new com.juick.User(); + u.UID = rs.getInt(1); + u.MessagesCount = rs.getInt(2); + return u; + }, new Object[]{uid}); if (users.size() < cnt) { - try { - stmt = sql.prepareStatement("SELECT user_id,user_id_to FROM pm_streams WHERE (user_id=? OR user_id_to=?) AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) ORDER BY lastmessage DESC LIMIT ?"); - stmt.setInt(1, uid); - stmt.setInt(2, uid); - stmt.setInt(3, cnt * 2); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - int uuid = rs.getInt(1) + rs.getInt(2) - uid; - if (!haveUserInArray(users, uuid)) { - com.juick.User u = new com.juick.User(); + List addUsers = sql.query("SELECT user_id,user_id_to " + + "FROM pm_streams WHERE (user_id=? OR user_id_to=?) " + + "AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) " + + "ORDER BY lastmessage DESC LIMIT ?", (rs, num) -> { + User u = new com.juick.User(); + int uuid = rs.getInt(1) + rs.getInt(2) - uid; u.UID = uuid; - users.add(u); - if (users.size() >= cnt) { - break; - } + return u; + }, new Object[]{uid, uid, cnt * 2}); + for (User addUser : addUsers) { + if (!haveUserInArray(users, addUser.UID)) { + users.add(addUser); + if (users.size() >= cnt) { + break; } } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); } } - if (!users.isEmpty()) { UserQueries.fillUsersByID(sql, users); } @@ -206,45 +145,22 @@ public class PMQueries { return false; } - public static List getPMMessages(Connection sql, int uid, int uid_to) { - List msgs = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT user_id,txt,ts FROM pm WHERE (user_id=? AND user_id_to=?) OR (user_id_to=? AND user_id=?) ORDER BY ts DESC LIMIT 20"); - stmt.setInt(1, uid); - stmt.setInt(2, uid_to); - stmt.setInt(3, uid); - stmt.setInt(4, uid_to); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - int uuid = rs.getInt(1); - msg.User = new com.juick.User(); - msg.User.UID = uuid; - msg.Text = rs.getString(2); - msg.TimestampString = rs.getString(3); - msgs.add(0, msg); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - PreparedStatement stmt2 = null; - try { - stmt2 = sql.prepareStatement("UPDATE pm_streams SET lastview=NOW(),unread=0 WHERE user_id_to=? AND user_id=?"); - stmt2.setInt(1, uid); - stmt2.setInt(2, uid_to); - stmt2.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt2); - } + public static List getPMMessages(JdbcTemplate sql, int uid, int uid_to) { + List msgs = sql.query("SELECT user_id,txt,ts FROM pm " + + "WHERE (user_id=? AND user_id_to=?) " + + "OR (user_id_to=? AND user_id=?) ORDER BY ts DESC LIMIT 20", + (rs, rowNum) -> { + com.juick.Message msg = new com.juick.Message(); + int uuid = rs.getInt(1); + msg.User = new com.juick.User(); + msg.User.UID = uuid; + msg.Text = rs.getString(2); + msg.TimestampString = rs.getString(3); + return msg; + }, new Object[]{uid, uid_to, uid, uid_to}); + + sql.update("UPDATE pm_streams SET lastview=NOW(),unread=0 " + + "WHERE user_id_to=? AND user_id=?", new Object[]{uid, uid_to}); return msgs; } @@ -270,7 +186,7 @@ public class PMQueries { msgs.add(0, msg); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -299,7 +215,7 @@ public class PMQueries { msgs.add(0, msg); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 1c5e27d1..076019ff 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -18,6 +18,8 @@ package com.juick.server; import com.juick.Tag; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import java.sql.Connection; import java.sql.PreparedStatement; @@ -28,6 +30,8 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; /** * @@ -61,26 +65,15 @@ public class TagQueries { return ret; } - public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { - com.juick.Tag ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tag_id,synonym_id,name FROM tags WHERE name=?"); - stmt.setString(1, tag); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = new com.juick.Tag(); - ret.TID = rs.getInt(1); - ret.SynonymID = rs.getInt(2); - ret.Name = rs.getString(3); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { + Tag ret = sql.queryForObject("SELECT tag_id,synonym_id,name FROM tags WHERE name=?", + new Object[]{tag}, (rs, rowNum) -> { + Tag ret1 = new Tag(); + ret1.TID = rs.getInt(1); + ret1.SynonymID = rs.getInt(2); + ret1.Name = rs.getString(3); + return ret1; + }); if (ret == null && autoCreate) { ret = new com.juick.Tag(); @@ -91,7 +84,7 @@ public class TagQueries { return ret; } - public static List getTags(Connection sql, String[] tags, boolean autoCreate) { + public static List getTags(JdbcTemplate sql, String[] tags, boolean autoCreate) { List ret = new ArrayList<>(); for (String tag : tags) { @@ -127,26 +120,16 @@ public class TagQueries { return ret; } - public static int createTag(Connection sql, String name) { - int ret = 0; + public static int createTag(JdbcTemplate sql, String name) { + KeyHolder holder = new GeneratedKeyHolder(); + sql.update(con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO tags(name) VALUES (?)", + Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, name); + return stmt; + }, holder); - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); - stmt.setString(1, name); - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return ret; + return holder.getKey().intValue(); } public static List getUserTagsAll(Connection sql, int uid) { diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index bf56e974..27ae2870 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -19,7 +19,6 @@ package com.juick.server; import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; @@ -29,10 +28,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; -import java.util.UUID; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -44,7 +40,7 @@ public class UserQueries { static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - private static Logger logger = Logger.getLogger(UserQueries.class.getName()); + private static final Logger LOGGER = Logger.getLogger(UserQueries.class.getName()); static class UserMapper implements RowMapper { @Override @@ -115,7 +111,7 @@ public class UserQueries { uids.toArray()); } - public static boolean fillUsersByID(Connection sql, List users) { + public static boolean fillUsersByID(JdbcTemplate sql, List users) { boolean ret = false; String uids = ""; @@ -127,54 +123,27 @@ public class UserQueries { uids += users.get(i).UID; } - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + uids + ")"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - int uid = rs.getInt(1); - for (int i = 0; i < usersSize; i++) { - if (users.get(i).UID == uid) { - users.get(i).UName = rs.getString(2); - ret = true; - } - } - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + uids + ")", + (rs, num) -> { + User u = users.get(num); + u.UName = rs.getString(2); + return u; + }); - return ret; + return true; } - public static List getUsersByJID(Connection sql, List jids) { - List users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick,jids.jid FROM users " + - "INNER JOIN jids ON jids.user_id=users.id " + - "WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - user.JID = rs.getString(3); - users.add(user); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return users; + public static List getUsersByJID(JdbcTemplate sql, List jids) { + return sql.query("SELECT users.id,users.nick,jids.jid FROM users " + + "INNER JOIN jids ON jids.user_id=users.id " + + "WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")", + (rs, rowNum) -> { + com.juick.User user = new com.juick.User(); + user.UID = rs.getInt(1); + user.UName = rs.getString(2); + user.JID = rs.getString(3); + return user; + }); } public static String getJIDbyUID(JdbcTemplate sql, int uid) { @@ -246,7 +215,7 @@ public class UserQueries { } } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -266,7 +235,7 @@ public class UserQueries { ret = rs.getInt(1); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -281,7 +250,7 @@ public class UserQueries { stmt.setInt(2, uid); stmt.executeUpdate(); } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -300,7 +269,7 @@ public class UserQueries { ret = rs.getInt(1) < 3600; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -321,7 +290,7 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -342,34 +311,18 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static boolean isInBLAny(Connection sql, int uid, int uid2) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE (user_id=? AND bl_user_id=?) OR (user_id=? AND bl_user_id=?)"); - stmt.setInt(1, uid); - stmt.setInt(2, uid2); - stmt.setInt(3, uid2); - stmt.setInt(4, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + public static boolean isInBLAny(JdbcTemplate sql, int uid, int uid2) { + List res = sql.queryForList("SELECT 1 FROM bl_users " + + "WHERE (user_id=? AND bl_user_id=?) " + + "OR (user_id=? AND bl_user_id=?)", Integer.class, new Object[] {uid, uid2, uid2, uid}); + return res.get(0) == 1; } public static List checkBL(Connection sql, int visitor, List uids) { @@ -386,7 +339,7 @@ public class UserQueries { ret.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -408,15 +361,15 @@ public class UserQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } return ret; } - public static List getUserRead(Connection sql, int uid) { - return SQLHelpers.getArrayInteger(sql, "SELECT user_id FROM subscr_users WHERE suser_id=?", uid); + public static List getUserRead(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid); } public static List getUserReadLeastPopular(Connection sql, int uid, int cnt) { @@ -437,7 +390,7 @@ public class UserQueries { users.add(u); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -445,8 +398,8 @@ public class UserQueries { return users; } - public static List getUserReaders(Connection sql, int uid) { - return SQLHelpers.getArrayInteger(sql, "SELECT suser_id FROM subscr_users WHERE user_id=?", uid); + public static List getUserReaders(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT suser_id FROM subscr_users WHERE user_id=?", Integer.class, uid); } public static List getUserBLUsers(Connection sql, int uid) { @@ -466,7 +419,7 @@ public class UserQueries { users.add(u); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -496,7 +449,7 @@ public class UserQueries { return stmt2.execute(); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } return false; } -- cgit v1.2.3 From 1ef890d21db2ed9aeefc37b71ff82d0f59df8516 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 13:14:11 +0300 Subject: housekeeping --- .../java/com/juick/server/MessagesQueries.java | 74 +++++++++++----------- src/main/java/com/juick/server/PMQueries.java | 1 - src/main/java/com/juick/server/TagQueries.java | 11 ++-- 3 files changed, 41 insertions(+), 45 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index a8605d3a..cff8caff 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -17,7 +17,6 @@ */ package com.juick.server; -import com.juick.Message; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Connection; @@ -31,7 +30,6 @@ import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import org.springframework.jdbc.core.RowMapper; /** * @@ -39,7 +37,7 @@ import org.springframework.jdbc.core.RowMapper; */ public class MessagesQueries { - private static final Logger logger = Logger.getLogger(MessagesQueries.class.getName()); + private static final Logger LOGGER = Logger.getLogger(MessagesQueries.class.getName()); public static int createMessage(Connection sql, int uid, String txt, String attachment, List tags) { int mid = 0; @@ -60,7 +58,7 @@ public class MessagesQueries { mid = rs.getInt(1); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -82,7 +80,7 @@ public class MessagesQueries { stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); stmt.executeUpdate(); } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -98,7 +96,7 @@ public class MessagesQueries { stmt.setString(3, txt); stmt.executeUpdate(); } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -127,7 +125,7 @@ public class MessagesQueries { ridnew = 0; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -175,7 +173,7 @@ public class MessagesQueries { privacy = rs.getInt(2); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -198,7 +196,7 @@ public class MessagesQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -219,7 +217,7 @@ public class MessagesQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -264,7 +262,7 @@ public class MessagesQueries { msg.Hidden = rs.getBoolean(13); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -285,7 +283,7 @@ public class MessagesQueries { msg.Text = rs.getString(3); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -316,7 +314,7 @@ public class MessagesQueries { msg.Text = rs.getString(6); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -339,7 +337,7 @@ public class MessagesQueries { user.UName = rs.getString(2); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -365,7 +363,7 @@ public class MessagesQueries { tags.add(t); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -387,7 +385,7 @@ public class MessagesQueries { tids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -409,7 +407,7 @@ public class MessagesQueries { users.add(rs.getString(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -457,7 +455,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -492,7 +490,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -521,7 +519,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -550,7 +548,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -614,7 +612,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -641,7 +639,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -669,7 +667,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -696,7 +694,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -723,7 +721,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -751,7 +749,7 @@ public class MessagesQueries { mids0.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -766,7 +764,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -795,7 +793,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -824,7 +822,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -851,7 +849,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -878,7 +876,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -907,7 +905,7 @@ public class MessagesQueries { mids0.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -922,7 +920,7 @@ public class MessagesQueries { mids.add(rs.getInt(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -1001,7 +999,7 @@ public class MessagesQueries { replies.add(msg); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -1027,7 +1025,7 @@ public class MessagesQueries { } ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -1038,7 +1036,7 @@ public class MessagesQueries { stmt.setInt(1, mid); stmt.executeUpdate(); } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } @@ -1056,7 +1054,7 @@ public class MessagesQueries { stmt.setInt(1, mid); ret = stmt.executeUpdate() > 0; } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(null, stmt); } diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index a2d4545c..d49bb527 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -28,7 +28,6 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; /** * diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 076019ff..5bc87e28 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -19,7 +19,6 @@ package com.juick.server; import com.juick.Tag; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; import java.sql.Connection; import java.sql.PreparedStatement; @@ -39,7 +38,7 @@ import org.springframework.jdbc.support.KeyHolder; */ public class TagQueries { - private static final Logger logger = Logger.getLogger(TagQueries.class.getName()); + private static final Logger LOGGER = Logger.getLogger(TagQueries.class.getName()); public static com.juick.Tag getTag(Connection sql, int tid) { com.juick.Tag ret = null; @@ -57,7 +56,7 @@ public class TagQueries { ret.Name = rs.getString(2); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -112,7 +111,7 @@ public class TagQueries { ret = rs.getInt(1) == 1; } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -149,7 +148,7 @@ public class TagQueries { tags.add(t); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } @@ -170,7 +169,7 @@ public class TagQueries { tags.add(rs.getString(1)); } } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); + LOGGER.log(Level.SEVERE, "sql exception", e); } finally { Utils.finishSQL(rs, stmt); } -- cgit v1.2.3 From 2635d6187ecd48c732d21e4f510f1df8451ce646 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 14:49:38 +0300 Subject: spring-jdbc WIP --- .../java/com/juick/server/MessagesQueries.java | 230 +++++++-------------- 1 file changed, 79 insertions(+), 151 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index cff8caff..8f658b42 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -17,6 +17,8 @@ */ package com.juick.server; +import com.juick.Message; +import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Connection; @@ -30,6 +32,7 @@ import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.springframework.jdbc.core.RowMapper; /** * @@ -228,67 +231,46 @@ public class MessagesQueries { return SQLHelpers.getInt(sql, "SELECT privacy FROM messages WHERE message_id=?", mid, -4); } - public static com.juick.Message getMessage(Connection sql, int mid) { - com.juick.Message msg = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.readonly,messages.privacy,messages.replies,messages.attach,messages.place_id,messages.lat,messages.lon,messages.likes,messages.hidden FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - msg = new com.juick.Message(); - msg.MID = mid; - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); - msg.TimeAgo = rs.getInt(3); - msg.TimestampString = rs.getString(4); - msg.ReadOnly = rs.getBoolean(5); - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.Replies = rs.getInt(7); - msg.AttachmentType = rs.getString(8); - //if (rs.getInt(9) > 0) { - // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); - //} - if (rs.getDouble(10) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(10); - msg.Place.lon = rs.getDouble(11); - } - msg.Likes = rs.getInt(12); - msg.Hidden = rs.getBoolean(13); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (msg == null) { - return null; - } - - try { - stmt = sql.prepareStatement("SELECT tags,repliesby,txt FROM messages_txt WHERE message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - if (rs.getString(1) != null) { - msg.parseTags(rs.getString(1)); - } - msg.RepliesBy = rs.getString(2); - msg.Text = rs.getString(3); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msg; + public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { + return sql.queryForObject("SELECT messages.user_id,users.nick," + + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + + "DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s')," + + "messages.readonly,messages.privacy,messages.replies," + + "messages.attach,messages.place_id,messages.lat," + + "messages.lon,messages.likes,messages.hidden," + + "txt.tags,txt.repliesby,txt.txt FROM messages " + + "INNER JOIN users ON messages.user_id=users.id " + + "INNER JOIN messages_txt " + + "ON messages.message_id = messages_txt.message_id " + + "WHERE messages.message_id=?", new Object[]{mid}, + (rs, rowNum) -> { + Message msg = new Message(); + msg.MID = mid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.TimeAgo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.ReadOnly = rs.getBoolean(5); + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Replies = rs.getInt(7); + msg.AttachmentType = rs.getString(8); + //if (rs.getInt(9) > 0) { + // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); + //} + if (rs.getDouble(10) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(10); + msg.Place.lon = rs.getDouble(11); + } + msg.Likes = rs.getInt(12); + msg.Hidden = rs.getBoolean(13); + msg.parseTags(rs.getString(14)); + msg.RepliesBy = rs.getString(15); + msg.Text = rs.getString(16); + return msg; + }); } public static com.juick.Message getReply(Connection sql, int mid, int rid) { @@ -322,26 +304,16 @@ public class MessagesQueries { return msg; } - public static com.juick.User getMessageAuthor(Connection sql, int mid) { - com.juick.User user = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT messages.user_id,users.nick FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return user; + public static User getMessageAuthor(JdbcTemplate sql, int mid) { + return sql.queryForObject("SELECT messages.user_id,users.nick " + + "FROM messages INNER JOIN users " + + "ON messages.user_id=users.id WHERE messages.message_id=?", + new Object[]{mid}, (rs, num) -> { + User res = new com.juick.User(); + res.UID = rs.getInt(1); + res.UName = rs.getString(2); + return res; + }); } public static List getMessageTags(Connection sql, int mid) { @@ -829,31 +801,18 @@ public class MessagesQueries { return mids; } - public static List getUserRecommendations(Connection sql, int UID, int before) { - List mids = new ArrayList<>(20); + public static List getUserRecommendations(JdbcTemplate sql, int UID, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM favorites " + + "WHERE user_id=? AND message_id 0) { - stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id=? AND message_id getUserPhotos(Connection sql, int UID, int privacy, int before) { @@ -1007,58 +966,27 @@ public class MessagesQueries { return replies; } - public static boolean setMessagePopular(Connection sql, int mid, int popular) { - boolean ret = false; - - PreparedStatement stmt = null; - try { - if (popular == -2) { - stmt = sql.prepareStatement("UPDATE messages SET hidden=1 WHERE message_id=?"); - stmt.setInt(1, mid); - } else if (popular == -1) { - stmt = sql.prepareStatement("UPDATE messages SET popular=0 WHERE message_id=?"); - stmt.setInt(1, mid); - } else { - stmt = sql.prepareStatement("UPDATE messages SET popular=? WHERE message_id=?"); - stmt.setInt(1, popular); - stmt.setInt(2, mid); - } - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); + public static boolean setMessagePopular(JdbcTemplate sql, int mid, int popular) { + boolean ret; + switch (popular) { + case -2: + ret = sql.update("UPDATE messages SET hidden=1 WHERE message_id=?", mid) > 0; + break; + case -1: + ret = sql.update("UPDATE messages SET popular=0 WHERE message_id=?", mid) > 0; + break; + default: + ret = sql.update("UPDATE messages SET popular=? WHERE message_id=?", popular, mid) > 0; + break; } - if (popular == -1) { - try { - stmt = sql.prepareStatement("INSERT INTO top_ignore_messages VALUES (?)"); - stmt.setInt(1, mid); - stmt.executeUpdate(); - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + ret = sql.update("INSERT INTO top_ignore_messages VALUES (?)", mid) > 0; } - return ret; } - public static boolean setMessagePrivacy(Connection sql, int mid) { - boolean ret = false; - - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE messages SET privacy=1 WHERE message_id=?"); - stmt.setInt(1, mid); - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - - return ret; + public static boolean setMessagePrivacy(JdbcTemplate sql, int mid) { + return sql.update("UPDATE messages SET privacy=1 WHERE message_id=?", + mid) > 0; } } -- cgit v1.2.3 From 392b0e333248665a72eaf1febee4ccea9a7edf1f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 15:01:01 +0300 Subject: spring-jdbc WIP --- src/main/java/com/juick/server/MessagesQueries.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 8f658b42..91ee1913 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -32,7 +32,6 @@ import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import org.springframework.jdbc.core.RowMapper; /** * @@ -240,8 +239,8 @@ public class MessagesQueries { + "messages.lon,messages.likes,messages.hidden," + "txt.tags,txt.repliesby,txt.txt FROM messages " + "INNER JOIN users ON messages.user_id=users.id " - + "INNER JOIN messages_txt " - + "ON messages.message_id = messages_txt.message_id " + + "INNER JOIN messages_txt AS txt " + + "ON messages.message_id=txt.message_id " + "WHERE messages.message_id=?", new Object[]{mid}, (rs, rowNum) -> { Message msg = new Message(); -- cgit v1.2.3 From 0ac9d76d6348f487ada697678395d15abee244c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 15:20:29 +0300 Subject: spring-jdbc WIP --- .../java/com/juick/server/MessagesQueries.java | 48 +++++++++------------- 1 file changed, 19 insertions(+), 29 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 91ee1913..4086cb13 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -272,35 +272,25 @@ public class MessagesQueries { }); } - public static com.juick.Message getReply(Connection sql, int mid, int rid) { - com.juick.Message msg = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT replies.user_id,users.nick,replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach,replies.txt FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? AND replies.reply_id=?"); - stmt.setInt(1, mid); - stmt.setInt(2, rid); - rs = stmt.executeQuery(); - if (rs.first()) { - msg = new com.juick.Message(); - msg.MID = mid; - msg.RID = rid; - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); - msg.ReplyTo = rs.getInt(3); - msg.TimestampString = rs.getString(4); - msg.AttachmentType = rs.getString(5); - msg.Text = rs.getString(6); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msg; + public static com.juick.Message getReply(JdbcTemplate sql, int mid, int rid) { + return sql.queryForObject("SELECT replies.user_id,users.nick," + + "replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s')," + + "replies.attach,replies.txt FROM replies INNER JOIN users " + + "ON replies.user_id=users.id " + + "WHERE replies.message_id=? AND replies.reply_id=?", + new Object[]{mid, rid}, (rs, num) -> { + Message msg = new Message(); + msg.MID = mid; + msg.RID = rid; + msg.User = new com.juick.User(); + msg.User.UID = rs.getInt(1); + msg.User.UName = rs.getString(2); + msg.ReplyTo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.AttachmentType = rs.getString(5); + msg.Text = rs.getString(6); + return msg; + }); } public static User getMessageAuthor(JdbcTemplate sql, int mid) { -- cgit v1.2.3 From f14eea2cfc80dac5e9b304a57457e7f82842a8ed Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 15:49:29 +0300 Subject: spring-jdbc WIP --- src/main/java/com/juick/server/ShowQueries.java | 58 ++++--------------------- 1 file changed, 9 insertions(+), 49 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/ShowQueries.java b/src/main/java/com/juick/server/ShowQueries.java index 06aafb2d..05a58e9a 100644 --- a/src/main/java/com/juick/server/ShowQueries.java +++ b/src/main/java/com/juick/server/ShowQueries.java @@ -2,71 +2,31 @@ package com.juick.server; import com.juick.User; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.springframework.jdbc.core.JdbcTemplate; /** * Created by vt on 10/01/16. */ public class ShowQueries { - private static final Logger logger = Logger.getLogger(ShowQueries.class.getName()); - - public static List getRecommendedUsers(Connection sql, User forUser) { - List result = new ArrayList<>(); - PreparedStatement preparedStatement = null; - ResultSet rs = null; - try { - preparedStatement = sql.prepareStatement("SELECT users.nick FROM subscr_users INNER JOIN users " + + public static List getRecommendedUsers(JdbcTemplate sql, User forUser) { + return sql.queryForList("SELECT users.nick FROM subscr_users INNER JOIN users " + "ON subscr_users.user_id=users.id " + "WHERE subscr_users.user_id NOT IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " + "AND subscr_users.suser_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " + "AND subscr_users.user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + "AND subscr_users.user_id!=? AND users.lastmessage>UNIX_TIMESTAMP()-259200 " + - "GROUP BY subscr_users.user_id ORDER BY count(*) DESC LIMIT 10"); - preparedStatement.setInt(1, forUser.UID); - preparedStatement.setInt(2, forUser.UID); - preparedStatement.setInt(3, forUser.UID); - preparedStatement.setInt(4, forUser.UID); - rs = preparedStatement.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - result.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql error", e); - } finally { - Utils.finishSQL(rs, preparedStatement); - } - return result; + "GROUP BY subscr_users.user_id ORDER BY count(*) DESC LIMIT 10", + String.class, new Object[] {forUser.UID, forUser.UID, forUser.UID, forUser.UID}); } - public static List getTopUsers(Connection sql) { - List result = new ArrayList<>(); - PreparedStatement preparedStatement = null; - ResultSet rs = null; - try { - preparedStatement = sql.prepareStatement("SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " + + public static List getTopUsers(JdbcTemplate sql) { + return sql.queryForList("SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " + "FROM (subscr_users INNER JOIN users ON subscr_users.user_id=users.id) " + "INNER JOIN useroptions ON users.id=useroptions.user_id " + "WHERE useroptions.privacy_view>0 AND users.lastmessage>UNIX_TIMESTAMP()-259200 " + - "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10"); - rs = preparedStatement.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - result.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql error", e); - } finally { - Utils.finishSQL(rs, preparedStatement); - } - return result; + "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10", + String.class); } } -- cgit v1.2.3 From 09fd6b8ee40802eceff9746d3c23aa8446ccbd64 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 15 Jan 2016 22:56:33 +0300 Subject: update core --- .../java/com/juick/server/MessagesQueries.java | 50 +++++++++++----------- src/main/java/com/juick/server/PMQueries.java | 30 ++++++------- src/main/java/com/juick/server/ShowQueries.java | 2 +- src/main/java/com/juick/server/UserQueries.java | 30 ++++++------- 4 files changed, 56 insertions(+), 56 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 4086cb13..9cbf4240 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -244,10 +244,10 @@ public class MessagesQueries { + "WHERE messages.message_id=?", new Object[]{mid}, (rs, rowNum) -> { Message msg = new Message(); - msg.MID = mid; - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); + msg.setMID(mid); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); msg.TimeAgo = rs.getInt(3); msg.TimestampString = rs.getString(4); msg.ReadOnly = rs.getBoolean(5); @@ -267,7 +267,7 @@ public class MessagesQueries { msg.Hidden = rs.getBoolean(13); msg.parseTags(rs.getString(14)); msg.RepliesBy = rs.getString(15); - msg.Text = rs.getString(16); + msg.setText(rs.getString(16)); return msg; }); } @@ -280,15 +280,15 @@ public class MessagesQueries { + "WHERE replies.message_id=? AND replies.reply_id=?", new Object[]{mid, rid}, (rs, num) -> { Message msg = new Message(); - msg.MID = mid; - msg.RID = rid; - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); + msg.setMID(mid); + msg.setRID(rid); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); msg.ReplyTo = rs.getInt(3); msg.TimestampString = rs.getString(4); msg.AttachmentType = rs.getString(5); - msg.Text = rs.getString(6); + msg.setText(rs.getString(6)); return msg; }); } @@ -299,8 +299,8 @@ public class MessagesQueries { + "ON messages.user_id=users.id WHERE messages.message_id=?", new Object[]{mid}, (rs, num) -> { User res = new com.juick.User(); - res.UID = rs.getInt(1); - res.UName = rs.getString(2); + res.setUID(rs.getInt(1)); + res.setUName(rs.getString(2)); return res; }); } @@ -893,18 +893,18 @@ public class MessagesQueries { + "IN (" + Utils.convertArrayInt2String(mids) + ") " + "ORDER BY messages.message_id DESC", (rs, rowNum) -> { com.juick.Message msg = new com.juick.Message(); - msg.User = new com.juick.User(); + msg.setUser(new User()); - msg.MID = rs.getInt(1); - msg.User.UID = rs.getInt(2); - msg.User.UName = rs.getString(3); + msg.setMID(rs.getInt(1)); + msg.getUser().setUID(rs.getInt(2)); + msg.getUser().setUName(rs.getString(3)); if (rs.getString(4) != null) { msg.parseTags(rs.getString(4)); } msg.ReadOnly = rs.getInt(5) == 1; msg.Privacy = rs.getInt(6); msg.FriendsOnly = msg.Privacy < 0; - msg.Text = rs.getString(7); + msg.setText(rs.getString(7)); msg.TimeAgo = rs.getInt(8); msg.TimestampString = rs.getString(9); msg.Replies = rs.getInt(10); @@ -932,14 +932,14 @@ public class MessagesQueries { rs.beforeFirst(); while (rs.next()) { com.juick.Message msg = new com.juick.Message(); - msg.MID = mid; - msg.RID = rs.getInt(1); + msg.setMID(mid); + msg.setRID(rs.getInt(1)); msg.ReplyTo = rs.getInt(2); - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(3); - msg.User.UName = rs.getString(4); - msg.User.Banned = rs.getBoolean(5); - msg.Text = rs.getString(6); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(3)); + msg.getUser().setUName(rs.getString(4)); + msg.getUser().Banned = rs.getBoolean(5); + msg.setText(rs.getString(6)); msg.TimeAgo = rs.getInt(7); msg.TimestampString = rs.getString(8); msg.AttachmentType = rs.getString(9); diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index d49bb527..7984844a 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -104,7 +104,7 @@ public class PMQueries { + "WHERE user_id_to=? AND unread>0 " + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { com.juick.User u = new com.juick.User(); - u.UID = rs.getInt(1); + u.setUID(rs.getInt(1)); u.MessagesCount = rs.getInt(2); return u; }, new Object[]{uid}); @@ -116,11 +116,11 @@ public class PMQueries { + "ORDER BY lastmessage DESC LIMIT ?", (rs, num) -> { User u = new com.juick.User(); int uuid = rs.getInt(1) + rs.getInt(2) - uid; - u.UID = uuid; + u.setUID(uuid); return u; }, new Object[]{uid, uid, cnt * 2}); for (User addUser : addUsers) { - if (!haveUserInArray(users, addUser.UID)) { + if (!haveUserInArray(users, addUser.getUID())) { users.add(addUser); if (users.size() >= cnt) { break; @@ -137,7 +137,7 @@ public class PMQueries { public static boolean haveUserInArray(List arr, int uid) { for (User user : arr) { - if (user.UID == uid) { + if (user.getUID() == uid) { return true; } } @@ -151,9 +151,9 @@ public class PMQueries { (rs, rowNum) -> { com.juick.Message msg = new com.juick.Message(); int uuid = rs.getInt(1); - msg.User = new com.juick.User(); - msg.User.UID = uuid; - msg.Text = rs.getString(2); + msg.setUser(new User()); + msg.getUser().setUID(uuid); + msg.setText(rs.getString(2)); msg.TimestampString = rs.getString(3); return msg; }, new Object[]{uid, uid_to, uid, uid_to}); @@ -176,10 +176,10 @@ public class PMQueries { rs.beforeFirst(); while (rs.next()) { com.juick.Message msg = new com.juick.Message(); - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); - msg.Text = rs.getString(3); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); msg.TimestampString = rs.getString(5); msgs.add(0, msg); @@ -205,10 +205,10 @@ public class PMQueries { rs.beforeFirst(); while (rs.next()) { com.juick.Message msg = new com.juick.Message(); - msg.User = new com.juick.User(); - msg.User.UID = rs.getInt(1); - msg.User.UName = rs.getString(2); - msg.Text = rs.getString(3); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); msg.TimestampString = rs.getString(5); msgs.add(0, msg); diff --git a/src/main/java/com/juick/server/ShowQueries.java b/src/main/java/com/juick/server/ShowQueries.java index 05a58e9a..3ddff412 100644 --- a/src/main/java/com/juick/server/ShowQueries.java +++ b/src/main/java/com/juick/server/ShowQueries.java @@ -18,7 +18,7 @@ public class ShowQueries { "AND subscr_users.user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + "AND subscr_users.user_id!=? AND users.lastmessage>UNIX_TIMESTAMP()-259200 " + "GROUP BY subscr_users.user_id ORDER BY count(*) DESC LIMIT 10", - String.class, new Object[] {forUser.UID, forUser.UID, forUser.UID, forUser.UID}); + String.class, new Object[] {forUser.getUID(), forUser.getUID(), forUser.getUID(), forUser.getUID()}); } public static List getTopUsers(JdbcTemplate sql) { diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 27ae2870..2cd557c6 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -46,8 +46,8 @@ public class UserQueries { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); - user.UID = rs.getInt("id"); - user.UName = rs.getString("nick"); + user.setUID(rs.getInt("id")); + user.setUName(rs.getString("nick")); user.Banned = rs.getBoolean("banned"); return user; } @@ -120,13 +120,13 @@ public class UserQueries { if (i > 0) { uids += ","; } - uids += users.get(i).UID; + uids += users.get(i).getUID(); } sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + uids + ")", (rs, num) -> { User u = users.get(num); - u.UName = rs.getString(2); + u.setUName(rs.getString(2)); return u; }); @@ -139,9 +139,9 @@ public class UserQueries { + "WHERE jids.jid IN (" + Utils.convertArrayString2String(jids) + ")", (rs, rowNum) -> { com.juick.User user = new com.juick.User(); - user.UID = rs.getInt(1); - user.UName = rs.getString(2); - user.JID = rs.getString(3); + user.setUID(rs.getInt(1)); + user.setUName(rs.getString(2)); + user.setJID(rs.getString(3)); return user; }); } @@ -166,7 +166,7 @@ public class UserQueries { User user = sql.queryForObject("SELECT logins.user_id,users.nick, users.banned FROM logins " + "INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?", new UserMapper(), hash); - user.AuthHash = hash; + user.setAuthHash(hash); return user; } @@ -385,8 +385,8 @@ public class UserQueries { rs.beforeFirst(); while (rs.next()) { com.juick.User u = new com.juick.User(); - u.UID = rs.getInt(1); - u.UName = rs.getString(2); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); users.add(u); } } catch (SQLException e) { @@ -414,8 +414,8 @@ public class UserQueries { rs.beforeFirst(); while (rs.next()) { com.juick.User u = new com.juick.User(); - u.UID = rs.getInt(1); - u.UName = rs.getString(2); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); users.add(u); } } catch (SQLException e) { @@ -434,7 +434,7 @@ public class UserQueries { sql.prepareStatement("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + "VALUES (?,?,?,?)" + " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?"); - stmt.setInt(1, user.UID); + stmt.setInt(1, user.getUID()); stmt.setString(2, accessToken); stmt.setString(3, accessTokenSecret); stmt.setString(4, screenName); @@ -445,7 +445,7 @@ public class UserQueries { PreparedStatement stmt2 = sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id,jid) " + "VALUES (?,1741,'juick\\@twitter.juick.com')"); - stmt2.setInt(1, user.UID); + stmt2.setInt(1, user.getUID()); return stmt2.execute(); } } catch (SQLException e) { @@ -483,7 +483,7 @@ public class UserQueries { "UPDATE jids SET active=? WHERE user_id=? AND jid=?"); int newStatus = jidStatus == ActiveStatus.Active ? 1 : 0; preparedStatement.setInt(1, newStatus); - preparedStatement.setInt(2, user.UID); + preparedStatement.setInt(2, user.getUID()); preparedStatement.setString(3, JID); return preparedStatement; -- cgit v1.2.3 From 714a267ae35354e86512288e0ac91990937f95ab Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 16 Jan 2016 18:23:21 +0300 Subject: almost working api project --- .../java/com/juick/server/MessagesQueries.java | 187 +++++++++------------ src/main/java/com/juick/server/PMQueries.java | 10 +- src/main/java/com/juick/server/TagQueries.java | 31 +--- src/main/java/com/juick/server/UserQueries.java | 26 +-- 4 files changed, 103 insertions(+), 151 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 9cbf4240..fd664909 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -19,7 +19,10 @@ package com.juick.server; import com.juick.Message; import com.juick.User; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; import java.sql.Connection; import java.sql.PreparedStatement; @@ -41,31 +44,25 @@ public class MessagesQueries { private static final Logger LOGGER = Logger.getLogger(MessagesQueries.class.getName()); - public static int createMessage(Connection sql, int uid, String txt, String attachment, List tags) { - int mid = 0; - - PreparedStatement stmt = null; - ResultSet rs; - try { - stmt = sql.prepareStatement("INSERT INTO messages(user_id,attach) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + public static int createMessage(JdbcTemplate sql, int uid, String txt, String attachment, List tags) { + KeyHolder holder = new GeneratedKeyHolder(); + sql.update(con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO messages(user_id,attach) VALUES (?,?)", + Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, uid); if (attachment != null) { stmt.setString(2, attachment); } else { stmt.setNull(2, Types.VARCHAR); } - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.first()) { - mid = rs.getInt(1); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + return stmt; + }, holder); + + int mid = holder.getKey().intValue(); + if (mid > 0) { + String tagsNames = ""; String tagsIDs = ""; @@ -77,42 +74,33 @@ public class MessagesQueries { tagsNames += tags.get(i).Name; tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; } - - try { - stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); - stmt.executeUpdate(); - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); + if (tags.size() > 0) { + sql.execute("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); } - - try { - stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); + final String finalTagsNames = tagsNames; + sql.update(con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) " + + "VALUES (?,?,?)", Statement.NO_GENERATED_KEYS); stmt.setInt(1, mid); - if (tagsNames.isEmpty()) { + if (finalTagsNames.isEmpty()) { stmt.setNull(2, Types.VARCHAR); } else { - stmt.setString(2, tagsNames); + stmt.setString(2, finalTagsNames); } stmt.setString(3, txt); - stmt.executeUpdate(); - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + return stmt; + }); } return mid; } - public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { + public static int createReply(JdbcTemplate sql, int mid, int rid, int uid, String txt, String attachment) { int ridnew = getReplyIDIncrement(sql, mid); - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO replies(message_id,reply_id,user_id,replyto,attach,txt) VALUES (?,?,?,?,?,?)"); + sql.update( con -> { + PreparedStatement stmt = con.prepareStatement("INSERT INTO replies(message_id,reply_id,user_id," + + "replyto,attach,txt) VALUES (?,?,?,?,?,?)", Statement.NO_GENERATED_KEYS); stmt.setInt(1, mid); stmt.setInt(2, ridnew); stmt.setInt(3, uid); @@ -123,32 +111,18 @@ public class MessagesQueries { stmt.setNull(5, Types.VARCHAR); } stmt.setString(6, txt); - if (stmt.executeUpdate() == 0) { - ridnew = 0; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + return stmt; + }); if (ridnew > 0) { - SQLHelpers.executeInt(sql, "UPDATE messages SET replies=replies+1 WHERE message_id=?", mid); + sql.update("UPDATE messages SET replies=replies+1 WHERE message_id=?", mid); } return ridnew; } - public static int getReplyIDIncrement(Connection sql, int mid) { - int rid = 0; - - SQLHelpers.execute(sql, "LOCK TABLES messages WRITE"); - if (SQLHelpers.executeInt(sql, "UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid) == 1) { - rid = SQLHelpers.getInt(sql, "SELECT maxreplyid FROM messages WHERE message_id=?", mid, 0); - } - SQLHelpers.execute(sql, "UNLOCK TABLES"); - - return rid; + public static int getReplyIDIncrement(JdbcTemplate sql, int mid) { + return sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); } public static boolean recommendMessage(Connection sql, int mid, int vuid) { @@ -230,51 +204,56 @@ public class MessagesQueries { return SQLHelpers.getInt(sql, "SELECT privacy FROM messages WHERE message_id=?", mid, -4); } + public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { - return sql.queryForObject("SELECT messages.user_id,users.nick," - + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," - + "DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s')," - + "messages.readonly,messages.privacy,messages.replies," - + "messages.attach,messages.place_id,messages.lat," - + "messages.lon,messages.likes,messages.hidden," - + "txt.tags,txt.repliesby,txt.txt FROM messages " - + "INNER JOIN users ON messages.user_id=users.id " - + "INNER JOIN messages_txt AS txt " - + "ON messages.message_id=txt.message_id " - + "WHERE messages.message_id=?", new Object[]{mid}, - (rs, rowNum) -> { - Message msg = new Message(); - msg.setMID(mid); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.TimeAgo = rs.getInt(3); - msg.TimestampString = rs.getString(4); - msg.ReadOnly = rs.getBoolean(5); - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.Replies = rs.getInt(7); - msg.AttachmentType = rs.getString(8); - //if (rs.getInt(9) > 0) { - // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); - //} - if (rs.getDouble(10) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(10); - msg.Place.lon = rs.getDouble(11); - } - msg.Likes = rs.getInt(12); - msg.Hidden = rs.getBoolean(13); - msg.parseTags(rs.getString(14)); - msg.RepliesBy = rs.getString(15); - msg.setText(rs.getString(16)); - return msg; - }); + try { + return sql.queryForObject("SELECT messages.user_id,users.nick," + + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + + "messages.ts," + + "messages.readonly,messages.privacy,messages.replies," + + "messages.attach,messages.place_id,messages.lat," + + "messages.lon,messages.likes,messages.hidden," + + "txt.tags,txt.repliesby,txt.txt FROM messages " + + "INNER JOIN users ON messages.user_id=users.id " + + "INNER JOIN messages_txt AS txt " + + "ON messages.message_id=txt.message_id " + + "WHERE messages.message_id=?", new Object[]{mid}, + (rs, rowNum) -> { + Message msg = new Message(); + msg.setMID(mid); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.TimeAgo = rs.getInt(3); + msg.setDate(rs.getDate(4)); + msg.ReadOnly = rs.getBoolean(5); + msg.Privacy = rs.getInt(6); + msg.FriendsOnly = msg.Privacy < 0; + msg.Replies = rs.getInt(7); + msg.AttachmentType = rs.getString(8); + //if (rs.getInt(9) > 0) { + // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); + //} + if (rs.getDouble(10) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(10); + msg.Place.lon = rs.getDouble(11); + } + msg.Likes = rs.getInt(12); + msg.Hidden = rs.getBoolean(13); + msg.parseTags(rs.getString(14)); + msg.RepliesBy = rs.getString(15); + msg.setText(rs.getString(16)); + return msg; + }); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static com.juick.Message getReply(JdbcTemplate sql, int mid, int rid) { return sql.queryForObject("SELECT replies.user_id,users.nick," - + "replies.replyto,DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s')," + + "replies.replyto,replies.ts," + "replies.attach,replies.txt FROM replies INNER JOIN users " + "ON replies.user_id=users.id " + "WHERE replies.message_id=? AND replies.reply_id=?", @@ -286,7 +265,7 @@ public class MessagesQueries { msg.getUser().setUID(rs.getInt(1)); msg.getUser().setUName(rs.getString(2)); msg.ReplyTo = rs.getInt(3); - msg.TimestampString = rs.getString(4); + msg.setDate(rs.getDate(4)); msg.AttachmentType = rs.getString(5); msg.setText(rs.getString(6)); return msg; @@ -883,7 +862,7 @@ public class MessagesQueries { return sql.query("SELECT messages.message_id,messages.user_id,users.nick," + "messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt," + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," - + "DATE_FORMAT(messages.ts,'%Y-%m-%d %H:%i:%s'),messages.replies," + + "messages.ts,messages.replies," + "messages_txt.repliesby,messages.attach,messages.lat," + "messages.lon,messages.likes " + "FROM (messages INNER JOIN messages_txt " @@ -906,7 +885,7 @@ public class MessagesQueries { msg.FriendsOnly = msg.Privacy < 0; msg.setText(rs.getString(7)); msg.TimeAgo = rs.getInt(8); - msg.TimestampString = rs.getString(9); + msg.setDate(rs.getDate(9)); msg.Replies = rs.getInt(10); msg.RepliesBy = rs.getString(11); msg.AttachmentType = rs.getString(12); @@ -926,7 +905,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),DATE_FORMAT(replies.ts,'%Y-%m-%d %H:%i:%s'),replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); + stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts,replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); stmt.setInt(1, mid); rs = stmt.executeQuery(); rs.beforeFirst(); @@ -941,7 +920,7 @@ public class MessagesQueries { msg.getUser().Banned = rs.getBoolean(5); msg.setText(rs.getString(6)); msg.TimeAgo = rs.getInt(7); - msg.TimestampString = rs.getString(8); + msg.setDate(rs.getDate(8)); msg.AttachmentType = rs.getString(9); replies.add(msg); diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 7984844a..345f641f 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -154,7 +154,7 @@ public class PMQueries { msg.setUser(new User()); msg.getUser().setUID(uuid); msg.setText(rs.getString(2)); - msg.TimestampString = rs.getString(3); + msg.setDate(rs.getDate(3)); return msg; }, new Object[]{uid, uid_to, uid, uid_to}); @@ -170,7 +170,7 @@ public class PMQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20"); stmt.setInt(1, uid); rs = stmt.executeQuery(); rs.beforeFirst(); @@ -181,7 +181,7 @@ public class PMQueries { msg.getUser().setUName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.TimestampString = rs.getString(5); + msg.setDate(rs.getDate(5)); msgs.add(0, msg); } } catch (SQLException e) { @@ -199,7 +199,7 @@ public class PMQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),DATE_FORMAT(pm.ts,'%Y-%m-%d %H:%i:%s') FROM pm INNER JOIN users ON pm.user_id_to=users.id WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20"); stmt.setInt(1, uid); rs = stmt.executeQuery(); rs.beforeFirst(); @@ -210,7 +210,7 @@ public class PMQueries { msg.getUser().setUName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.TimestampString = rs.getString(5); + msg.setDate(rs.getDate(5)); msgs.add(0, msg); } } catch (SQLException e) { diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 5bc87e28..22839ad0 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -40,28 +40,15 @@ public class TagQueries { private static final Logger LOGGER = Logger.getLogger(TagQueries.class.getName()); - public static com.juick.Tag getTag(Connection sql, int tid) { - com.juick.Tag ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT synonym_id,name FROM tags WHERE tag_id=?"); - stmt.setInt(1, tid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = new com.juick.Tag(); - ret.TID = tid; - ret.SynonymID = rs.getInt(1); - ret.Name = rs.getString(2); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return ret; + public static com.juick.Tag getTag(JdbcTemplate sql, int tid) { + return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", + new Object[]{tid}, (rs, num) -> { + Tag ret = new Tag(); + ret.TID = tid; + ret.SynonymID = rs.getInt(1); + ret.Name = rs.getString(2); + return ret; + }); } public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 2cd557c6..5588d8e4 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -199,27 +199,13 @@ public class UserQueries { return uname != null && uname.length() >= 2 && uname.length() <= 16 && uname.matches("[a-zA-Z0-9\\-]+"); } - public static int checkPassword(Connection sql, String username, String password) { - int uid = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT id,passw FROM users WHERE nick=?"); - stmt.setString(1, username); - rs = stmt.executeQuery(); - if (rs.first()) { - if (password.equals(rs.getString(2))) { - uid = rs.getInt(1); - } else { - uid = -1; - } - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static int checkPassword(JdbcTemplate sql, String username, String password) { + String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); + if (realPassword.equals(password)) { + return UserQueries.getUserByName(sql, username).getUID(); + } else { + return -1; } - return uid; } public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { -- cgit v1.2.3 From 279765857724cf35addc0e3f5e7180a8724d23b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 16 Jan 2016 19:04:26 +0300 Subject: fix time of messages --- src/main/java/com/juick/server/MessagesQueries.java | 8 ++++---- src/main/java/com/juick/server/PMQueries.java | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index fd664909..1d8eceb3 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -225,7 +225,7 @@ public class MessagesQueries { msg.getUser().setUID(rs.getInt(1)); msg.getUser().setUName(rs.getString(2)); msg.TimeAgo = rs.getInt(3); - msg.setDate(rs.getDate(4)); + msg.setDate(rs.getTimestamp(4)); msg.ReadOnly = rs.getBoolean(5); msg.Privacy = rs.getInt(6); msg.FriendsOnly = msg.Privacy < 0; @@ -265,7 +265,7 @@ public class MessagesQueries { msg.getUser().setUID(rs.getInt(1)); msg.getUser().setUName(rs.getString(2)); msg.ReplyTo = rs.getInt(3); - msg.setDate(rs.getDate(4)); + msg.setDate(rs.getTimestamp(4)); msg.AttachmentType = rs.getString(5); msg.setText(rs.getString(6)); return msg; @@ -885,7 +885,7 @@ public class MessagesQueries { msg.FriendsOnly = msg.Privacy < 0; msg.setText(rs.getString(7)); msg.TimeAgo = rs.getInt(8); - msg.setDate(rs.getDate(9)); + msg.setDate(rs.getTimestamp(9)); msg.Replies = rs.getInt(10); msg.RepliesBy = rs.getString(11); msg.AttachmentType = rs.getString(12); @@ -920,7 +920,7 @@ public class MessagesQueries { msg.getUser().Banned = rs.getBoolean(5); msg.setText(rs.getString(6)); msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getDate(8)); + msg.setDate(rs.getTimestamp(8)); msg.AttachmentType = rs.getString(9); replies.add(msg); diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 345f641f..9ff464ba 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -154,7 +154,7 @@ public class PMQueries { msg.setUser(new User()); msg.getUser().setUID(uuid); msg.setText(rs.getString(2)); - msg.setDate(rs.getDate(3)); + msg.setDate(rs.getTimestamp(3)); return msg; }, new Object[]{uid, uid_to, uid, uid_to}); @@ -181,7 +181,7 @@ public class PMQueries { msg.getUser().setUName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getDate(5)); + msg.setDate(rs.getTimestamp(5)); msgs.add(0, msg); } } catch (SQLException e) { @@ -210,7 +210,7 @@ public class PMQueries { msg.getUser().setUName(rs.getString(2)); msg.setText(rs.getString(3)); msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getDate(5)); + msg.setDate(rs.getTimestamp(5)); msgs.add(0, msg); } } catch (SQLException e) { -- cgit v1.2.3 From de32a6e9e994b830fdf0768d0825559a85d19654 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 16 Jan 2016 23:36:20 +0300 Subject: message queries almost done --- .../java/com/juick/server/MessagesQueries.java | 807 ++++++--------------- .../com/juick/server/SubscriptionsQueries.java | 127 +--- src/main/java/com/juick/server/UserQueries.java | 42 +- .../java/com/juick/server/helpers/PrivacyOpts.java | 29 + 4 files changed, 303 insertions(+), 702 deletions(-) create mode 100644 src/main/java/com/juick/server/helpers/PrivacyOpts.java (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 1d8eceb3..bcb2d08d 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -19,21 +19,18 @@ package com.juick.server; import com.juick.Message; import com.juick.User; +import com.juick.server.helpers.PrivacyOpts; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; -import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -125,83 +122,53 @@ public class MessagesQueries { return sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); } - public static boolean recommendMessage(Connection sql, int mid, int vuid) { - boolean res = SQLHelpers.execute(sql, "INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; + public static boolean recommendMessage(JdbcTemplate sql, int mid, int vuid) { + boolean res = sql.update("INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; if (res) { - SQLHelpers.executeInt(sql, "UPDATE messages SET likes=likes+1 WHERE message_id=?", mid); + sql.update("UPDATE messages SET likes=likes+1 WHERE message_id=?", mid); } return res; } - public static boolean canViewThread(Connection sql, int mid, int uid) { + public static boolean canViewThread(JdbcTemplate sql, int mid, int uid) { - int privacy = 0; - int owner_uid = 0; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT user_id,privacy FROM messages WHERE messages.message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - owner_uid = rs.getInt(1); - privacy = rs.getInt(2); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + PrivacyOpts privacyOpts = sql.queryForObject("SELECT user_id,privacy FROM messages WHERE messages.message_id=?", + new Object[] {mid}, + (rs, rowNum) -> { + PrivacyOpts res = new PrivacyOpts(); + res.setUid(rs.getInt(1)); + res.setPrivacy(rs.getInt(2)); + return res; + }); - return privacy >= 0 - || uid == owner_uid - || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)); + return privacyOpts.getPrivacy() >= 0 + || uid == privacyOpts.getUid() + || ((privacyOpts.getPrivacy() == -1 || privacyOpts.getPrivacy() == -2) && uid > 0 + && UserQueries.isInWL(sql, privacyOpts.getUid(), uid)); } - public static boolean isReadOnly(Connection sql, int mid) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean isReadOnly(JdbcTemplate sql, int mid) { try { - stmt = sql.prepareStatement("SELECT readonly FROM messages WHERE message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT readonly FROM messages WHERE message_id=?", new Object[]{mid}, Integer.class) == 1; + } catch (EmptyResultDataAccessException e) { + return false; } - return ret; } - public static boolean isSubscribed(Connection sql, int uid, int mid) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean isSubscribed(JdbcTemplate sql, int uid, int mid) { try { - stmt = sql.prepareStatement("SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?"); - stmt.setInt(1, uid); - stmt.setInt(2, mid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?", new Object[]{uid, mid}, Integer.class) == 1; + } catch (EmptyResultDataAccessException e) { + return false; } - return ret; } - public static int getMessagePrivacy(Connection sql, int mid) { - return SQLHelpers.getInt(sql, "SELECT privacy FROM messages WHERE message_id=?", mid, -4); + public static int getMessagePrivacy(JdbcTemplate sql, int mid) { + try { + return sql.queryForObject("SELECT privacy FROM messages WHERE message_id=?", new Object[]{mid}, Integer.class); + } catch (EmptyResultDataAccessException e) { + return -4; + } } @@ -252,24 +219,28 @@ public class MessagesQueries { } public static com.juick.Message getReply(JdbcTemplate sql, int mid, int rid) { - return sql.queryForObject("SELECT replies.user_id,users.nick," - + "replies.replyto,replies.ts," - + "replies.attach,replies.txt FROM replies INNER JOIN users " - + "ON replies.user_id=users.id " - + "WHERE replies.message_id=? AND replies.reply_id=?", - new Object[]{mid, rid}, (rs, num) -> { - Message msg = new Message(); - msg.setMID(mid); - msg.setRID(rid); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.ReplyTo = rs.getInt(3); - msg.setDate(rs.getTimestamp(4)); - msg.AttachmentType = rs.getString(5); - msg.setText(rs.getString(6)); - return msg; - }); + try { + return sql.queryForObject("SELECT replies.user_id,users.nick," + + "replies.replyto,replies.ts," + + "replies.attach,replies.txt FROM replies INNER JOIN users " + + "ON replies.user_id=users.id " + + "WHERE replies.message_id=? AND replies.reply_id=?", + new Object[]{mid, rid}, (rs, num) -> { + Message msg = new Message(); + msg.setMID(mid); + msg.setRID(rid); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.ReplyTo = rs.getInt(3); + msg.setDate(rs.getTimestamp(4)); + msg.AttachmentType = rs.getString(5); + msg.setText(rs.getString(6)); + return msg; + }); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static User getMessageAuthor(JdbcTemplate sql, int mid) { @@ -284,215 +255,96 @@ public class MessagesQueries { }); } - public static List getMessageTags(Connection sql, int mid) { - List tags = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Tag t = new com.juick.Tag(); - t.TID = rs.getInt(1); - t.SynonymID = rs.getInt(2); - t.Name = rs.getString(3); - t.UsageCnt = rs.getInt(4); - tags.add(t); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return tags; + public static List getMessageTags(JdbcTemplate sql, int mid) { + return sql.query("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " + + "INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)", + new Object[]{mid}, (rs, num) -> { + com.juick.Tag t = new com.juick.Tag(); + t.TID = rs.getInt(1); + t.SynonymID = rs.getInt(2); + t.Name = rs.getString(3); + t.UsageCnt = rs.getInt(4); + return t; + }); } - public static List getMessageTagsIDs(Connection sql, int mid) { - List tids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tag_id FROM messages_tags WHERE message_id=?"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - tids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return tids; + public static List getMessageTagsIDs(JdbcTemplate sql, int mid) { + return sql.queryForList("SELECT tag_id FROM messages_tags WHERE message_id=?", new Object[] {mid}, Integer.class); } - public static ArrayList getMessageRecommendations(Connection sql, int mid) { - ArrayList users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.nick FROM favorites INNER JOIN users ON (favorites.message_id=? AND favorites.user_id=users.id)"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - users.add(rs.getString(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return users; + public static List getMessageRecommendations(JdbcTemplate sql, int mid) { + return sql.queryForList("SELECT users.nick FROM favorites INNER JOIN users " + + "ON (favorites.message_id=? AND favorites.user_id=users.id)", + new Object[] {mid}, String.class); } - public static List getAll(Connection sql, int visitor_uid, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (visitor_uid > 1) { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + - " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, before); - stmt.setInt(2, visitor_uid); - stmt.setInt(3, visitor_uid); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0" + - " AND (privacy>0 OR user_id=?)" + - " AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + - " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, visitor_uid); - stmt.setInt(2, visitor_uid); - } + public static List getAll(JdbcTemplate sql, int visitor_uid, int before) { + if (visitor_uid > 1) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20", + new Object[]{before, visitor_uid, visitor_uid}, Integer.class); } else { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND user_id NOT IN (SELECT id from users WHERE banned=1) " + - " ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, before); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0" + - " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); - } - - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); + return sql.queryForList("SELECT message_id FROM messages WHERE hidden=0" + + " AND (privacy>0 OR user_id=?)" + + " AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20", + new Object[]{visitor_uid, visitor_uid}, Integer.class); } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return mids; - } - - public static List getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { + } else { if (before > 0) { -// stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); - stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.message_id0 OR messages.user_id=?) ORDER BY message_id DESC LIMIT ?"); - stmt.setInt(1, tid); - stmt.setInt(2, tid); - stmt.setInt(3, before); - stmt.setInt(4, visitor_uid); - stmt.setInt(5, cnt); + return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 AND user_id NOT IN (SELECT id from users WHERE banned=1) " + + " ORDER BY message_id DESC LIMIT 20", + new Object[]{before}, Integer.class); } else { - //stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (SELECT tag_id FROM tags WHERE tag_id=? OR synonym_id=?) AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); - stmt = sql.prepareStatement("SELECT message_id FROM (tags INNER JOIN messages_tags ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) INNER JOIN messages USING(message_id) WHERE messages.privacy>0 OR messages.user_id=? ORDER BY message_id DESC LIMIT ?"); - stmt.setInt(1, tid); - stmt.setInt(2, tid); - stmt.setInt(3, visitor_uid); - stmt.setInt(4, cnt); + return sql.queryForList("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0" + + " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20", + Integer.class); } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); } - return mids; } - public static List getTags(Connection sql, String tids, int visitor_uid, int before, int cnt) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); - stmt.setInt(1, before); - stmt.setInt(2, visitor_uid); - stmt.setInt(3, cnt); - } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); - stmt.setInt(1, visitor_uid); - stmt.setInt(2, cnt); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getTag(JdbcTemplate sql, int tid, int visitor_uid, int before, int cnt) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM (tags INNER JOIN messages_tags " + + "ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) " + + "INNER JOIN messages USING(message_id) WHERE messages.message_id0 OR messages.user_id=?) ORDER BY message_id DESC LIMIT ?", + new Object[]{tid, tid, before, visitor_uid, cnt}, Integer.class); + } else { + return sql.queryForList("SELECT message_id FROM (tags INNER JOIN messages_tags " + + "ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) " + + "INNER JOIN messages USING(message_id) WHERE messages.privacy>0 OR messages.user_id=? " + + "ORDER BY message_id DESC LIMIT ?", + new Object[]{tid, tid, visitor_uid, cnt}, Integer.class); } - return mids; } - public static List getPlace(Connection sql, int place_id, int visitor_uid, int before) { - List mids = new ArrayList<>(20); + public static List getTags(JdbcTemplate sql, String tids, int visitor_uid, int before, int cnt) { + if (before > 0) { + return sql.queryForList("SELECT messages.message_id FROM messages_tags " + + "INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") " + + "AND messages.message_id0 OR messages.user_id=?) " + + "ORDER BY messages.message_id DESC LIMIT ?", new Object[]{before, visitor_uid, cnt}, Integer.class); + } else { + return sql.queryForList("SELECT messages.message_id FROM messages_tags " + + "INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") " + + "AND (messages.privacy>0 OR messages.user_id=?) " + + "ORDER BY messages.message_id DESC LIMIT ?", new Object[]{visitor_uid, cnt}, Integer.class); + } + } - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND message_id0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, place_id); - stmt.setInt(2, before); - stmt.setInt(3, visitor_uid); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, place_id); - stmt.setInt(2, visitor_uid); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getPlace(JdbcTemplate sql, int place_id, int visitor_uid, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE place_id=? AND message_id0 OR user_id=?) ORDER BY message_id DESC LIMIT 20", + new Object[]{place_id, before, visitor_uid}, Integer.class); + } else { + return sql.queryForList("SELECT message_id FROM messages WHERE place_id=? AND (privacy>0 OR user_id=?) " + + "ORDER BY message_id DESC LIMIT 20", new Object[]{place_id, visitor_uid}, Integer.class); } - return mids; } public static List getMyFeed(JdbcTemplate sql, int uid, int before) { @@ -532,241 +384,103 @@ public class MessagesQueries { return mids; } - public static List getPrivate(Connection sql, int uid, int before) { - List mids = new ArrayList<>(20); + public static List getPrivate(JdbcTemplate sql, int uid, int before) { - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 AND message_id 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 AND message_id getDiscussions(Connection sql, int uid, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id getDiscussions(JdbcTemplate sql, int uid, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id getRecommended(Connection sql, int uid, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM favorites WHERE user_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id getRecommended(JdbcTemplate sql, int uid, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM favorites WHERE user_id " + + "IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id getPopular(Connection sql, int before) { - List mids = new ArrayList<>(20); + public static List getPopular(JdbcTemplate sql, int before) { - PreparedStatement stmt = null; - ResultSet rs = null; - try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, before); + return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 " + + "AND popular>0 ORDER BY message_id DESC LIMIT 20", new Object[] {before}, Integer.class); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND popular>0 ORDER BY message_id DESC LIMIT 20"); + return sql.queryForList("SELECT message_id FROM messages WHERE privacy>0 " + + "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class); } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return mids; } - public static List getPhotos(Connection sql, int visitor_uid, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, before); - stmt.setInt(2, visitor_uid); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, visitor_uid); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getPhotos(JdbcTemplate sql, int visitor_uid, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) " + + "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " + + "ORDER BY message_id DESC LIMIT 20", new Object[]{before, visitor_uid}, Integer.class); + } else { + return sql.queryForList("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) " + + "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " + + "ORDER BY message_id DESC LIMIT 20", new Object[]{visitor_uid}, Integer.class); } - return mids; } - public static List getSearch(Connection sql, Connection sqlSearch, String search, int before) { - List mids0 = new ArrayList<>(20); + public static List getSearch(JdbcTemplate sql, JdbcTemplate sqlSearch, String search, int before) { + List mids; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - - if (before > 0) { - stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id 0) { + mids = sqlSearch.queryForList("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id mids = new ArrayList<>(20); - if (mids0.size() > 0) { - try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + if (mids.size() > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE message_id " + + "IN (" + Utils.convertArrayInt2String(mids) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20", + Integer.class); } - return mids; } - public static List getUserBlog(Connection sql, int UID, int privacy, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + privacy + " ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - stmt.setInt(2, before); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getUserBlog(JdbcTemplate sql, int UID, int privacy, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + + privacy + " ORDER BY message_id DESC LIMIT 20", new Object[]{UID, before}, Integer.class); + } else { + return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + + " ORDER BY message_id DESC LIMIT 20", new Object[]{UID}, Integer.class); } - return mids; } - public static List getUserTag(Connection sql, int UID, int TID, int privacy, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.message_id=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - stmt.setInt(2, TID); - stmt.setInt(3, before); - } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - stmt.setInt(2, TID); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getUserTag(JdbcTemplate sql, int UID, int TID, int privacy, int before) { + if (before > 0) { + return sql.queryForList("SELECT messages.message_id FROM messages_tags INNER JOIN messages " + + "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + + "AND messages.message_id=" + privacy + + " ORDER BY messages.message_id DESC LIMIT 20", + new Object[] {UID, TID, before}, Integer.class); + } else { + return sql.queryForList("SELECT messages.message_id FROM messages_tags INNER JOIN messages " + + "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + + "AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20", + new Object[]{UID, TID}, Integer.class); } - return mids; } public static List getUserRecommendations(JdbcTemplate sql, int UID, int before) { @@ -783,76 +497,35 @@ public class MessagesQueries { } } - public static List getUserPhotos(Connection sql, int UID, int privacy, int before) { - List mids = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - stmt.setInt(2, before); - } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, UID); - } - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List getUserPhotos(JdbcTemplate sql, int UID, int privacy, int before) { + if (before > 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND message_id=" + + privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20", + new Object[] {UID, before}, Integer.class); + } else { + return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20", + new Object[] {UID}, Integer.class); } - return mids; } - public static List getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { - List mids0 = new ArrayList<>(20); + public static List getUserSearch(JdbcTemplate sql, JdbcTemplate sqlSearch, int UID, String search, int privacy, int before) { + List mids; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (before > 0) { - stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND id mids = new ArrayList<>(20); - if (mids0.size() > 0) { - try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArrayInt2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - mids.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } + if (before > 0) { + mids = sqlSearch.queryForList("SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND id 0) { + return sql.queryForList("SELECT message_id FROM messages WHERE message_id IN (" + + Utils.convertArrayInt2String(mids) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC", + Integer.class); + } return mids; } @@ -899,39 +572,25 @@ public class MessagesQueries { }); } - public static List getReplies(Connection sql, int mid) { - List replies = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt,TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts,replies.attach FROM replies INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC"); - stmt.setInt(1, mid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - msg.setMID(mid); - msg.setRID(rs.getInt(1)); - msg.ReplyTo = rs.getInt(2); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(3)); - msg.getUser().setUName(rs.getString(4)); - msg.getUser().Banned = rs.getBoolean(5); - msg.setText(rs.getString(6)); - msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getTimestamp(8)); - msg.AttachmentType = rs.getString(9); - - replies.add(msg); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return replies; + public static List getReplies(JdbcTemplate sql, int mid) { + return sql.query("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,users.banned,replies.txt," + + "TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts,replies.attach FROM replies INNER JOIN users " + + "ON replies.user_id=users.id WHERE replies.message_id=? ORDER BY replies.reply_id ASC", + new Object[]{mid}, (rs, num) -> { + com.juick.Message msg = new com.juick.Message(); + msg.setMID(mid); + msg.setRID(rs.getInt(1)); + msg.ReplyTo = rs.getInt(2); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(3)); + msg.getUser().setUName(rs.getString(4)); + msg.getUser().Banned = rs.getBoolean(5); + msg.setText(rs.getString(6)); + msg.TimeAgo = rs.getInt(7); + msg.setDate(rs.getTimestamp(8)); + msg.AttachmentType = rs.getString(9); + return msg; + }); } public static boolean setMessagePopular(JdbcTemplate sql, int mid, int popular) { 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 getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { - List jids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - - String tbl = "subscr_jids_" + mid; + public static List getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { + List userids = UserQueries.getUserRead(sql, uid); List 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 tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + + "WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + ") AND suser_id!=" + uid, Integer.class); + Set set = new HashSet<>(); + set.addAll(userids); + set.addAll(tagUsers); + return UserQueries.getUsersByID(sql, new ArrayList<>(set)); } - public static List getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { - List 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 getUsersSubscribedToComments(JdbcTemplate sql, int mid, int ignore_uid) { + List 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 getJIDSubscribedToUserRecommendations(Connection sql, int uid, int mid, int muid) { - List jids = new ArrayList<>(); - + public static List getUsersSubscribedToUserRecommendations(JdbcTemplate sql, int uid, int mid, int muid) { List 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 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; } } diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 5588d8e4..21373b7d 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -18,6 +18,7 @@ package com.juick.server; import com.juick.User; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; @@ -46,9 +47,9 @@ public class UserQueries { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); - user.setUID(rs.getInt("id")); - user.setUName(rs.getString("nick")); - user.Banned = rs.getBoolean("banned"); + user.setUID(rs.getInt(1)); + user.setUName(rs.getString(2)); + user.Banned = rs.getBoolean(3); return user; } } @@ -88,9 +89,13 @@ public class UserQueries { } public static User getUserByName(JdbcTemplate sql, String username) { - return sql.queryForObject("SELECT id,nick,banned FROM users WHERE nick=?", - new Object[] {username}, - new UserMapper()); + try { + return sql.queryForObject("SELECT id,nick,banned FROM users WHERE nick=?", + new Object[]{username}, + new UserMapper()); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static User getUserByJID(JdbcTemplate sql, String jid) { @@ -107,8 +112,7 @@ public class UserQueries { public static List getUsersByID(JdbcTemplate sql, List uids) { return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")", - new UserMapper(), - uids.toArray()); + new UserMapper()); } public static boolean fillUsersByID(JdbcTemplate sql, List users) { @@ -262,25 +266,9 @@ public class UserQueries { return ret; } - public static boolean isInWL(Connection sql, int uid, int check) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?"); - stmt.setInt(1, uid); - stmt.setInt(2, check); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; + public static boolean isInWL(JdbcTemplate sql, int uid, int check) { + return sql.queryForObject("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?", + new Object[] {uid, check}, Integer.class) == 1; } public static boolean isInBL(Connection sql, int uid, int check) { diff --git a/src/main/java/com/juick/server/helpers/PrivacyOpts.java b/src/main/java/com/juick/server/helpers/PrivacyOpts.java new file mode 100644 index 00000000..66cf9410 --- /dev/null +++ b/src/main/java/com/juick/server/helpers/PrivacyOpts.java @@ -0,0 +1,29 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 16/01/16. + */ +public class PrivacyOpts { + private int uid; + private int privacy; + + public PrivacyOpts() { + + } + + public int getUid() { + return uid; + } + + public void setUid(int uid) { + this.uid = uid; + } + + public int getPrivacy() { + return privacy; + } + + public void setPrivacy(int privacy) { + this.privacy = privacy; + } +} -- cgit v1.2.3 From 9232bdcb144bc3b94f4b825b3a4cb588078bd72d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 17 Jan 2016 00:15:07 +0300 Subject: fix getTag --- src/main/java/com/juick/server/TagQueries.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 22839ad0..f3ab480e 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -18,6 +18,7 @@ package com.juick.server; import com.juick.Tag; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import java.sql.Connection; @@ -52,15 +53,19 @@ public class TagQueries { } public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { - Tag ret = sql.queryForObject("SELECT tag_id,synonym_id,name FROM tags WHERE name=?", - new Object[]{tag}, (rs, rowNum) -> { - Tag ret1 = new Tag(); - ret1.TID = rs.getInt(1); - ret1.SynonymID = rs.getInt(2); - ret1.Name = rs.getString(3); - return ret1; - }); - + Tag ret = null; + try { + ret = sql.queryForObject("SELECT tag_id,synonym_id,name FROM tags WHERE name=?", + new Object[]{tag}, (rs, rowNum) -> { + Tag ret1 = new Tag(); + ret1.TID = rs.getInt(1); + ret1.SynonymID = rs.getInt(2); + ret1.Name = rs.getString(3); + return ret1; + }); + } catch (EmptyResultDataAccessException e) { + // tag not found + } if (ret == null && autoCreate) { ret = new com.juick.Tag(); ret.TID = createTag(sql, tag); -- cgit v1.2.3 From 27e4beeb2ed195985942262f3f94a8c5b069ba66 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 17 Jan 2016 01:06:42 +0300 Subject: fix bl query --- src/main/java/com/juick/server/UserQueries.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 21373b7d..948b42d4 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -293,10 +293,13 @@ public class UserQueries { } public static boolean isInBLAny(JdbcTemplate sql, int uid, int uid2) { - List res = sql.queryForList("SELECT 1 FROM bl_users " - + "WHERE (user_id=? AND bl_user_id=?) " - + "OR (user_id=? AND bl_user_id=?)", Integer.class, new Object[] {uid, uid2, uid2, uid}); - return res.get(0) == 1; + try { + return sql.queryForObject("SELECT 1 FROM bl_users " + + "WHERE (user_id=? AND bl_user_id=?) " + + "OR (user_id=? AND bl_user_id=?)", new Object[]{uid, uid2, uid2, uid}, Integer.class) == 1; + } catch (EmptyResultDataAccessException e) { + return false; + } } public static List checkBL(Connection sql, int visitor, List uids) { -- cgit v1.2.3 From 8ae5d9e3ce643bf0b8488c4566eff133b2cb2f11 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 17 Jan 2016 01:36:57 +0300 Subject: fix replyidincrement --- src/main/java/com/juick/server/MessagesQueries.java | 3 ++- src/main/java/com/juick/server/SubscriptionsQueries.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index bcb2d08d..422cfe7d 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -119,7 +119,8 @@ public class MessagesQueries { } public static int getReplyIDIncrement(JdbcTemplate sql, int mid) { - return sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); + sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); + return sql.queryForObject("SELECT maxreplyid FROM messages WHERE message_id=?", new Object[] {mid}, Integer.class); } public static boolean recommendMessage(JdbcTemplate sql, int mid, int vuid) { diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index 8207f499..3a76e23e 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -93,6 +93,6 @@ public class SubscriptionsQueries { } 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; + return sql.update("INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; } } -- cgit v1.2.3 From 8a77f7d91311f8c4d8ef026dacb4abbe0fa764d1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 02:04:46 +0300 Subject: refactoring --- .../java/com/juick/server/MessagesQueries.java | 21 +++++++++--------- src/main/java/com/juick/server/UserQueries.java | 25 ++++++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 422cfe7d..c4919a8d 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -349,31 +349,30 @@ public class MessagesQueries { } public static List getMyFeed(JdbcTemplate sql, int uid, int before) { - List mids = new ArrayList<>(40); + List mids; if (before > 0) { mids = sql.queryForList("SELECT message_id FROM messages " + "INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) " + "WHERE message_id=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", - Integer.class, new Object[]{uid, before, uid}); + Integer.class, uid, before, uid); } else { mids = sql.queryForList("SELECT message_id FROM messages " + "INNER JOIN subscr_users ON (subscr_users.suser_id=? " + "AND subscr_users.user_id=messages.user_id) " + "WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " + "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20", - Integer.class, new Object[]{uid, uid}); + Integer.class, uid, uid); } if (before > 0) { mids.addAll(sql.queryForList("SELECT message_id FROM messages " + "WHERE user_id=? AND message_id getDiscussions(JdbcTemplate sql, int uid, int before) { if (before > 0) { return sql.queryForList("SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id 0) { return sql.queryForList("SELECT message_id FROM favorites WHERE user_id " + "IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 " + - "AND popular>0 ORDER BY message_id DESC LIMIT 20", new Object[] {before}, Integer.class); + "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class, before); } else { return sql.queryForList("SELECT message_id FROM messages WHERE privacy>0 " + "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class); diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 948b42d4..226e21b1 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -100,14 +100,12 @@ public class UserQueries { public static User getUserByJID(JdbcTemplate sql, String jid) { return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new Object[] {jid}, - new UserMapper()); + new UserMapper(), jid); } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.query("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new UserMapper(), - unames.toArray()); + return sql.query("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + new UserMapper()); } public static List getUsersByID(JdbcTemplate sql, List uids) { @@ -204,10 +202,19 @@ public class UserQueries { } public static int checkPassword(JdbcTemplate sql, String username, String password) { - String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); - if (realPassword.equals(password)) { - return UserQueries.getUserByName(sql, username).getUID(); - } else { + try { + String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); + if (realPassword.equals(password)) { + User user = UserQueries.getUserByName(sql, username); + if (user != null) { + return user.getUID(); + } else { + return -1; + } + } else { + return -1; + } + } catch (EmptyResultDataAccessException e) { return -1; } } -- cgit v1.2.3 From 3e589d7566164634a946018e61ccb36093d1924b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 02:08:59 +0300 Subject: fix getUsersByName --- src/main/java/com/juick/server/UserQueries.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 226e21b1..cfa52a16 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -104,7 +104,7 @@ public class UserQueries { } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.query("SELECT id,nick FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", new UserMapper()); } -- cgit v1.2.3 From 35bce4efb5fa1eb547dfe2301e86afd10b0283e9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 02:11:16 +0300 Subject: fix getUsersByName, take 3 --- src/main/java/com/juick/server/UserQueries.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index cfa52a16..1ec0e512 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -104,8 +104,8 @@ public class UserQueries { } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", - new UserMapper()); + return sql.queryForList("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + User.class, new UserMapper()); } public static List getUsersByID(JdbcTemplate sql, List uids) { -- cgit v1.2.3 From e292163fd4c343764889f1d6161720a2d57dfc84 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 26 Jan 2016 02:14:22 +0300 Subject: Revert "fix getUsersByName, take 3" This reverts commit 35bce4efb5fa1eb547dfe2301e86afd10b0283e9. --- src/main/java/com/juick/server/UserQueries.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 1ec0e512..cfa52a16 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -104,8 +104,8 @@ public class UserQueries { } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.queryForList("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", - User.class, new UserMapper()); + return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + new UserMapper()); } public static List getUsersByID(JdbcTemplate sql, List uids) { -- cgit v1.2.3 From 69a435dfef1bc656dc1d9d3da0f049f86d18fdaf Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 May 2016 03:37:04 +0300 Subject: fix fill users --- src/main/java/com/juick/server/PMQueries.java | 12 ++++++------ src/main/java/com/juick/server/UserQueries.java | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 9ff464ba..d1d826cc 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -39,20 +39,20 @@ public class PMQueries { public static boolean createPM(JdbcTemplate sql, int uid_from, int uid_to, String body) { boolean ret = sql.update("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)", - new Object[] {uid_from, uid_to, body}) > 0; + uid_from, uid_to, body) > 0; if (ret) { sql.update("INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) " + "VALUES (?,?,NOW(),1) " + "ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1", - new Object[] {uid_from, uid_to}); + uid_from, uid_to); } return ret; } public static boolean addPMinRoster(JdbcTemplate sql, int uid, String jid) { return sql.update("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)", - new Object[] {uid, jid}) > 0; + uid, jid) > 0; } public static boolean removePMinRoster(Connection sql, int uid, String jid) { @@ -74,7 +74,7 @@ public class PMQueries { public static boolean havePMinRoster(JdbcTemplate sql, int uid, String jid) { List res = sql.queryForList("SELECT 1 FROM pm_inroster " + "WHERE user_id=? AND jid=?", Integer.class, - new Object[] {uid, jid}); + uid, jid); return res.size() > 0; } @@ -107,7 +107,7 @@ public class PMQueries { u.setUID(rs.getInt(1)); u.MessagesCount = rs.getInt(2); return u; - }, new Object[]{uid}); + }, uid); if (users.size() < cnt) { List addUsers = sql.query("SELECT user_id,user_id_to " @@ -118,7 +118,7 @@ public class PMQueries { int uuid = rs.getInt(1) + rs.getInt(2) - uid; u.setUID(uuid); return u; - }, new Object[]{uid, uid, cnt * 2}); + }, uid, uid, cnt * 2); for (User addUser : addUsers) { if (!haveUserInArray(users, addUser.getUID())) { users.add(addUser); diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index cfa52a16..184d26f5 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -127,7 +127,8 @@ public class UserQueries { sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + uids + ")", (rs, num) -> { - User u = users.get(num); + User u = new User(); + u.setUID(rs.getInt(1)); u.setUName(rs.getString(2)); return u; }); -- cgit v1.2.3 From 0a9cd317b878d6c2c777ec4eb19173a7d651abe5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 May 2016 15:34:44 +0300 Subject: fix fill users, part 2 --- src/main/java/com/juick/server/PMQueries.java | 40 ++++++--------------------- 1 file changed, 8 insertions(+), 32 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index d1d826cc..6de30995 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -100,39 +100,15 @@ public class PMQueries { } public static List getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { - List users = sql.query("SELECT user_id,unread FROM pm_streams " - + "WHERE user_id_to=? AND unread>0 " - + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { + return sql.query("SELECT users.id, users.nick,pm_streams.unread FROM pm_streams " + + "INNER JOIN users ON users.id = pm_streams.user_id WHERE user_id_to=? " + + "ORDER BY pm_streams.lastmessage DESC LIMIT ?", (rs, rowNum) -> { com.juick.User u = new com.juick.User(); u.setUID(rs.getInt(1)); - u.MessagesCount = rs.getInt(2); + u.setUName(rs.getString(2)); + u.MessagesCount = rs.getInt(3); return u; - }, uid); - - if (users.size() < cnt) { - List addUsers = sql.query("SELECT user_id,user_id_to " - + "FROM pm_streams WHERE (user_id=? OR user_id_to=?) " - + "AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) " - + "ORDER BY lastmessage DESC LIMIT ?", (rs, num) -> { - User u = new com.juick.User(); - int uuid = rs.getInt(1) + rs.getInt(2) - uid; - u.setUID(uuid); - return u; - }, uid, uid, cnt * 2); - for (User addUser : addUsers) { - if (!haveUserInArray(users, addUser.getUID())) { - users.add(addUser); - if (users.size() >= cnt) { - break; - } - } - } - } - if (!users.isEmpty()) { - UserQueries.fillUsersByID(sql, users); - } - - return users; + }, uid, cnt); } public static boolean haveUserInArray(List arr, int uid) { @@ -156,10 +132,10 @@ public class PMQueries { msg.setText(rs.getString(2)); msg.setDate(rs.getTimestamp(3)); return msg; - }, new Object[]{uid, uid_to, uid, uid_to}); + }, uid, uid_to, uid, uid_to); sql.update("UPDATE pm_streams SET lastview=NOW(),unread=0 " - + "WHERE user_id_to=? AND user_id=?", new Object[]{uid, uid_to}); + + "WHERE user_id_to=? AND user_id=?", uid, uid_to); return msgs; } -- cgit v1.2.3 From c95b63af82ed58c68876563692340d74ced156f2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 12 May 2016 09:55:00 +0300 Subject: fix reply id increment --- .../java/com/juick/server/MessagesQueries.java | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index c4919a8d..bfd0d71a 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -21,11 +21,14 @@ import com.juick.Message; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; @@ -119,8 +122,28 @@ public class MessagesQueries { } public static int getReplyIDIncrement(JdbcTemplate sql, int mid) { - sql.update("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid); - return sql.queryForObject("SELECT maxreplyid FROM messages WHERE message_id=?", new Object[] {mid}, Integer.class); + return sql.execute((ConnectionCallback) conn -> { + conn.setAutoCommit(false); + try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?")) { + ps.setInt(1, mid); + if (ps.executeUpdate() != 1) { + throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); + } + } + try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid FROM messages WHERE message_id=?")) { + ps.setInt(1, mid); + try (ResultSet resultSet = ps.executeQuery()) { + if (resultSet.next()) { + int rv = resultSet.getInt(1); + conn.commit(); + return rv; + } else { + throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0); + } + } + } + }); + } public static boolean recommendMessage(JdbcTemplate sql, int mid, int vuid) { -- cgit v1.2.3 From ea43e78cb8dc20c2f54a15e37dba638b963f65ec Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 13 May 2016 16:12:44 +0300 Subject: telegram login --- src/main/java/com/juick/server/UserQueries.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 184d26f5..2ae47da4 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -63,6 +63,16 @@ public class UserQueries { } return hash; } + public static String getSignUpHashByTelegramID(JdbcTemplate sql, Long telegramId) { + try { + return sql.queryForObject("SELECT loginhash FROM telegram WHERE tg_id=? AND user_id IS NULL", + String.class, telegramId); + } catch (EmptyResultDataAccessException e) { + String hash = UUID.randomUUID().toString(); + sql.update("INSERT INTO telegram(tg_id, loginhash) VALUES (?, ?)", telegramId, hash); + return hash; + } + } public static int createUser(JdbcTemplate sql, String username, String password) { KeyHolder holder = new GeneratedKeyHolder(); -- cgit v1.2.3 From 3fe5180ff7f2488966d3c940629ddefcfc84467f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 19:45:29 +0300 Subject: make UserMapper public --- src/main/java/com/juick/server/UserQueries.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 2ae47da4..1bab7c7f 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -43,7 +43,7 @@ public class UserQueries { private static final Logger LOGGER = Logger.getLogger(UserQueries.class.getName()); - static class UserMapper implements RowMapper { + public static class UserMapper implements RowMapper { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); -- cgit v1.2.3 From a41109795f6f6a3803d52606df9bd1a0da641f43 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 21:13:16 +0300 Subject: fix hash query --- src/main/java/com/juick/server/UserQueries.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 1bab7c7f..af7997ea 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -184,19 +184,18 @@ public class UserQueries { } public static String getHashByUID(JdbcTemplate sql, int uid) { - String hash = sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); - - if (hash == null) { - hash = generateHash(16); - final String finalHash = hash; + try { + return sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); + } catch (EmptyResultDataAccessException e){ + String hash = generateHash(16); sql.update(con -> { PreparedStatement stmt = con.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); stmt.setInt(1, uid); - stmt.setString(2, finalHash); + stmt.setString(2, hash); return stmt; }); + return hash; } - return hash; } public static String generateHash(int len) { -- cgit v1.2.3 From b633724c75081eb05b70ceee74636f19576a4483 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 23:22:07 +0300 Subject: optional getUserByUID --- src/main/java/com/juick/server/UserQueries.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index af7997ea..068f0c4b 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -92,10 +92,13 @@ public class UserQueries { return uid; } - public static User getUserByUID(JdbcTemplate sql, int uid) { - return sql.queryForObject("SELECT id, nick,banned FROM users WHERE id=?", - new Object[] {uid}, - new UserMapper()); + public static Optional getUserByUID(JdbcTemplate sql, int uid) { + try { + return Optional.of(sql.queryForObject("SELECT id, nick,banned FROM users WHERE id=?", + new UserMapper(), uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } } public static User getUserByName(JdbcTemplate sql, String username) { -- cgit v1.2.3 From 1ce6bc55817129159b1cda5076d039e9c3bc8508 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 24 May 2016 14:43:49 +0300 Subject: fix telegram signup --- src/main/java/com/juick/server/UserQueries.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 068f0c4b..c9f804cf 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -63,13 +63,13 @@ public class UserQueries { } return hash; } - public static String getSignUpHashByTelegramID(JdbcTemplate sql, Long telegramId) { + public static String getSignUpHashByTelegramID(JdbcTemplate sql, Long telegramId, String username) { try { return sql.queryForObject("SELECT loginhash FROM telegram WHERE tg_id=? AND user_id IS NULL", String.class, telegramId); } catch (EmptyResultDataAccessException e) { String hash = UUID.randomUUID().toString(); - sql.update("INSERT INTO telegram(tg_id, loginhash) VALUES (?, ?)", telegramId, hash); + sql.update("INSERT INTO telegram(tg_id, loginhash, tg_name) VALUES (?, ?, ?)", telegramId, hash, username); return hash; } } -- cgit v1.2.3 From c1fcf803adfe06a0a54b3e51c4fd2d67e6b732d8 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 24 Jun 2016 16:13:46 +0300 Subject: fix uid queries --- src/main/java/com/juick/server/UserQueries.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index c9f804cf..817ae5d3 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -171,11 +171,19 @@ public class UserQueries { } public static int getUIDbyName(JdbcTemplate sql, String uname) { - return sql.queryForObject("SELECT id FROM users WHERE nick=?", Integer.class, uname); + try { + return sql.queryForObject("SELECT id FROM users WHERE nick=?", Integer.class, uname); + } catch (EmptyResultDataAccessException e) { + return 0; + } } public static int getUIDbyHash(JdbcTemplate sql, String hash) { - return sql.queryForObject("SELECT user_id FROM logins WHERE hash=?", Integer.class, hash); + try { + return sql.queryForObject("SELECT user_id FROM logins WHERE hash=?", Integer.class, hash); + } catch (EmptyResultDataAccessException e) { + return 0; + } } public static com.juick.User getUserByHash(JdbcTemplate sql, String hash) { -- cgit v1.2.3 From eca30236892175d7071e6d0d39f474148d655ffa Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 24 Jun 2016 16:22:20 +0300 Subject: fix tags queries --- src/main/java/com/juick/server/SubscriptionsQueries.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index 3a76e23e..b271435b 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -6,6 +6,7 @@ package com.juick.server; import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; import java.sql.Connection; import java.sql.PreparedStatement; @@ -55,12 +56,14 @@ public class SubscriptionsQueries { public static List getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { List userids = UserQueries.getUserRead(sql, uid); - List tags = MessagesQueries.getMessageTagsIDs(sql, mid); - List tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + - "WHERE tag_id IN (" + Utils.convertArrayInt2String(tags) + ") AND suser_id!=" + uid, Integer.class); Set set = new HashSet<>(); set.addAll(userids); - set.addAll(tagUsers); + List tags = MessagesQueries.getMessageTagsIDs(sql, mid); + if (tags.size() > 0) { + List tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + + "WHERE tag_id IN (" + StringUtils.arrayToCommaDelimitedString(tags.toArray()) + ") AND suser_id!=" + uid, Integer.class); + set.addAll(tagUsers); + } return UserQueries.getUsersByID(sql, new ArrayList<>(set)); } -- cgit v1.2.3 From 9e740f48cca7332c67a165c6cf60e09bc549e686 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 10:33:04 +0300 Subject: more spring-jdbc --- build.gradle | 3 +- src/main/java/com/juick/server/AdsQueries.java | 40 +++-- .../java/com/juick/server/CrosspostQueries.java | 74 ++++----- src/main/java/com/juick/server/PMQueries.java | 173 ++++++++------------- src/main/java/com/juick/server/TagQueries.java | 58 +++---- src/main/java/com/juick/server/UserQueries.java | 73 +++++---- 6 files changed, 169 insertions(+), 252 deletions(-) (limited to 'src/main') diff --git a/build.gradle b/build.gradle index b82c3d5c..9f5bc86d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,5 +5,6 @@ repositories { } dependencies { - compile "org.springframework:spring-jdbc:4.2.4.RELEASE" + compile "org.springframework:spring-jdbc:4.3.0.RELEASE" + compile "org.apache.commons:commons-lang3:3.4" } \ No newline at end of file diff --git a/src/main/java/com/juick/server/AdsQueries.java b/src/main/java/com/juick/server/AdsQueries.java index ad9b7149..06590817 100644 --- a/src/main/java/com/juick/server/AdsQueries.java +++ b/src/main/java/com/juick/server/AdsQueries.java @@ -17,11 +17,8 @@ */ package com.juick.server; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; /** * @@ -29,27 +26,26 @@ import java.util.logging.Logger; */ public class AdsQueries { - private static final Logger logger = Logger.getLogger(AdsQueries.class.getName()); - - public static int getAdMID(Connection sql, int uid) { + public static int getAdMID(JdbcTemplate sql, int uid) { if (uid > 0) { - return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); + try { + return sql.queryForObject("SELECT message_id FROM ads_messages " + + "WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? " + + "AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", + Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return 0; + } } else { - return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", 0); + try { + return sql.queryForObject("SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", Integer.class); + } catch (EmptyResultDataAccessException e) { + return 0; + } } } - public static void logAdMID(Connection sql, int uid, int mid) { - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())"); - stmt.setInt(1, uid); - stmt.setInt(2, mid); - stmt.executeUpdate(); - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + public static void logAdMID(JdbcTemplate sql, int uid, int mid) { + sql.update("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())", uid, mid); } } diff --git a/src/main/java/com/juick/server/CrosspostQueries.java b/src/main/java/com/juick/server/CrosspostQueries.java index b44b6b59..04898d0a 100644 --- a/src/main/java/com/juick/server/CrosspostQueries.java +++ b/src/main/java/com/juick/server/CrosspostQueries.java @@ -17,12 +17,11 @@ */ package com.juick.server; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.Optional; /** * @@ -30,55 +29,34 @@ import java.util.logging.Logger; */ public class CrosspostQueries { - private static final Logger logger = Logger.getLogger(CrosspostQueries.class.getName()); - - public static String[] getTwitterTokens(Connection sql, int uid) { - String tokens[] = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional> getTwitterTokens(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - tokens = new String[2]; - tokens[0] = rs.getString(1); - tokens[1] = rs.getString(2); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1", + (rs, num) -> { + return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); + }, uid); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - - return tokens; } - public static String getFacebookToken(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL AND crosspost=1", uid); + public static Optional getFacebookToken(JdbcTemplate sql, int uid) { + try { + return Optional.of(sql.queryForObject("SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL " + + "AND crosspost=1", String.class, uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } } - public static String[] getVKTokens(Connection sql, int uid) { - String tokens[] = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional> getVKTokens(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - tokens = new String[2]; - tokens[0] = rs.getString(1); - tokens[1] = rs.getString(2); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1", + (rs, num) -> { + return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); + }, uid); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - - return tokens; } } diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 6de30995..772a1c28 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -18,16 +18,10 @@ package com.juick.server; import com.juick.User; +import org.springframework.dao.EmptyResultDataAccessException; +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.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.springframework.jdbc.core.JdbcTemplate; /** * @@ -35,8 +29,6 @@ import org.springframework.jdbc.core.JdbcTemplate; */ public class PMQueries { - private static final Logger LOGGER = Logger.getLogger(PMQueries.class.getName()); - public static boolean createPM(JdbcTemplate sql, int uid_from, int uid_to, String body) { boolean ret = sql.update("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)", uid_from, uid_to, body) > 0; @@ -55,20 +47,8 @@ public class PMQueries { uid, jid) > 0; } - public static boolean removePMinRoster(Connection sql, int uid, String jid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("DELETE FROM pm_inroster WHERE user_id=? AND jid=?"); - stmt.setInt(1, uid); - stmt.setString(2, jid); - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; + public static boolean removePMinRoster(JdbcTemplate sql, int uid, String jid) { + return sql.update("DELETE FROM pm_inroster WHERE user_id=? AND jid=?", uid, jid) > 0; } public static boolean havePMinRoster(JdbcTemplate sql, int uid, String jid) { @@ -78,37 +58,49 @@ public class PMQueries { return res.size() > 0; } - public static String getLastView(Connection sql, int uid_from, int uid_to) { - String ret = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static String getLastView(JdbcTemplate sql, int uid_from, int uid_to) { try { - stmt = sql.prepareStatement("SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?"); - stmt.setInt(1, uid_from); - stmt.setInt(2, uid_to); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getString(1); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?", String.class, + uid_from, uid_to); + } catch (EmptyResultDataAccessException e) { + return null; } - return ret; } public static List getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { - return sql.query("SELECT users.id, users.nick,pm_streams.unread FROM pm_streams " + - "INNER JOIN users ON users.id = pm_streams.user_id WHERE user_id_to=? " + - "ORDER BY pm_streams.lastmessage DESC LIMIT ?", (rs, rowNum) -> { - com.juick.User u = new com.juick.User(); - u.setUID(rs.getInt(1)); - u.setUName(rs.getString(2)); - u.MessagesCount = rs.getInt(3); - return u; - }, uid, cnt); + List qusers = sql.query("SELECT user_id,unread FROM pm_streams " + + "WHERE user_id_to=? AND unread>0 " + + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { + com.juick.User u = new com.juick.User(); + u.setUID(rs.getInt(1)); + u.MessagesCount = rs.getInt(2); + return u; + }, uid); + + if (qusers.size() < cnt) { + List addUsers = sql.query("SELECT user_id,user_id_to " + + "FROM pm_streams WHERE (user_id=? OR user_id_to=?) " + + "AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) " + + "ORDER BY lastmessage DESC LIMIT ?", (rs, num) -> { + User u = new com.juick.User(); + int uuid = rs.getInt(1) + rs.getInt(2) - uid; + u.setUID(uuid); + return u; + }, uid, uid, cnt * 2); + for (User addUser : addUsers) { + if (!haveUserInArray(qusers, addUser.getUID())) { + qusers.add(addUser); + if (qusers.size() >= cnt) { + break; + } + } + } + } + if (!qusers.isEmpty()) { + UserQueries.fillUsersByID(sql, qusers); + } + + return qusers; } public static boolean haveUserInArray(List arr, int uid) { @@ -140,61 +132,34 @@ public class PMQueries { return msgs; } - public static List getLastPMInbox(Connection sql, int uid) { - List msgs = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - msgs.add(0, msg); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msgs; + public static List getLastPMInbox(JdbcTemplate sql, int uid) { + return sql.query("SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts " + + "FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20", + (rs, num) -> { + com.juick.Message msg = new com.juick.Message(); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.setText(rs.getString(3)); + msg.TimeAgo = rs.getInt(4); + msg.setDate(rs.getTimestamp(5)); + return msg; + }, uid); } - public static List getLastPMSent(Connection sql, int uid) { - List msgs = new ArrayList<>(20); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - msgs.add(0, msg); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return msgs; + public static List getLastPMSent(JdbcTemplate sql, int uid) { + return sql.query("SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW())," + + "pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id " + + "WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20", + (rs, num) -> { + com.juick.Message msg = new com.juick.Message(); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(1)); + msg.getUser().setUName(rs.getString(2)); + msg.setText(rs.getString(3)); + msg.TimeAgo = rs.getInt(4); + msg.setDate(rs.getTimestamp(5)); + return msg; + }, uid); } } diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index f3ab480e..d2159b5a 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -90,25 +90,12 @@ public class TagQueries { return ret; } - public static boolean getTagNoIndex(Connection sql, int tag_id) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean getTagNoIndex(JdbcTemplate sql, int tag_id) { try { - stmt = sql.prepareStatement("SELECT noindex FROM tags WHERE tag_id=?"); - stmt.setInt(1, tag_id); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT noindex FROM tags WHERE tag_id=?", Integer.class, tag_id) == 1; + } catch (EmptyResultDataAccessException e) { + return false; } - - return ret; } public static int createTag(JdbcTemplate sql, String name) { @@ -123,28 +110,17 @@ public class TagQueries { return holder.getKey().intValue(); } - public static List getUserTagsAll(Connection sql, int uid) { - List tags = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tags.name,COUNT(messages.message_id) 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 ORDER BY tags.name ASC"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Tag t = new com.juick.Tag(); - t.Name = rs.getString(1); - t.UsageCnt = rs.getInt(2); - tags.add(t); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return tags; + public static List getUserTagsAll(JdbcTemplate sql, int uid) { + return sql.query("SELECT tags.name,COUNT(messages.message_id) " + + "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 ORDER BY tags.name ASC", + (rs, rowNum) -> { + Tag t = new Tag(); + t.Name = rs.getString(1); + t.UsageCnt = rs.getInt(2); + return t; + }, uid); } public static List getUserBLTags(Connection sql, int uid) { @@ -167,4 +143,8 @@ public class TagQueries { } return tags; } + + public static List getPopularTags(JdbcTemplate sql) { + return sql.queryForList("SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class); + } } diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 817ae5d3..0d6a3485 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -23,6 +23,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; +import org.springframework.util.StringUtils; import java.sql.Connection; import java.sql.PreparedStatement; @@ -330,47 +331,22 @@ public class UserQueries { } } - public static List checkBL(Connection sql, int visitor, List uids) { - List ret = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT user_id FROM bl_users WHERE bl_user_id=? and user_id IN (" + Utils.convertArrayInt2String(uids) + ")"); - stmt.setInt(1, visitor); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - ret.add(rs.getInt(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + public static List checkBL(JdbcTemplate sql, int visitor, List uids) { + if (!uids.isEmpty()) { + return sql.queryForList("SELECT user_id FROM bl_users WHERE bl_user_id=? and user_id IN (" + + StringUtils.collectionToCommaDelimitedString(uids) + ")", Integer.class, visitor); + } else { + return new ArrayList<>(); } - - return ret; } - public static boolean isSubscribed(Connection sql, int uid, int check) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean isSubscribed(JdbcTemplate sql, int uid, int check) { try { - stmt = sql.prepareStatement("SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?"); - stmt.setInt(1, uid); - stmt.setInt(2, check); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?", + Integer.class, uid, check) == 1; + } catch (EmptyResultDataAccessException e) { + return false; } - return ret; } public static List getUserRead(JdbcTemplate sql, int uid) { @@ -403,8 +379,29 @@ public class UserQueries { return users; } - public static List getUserReaders(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT suser_id FROM subscr_users WHERE user_id=?", Integer.class, uid); + public List getUserReaders(JdbcTemplate sql, int uid) { + return sql.query("SELECT users.id, users.nick FROM subscr_users " + + "INNER JOIN users ON subscr_users.suser_id=users.id " + + "WHERE subscr_users.user_id=? ORDER BY users.nick", + (rs, num) -> { + com.juick.User u = new com.juick.User(); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); + return u; + }, uid); + } + + public List getUserFriends(JdbcTemplate sql, int uid) { + return sql.query("SELECT users.id,users.nick FROM subscr_users " + + "INNER JOIN users ON subscr_users.user_id=users.id " + + "WHERE subscr_users.suser_id=? AND users.id!=? " + + "ORDER BY users.nick", + (rs, num) -> { + com.juick.User u = new com.juick.User(); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); + return u; + }, uid, uid); } public static List getUserBLUsers(Connection sql, int uid) { -- cgit v1.2.3 From 6362eab34390b187944c7e3c12eb444e2fb2c455 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 13:15:57 +0300 Subject: more spring-jdbc --- src/main/java/com/juick/server/PushQueries.java | 108 ++++------ .../com/juick/server/SubscriptionsQueries.java | 38 +--- src/main/java/com/juick/server/TagQueries.java | 23 +-- src/main/java/com/juick/server/UserQueries.java | 220 +++++++-------------- 4 files changed, 128 insertions(+), 261 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PushQueries.java b/src/main/java/com/juick/server/PushQueries.java index c17972b0..54528993 100644 --- a/src/main/java/com/juick/server/PushQueries.java +++ b/src/main/java/com/juick/server/PushQueries.java @@ -4,14 +4,12 @@ */ package com.juick.server; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.util.StringUtils; + import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.Optional; /** * @@ -19,80 +17,58 @@ import java.util.logging.Logger; */ public class PushQueries { - private static final Logger logger = Logger.getLogger(PushQueries.class.getName()); - - public static String getAndroidRegID(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT regid FROM android WHERE user_id=?", uid); - } - - public static List getAndroidSubscribers(Connection sql, int uid) { - List regids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional getAndroidRegID(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT regid FROM android INNER JOIN subscr_users ON (subscr_users.user_id=? AND android.user_id=subscr_users.suser_id)"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - regids.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return Optional.of(sql.queryForObject("SELECT regid FROM android WHERE user_id=?", String.class, uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - return regids; + } - public static String getWinPhoneURL(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT url FROM winphone WHERE user_id=?", uid); + public static List getAndroidSubscribers(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT regid FROM android INNER JOIN subscr_users " + + "ON (subscr_users.user_id=? AND android.user_id=subscr_users.suser_id)", String.class, uid); } - public static List getWinPhoneSubscribers(Connection sql, int uid) { - List urls = new ArrayList<>(); + public static List getAndroidTokens(JdbcTemplate sql, List uids) { + return sql.queryForList("SELECT regid FROM android INNER JOIN users " + + "ON (users.id=android.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); + } - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional getWinPhoneURL(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT url FROM winphone INNER JOIN subscr_users ON (subscr_users.user_id=? AND winphone.user_id=subscr_users.suser_id)"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - urls.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return Optional.of(sql.queryForObject("SELECT url FROM winphone WHERE user_id=?", String.class, uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - return urls; } - public static String getAPNSToken(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT token from ios WHERE user_id=?", uid); + public static List getWinPhoneSubscribers(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT url FROM winphone INNER JOIN subscr_users " + + "ON (subscr_users.user_id=? AND winphone.user_id=subscr_users.suser_id)", String.class, uid); } - public static List getAPNSSubscribers(Connection sql, int uid) { - List urls = new ArrayList<>(); + public static List getWindowsTokens(JdbcTemplate sql, List uids) { + return sql.queryForList("SELECT url FROM winphone INNER JOIN users " + + "ON (users.id=winphone.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); + } - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional getAPNSToken(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT token FROM ios INNER JOIN subscr_users ON (subscr_users.user_id=? AND ios.user_id=subscr_users.suser_id)"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - urls.add(rs.getString(1)); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return Optional.of(sql.queryForObject("SELECT token from ios WHERE user_id=?", String.class, uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - return urls; } + + public static List getAPNSSubscribers(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT token FROM ios INNER JOIN subscr_users " + + "ON (subscr_users.user_id=? AND ios.user_id=subscr_users.suser_id)", String.class, uid); + } + public static List getAPNSTokens(JdbcTemplate sql, List uids) { + return sql.queryForList("SELECT token FROM ios INNER JOIN users " + + "ON (users.id=ios.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")", String.class); + } + } diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index b271435b..feebfb22 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -8,15 +8,10 @@ import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; -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; /** @@ -27,31 +22,16 @@ public class SubscriptionsQueries { private static final Logger logger = Logger.getLogger(SubscriptionsQueries.class.getName()); - public static List getJIDSubscribedToUser(Connection sql, int uid, boolean friendsonly) { - List jids = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - if (friendsonly == false) { - stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1"); - stmt.setInt(1, uid); - } else { - stmt = sql.prepareStatement("SELECT jids.jid FROM subscr_users INNER JOIN jids ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)"); - stmt.setInt(1, uid); - stmt.setInt(2, 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 getJIDSubscribedToUser(JdbcTemplate sql, int uid, boolean friendsonly) { + if (friendsonly == false) { + return sql.queryForList("SELECT jids.jid FROM subscr_users INNER JOIN jids " + + "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1", + String.class, uid); + } else { + return sql.queryForList("SELECT jids.jid FROM subscr_users INNER JOIN jids " + + "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 " + + "AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)", String.class, uid, uid); } - return jids; } public static List getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index d2159b5a..0094fc22 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -123,25 +123,10 @@ public class TagQueries { }, uid); } - public static List getUserBLTags(Connection sql, int uid) { - List tags = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tags.name FROM tags INNER JOIN bl_tags ON (bl_tags.user_id=? AND bl_tags.tag_id=tags.tag_id) ORDER BY tags.name"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - tags.add(rs.getString(1)); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - return tags; + public static List getUserBLTags(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT tags.name FROM tags INNER JOIN bl_tags " + + "ON (bl_tags.user_id=? AND bl_tags.tag_id=tags.tag_id) ORDER BY tags.name", + String.class, uid); } public static List getPopularTags(JdbcTemplate sql) { diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 0d6a3485..e7aee0f5 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -25,14 +25,11 @@ import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.util.StringUtils; -import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; /** * @@ -42,8 +39,6 @@ public class UserQueries { static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - private static final Logger LOGGER = Logger.getLogger(UserQueries.class.getName()); - public static class UserMapper implements RowMapper { @Override public User mapRow(ResultSet rs, int rowNum) throws SQLException { @@ -163,8 +158,8 @@ public class UserQueries { }); } - public static String getJIDbyUID(JdbcTemplate sql, int uid) { - return sql.queryForObject("SELECT jid FROM jids WHERE user_id=? AND active=1", String.class, uid); + public static List getJIDsbyUID(JdbcTemplate sql, int uid) { + return sql.queryForList("SELECT jid FROM jids WHERE user_id=? AND active=1", String.class, uid); } public static int getUIDbyJID(JdbcTemplate sql, String jid) { @@ -241,58 +236,26 @@ public class UserQueries { } } - public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { - int ret = defaultValue; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static int getUserOptionInt(JdbcTemplate sql, int uid, String option, int defaultValue) { try { - stmt = sql.prepareStatement("SELECT " + option + " FROM useroptions WHERE user_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT " + option + " FROM useroptions WHERE user_id=?", Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return defaultValue; } - return ret; } - public static void setUserOptionInt(Connection sql, int uid, String option, int value) { - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE useroptions SET " + option + "=? WHERE user_id=?"); - stmt.setInt(1, value); - stmt.setInt(2, uid); - stmt.executeUpdate(); - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(null, stmt); - } + public static void setUserOptionInt(JdbcTemplate sql, int uid, String option, int value) { + sql.update("UPDATE useroptions SET " + option + "=? WHERE user_id=?", value, uid); } - public static boolean getCanMedia(Connection sql, int uid) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean getCanMedia(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) < 3600; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + int res = sql.queryForObject("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?", + Integer.class, uid); + return res < 3600; + } catch (EmptyResultDataAccessException e) { + return false; } - return ret; } public static boolean isInWL(JdbcTemplate sql, int uid, int check) { @@ -300,25 +263,13 @@ public class UserQueries { new Object[] {uid, check}, Integer.class) == 1; } - public static boolean isInBL(Connection sql, int uid, int check) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static boolean isInBL(JdbcTemplate sql, int uid, int check) { try { - stmt = sql.prepareStatement("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?"); - stmt.setInt(1, uid); - stmt.setInt(2, check); - rs = stmt.executeQuery(); - if (rs.first()) { - ret = rs.getInt(1) == 1; - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?", + Integer.class, uid, check) == 1; + } catch (EmptyResultDataAccessException e) { + return false; } - return ret; } public static boolean isInBLAny(JdbcTemplate sql, int uid, int uid2) { @@ -353,33 +304,20 @@ public class UserQueries { return sql.queryForList("SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid); } - public static List getUserReadLeastPopular(Connection sql, int uid, int cnt) { - List users = new ArrayList<>(cnt); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM (subscr_users INNER JOIN users_subscr ON (subscr_users.suser_id=? AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?"); - stmt.setInt(1, uid); - stmt.setInt(2, cnt); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User u = new com.juick.User(); - u.setUID(rs.getInt(1)); - u.setUName(rs.getString(2)); - users.add(u); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return users; + public static List getUserReadLeastPopular(JdbcTemplate sql, int uid, int cnt) { + return sql.query("SELECT users.id,users.nick FROM (subscr_users " + + "INNER JOIN users_subscr ON (subscr_users.suser_id=? " + + "AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users " + + "ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?", + (rs, num) -> { + com.juick.User u = new com.juick.User(); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); + return u; + }, uid, cnt); } - public List getUserReaders(JdbcTemplate sql, int uid) { + public static List getUserReaders(JdbcTemplate sql, int uid) { return sql.query("SELECT users.id, users.nick FROM subscr_users " + "INNER JOIN users ON subscr_users.suser_id=users.id " + "WHERE subscr_users.user_id=? ORDER BY users.nick", @@ -391,7 +329,7 @@ public class UserQueries { }, uid); } - public List getUserFriends(JdbcTemplate sql, int uid) { + public static List getUserFriends(JdbcTemplate sql, int uid) { return sql.query("SELECT users.id,users.nick FROM subscr_users " + "INNER JOIN users ON subscr_users.user_id=users.id " + "WHERE subscr_users.suser_id=? AND users.id!=? " + @@ -404,72 +342,60 @@ public class UserQueries { }, uid, uid); } - public static List getUserBLUsers(Connection sql, int uid) { - List users = new ArrayList<>(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT users.id,users.nick FROM users INNER JOIN bl_users ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.User u = new com.juick.User(); - u.setUID(rs.getInt(1)); - u.setUName(rs.getString(2)); - users.add(u); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return users; + public static List getUserBLUsers(JdbcTemplate sql, int uid) { + return sql.query("SELECT users.id,users.nick FROM users INNER JOIN bl_users " + + "ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick", + (rs, num) -> { + com.juick.User u = new com.juick.User(); + u.setUID(rs.getInt(1)); + u.setUName(rs.getString(2)); + return u; + }, uid); } - public static boolean linkTwitterAccount(Connection sql, User user, String accessToken, + public static boolean linkTwitterAccount(JdbcTemplate sql, User user, String accessToken, String accessTokenSecret, String screenName) { - try { - PreparedStatement stmt = - sql.prepareStatement("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + - "VALUES (?,?,?,?)" + - " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?"); - stmt.setInt(1, user.getUID()); - stmt.setString(2, accessToken); - stmt.setString(3, accessTokenSecret); - stmt.setString(4, screenName); - stmt.setString(5, accessToken); - stmt.setString(6, accessTokenSecret); - stmt.setString(7, screenName); - if (stmt.execute()) { - PreparedStatement stmt2 = - sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id,jid) " + - "VALUES (?,1741,'juick\\@twitter.juick.com')"); - stmt2.setInt(1, user.getUID()); - return stmt2.execute(); - } - } catch (SQLException e) { - LOGGER.log(Level.SEVERE, "sql exception", e); + if (sql.update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + + "VALUES (?,?,?,?)" + + " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?", + user.getUID(), accessToken, accessTokenSecret, screenName, accessToken, accessTokenSecret, screenName) > 0) { + return sql.update("INSERT INTO subscr_users(user_id,suser_id,jid) " + + "VALUES (?,1741,'juick\\@twitter.juick.com')", user.getUID()) > 0; } return false; + } - public static int getStatsIRead(Connection sql, int uid) { - return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); + public static int getStatsIRead(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return 0; + } } - public static int getStatsMyReaders(Connection sql, int uid) { - return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE user_id=?", uid, 0); + public static int getStatsMyReaders(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT COUNT(*) FROM subscr_users WHERE user_id=?", Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return 0; + } } - public static int getStatsMessages(Connection sql, int uid) { - return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM messages WHERE user_id=?", uid, 0); + public static int getStatsMessages(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT COUNT(*) FROM messages WHERE user_id=?", Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return 0; + } } - public static int getStatsReplies(Connection sql, int uid) { - return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); + public static int getStatsReplies(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT COUNT(*) FROM replies WHERE user_id=?", Integer.class, uid); + } catch (EmptyResultDataAccessException e) { + return 0; + } } public enum ActiveStatus { -- cgit v1.2.3 From 4683d6b828d2bf6d56a53205c785f7982ce1be11 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 13:29:51 +0300 Subject: fixes --- .../java/com/juick/server/MessagesQueries.java | 22 +++++++++++++--------- src/main/java/com/juick/server/UserQueries.java | 12 ++++++++---- 2 files changed, 21 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index bfd0d71a..77998d16 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -268,15 +268,19 @@ public class MessagesQueries { } public static User getMessageAuthor(JdbcTemplate sql, int mid) { - return sql.queryForObject("SELECT messages.user_id,users.nick " - + "FROM messages INNER JOIN users " - + "ON messages.user_id=users.id WHERE messages.message_id=?", - new Object[]{mid}, (rs, num) -> { - User res = new com.juick.User(); - res.setUID(rs.getInt(1)); - res.setUName(rs.getString(2)); - return res; - }); + try { + return sql.queryForObject("SELECT messages.user_id,users.nick " + + "FROM messages INNER JOIN users " + + "ON messages.user_id=users.id WHERE messages.message_id=?", + new Object[]{mid}, (rs, num) -> { + User res = new com.juick.User(); + res.setUID(rs.getInt(1)); + res.setUName(rs.getString(2)); + return res; + }); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static List getMessageTags(JdbcTemplate sql, int mid) { diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index e7aee0f5..f071de39 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -100,8 +100,8 @@ public class UserQueries { public static User getUserByName(JdbcTemplate sql, String username) { try { return sql.queryForObject("SELECT id,nick,banned FROM users WHERE nick=?", - new Object[]{username}, - new UserMapper()); + new UserMapper(), + username); } catch (EmptyResultDataAccessException e) { return null; } @@ -259,8 +259,12 @@ public class UserQueries { } public static boolean isInWL(JdbcTemplate sql, int uid, int check) { - return sql.queryForObject("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?", - new Object[] {uid, check}, Integer.class) == 1; + try { + return sql.queryForObject("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?", + Integer.class, uid, check) == 1; + } catch (EmptyResultDataAccessException e) { + return false; + } } public static boolean isInBL(JdbcTemplate sql, int uid, int check) { -- cgit v1.2.3 From 9d182aa45b5e2428ba684973837a8d035c0d50a2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 14:15:09 +0300 Subject: fixes --- src/main/java/com/juick/server/UserQueries.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index f071de39..3a0e03ba 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -108,8 +108,12 @@ public class UserQueries { } public static User getUserByJID(JdbcTemplate sql, String jid) { - return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new UserMapper(), jid); + try { + return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", + new UserMapper(), jid); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static List getUsersByName(JdbcTemplate sql, List unames) { -- cgit v1.2.3 From bbc05316ebbbd8da30ac12135791e110cacbebd5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 27 Jun 2016 23:36:36 +0300 Subject: fix canviewthread --- .../java/com/juick/server/MessagesQueries.java | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 77998d16..cd098ec6 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -155,16 +155,18 @@ public class MessagesQueries { } public static boolean canViewThread(JdbcTemplate sql, int mid, int uid) { - - PrivacyOpts privacyOpts = sql.queryForObject("SELECT user_id,privacy FROM messages WHERE messages.message_id=?", - new Object[] {mid}, - (rs, rowNum) -> { - PrivacyOpts res = new PrivacyOpts(); - res.setUid(rs.getInt(1)); - res.setPrivacy(rs.getInt(2)); - return res; - }); - + PrivacyOpts privacyOpts; + try { + privacyOpts = sql.queryForObject("SELECT user_id,privacy FROM messages WHERE messages.message_id=?", + (rs, rowNum) -> { + PrivacyOpts res = new PrivacyOpts(); + res.setUid(rs.getInt(1)); + res.setPrivacy(rs.getInt(2)); + return res; + }, mid); + } catch (EmptyResultDataAccessException e) { + return true; + } return privacyOpts.getPrivacy() >= 0 || uid == privacyOpts.getUid() || ((privacyOpts.getPrivacy() == -1 || privacyOpts.getPrivacy() == -2) && uid > 0 -- cgit v1.2.3 From 87da42e3385e368f05d88ff125a78605a18f9cc3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 28 Jun 2016 10:36:04 +0300 Subject: fix getuserbyhash --- src/main/java/com/juick/server/UserQueries.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 3a0e03ba..ca562765 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -187,11 +187,15 @@ public class UserQueries { } public static com.juick.User getUserByHash(JdbcTemplate sql, String hash) { - User user = sql.queryForObject("SELECT logins.user_id,users.nick, users.banned FROM logins " + - "INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?", - new UserMapper(), hash); - user.setAuthHash(hash); - return user; + try { + User user = sql.queryForObject("SELECT logins.user_id,users.nick, users.banned FROM logins " + + "INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?", + new UserMapper(), hash); + user.setAuthHash(hash); + return user; + } catch (EmptyResultDataAccessException e) { + return new User(); + } } public static String getHashByUID(JdbcTemplate sql, int uid) { -- cgit v1.2.3 From 797942249b0d5e38285e806bc0e73e755e6ffede Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 29 Jun 2016 15:09:47 +0300 Subject: add more bot queries --- .../java/com/juick/server/MessagesQueries.java | 11 ++++ .../com/juick/server/SubscriptionsQueries.java | 22 ++++++++ src/main/java/com/juick/server/TagQueries.java | 65 ++++++++++++++++------ 3 files changed, 81 insertions(+), 17 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index cd098ec6..880e94e2 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -645,4 +645,15 @@ public class MessagesQueries { return sql.update("UPDATE messages SET privacy=1 WHERE message_id=?", mid) > 0; } + public static boolean deleteMessage(JdbcTemplate sql, int uid, int mid) { + if (sql.update("DELETE FROM messages WHERE message_id=? AND user_id=?", mid, uid) > 0) { + sql.update("DELETE FROM messages_txt WHERE message_id=?", mid); + sql.update("DELETE FROM replies WHERE message_id=?", mid); + sql.update("DELETE FROM subscr_messages WHERE message_id=?", mid); + sql.update("DELETE FROM messages_tags WHERE message_id=?", mid); + return true; + } + return false; + } + } diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index feebfb22..9f0372ff 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -4,6 +4,7 @@ */ package com.juick.server; +import com.juick.Tag; import com.juick.User; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; @@ -78,4 +79,25 @@ public class SubscriptionsQueries { public static boolean subscribeMessage(JdbcTemplate sql, int mid, int vuid) { return sql.update("INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; } + public static boolean unSubscribeMessage(JdbcTemplate sql, int mid, int vuid) { + return sql.update("DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", + mid, vuid) > 0; + } + public static boolean subscribeUser(JdbcTemplate sql, User user, User toUser) { + return sql.update("INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", + user.getUID(), toUser.getUID()) == 1; + } + public static boolean unSubscribeUser(JdbcTemplate sql, User user, User fromUser) { + return sql.update("DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", + user.getUID(), fromUser.getUID()) > 0; + } + public static boolean subscribeTag(JdbcTemplate sql, User user, Tag toTag) { + return sql.update("INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", + toTag.TID, user.getUID()) == 1; + } + public static boolean unSubscribeTag(JdbcTemplate sql, User user, Tag toTag) { + return sql.update("DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", + toTag.TID, user.getUID()) > 0; + } + } diff --git a/src/main/java/com/juick/server/TagQueries.java b/src/main/java/com/juick/server/TagQueries.java index 0094fc22..2258a8ea 100644 --- a/src/main/java/com/juick/server/TagQueries.java +++ b/src/main/java/com/juick/server/TagQueries.java @@ -20,18 +20,17 @@ package com.juick.server; import com.juick.Tag; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; -import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * @@ -39,17 +38,19 @@ import org.springframework.jdbc.support.KeyHolder; */ public class TagQueries { - private static final Logger LOGGER = Logger.getLogger(TagQueries.class.getName()); - public static com.juick.Tag getTag(JdbcTemplate sql, int tid) { - return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", - new Object[]{tid}, (rs, num) -> { - Tag ret = new Tag(); - ret.TID = tid; - ret.SynonymID = rs.getInt(1); - ret.Name = rs.getString(2); - return ret; - }); + try { + return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", + new Object[]{tid}, (rs, num) -> { + Tag ret = new Tag(); + ret.TID = tid; + ret.SynonymID = rs.getInt(1); + ret.Name = rs.getString(2); + return ret; + }); + } catch (EmptyResultDataAccessException e) { + return null; + } } public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { @@ -132,4 +133,34 @@ public class TagQueries { public static List getPopularTags(JdbcTemplate sql) { return sql.queryForList("SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class); } + public static List updateTags(JdbcTemplate sql, int mid, List newTags) { + List currentTags = MessagesQueries.getMessageTags(sql, mid); + newTags.stream().filter(currentTags::contains) + .forEach(t -> sql.update("DELETE FROM messages_tags WHERE message_id=? AND tag_id=?", mid, t.TID)); + newTags.stream().filter(t -> !currentTags.contains(t)) + .forEach(t -> sql.update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID)); + return MessagesQueries.getMessageTags(sql, mid); + } + + public static List fromString(JdbcTemplate sql, String txt, boolean tagsOnly) { + String patternString = tagsOnly ? "^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$" : "^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"; + Pattern tagsPattern = Pattern.compile(patternString); + if (tagsPattern.matcher(txt).matches()) { + Pattern tagPattern = Pattern.compile("\\*([^ \\r\\n\\t]+)"); + Matcher tagMatcher = tagPattern.matcher(txt); + List tags = new ArrayList<>(); + // TODO: process readonly, private, friends, public + while (tagMatcher.find()) { + for (int i = 1; i <= tagMatcher.groupCount(); i++) { + tags.add(getTag(sql, tagMatcher.group(i), true)); + } + } + return tags; + } + return Collections.emptyList(); + } + public static String toString(List tags) { + return tags.stream().map(t -> " *" + t.Name) + .collect(Collectors.joining()); + } } -- cgit v1.2.3 From 53a4aaacf585e70aadd3b1133319a5bb1e08f0c4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Jul 2016 12:27:19 +0300 Subject: fix pm conversations query --- src/main/java/com/juick/server/PMQueries.java | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/PMQueries.java b/src/main/java/com/juick/server/PMQueries.java index 772a1c28..74d5df3e 100644 --- a/src/main/java/com/juick/server/PMQueries.java +++ b/src/main/java/com/juick/server/PMQueries.java @@ -69,33 +69,13 @@ public class PMQueries { public static List getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { List qusers = sql.query("SELECT user_id,unread FROM pm_streams " - + "WHERE user_id_to=? AND unread>0 " - + "ORDER BY lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { + + "WHERE user_id_to=? " + + "ORDER BY unread DESC, lastmessage DESC LIMIT " + cnt, (rs, rowNum) -> { com.juick.User u = new com.juick.User(); u.setUID(rs.getInt(1)); u.MessagesCount = rs.getInt(2); return u; }, uid); - - if (qusers.size() < cnt) { - List addUsers = sql.query("SELECT user_id,user_id_to " - + "FROM pm_streams WHERE (user_id=? OR user_id_to=?) " - + "AND lastmessage>TIMESTAMPADD(MONTH,-1,NOW()) " - + "ORDER BY lastmessage DESC LIMIT ?", (rs, num) -> { - User u = new com.juick.User(); - int uuid = rs.getInt(1) + rs.getInt(2) - uid; - u.setUID(uuid); - return u; - }, uid, uid, cnt * 2); - for (User addUser : addUsers) { - if (!haveUserInArray(qusers, addUser.getUID())) { - qusers.add(addUser); - if (qusers.size() >= cnt) { - break; - } - } - } - } if (!qusers.isEmpty()) { UserQueries.fillUsersByID(sql, qusers); } -- cgit v1.2.3 From 67d53fe24081ade138b5d9756fd835575eb4ae79 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Jul 2016 14:00:31 +0300 Subject: fix getSubscribedUsers --- src/main/java/com/juick/server/SubscriptionsQueries.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/SubscriptionsQueries.java b/src/main/java/com/juick/server/SubscriptionsQueries.java index 9f0372ff..0cf3f169 100644 --- a/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -14,6 +14,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -36,9 +37,9 @@ public class SubscriptionsQueries { } public static List getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { - List userids = UserQueries.getUserRead(sql, uid); + List userids = UserQueries.getUserReaders(sql, uid); Set set = new HashSet<>(); - set.addAll(userids); + set.addAll(userids.stream().map(User::getUID).collect(Collectors.toList())); List tags = MessagesQueries.getMessageTagsIDs(sql, mid); if (tags.size() > 0) { List tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + -- cgit v1.2.3 From a732644480ff8b5b2ee46124af4d8c62944e39eb Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 1 Jul 2016 16:34:28 +0300 Subject: fix users queries --- src/main/java/com/juick/server/UserQueries.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index ca562765..db380d7a 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -117,13 +117,19 @@ public class UserQueries { } public static List getUsersByName(JdbcTemplate sql, List unames) { - return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", - new UserMapper()); + if (!unames.isEmpty()) { + return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (" + Utils.convertArrayString2String(unames) + ")", + new UserMapper()); + } + return Collections.emptyList(); } public static List getUsersByID(JdbcTemplate sql, List uids) { - return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")", - new UserMapper()); + if (!uids.isEmpty()) { + return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")", + new UserMapper()); + } + return Collections.emptyList(); } public static boolean fillUsersByID(JdbcTemplate sql, List users) { -- cgit v1.2.3 From a0d038f5e6aa28ef67162c591abe64d057a19fa5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 7 Jul 2016 10:22:01 +0300 Subject: filter top and photos by bl_users --- src/main/java/com/juick/server/MessagesQueries.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/server/MessagesQueries.java b/src/main/java/com/juick/server/MessagesQueries.java index 880e94e2..a0242543 100644 --- a/src/main/java/com/juick/server/MessagesQueries.java +++ b/src/main/java/com/juick/server/MessagesQueries.java @@ -446,14 +446,16 @@ public class MessagesQueries { } } - public static List getPopular(JdbcTemplate sql, int before) { + public static List getPopular(JdbcTemplate sql, int visitor_uid, int before) { if (before > 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 " + - "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class, before); + "AND popular>0 AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + + "ORDER BY message_id DESC LIMIT 20", Integer.class, before, visitor_uid); } else { return sql.queryForList("SELECT message_id FROM messages WHERE privacy>0 " + - "AND popular>0 ORDER BY message_id DESC LIMIT 20", Integer.class); + "AND popular>0 AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + + "ORDER BY message_id DESC LIMIT 20", Integer.class, visitor_uid); } } @@ -461,11 +463,13 @@ public class MessagesQueries { if (before > 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) " + "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " + - "ORDER BY message_id DESC LIMIT 20", new Object[]{before, visitor_uid}, Integer.class); + "AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + + "ORDER BY message_id DESC LIMIT 20", Integer.class, before, visitor_uid, visitor_uid); } else { return sql.queryForList("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) " + "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " + - "ORDER BY message_id DESC LIMIT 20", new Object[]{visitor_uid}, Integer.class); + "AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " + + "ORDER BY message_id DESC LIMIT 20", Integer.class, visitor_uid, visitor_uid); } } -- cgit v1.2.3