From 55f03c70caeec32ae71e5afe99c4eae5b6b192d3 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 24 Dec 2011 23:05:48 +0700 Subject: Initial commit --- .gitignore | 3 + build.xml | 74 ++ nbproject/build-impl.xml | 1056 +++++++++++++++++++++++++++++ nbproject/genfiles.properties | 8 + nbproject/project.properties | 73 ++ nbproject/project.xml | 25 + src/com/juick/server/ChatQueries.java | 86 +++ src/com/juick/server/MessagesQueries.java | 551 +++++++++++++++ src/com/juick/server/PlacesQueries.java | 55 ++ src/com/juick/server/TagQueries.java | 68 ++ src/com/juick/server/UserQueries.java | 218 ++++++ src/com/juick/server/Utils.java | 56 ++ 12 files changed, 2273 insertions(+) create mode 100644 .gitignore create mode 100644 build.xml create mode 100644 nbproject/build-impl.xml create mode 100644 nbproject/genfiles.properties create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml create mode 100644 src/com/juick/server/ChatQueries.java create mode 100644 src/com/juick/server/MessagesQueries.java create mode 100644 src/com/juick/server/PlacesQueries.java create mode 100644 src/com/juick/server/TagQueries.java create mode 100644 src/com/juick/server/UserQueries.java create mode 100644 src/com/juick/server/Utils.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..315ea15c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/dist/ +/build/ +/nbproject/private/ \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 00000000..cb4ef59d --- /dev/null +++ b/build.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project com.juick.server. + + + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 00000000..278d2839 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1056 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + Must select one file in the IDE or set profile.class + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 00000000..972737f5 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=d216b2d6 +build.xml.script.CRC32=8a81d8ba +build.xml.stylesheet.CRC32=28e38971@1.44.1.45 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=d216b2d6 +nbproject/build-impl.xml.script.CRC32=6acf7607 +nbproject/build-impl.xml.stylesheet.CRC32=0ae3a408@1.44.1.45 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 00000000..666e31d1 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=com.juick.server +application.vendor=ugnich +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/com.juick.server.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath=\ + ${reference.com_juick.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.6 +javac.target=1.6 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=true +platform.active=default_platform +project.com_juick=../com.juick +reference.com_juick.jar=${project.com_juick}/dist/com.juick.jar +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 00000000..3a3d1bf9 --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,25 @@ + + + org.netbeans.modules.java.j2seproject + + + com.juick.server + + + + + + + + + + com_juick + jar + + jar + clean + jar + + + + diff --git a/src/com/juick/server/ChatQueries.java b/src/com/juick/server/ChatQueries.java new file mode 100644 index 00000000..19f95637 --- /dev/null +++ b/src/com/juick/server/ChatQueries.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.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * + * @author Ugnich Anton + */ +public class ChatQueries { + + public static boolean subscribeTag(Connection sql, int uid, int tagid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO chat_subscr_tags(tag_id,user_id) VALUES (?,?)"); + stmt.setInt(1, tagid); + stmt.setInt(2, uid); + if (stmt.executeUpdate() > 0) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } + + public static boolean unsubscribeTag(Connection sql, int uid, int tagid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("DELETE FROM chat_subscr_tags WHERE tag_id=? AND user_id=?"); + stmt.setInt(1, tagid); + stmt.setInt(2, uid); + if (stmt.executeUpdate() > 0) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } + + public static boolean setSubscriptionsActive(Connection sql, int uid, boolean enabled) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE chat_subscr_tags SET active=? WHERE user_id=?"); + stmt.setInt(1, enabled ? 1 : 0); + stmt.setInt(2, uid); + if (stmt.executeUpdate() > 0) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } +} diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java new file mode 100644 index 00000000..3ff47ca7 --- /dev/null +++ b/src/com/juick/server/MessagesQueries.java @@ -0,0 +1,551 @@ +/* + * 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 MessagesQueries { + + 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,messages.ts,messages.readonly,messages.attach,messages.place_id,messages.lat,messages.lon 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.TimestampString = rs.getString(3); + msg.ReadOnly = rs.getBoolean(4); + msg.AttachmentType = rs.getString(5); + if (rs.getInt(6) > 0) { + msg.place = PlacesQueries.getPlace(sql, rs.getInt(6)); + } + if (rs.getDouble(7) != 0) { + msg.place = new com.juick.Place(); + msg.place.lat = rs.getDouble(7); + msg.place.lon = rs.getDouble(8); + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (msg == null) { + return null; + } + + try { + stmt = sql.prepareStatement("SELECT messages_txt.tags,messages_txt.txt FROM messages_txt WHERE message_id=?"); + stmt.setInt(1, mid); + rs = stmt.executeQuery(); + if (rs.first()) { + msg.parseTags(rs.getString(1)); + msg.Text = rs.getString(2); + } + } 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,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=?"); + 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 getAll(Connection sql, 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 WHERE messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages WHERE messages.privacy>0 ORDER BY messages.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 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_tags.tag_id=? AND messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, tid); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND messages.privacy>0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt.setInt(1, 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 getMyFeed(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 feed 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_access WHERE user_id=? AND message_id getIncoming(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 IN (SELECT suser_id FROM subscr_users WHERE user_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 messages.message_id IN (SELECT message_id FROM favorites WHERE user_id=11574) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND messages.message_id IN (SELECT message_id FROM favorites WHERE user_id=11574) 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 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 attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND attach IS NOT NULL 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 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.convertArray2String(mids0) + ") AND privacy>0 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 getUserBlog(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 message_id0 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>0 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 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_id0 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>0 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 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_id0 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>0 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 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.convertArray2String(mids0) + ") AND privacy>0 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; + } +} diff --git a/src/com/juick/server/PlacesQueries.java b/src/com/juick/server/PlacesQueries.java new file mode 100644 index 00000000..d61c89c3 --- /dev/null +++ b/src/com/juick/server/PlacesQueries.java @@ -0,0 +1,55 @@ +/* + * 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; + +/** + * + * @author Ugnich Anton + */ +public class PlacesQueries { + + public static com.juick.Place getPlace(Connection sql, int pid) { + com.juick.Place place = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT lat,lon,name FROM places WHERE place_id=?"); + stmt.setInt(1, pid); + rs = stmt.executeQuery(); + if (rs.first()) { + place = new com.juick.Place(); + place.pid = pid; + place.lat = rs.getDouble(1); + place.lon = rs.getDouble(2); + place.name = rs.getString(3); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return place; + } +} diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java new file mode 100644 index 00000000..8cb7ee1a --- /dev/null +++ b/src/com/juick/server/TagQueries.java @@ -0,0 +1,68 @@ +/* + * 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; + +/** + * + * @author Ugnich Anton + */ +public class TagQueries { + + public static int getTagID(Connection sql, String tag, boolean autoCreate) { + int tid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id FROM tags WHERE name=?"); + stmt.setString(1, tag); + rs = stmt.executeQuery(); + if (rs.first()) { + tid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (tid == 0 && autoCreate) { + try { + stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, tag); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + tid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return tid; + } +} diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java new file mode 100644 index 00000000..97cae91f --- /dev/null +++ b/src/com/juick/server/UserQueries.java @@ -0,0 +1,218 @@ +/* + * 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.Random; + +/** + * + * @author Ugnich Anton + */ +public class UserQueries { + + static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static com.juick.User getUserByNick(Connection sql, String username) { + com.juick.User user = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT id,nick 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); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static String getJIDbyUID(Connection sql, int UID) { + String jid = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT jid FROM jids WHERE user_id=? AND active=1"); + stmt.setInt(1, UID); + rs = stmt.executeQuery(); + if (rs.first()) { + jid = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return jid; + } + + public static int getUIDbyHash(Connection sql, String hash) { + int UID = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT user_id FROM logins WHERE hash=?"); + stmt.setString(1, hash); + rs = stmt.executeQuery(); + if (rs.first()) { + UID = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return UID; + } + + 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); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return user; + } + + public static String getHashByUID(Connection sql, int uid) { + String hash = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT logins.hash FROM logins WHERE user_id=?"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + if (rs.first()) { + hash = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (hash == null) { + hash = generateHash(16); + 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 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); + } + } +} diff --git a/src/com/juick/server/Utils.java b/src/com/juick/server/Utils.java new file mode 100644 index 00000000..997c4f32 --- /dev/null +++ b/src/com/juick/server/Utils.java @@ -0,0 +1,56 @@ +/* + * 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.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; + +/** + * + * @author Ugnich Anton + */ +public class Utils { + + public static String convertArray2String(ArrayList mids) { + String q = ""; + for (int i = 0; i < mids.size(); i++) { + if (i > 0) { + q += ","; + } + q += mids.get(i); + } + return q; + } + + 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 f656d844d0096cf8637746afaa1fab7dadaef261 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 27 Jan 2012 08:34:31 +0700 Subject: User.getCanMedia, Tag.getUserTagsAll --- src/com/juick/server/TagQueries.java | 25 +++++++++++++++++++++++++ src/com/juick/server/UserQueries.java | 20 ++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 8cb7ee1a..7d78e345 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -22,6 +22,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.ArrayList; /** * @@ -65,4 +66,28 @@ public class TagQueries { return tid; } + + 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; + } } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 97cae91f..906f7742 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -215,4 +215,24 @@ public class UserQueries { 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; + } } -- cgit v1.2.3 From 84de24ebd67552e8f1ee01a377a0437b00a8db0e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 14 Oct 2012 11:34:26 +0700 Subject: Privacy --- src/com/juick/server/MessagesQueries.java | 56 ++++++++++++++++++++++++++----- src/com/juick/server/UserQueries.java | 25 ++++++++++++-- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 3ff47ca7..b406eb39 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -29,6 +29,30 @@ import java.util.ArrayList; */ public class MessagesQueries { + 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 || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) || (privacy == -2 && uid == owner_uid); + } + public static com.juick.Message getMessage(Connection sql, int mid) { com.juick.Message msg = null; @@ -190,7 +214,21 @@ public class MessagesQueries { public static ArrayList getMyFeed(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + /* + my $sql="SELECT DISTINCT t.message_id FROM ("; + $sql.="SELECT STRAIGHT_JOIN message_id FROM subscr_users INNER JOIN messages ON subscr_users.user_id=messages.user_id WHERE privacy>=0 AND subscr_users.suser_id=$uid"; + $sql.=" UNION SELECT message_id FROM (subscr_users INNER JOIN messages ON subscr_users.user_id=messages.user_id) INNER JOIN wl_users ON messages.user_id=wl_users.user_id WHERE privacy=-1 AND subscr_users.suser_id=$uid AND wl_users.wl_user_id=$uid"; + $sql.=" UNION SELECT message_id FROM messages WHERE user_id=$uid"; + $sql.=" UNION SELECT message_id FROM subscr_messages WHERE suser_id=$uid"; + $sql.=" UNION SELECT message_id FROM favorites WHERE user_id=$uid"; + $sql.=" UNION SELECT message_id FROM favorites INNER JOIN subscr_users ON (subscr_users.suser_id=$uid AND favorites.user_id=subscr_users.user_id)"; + $sql.=") AS t LEFT JOIN messages_tags USING(message_id) WHERE (tag_id NOT IN (SELECT tag_id FROM bl_tags WHERE user_id=$uid) OR tag_id IS NULL)"; + if($before_mid>0) { + $sql.=" AND message_id<$before_mid"; + } + $sql.=" ORDER BY t.message_id DESC LIMIT 20"; + + */ PreparedStatement stmt = null; ResultSet rs = null; try { @@ -242,18 +280,18 @@ public class MessagesQueries { return mids; } - public static ArrayList getIncoming(Connection sql, int uid, int before) { + public static ArrayList 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 messages WHERE user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=?) AND message_id 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=0 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>0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=0 ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, UID); } rs = stmt.executeQuery(); @@ -483,11 +521,11 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id0 AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id=0 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>0 AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND privacy>=0 AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, UID); } rs = stmt.executeQuery(); @@ -533,7 +571,7 @@ public class MessagesQueries { ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>=0 ORDER BY message_id DESC"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 906f7742..bd392ade 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -121,11 +121,11 @@ public class UserQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT logins.hash FROM logins WHERE user_id=?"); + stmt = sql.prepareStatement("SELECT hash FROM logins WHERE user_id=?"); stmt.setInt(1, uid); rs = stmt.executeQuery(); if (rs.first()) { - hash = rs.getString(2); + hash = rs.getString(1); } } catch (SQLException e) { System.err.println(e); @@ -235,4 +235,25 @@ 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) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } } -- cgit v1.2.3 From 6df2be1e680adb8f9c8a186fae05dd7357280c95 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 14 Oct 2012 11:45:34 +0700 Subject: getMessageRecommendations --- src/com/juick/server/MessagesQueries.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index b406eb39..3ec412b0 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -160,6 +160,31 @@ public class MessagesQueries { return user; } + public static String getMessageRecommendations(Connection sql, int mid) { + String ret = ""; + + 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()) { + if (!ret.isEmpty()) { + ret += ", "; + } + ret += "@" + rs.getString(1) + ""; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + public static ArrayList getAll(Connection sql, int before) { ArrayList mids = new ArrayList(20); -- cgit v1.2.3 From bde1bae2a20811cb650608b49459c18f837ba9a5 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 14 Oct 2012 11:51:31 +0700 Subject: getMessageRecommendations ArrayList --- src/com/juick/server/MessagesQueries.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 3ec412b0..80d7a280 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -160,8 +160,8 @@ public class MessagesQueries { return user; } - public static String getMessageRecommendations(Connection sql, int mid) { - String ret = ""; + public static ArrayList getMessageRecommendations(Connection sql, int mid) { + ArrayList users = new ArrayList(); PreparedStatement stmt = null; ResultSet rs = null; @@ -171,10 +171,7 @@ public class MessagesQueries { rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { - if (!ret.isEmpty()) { - ret += ", "; - } - ret += "@" + rs.getString(1) + ""; + users.add(rs.getString(1)); } } catch (SQLException e) { System.err.println(e); @@ -182,7 +179,7 @@ public class MessagesQueries { Utils.finishSQL(rs, stmt); } - return ret; + return users; } public static ArrayList getAll(Connection sql, int before) { -- cgit v1.2.3 From 4d77c09e6d382548edae2509ccbd56655cbe3737 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 16 Oct 2012 17:03:36 +0700 Subject: get user stats --- src/com/juick/server/UserQueries.java | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index bd392ade..2de8eed7 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -256,4 +256,80 @@ public class UserQueries { } return ret; } + + public static int getStatsIRead(Connection sql, int uid) { + int ret = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users WHERE suser_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 int getStatsMyReaders(Connection sql, int uid) { + int ret = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users 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 int getStatsMessages(Connection sql, int uid) { + int ret = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT COUNT(*) FROM messages 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 int getStatsReplies(Connection sql, int uid) { + int ret = 0; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT COUNT(*) FROM replies 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; + } } -- cgit v1.2.3 From 864552bd5e4913a674845b2a9d7fe2fbb604439b Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 28 Oct 2012 15:49:50 +0700 Subject: SQL optimization --- src/com/juick/server/MessagesQueries.java | 34 ++++++++++++------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 80d7a280..87784f63 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -236,31 +236,20 @@ public class MessagesQueries { public static ArrayList getMyFeed(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - /* - my $sql="SELECT DISTINCT t.message_id FROM ("; - $sql.="SELECT STRAIGHT_JOIN message_id FROM subscr_users INNER JOIN messages ON subscr_users.user_id=messages.user_id WHERE privacy>=0 AND subscr_users.suser_id=$uid"; - $sql.=" UNION SELECT message_id FROM (subscr_users INNER JOIN messages ON subscr_users.user_id=messages.user_id) INNER JOIN wl_users ON messages.user_id=wl_users.user_id WHERE privacy=-1 AND subscr_users.suser_id=$uid AND wl_users.wl_user_id=$uid"; - $sql.=" UNION SELECT message_id FROM messages WHERE user_id=$uid"; - $sql.=" UNION SELECT message_id FROM subscr_messages WHERE suser_id=$uid"; - $sql.=" UNION SELECT message_id FROM favorites WHERE user_id=$uid"; - $sql.=" UNION SELECT message_id FROM favorites INNER JOIN subscr_users ON (subscr_users.suser_id=$uid AND favorites.user_id=subscr_users.user_id)"; - $sql.=") AS t LEFT JOIN messages_tags USING(message_id) WHERE (tag_id NOT IN (SELECT tag_id FROM bl_tags WHERE user_id=$uid) OR tag_id IS NULL)"; - if($before_mid>0) { - $sql.=" AND message_id<$before_mid"; - } - $sql.=" ORDER BY t.message_id DESC LIMIT 20"; - - */ PreparedStatement stmt = null; ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM feed WHERE user_id=? AND message_id=0 OR (privacy=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); stmt.setInt(2, before); + stmt.setInt(3, uid); + stmt.setInt(4, uid); } else { - stmt = sql.prepareStatement("SELECT message_id FROM feed WHERE user_id=? ORDER BY message_id DESC LIMIT 20"); + 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=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); + stmt.setInt(2, uid); + stmt.setInt(3, uid); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -282,12 +271,15 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages_access WHERE user_id=? AND message_id 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND messages.message_id IN (SELECT message_id FROM favorites WHERE user_id=11574) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages INNER JOIN favorites ON (favorites.user_id=11574 AND favorites.message_id=messages.message_id) WHERE messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); stmt.setInt(1, before); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND messages.message_id IN (SELECT message_id FROM favorites WHERE user_id=11574) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages INNER JOIN favorites ON (favorites.user_id=11574 AND favorites.message_id=messages.message_id) WHERE messages.privacy>0 ORDER BY messages.message_id DESC LIMIT 20"); } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From 691738fa2c9f4b8ac3789b21767e52f35ae72b5a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 29 Oct 2012 00:22:13 +0700 Subject: isInBL, isSubscribed --- src/com/juick/server/UserQueries.java | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 2de8eed7..a49da34f 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -257,6 +257,48 @@ public class UserQueries { 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 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 int getStatsIRead(Connection sql, int uid) { int ret = 0; PreparedStatement stmt = null; -- cgit v1.2.3 From 860b00003367e5a66ffc8ffe373542e424516249 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 2 Nov 2012 14:44:28 +0700 Subject: MessagesQueries.isSubscribed --- src/com/juick/server/MessagesQueries.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 87784f63..1df45803 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -53,6 +53,27 @@ public class MessagesQueries { return privacy >= 0 || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) || (privacy == -2 && uid == owner_uid); } + 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 com.juick.Message getMessage(Connection sql, int mid) { com.juick.Message msg = null; -- cgit v1.2.3 From 4c311735dcf5d6e54dfb1d2d751a2bd42193e168 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 2 Nov 2012 17:32:20 +0700 Subject: getMessages checkBL --- src/com/juick/server/MessagesQueries.java | 52 ++++++++++++++++++++++++++++--- src/com/juick/server/UserQueries.java | 23 ++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 1df45803..abcdd285 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -93,12 +93,12 @@ public class MessagesQueries { msg.ReadOnly = rs.getBoolean(4); msg.AttachmentType = rs.getString(5); if (rs.getInt(6) > 0) { - msg.place = PlacesQueries.getPlace(sql, rs.getInt(6)); + msg.Place = PlacesQueries.getPlace(sql, rs.getInt(6)); } if (rs.getDouble(7) != 0) { - msg.place = new com.juick.Place(); - msg.place.lat = rs.getDouble(7); - msg.place.lon = rs.getDouble(8); + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(7); + msg.Place.lon = rs.getDouble(8); } } } catch (SQLException e) { @@ -621,4 +621,48 @@ public class MessagesQueries { 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()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id IN (" + Utils.convertArray2String(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); + msg.parseTags(rs.getString(4)); + msg.ReadOnly = rs.getInt(5) == 1; + msg.Privacy = rs.getInt(6); + 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.getInt(13) > 0) { + msg.Place = new com.juick.Place(); + msg.Place.pid = rs.getInt(13); + msg.Place.name = rs.getString(14); + msg.Place.lat = rs.getDouble(15); + msg.Place.lon = rs.getDouble(16); + } + + msgs.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return msgs; + } } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index a49da34f..ff1d0e24 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -21,6 +21,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Random; /** @@ -278,6 +279,28 @@ public class UserQueries { 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.convertArray2String(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; -- cgit v1.2.3 From 83a82852057e48ef595057987b057496d5b562c6 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 2 Nov 2012 18:32:24 +0700 Subject: getReplies isReadonly --- src/com/juick/server/MessagesQueries.java | 58 ++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index abcdd285..f7625a19 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -53,6 +53,26 @@ public class MessagesQueries { return privacy >= 0 || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) || (privacy == -2 && uid == owner_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; @@ -638,7 +658,9 @@ public class MessagesQueries { msg.MID = rs.getInt(1); msg.User.UID = rs.getInt(2); msg.User.UName = rs.getString(3); - msg.parseTags(rs.getString(4)); + if (rs.getString(4) != null) { + msg.parseTags(rs.getString(4)); + } msg.ReadOnly = rs.getInt(5) == 1; msg.Privacy = rs.getInt(6); msg.Text = rs.getString(7); @@ -665,4 +687,38 @@ public class MessagesQueries { 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,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.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.Text = rs.getString(5); + msg.TimeAgo = rs.getInt(6); + msg.TimestampString = rs.getString(7); + msg.AttachmentType = rs.getString(8); + + replies.add(msg); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return replies; + } } -- cgit v1.2.3 From ac4323e6d01264e9830f952ceacb192795845455 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 4 Nov 2012 22:54:16 +0700 Subject: getMessage improvement --- src/com/juick/server/MessagesQueries.java | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index f7625a19..7b4b800d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -100,7 +100,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT messages.user_id,users.nick,messages.ts,messages.readonly,messages.attach,messages.place_id,messages.lat,messages.lon FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + stmt = sql.prepareStatement("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 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()) { @@ -109,16 +109,19 @@ public class MessagesQueries { msg.User = new com.juick.User(); msg.User.UID = rs.getInt(1); msg.User.UName = rs.getString(2); - msg.TimestampString = rs.getString(3); - msg.ReadOnly = rs.getBoolean(4); - msg.AttachmentType = rs.getString(5); - if (rs.getInt(6) > 0) { - msg.Place = PlacesQueries.getPlace(sql, rs.getInt(6)); + msg.TimeAgo = rs.getInt(3); + msg.TimestampString = rs.getString(4); + msg.ReadOnly = rs.getBoolean(5); + msg.Privacy = rs.getInt(6); + 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(7) != 0) { + if (rs.getDouble(10) != 0) { msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(7); - msg.Place.lon = rs.getDouble(8); + msg.Place.lat = rs.getDouble(10); + msg.Place.lon = rs.getDouble(11); } } } catch (SQLException e) { @@ -132,12 +135,15 @@ public class MessagesQueries { } try { - stmt = sql.prepareStatement("SELECT messages_txt.tags,messages_txt.txt FROM messages_txt WHERE message_id=?"); + stmt = sql.prepareStatement("SELECT tags,repliesby,txt FROM messages_txt WHERE message_id=?"); stmt.setInt(1, mid); rs = stmt.executeQuery(); if (rs.first()) { - msg.parseTags(rs.getString(1)); - msg.Text = rs.getString(2); + 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); -- cgit v1.2.3 From 55b0d951675383f1973c334608c9d6b9e04b9b04 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 11 Nov 2012 14:58:24 +0700 Subject: hasAccessToMessage --- src/com/juick/server/MessagesQueries.java | 154 +++++++++++++++++------------- 1 file changed, 89 insertions(+), 65 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 7b4b800d..90c90ceb 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -28,12 +28,12 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class MessagesQueries { - + public static boolean canViewThread(Connection sql, int mid, int uid) { - + int privacy = 0; int owner_uid = 0; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -49,13 +49,37 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - - return privacy >= 0 || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) || (privacy == -2 && uid == owner_uid); + + return privacy >= 0 + || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) + || (privacy == -2 && (uid == owner_uid || hasAccessToMessage(sql, mid, uid))); } - + + public static boolean hasAccessToMessage(Connection sql, int mid, int uid) { + boolean ret = false; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT 1 FROM messages_access WHERE message_id=? AND user_id=?"); + stmt.setInt(1, mid); + stmt.setInt(2, 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 boolean isReadOnly(Connection sql, int mid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -72,10 +96,10 @@ public class MessagesQueries { } return ret; } - + public static boolean isSubscribed(Connection sql, int uid, int mid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -93,10 +117,10 @@ public class MessagesQueries { } return ret; } - + public static com.juick.Message getMessage(Connection sql, int mid) { com.juick.Message msg = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -129,11 +153,11 @@ public class MessagesQueries { } 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); @@ -150,13 +174,13 @@ public class MessagesQueries { } 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 { @@ -181,13 +205,13 @@ public class MessagesQueries { } 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 { @@ -206,10 +230,10 @@ public class MessagesQueries { } return user; } - + public static ArrayList getMessageRecommendations(Connection sql, int mid) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -225,13 +249,13 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return users; } - + public static ArrayList getAll(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -253,10 +277,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getTag(Connection sql, int tid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -280,7 +304,7 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getMyFeed(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); PreparedStatement stmt = null; @@ -310,10 +334,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getPrivate(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -340,10 +364,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getDiscussions(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -365,13 +389,13 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return mids; } - + public static ArrayList getRecommended(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -393,14 +417,14 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - - + + return mids; } - + public static ArrayList getPopular(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -422,10 +446,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getPhotos(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -447,10 +471,10 @@ public class MessagesQueries { } 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 { @@ -472,7 +496,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { @@ -488,13 +512,13 @@ public class MessagesQueries { Utils.finishSQL(rs, stmt); } } - + return mids; } - + public static ArrayList getUserBlog(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -518,10 +542,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserTag(Connection sql, int UID, int TID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -547,10 +571,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserRecommendations(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -574,10 +598,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserPhotos(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -601,10 +625,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int before) { ArrayList mids0 = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -628,7 +652,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { @@ -644,13 +668,13 @@ public class MessagesQueries { 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 { @@ -660,7 +684,7 @@ public class MessagesQueries { 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); @@ -682,7 +706,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(15); msg.Place.lon = rs.getDouble(16); } - + msgs.add(msg); } } catch (SQLException e) { @@ -690,13 +714,13 @@ public class MessagesQueries { } 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 { @@ -716,7 +740,7 @@ public class MessagesQueries { msg.TimeAgo = rs.getInt(6); msg.TimestampString = rs.getString(7); msg.AttachmentType = rs.getString(8); - + replies.add(msg); } } catch (SQLException e) { @@ -724,7 +748,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return replies; } } -- cgit v1.2.3 From 9010502f7e50688b72300b60646eac5b89a80d9e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 16 Dec 2012 12:00:31 +0700 Subject: messages privacy fix --- src/com/juick/server/MessagesQueries.java | 139 +++++++++++++++--------------- 1 file changed, 70 insertions(+), 69 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 90c90ceb..c17b9d3d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -28,12 +28,12 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class MessagesQueries { - + public static boolean canViewThread(Connection sql, int mid, int uid) { - + int privacy = 0; int owner_uid = 0; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -49,15 +49,16 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return privacy >= 0 - || (privacy == -1 && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) - || (privacy == -2 && (uid == owner_uid || hasAccessToMessage(sql, mid, uid))); + || uid == owner_uid + || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) + || (privacy == -3 && uid > 0 && hasAccessToMessage(sql, mid, uid)); } - + public static boolean hasAccessToMessage(Connection sql, int mid, int uid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -73,13 +74,13 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return ret; } - + public static boolean isReadOnly(Connection sql, int mid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -96,10 +97,10 @@ public class MessagesQueries { } return ret; } - + public static boolean isSubscribed(Connection sql, int uid, int mid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -117,10 +118,10 @@ public class MessagesQueries { } return ret; } - + public static com.juick.Message getMessage(Connection sql, int mid) { com.juick.Message msg = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -153,11 +154,11 @@ public class MessagesQueries { } 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); @@ -174,13 +175,13 @@ public class MessagesQueries { } 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 { @@ -205,13 +206,13 @@ public class MessagesQueries { } 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 { @@ -230,10 +231,10 @@ public class MessagesQueries { } return user; } - + public static ArrayList getMessageRecommendations(Connection sql, int mid) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -249,13 +250,13 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return users; } - + public static ArrayList getAll(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -277,10 +278,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getTag(Connection sql, int tid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -304,7 +305,7 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getMyFeed(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); PreparedStatement stmt = null; @@ -334,10 +335,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getPrivate(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -364,10 +365,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getDiscussions(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -389,13 +390,13 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return mids; } - + public static ArrayList getRecommended(Connection sql, int uid, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -417,14 +418,14 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - - + + return mids; } - + public static ArrayList getPopular(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -446,10 +447,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getPhotos(Connection sql, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -471,10 +472,10 @@ public class MessagesQueries { } 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 { @@ -496,7 +497,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { @@ -512,13 +513,13 @@ public class MessagesQueries { Utils.finishSQL(rs, stmt); } } - + return mids; } - + public static ArrayList getUserBlog(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -542,10 +543,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserTag(Connection sql, int UID, int TID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -571,10 +572,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserRecommendations(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -598,10 +599,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserPhotos(Connection sql, int UID, int before) { ArrayList mids = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -625,10 +626,10 @@ public class MessagesQueries { } return mids; } - + public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int before) { ArrayList mids0 = new ArrayList(20); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -652,7 +653,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { @@ -668,13 +669,13 @@ public class MessagesQueries { 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 { @@ -684,7 +685,7 @@ public class MessagesQueries { 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); @@ -706,7 +707,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(15); msg.Place.lon = rs.getDouble(16); } - + msgs.add(msg); } } catch (SQLException e) { @@ -714,13 +715,13 @@ public class MessagesQueries { } 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 { @@ -740,7 +741,7 @@ public class MessagesQueries { msg.TimeAgo = rs.getInt(6); msg.TimestampString = rs.getString(7); msg.AttachmentType = rs.getString(8); - + replies.add(msg); } } catch (SQLException e) { @@ -748,7 +749,7 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } - + return replies; } } -- cgit v1.2.3 From 10f1681919c312597a71be2597626daa4d3739be Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 26 Dec 2012 17:48:00 +0700 Subject: MessagesQueries.getPlace --- src/com/juick/server/MessagesQueries.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index c17b9d3d..b2251048 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -306,6 +306,33 @@ public class MessagesQueries { return mids; } + public static ArrayList getPlace(Connection sql, int place_id, 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 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + stmt.setInt(2, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE place_id=? AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, place_id); + } + 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(20); PreparedStatement stmt = null; -- cgit v1.2.3 From b74e6c267498e6fda56a5167346623bf55a8f01e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 31 Dec 2012 16:34:51 +0700 Subject: MessagesQueries privacy --- src/com/juick/server/MessagesQueries.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index b2251048..227b3fea 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -544,18 +544,18 @@ public class MessagesQueries { return mids; } - public static ArrayList getUserBlog(Connection sql, int UID, int before) { + 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=0 ORDER BY message_id DESC LIMIT 20"); + 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>=0 ORDER BY message_id DESC LIMIT 20"); + 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(); @@ -571,19 +571,19 @@ public class MessagesQueries { return mids; } - public static ArrayList getUserTag(Connection sql, int UID, int TID, int before) { + 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_id0 ORDER BY messages.message_id DESC LIMIT 20"); + 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>0 ORDER BY messages.message_id DESC LIMIT 20"); + 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); } @@ -627,18 +627,18 @@ public class MessagesQueries { return mids; } - public static ArrayList getUserPhotos(Connection sql, int UID, int before) { + public static ArrayList 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=0 AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + 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>=0 AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20"); + 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(); @@ -654,7 +654,7 @@ public class MessagesQueries { return mids; } - public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int before) { + public static ArrayList getUserSearch(Connection sql, Connection sqlSearch, int UID, String search, int privacy, int before) { ArrayList mids0 = new ArrayList(20); PreparedStatement stmt = null; @@ -684,7 +684,7 @@ public class MessagesQueries { ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>=0 ORDER BY message_id DESC"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { -- cgit v1.2.3 From 917e44c66ee99f31b0ac655c56b598d71014652e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 6 Mar 2013 18:25:50 +0700 Subject: MessagesQueries.getMessageTagsIDs MessagesQueries visitor_id UserQueries.createUser --- src/com/juick/server/MessagesQueries.java | 68 ++++++++++++++++++++++--------- src/com/juick/server/UserQueries.java | 24 +++++++++++ 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 227b3fea..ad00b9d2 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -232,6 +232,28 @@ public class MessagesQueries { return user; } + 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(); @@ -254,17 +276,19 @@ public class MessagesQueries { return users; } - public static ArrayList getAll(Connection sql, int before) { + public static ArrayList getAll(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 messages.message_id FROM messages WHERE messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); + stmt.setInt(2, visitor_uid); } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages WHERE messages.privacy>0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -279,19 +303,21 @@ public class MessagesQueries { return mids; } - public static ArrayList getTag(Connection sql, int tid, int before) { + public static ArrayList getTag(Connection sql, int tid, int visitor_uid, 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_tags.tag_id=? AND messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT 20"); stmt.setInt(1, tid); stmt.setInt(2, before); + stmt.setInt(3, visitor_uid); } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND messages.privacy>0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT 20"); stmt.setInt(1, tid); + stmt.setInt(2, visitor_uid); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -306,19 +332,21 @@ public class MessagesQueries { return mids; } - public static ArrayList getPlace(Connection sql, int place_id, int before) { + 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 ORDER BY message_id DESC LIMIT 20"); + 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 ORDER BY message_id DESC LIMIT 20"); + 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(); @@ -339,13 +367,13 @@ public class MessagesQueries { 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=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); stmt.setInt(2, before); stmt.setInt(3, uid); stmt.setInt(4, 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=-1 AND messages.user_id IN (SELECT user_id FROM wl_users WHERE wl_user_id=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); stmt.setInt(2, uid); stmt.setInt(3, uid); @@ -475,17 +503,19 @@ public class MessagesQueries { return mids; } - public static ArrayList getPhotos(Connection sql, int before) { + 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 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 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 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 ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, visitor_uid); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -507,11 +537,11 @@ public class MessagesQueries { 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.convertArray2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { @@ -578,12 +608,12 @@ public class MessagesQueries { 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 = 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 = 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); } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index ff1d0e24..17873676 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -21,6 +21,7 @@ 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; @@ -32,6 +33,29 @@ public class UserQueries { static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + public static int createUser(Connection sql, String username, String password, String lang) { + int uid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO users(nick,passw,lang) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, username); + stmt.setString(2, password); + stmt.setString(3, lang); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + uid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return uid; + } + public static com.juick.User getUserByNick(Connection sql, String username) { com.juick.User user = null; -- cgit v1.2.3 From de81a033905d1c4cd18ac9b5af8a7f8fb853b121 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 14 Apr 2013 06:44:35 +0700 Subject: PushQueries --- src/com/juick/server/MessagesQueries.java | 4 +- src/com/juick/server/PushQueries.java | 61 +++++++++++++++++++++++++++++++ src/com/juick/server/UserQueries.java | 12 ++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 src/com/juick/server/PushQueries.java diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index ad00b9d2..42b45c24 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -283,11 +283,11 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND lang!='fa' 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=?) ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND lang!='fa' ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, visitor_uid); } rs = stmt.executeQuery(); diff --git a/src/com/juick/server/PushQueries.java b/src/com/juick/server/PushQueries.java new file mode 100644 index 00000000..a532622a --- /dev/null +++ b/src/com/juick/server/PushQueries.java @@ -0,0 +1,61 @@ +/* + * 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 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 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/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 17873676..a9f00f94 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -53,6 +53,18 @@ public class UserQueries { } finally { Utils.finishSQL(rs, stmt); } + + PreparedStatement stmt2 = null; + try { + stmt2 = sql.prepareStatement("INSERT INTO useroptions(user_id) VALUES (?)"); + stmt2.setInt(1, uid); + stmt2.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return uid; } -- cgit v1.2.3 From a1ecda62eef3b802dbfa044f75ec0050de6ba75e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 20 Jun 2013 18:22:12 +0700 Subject: SQLHelpers.getArrayInteger Users.getUserRead Users.getUserReaders --- src/com/juick/server/MessagesQueries.java | 8 +++---- src/com/juick/server/SQLHelpers.java | 40 +++++++++++++++++++++++++++++++ src/com/juick/server/UserQueries.java | 10 +++++++- 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/com/juick/server/SQLHelpers.java diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 42b45c24..55fc3898 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -283,11 +283,11 @@ 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 lang!='fa' ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND lang='ru' 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 lang!='fa' ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND lang='ru' ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, visitor_uid); } rs = stmt.executeQuery(); @@ -367,13 +367,13 @@ public class MessagesQueries { 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=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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=?))) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); stmt.setInt(2, before); stmt.setInt(3, uid); stmt.setInt(4, 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=?)) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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=?))) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, uid); stmt.setInt(2, uid); stmt.setInt(3, uid); diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java new file mode 100644 index 00000000..dd83a915 --- /dev/null +++ b/src/com/juick/server/SQLHelpers.java @@ -0,0 +1,40 @@ +/* + * 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 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/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index a9f00f94..652809d6 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -64,7 +64,7 @@ public class UserQueries { } finally { Utils.finishSQL(null, stmt); } - + return uid; } @@ -358,6 +358,14 @@ public class UserQueries { 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 getUserReaders(Connection sql, int uid) { + return SQLHelpers.getArrayInteger(sql, "SELECT suser_id FROM subscr_users WHERE user_id=?", uid); + } + public static int getStatsIRead(Connection sql, int uid) { int ret = 0; PreparedStatement stmt = null; -- cgit v1.2.3 From 505feda055a1f5c59aea53f26680bbbbb0553a0a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 20 Jun 2013 18:26:34 +0700 Subject: SQLHelpers.getInt --- src/com/juick/server/SQLHelpers.java | 19 ++++++++++ src/com/juick/server/UserQueries.java | 68 +++-------------------------------- 2 files changed, 23 insertions(+), 64 deletions(-) diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index dd83a915..7879c244 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -16,6 +16,25 @@ import java.util.ArrayList; */ public class SQLHelpers { + 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 ArrayList getArrayInteger(Connection sql, String query, int param) { ArrayList ret = new ArrayList(); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 652809d6..e44017fa 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -367,78 +367,18 @@ public class UserQueries { } public static int getStatsIRead(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users WHERE suser_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; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", uid, 0); } public static int getStatsMyReaders(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM subscr_users 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; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM subscr_users WHERE user_id=?", uid, 0); } public static int getStatsMessages(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM messages 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; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM messages WHERE user_id=?", uid, 0); } public static int getStatsReplies(Connection sql, int uid) { - int ret = 0; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT COUNT(*) FROM replies 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; + return SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM replies WHERE user_id=?", uid, 0); } } -- cgit v1.2.3 From 781648961858a5180098954fc32463c32c88eaa6 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 20 Jun 2013 18:30:54 +0700 Subject: User.getUsersByID --- src/com/juick/server/UserQueries.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index e44017fa..d6f6045c 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -90,6 +90,30 @@ public class UserQueries { return user; } + 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.convertArray2String(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 String getJIDbyUID(Connection sql, int UID) { String jid = null; -- cgit v1.2.3 From 1afa0e255d694840c77c19c1effed6c0d0a92f1f Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 21 Jun 2013 00:48:01 +0700 Subject: SQLHelpers --- src/com/juick/server/SQLHelpers.java | 57 ++++++++++++++++++++++++++++++++ src/com/juick/server/UserQueries.java | 62 +++++++---------------------------- 2 files changed, 69 insertions(+), 50 deletions(-) diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index 7879c244..dd3c4bc3 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -35,6 +35,63 @@ public class SQLHelpers { 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(); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index d6f6045c..f0c5a57f 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -114,45 +114,16 @@ public class UserQueries { return users; } - public static String getJIDbyUID(Connection sql, int UID) { - String jid = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT jid FROM jids WHERE user_id=? AND active=1"); - stmt.setInt(1, UID); - rs = stmt.executeQuery(); - if (rs.first()) { - jid = rs.getString(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } + public static String getJIDbyUID(Connection sql, int uid) { + return SQLHelpers.getString(sql, "SELECT jid FROM jids WHERE user_id=? AND active=1", uid); + } - return jid; + public static int getUIDbyJID(Connection sql, String jid) { + return SQLHelpers.getInt(sql, "SELECT user_id FROM jids WHERE jid=?", jid, 0); } public static int getUIDbyHash(Connection sql, String hash) { - int UID = 0; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT user_id FROM logins WHERE hash=?"); - stmt.setString(1, hash); - rs = stmt.executeQuery(); - if (rs.first()) { - UID = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return UID; + return SQLHelpers.getInt(sql, "SELECT user_id FROM logins WHERE hash=?", hash, 0); } public static com.juick.User getUserByHash(Connection sql, String hash) { @@ -178,24 +149,11 @@ public class UserQueries { } public static String getHashByUID(Connection sql, int uid) { - String hash = null; - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT hash FROM logins WHERE user_id=?"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - hash = rs.getString(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } + 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); @@ -220,6 +178,10 @@ public class UserQueries { return sb.toString(); } + public static boolean checkUserNameValid(String uname) { + return uname != null && uname.length() > 1 && 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; -- cgit v1.2.3 From 378b014b82158d06a6ee986cb7522a04bc59f5bc Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 16 Jul 2013 04:07:15 +0700 Subject: getUsersByName, getUsersByJID --- src/com/juick/server/MessagesQueries.java | 6 ++-- src/com/juick/server/UserQueries.java | 53 +++++++++++++++++++++++++++++-- src/com/juick/server/Utils.java | 13 +++++++- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 55fc3898..7eaaff2e 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -558,7 +558,7 @@ public class MessagesQueries { ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(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 ORDER BY message_id DESC LIMIT 20"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { @@ -714,7 +714,7 @@ public class MessagesQueries { ArrayList mids = new ArrayList(20); if (mids0.size() > 0) { try { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id IN (" + Utils.convertArray2String(mids0) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC"); + 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()) { @@ -736,7 +736,7 @@ public class MessagesQueries { 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()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id IN (" + Utils.convertArray2String(mids) + ") ORDER BY messages.message_id DESC"); + 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()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id IN (" + Utils.convertArrayInt2String(mids) + ") ORDER BY messages.message_id DESC"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index f0c5a57f..f1b235af 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -68,7 +68,7 @@ public class UserQueries { return uid; } - public static com.juick.User getUserByNick(Connection sql, String username) { + public static com.juick.User getUserByName(Connection sql, String username) { com.juick.User user = null; PreparedStatement stmt = null; @@ -90,13 +90,36 @@ public class UserQueries { 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.convertArray2String(uids) + ")"); + stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE id IN (" + Utils.convertArrayInt2String(uids) + ")"); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { @@ -114,6 +137,30 @@ public class UserQueries { return users; } + 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); } @@ -307,7 +354,7 @@ public class UserQueries { 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.convertArray2String(uids) + ")"); + 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(); diff --git a/src/com/juick/server/Utils.java b/src/com/juick/server/Utils.java index 997c4f32..d37931d0 100644 --- a/src/com/juick/server/Utils.java +++ b/src/com/juick/server/Utils.java @@ -28,7 +28,7 @@ import java.util.ArrayList; */ public class Utils { - public static String convertArray2String(ArrayList mids) { + public static String convertArrayInt2String(ArrayList mids) { String q = ""; for (int i = 0; i < mids.size(); i++) { if (i > 0) { @@ -39,6 +39,17 @@ public class Utils { 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 void finishSQL(ResultSet rs, Statement stmt) { if (rs != null) { try { -- cgit v1.2.3 From 9fa3da4667af7a93da5826f3eaa00d5384971179 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 16 Jul 2013 04:07:30 +0700 Subject: GroupQueries --- src/com/juick/server/GroupsQueries.java | 149 ++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/com/juick/server/GroupsQueries.java diff --git a/src/com/juick/server/GroupsQueries.java b/src/com/juick/server/GroupsQueries.java new file mode 100644 index 00000000..45a4942d --- /dev/null +++ b/src/com/juick/server/GroupsQueries.java @@ -0,0 +1,149 @@ +/* + * 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 com.juick.Group; +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 GroupsQueries { + + public static ArrayList getGroups(Connection sql, int uid) { + ArrayList groups = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT groups.group_id,groups.name,COUNT(groups_users.user_id) FROM groups LEFT JOIN groups_users USING(group_id) WHERE groups.user_id=? GROUP BY group_id"); + stmt.setInt(1, uid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Group g = new com.juick.Group(); + g.GID = rs.getInt(1); + g.Name = rs.getString(2); + g.UsersCnt = rs.getInt(3); + groups.add(g); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return groups; + } + + public static int getGroupID(Connection sql, int uid, String name) { + int gid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT group_id FROM groups WHERE user_id=? AND name=?"); + stmt.setInt(1, uid); + stmt.setString(2, name); + rs = stmt.executeQuery(); + if (rs.first()) { + gid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (gid == 0) { + try { + stmt = sql.prepareStatement("INSERT INTO groups(user_id,name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, name); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + gid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + } + + return gid; + } + + public static boolean removeGroupUser(Connection sql, int gid, int uid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("DELETE FROM groups_users WHERE group_id=? AND user_id=?"); + stmt.setInt(1, gid); + stmt.setInt(2, uid); + if (stmt.executeUpdate() > 0) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ret) { + int cnt = SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM groups_users WHERE group_id=?", gid, -1); + if (cnt == 0) { + try { + stmt = sql.prepareStatement("DELETE FROM groups WHERE group_id=?"); + stmt.setInt(1, gid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + } + + return ret; + } + + public static boolean addGroupUser(Connection sql, int gid, int uid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO groups_users(group_id,user_id) VALUES (?,?)"); + stmt.setInt(1, gid); + stmt.setInt(2, uid); + if (stmt.executeUpdate() > 0) { + ret = true; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return ret; + } +} -- cgit v1.2.3 From b7887266662d95bd70ad00f046d9205f9edc4306 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 21 Jul 2013 23:33:58 +0700 Subject: CrosspostQueries --- src/com/juick/server/CrosspostQueries.java | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/com/juick/server/CrosspostQueries.java diff --git a/src/com/juick/server/CrosspostQueries.java b/src/com/juick/server/CrosspostQueries.java new file mode 100644 index 00000000..09d951c1 --- /dev/null +++ b/src/com/juick/server/CrosspostQueries.java @@ -0,0 +1,57 @@ +/* + * 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=?"); + 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); + } +} -- cgit v1.2.3 From 9b8dbec964ec345f1015eb49a7dedf7803c93164 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 21 Jul 2013 23:34:23 +0700 Subject: Subscribe to @juick when creating a new user --- src/com/juick/server/UserQueries.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index f1b235af..c170b6cd 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -65,6 +65,17 @@ public class UserQueries { Utils.finishSQL(null, stmt); } + PreparedStatement stmt3 = null; + try { + stmt3 = sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)"); + stmt3.setInt(1, uid); + stmt3.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + return uid; } -- cgit v1.2.3 From b944a64c6b54b9819542ab2d43d85114ef2908e1 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 24 Aug 2013 15:07:51 +0700 Subject: PMQueries; UserQueries.getUserByUID; UserQueries.fillUsersByID --- src/com/juick/server/MessagesQueries.java | 4 + src/com/juick/server/PMQueries.java | 171 ++++++++++++++++++++++++++++++ src/com/juick/server/UserQueries.java | 62 +++++++++++ 3 files changed, 237 insertions(+) create mode 100644 src/com/juick/server/PMQueries.java diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 7eaaff2e..444cd13d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -119,6 +119,10 @@ public class MessagesQueries { 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; diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java new file mode 100644 index 00000000..42f3aefd --- /dev/null +++ b/src/com/juick/server/PMQueries.java @@ -0,0 +1,171 @@ +/* + * 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 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=? 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, stmt); + } + + return msgs; + } +} diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index c170b6cd..bb4d19df 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -79,6 +79,28 @@ public class UserQueries { 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 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); + } + } 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; @@ -148,6 +170,42 @@ public class UserQueries { 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(); @@ -180,6 +238,10 @@ public class UserQueries { 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); } -- cgit v1.2.3 From 102925eebf91f45f02824c597c8ec393017aa1cb Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 30 Aug 2013 13:50:25 +0700 Subject: PMQueries.inroster; UserQueries.getUserByJID --- src/com/juick/server/PMQueries.java | 74 +++++++++++++++++++++++++++++++++++ src/com/juick/server/PushQueries.java | 9 ++++- src/com/juick/server/UserQueries.java | 23 +++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index 42f3aefd..91ebbee2 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -61,6 +61,80 @@ public class PMQueries { 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(); diff --git a/src/com/juick/server/PushQueries.java b/src/com/juick/server/PushQueries.java index a532622a..84f42722 100644 --- a/src/com/juick/server/PushQueries.java +++ b/src/com/juick/server/PushQueries.java @@ -16,6 +16,10 @@ import java.util.ArrayList; */ 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(); @@ -37,6 +41,10 @@ public class PushQueries { 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(); @@ -57,5 +65,4 @@ public class PushQueries { } return urls; } - } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index bb4d19df..e56accdb 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -123,6 +123,29 @@ public class UserQueries { 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(); -- cgit v1.2.3 From f5c7fe7a3f295f9b49bf19057b07b82d3e475223 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 17 Oct 2013 20:21:02 +0700 Subject: Messages.getAll hack for uid=1 --- src/com/juick/server/MessagesQueries.java | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 444cd13d..5e3cc238 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -286,13 +286,25 @@ public class MessagesQueries { 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 lang='ru' ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, before); - stmt.setInt(2, visitor_uid); + if (visitor_uid > 1) { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 OR user_id=?) AND lang='ru' 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 lang='ru' 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 { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) AND lang='ru' ORDER BY message_id DESC LIMIT 20"); - stmt.setInt(1, visitor_uid); + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND lang='ru' ORDER BY message_id DESC LIMIT 20"); + stmt.setInt(1, before); + } else { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND lang='ru' ORDER BY message_id DESC LIMIT 20"); + } + } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From 4757bfefd416ba905a8a1df73c570e3e700aeefb Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 17 Oct 2013 20:23:34 +0700 Subject: Messages.getPopular SQL changed --- src/com/juick/server/MessagesQueries.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 5e3cc238..ede236db 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -501,10 +501,10 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages INNER JOIN favorites ON (favorites.user_id=11574 AND favorites.message_id=messages.message_id) WHERE messages.message_id0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND popular=1 ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages INNER JOIN favorites ON (favorites.user_id=11574 AND favorites.message_id=messages.message_id) WHERE messages.privacy>0 ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND popular=1 ORDER BY message_id DESC LIMIT 20"); } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From cca09ef6bc8db13ee3a3fae45d1f47da40894c5a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 24 Oct 2013 16:39:31 +0700 Subject: MessagesQueries.getTag cnt --- src/com/juick/server/MessagesQueries.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index ede236db..613b09f1 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -319,21 +319,23 @@ public class MessagesQueries { return mids; } - public static ArrayList getTag(Connection sql, int tid, int visitor_uid, int before) { + 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=? AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); stmt.setInt(1, tid); stmt.setInt(2, before); stmt.setInt(3, visitor_uid); + stmt.setInt(4, cnt); } else { - stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT messages.message_id FROM messages_tags INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id=? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); stmt.setInt(1, tid); stmt.setInt(2, visitor_uid); + stmt.setInt(3, cnt); } rs = stmt.executeQuery(); rs.beforeFirst(); -- cgit v1.2.3 From 76e1829a199d0deb9c582951a9c7a09170a005f7 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 31 Oct 2013 14:04:36 +0700 Subject: AdsQueries --- src/com/juick/server/AdsQueries.java | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/com/juick/server/AdsQueries.java diff --git a/src/com/juick/server/AdsQueries.java b/src/com/juick/server/AdsQueries.java new file mode 100644 index 00000000..f1b37fb1 --- /dev/null +++ b/src/com/juick/server/AdsQueries.java @@ -0,0 +1,47 @@ +/* + * 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) { + 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); + } + + 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); + } + } +} -- cgit v1.2.3 From 73407176f75e66bd47f08046496e0a301217a010 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 31 Oct 2013 14:05:00 +0700 Subject: SQLHelpers.executeInt --- src/com/juick/server/PMQueries.java | 2 +- src/com/juick/server/SQLHelpers.java | 15 +++++++++++++++ src/com/juick/server/TagQueries.java | 19 +++---------------- src/com/juick/server/UserQueries.java | 23 ++--------------------- 4 files changed, 21 insertions(+), 38 deletions(-) diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index 91ebbee2..6399802f 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -237,7 +237,7 @@ public class PMQueries { } catch (SQLException e) { System.err.println(e); } finally { - Utils.finishSQL(null, stmt); + Utils.finishSQL(null, stmt2); } return msgs; diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index dd3c4bc3..479fd1cc 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -16,6 +16,21 @@ import java.util.ArrayList; */ public class SQLHelpers { + 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 param, int defvalue) { int ret = defvalue; PreparedStatement stmt = null; diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 7d78e345..e811caf4 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -31,24 +31,11 @@ import java.util.ArrayList; public class TagQueries { public static int getTagID(Connection sql, String tag, boolean autoCreate) { - int tid = 0; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT tag_id FROM tags WHERE name=?"); - stmt.setString(1, tag); - rs = stmt.executeQuery(); - if (rs.first()) { - tid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } + int tid = SQLHelpers.getInt(sql, "SELECT tag_id FROM tags WHERE name=?", tag, 0); if (tid == 0 && autoCreate) { + PreparedStatement stmt = null; + ResultSet rs = null; try { stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, tag); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index e56accdb..f127cbca 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -54,27 +54,8 @@ public class UserQueries { Utils.finishSQL(rs, stmt); } - PreparedStatement stmt2 = null; - try { - stmt2 = sql.prepareStatement("INSERT INTO useroptions(user_id) VALUES (?)"); - stmt2.setInt(1, uid); - stmt2.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - PreparedStatement stmt3 = null; - try { - stmt3 = sql.prepareStatement("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)"); - stmt3.setInt(1, uid); - stmt3.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, 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; } -- cgit v1.2.3 From dbf04c83e5b5aaef220e543554c66e74680ba62e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 31 Oct 2013 14:06:21 +0700 Subject: -Groups, -Chats, -Places --- src/com/juick/server/ChatQueries.java | 86 ----------------- src/com/juick/server/GroupsQueries.java | 149 ------------------------------ src/com/juick/server/MessagesQueries.java | 8 +- src/com/juick/server/PlacesQueries.java | 55 ----------- 4 files changed, 4 insertions(+), 294 deletions(-) delete mode 100644 src/com/juick/server/ChatQueries.java delete mode 100644 src/com/juick/server/GroupsQueries.java delete mode 100644 src/com/juick/server/PlacesQueries.java diff --git a/src/com/juick/server/ChatQueries.java b/src/com/juick/server/ChatQueries.java deleted file mode 100644 index 19f95637..00000000 --- a/src/com/juick/server/ChatQueries.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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.SQLException; - -/** - * - * @author Ugnich Anton - */ -public class ChatQueries { - - public static boolean subscribeTag(Connection sql, int uid, int tagid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO chat_subscr_tags(tag_id,user_id) VALUES (?,?)"); - stmt.setInt(1, tagid); - stmt.setInt(2, uid); - if (stmt.executeUpdate() > 0) { - ret = true; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - return ret; - } - - public static boolean unsubscribeTag(Connection sql, int uid, int tagid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("DELETE FROM chat_subscr_tags WHERE tag_id=? AND user_id=?"); - stmt.setInt(1, tagid); - stmt.setInt(2, uid); - if (stmt.executeUpdate() > 0) { - ret = true; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - return ret; - } - - public static boolean setSubscriptionsActive(Connection sql, int uid, boolean enabled) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("UPDATE chat_subscr_tags SET active=? WHERE user_id=?"); - stmt.setInt(1, enabled ? 1 : 0); - stmt.setInt(2, uid); - if (stmt.executeUpdate() > 0) { - ret = true; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - return ret; - } -} diff --git a/src/com/juick/server/GroupsQueries.java b/src/com/juick/server/GroupsQueries.java deleted file mode 100644 index 45a4942d..00000000 --- a/src/com/juick/server/GroupsQueries.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * 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 com.juick.Group; -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 GroupsQueries { - - public static ArrayList getGroups(Connection sql, int uid) { - ArrayList groups = new ArrayList(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT groups.group_id,groups.name,COUNT(groups_users.user_id) FROM groups LEFT JOIN groups_users USING(group_id) WHERE groups.user_id=? GROUP BY group_id"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Group g = new com.juick.Group(); - g.GID = rs.getInt(1); - g.Name = rs.getString(2); - g.UsersCnt = rs.getInt(3); - groups.add(g); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return groups; - } - - public static int getGroupID(Connection sql, int uid, String name) { - int gid = 0; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT group_id FROM groups WHERE user_id=? AND name=?"); - stmt.setInt(1, uid); - stmt.setString(2, name); - rs = stmt.executeQuery(); - if (rs.first()) { - gid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (gid == 0) { - try { - stmt = sql.prepareStatement("INSERT INTO groups(user_id,name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); - stmt.setString(1, name); - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.first()) { - gid = rs.getInt(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - } - - return gid; - } - - public static boolean removeGroupUser(Connection sql, int gid, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("DELETE FROM groups_users WHERE group_id=? AND user_id=?"); - stmt.setInt(1, gid); - stmt.setInt(2, uid); - if (stmt.executeUpdate() > 0) { - ret = true; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - if (ret) { - int cnt = SQLHelpers.getInt(sql, "SELECT COUNT(*) FROM groups_users WHERE group_id=?", gid, -1); - if (cnt == 0) { - try { - stmt = sql.prepareStatement("DELETE FROM groups WHERE group_id=?"); - stmt.setInt(1, gid); - stmt.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - } - } - - return ret; - } - - public static boolean addGroupUser(Connection sql, int gid, int uid) { - boolean ret = false; - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO groups_users(group_id,user_id) VALUES (?,?)"); - stmt.setInt(1, gid); - stmt.setInt(2, uid); - if (stmt.executeUpdate() > 0) { - ret = true; - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - return ret; - } -} diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 613b09f1..017c3350 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -144,9 +144,9 @@ public class MessagesQueries { msg.Privacy = rs.getInt(6); 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.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); @@ -319,7 +319,7 @@ public class MessagesQueries { return mids; } - public static ArrayList getTag(Connection sql, int tid, int visitor_uid, int before,int cnt) { + public static ArrayList getTag(Connection sql, int tid, int visitor_uid, int before, int cnt) { ArrayList mids = new ArrayList(20); PreparedStatement stmt = null; diff --git a/src/com/juick/server/PlacesQueries.java b/src/com/juick/server/PlacesQueries.java deleted file mode 100644 index d61c89c3..00000000 --- a/src/com/juick/server/PlacesQueries.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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; - -/** - * - * @author Ugnich Anton - */ -public class PlacesQueries { - - public static com.juick.Place getPlace(Connection sql, int pid) { - com.juick.Place place = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT lat,lon,name FROM places WHERE place_id=?"); - stmt.setInt(1, pid); - rs = stmt.executeQuery(); - if (rs.first()) { - place = new com.juick.Place(); - place.pid = pid; - place.lat = rs.getDouble(1); - place.lon = rs.getDouble(2); - place.name = rs.getString(3); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - return place; - } -} -- cgit v1.2.3 From 2ddc060cee3e4b7f71819f95b833d802613a51b5 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 16 Nov 2013 03:24:33 +0700 Subject: MessagesQueries.getMyFeed + my own messages --- src/com/juick/server/MessagesQueries.java | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 017c3350..8a3b1268 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -22,6 +22,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; /** * @@ -380,21 +381,19 @@ public class MessagesQueries { } public static ArrayList getMyFeed(Connection sql, int uid, int before) { - ArrayList mids = new ArrayList(20); + 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=?))) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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); - stmt.setInt(4, 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=?))) AND message_id NOT IN (SELECT message_id FROM messages_tags WHERE tag_id IN (SELECT tag_id FROM bl_tags WHERE user_id=?)) ORDER BY message_id DESC LIMIT 20"); + 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); - stmt.setInt(3, uid); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -406,6 +405,33 @@ public class MessagesQueries { } finally { Utils.finishSQL(rs, stmt); } + + try { + if (before > 0) { + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE user_id=? AND message_id 0) { - stmt = sqlSearch.prepareStatement("SELECT @id AS message_id FROM messages WHERE MATCH(?) AND @id getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { + ArrayList jids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT jid FROM subscr_messages WHERE message_id=? AND active=b'1' AND suser_id!=? AND jid!=''"); + 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; + } +} -- cgit v1.2.3 From c0ccfe940d00f4eeb888c5f721e8a63c415daeb3 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 17 Nov 2013 02:13:22 +0700 Subject: SubscriptionsQueries.getJIDSubscribedToUser --- src/com/juick/server/SubscriptionsQueries.java | 29 +++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java index d7b7fbc3..9f9468cd 100644 --- a/src/com/juick/server/SubscriptionsQueries.java +++ b/src/com/juick/server/SubscriptionsQueries.java @@ -16,13 +16,40 @@ import java.util.ArrayList; */ 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 getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { ArrayList jids = new ArrayList(); PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT jid FROM subscr_messages WHERE message_id=? AND active=b'1' AND suser_id!=? AND jid!=''"); + 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(); -- cgit v1.2.3 From 236876e168fd865230fb94c53be4cac4ada6e46b Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 19 Nov 2013 02:46:34 +0700 Subject: MessagesQueries: -places, *search sql --- src/com/juick/server/MessagesQueries.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 8a3b1268..1aeb45f8 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -735,12 +735,12 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sqlSearch.prepareStatement("SELECT @id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND @id 0) { + if (rs.getDouble(13) != 0) { msg.Place = new com.juick.Place(); - msg.Place.pid = rs.getInt(13); - msg.Place.name = rs.getString(14); - msg.Place.lat = rs.getDouble(15); - msg.Place.lon = rs.getDouble(16); + msg.Place.lat = rs.getDouble(13); + msg.Place.lon = rs.getDouble(14); } msgs.add(msg); -- cgit v1.2.3 From e59500659d5068557337e94dbf8d0955f08c6869 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 19 Nov 2013 04:40:43 +0700 Subject: DATE_FORMAT --- src/com/juick/server/MessagesQueries.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 1aeb45f8..2de875d4 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -130,7 +130,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("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 FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + 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 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()) { @@ -190,7 +190,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("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=?"); + 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(); @@ -780,7 +780,7 @@ public class MessagesQueries { 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()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.lat,messages.lon 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"); + 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 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()) { @@ -824,7 +824,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,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 = sql.prepareStatement("SELECT replies.reply_id,replies.replyto,replies.user_id,users.nick,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(); -- cgit v1.2.3 From 4844ef8ea55d8b88619af8400df1bbf690842f7f Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 20 Nov 2013 04:14:35 +0700 Subject: UserQueries.isInBLAny; PMQueries lastmessage time fix --- src/com/juick/server/PMQueries.java | 2 +- src/com/juick/server/UserQueries.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index 6399802f..b0c2911a 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -159,7 +159,7 @@ public class PMQueries { if (users.size() < cnt) { try { - stmt = sql.prepareStatement("SELECT user_id,user_id_to FROM pm_streams WHERE user_id=? OR user_id_to=? ORDER BY lastmessage DESC LIMIT ?"); + 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); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index f127cbca..b1198e4d 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -425,6 +425,29 @@ public class UserQueries { 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(); -- cgit v1.2.3 From 5ec828b377b18d6cf56643796de8fdd44e8f1091 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 26 Nov 2013 15:57:46 +0700 Subject: getSignUpHashByJID --- src/com/juick/server/UserQueries.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index b1198e4d..716f8f0f 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -24,6 +24,7 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Random; +import java.util.UUID; /** * @@ -33,6 +34,27 @@ 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, String lang) { int uid = 0; -- cgit v1.2.3 From 2bc4ccc437bb036de4ac5db23af8a276f1a708d5 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 19 Dec 2013 02:04:18 +0700 Subject: SubscriptionQueries.getJIDSubscribedToUserAndTags --- src/com/juick/server/SubscriptionsQueries.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java index 9f9468cd..ca382fc7 100644 --- a/src/com/juick/server/SubscriptionsQueries.java +++ b/src/com/juick/server/SubscriptionsQueries.java @@ -43,6 +43,28 @@ public class SubscriptionsQueries { return jids; } + public static ArrayList getJIDSubscribedToUserAndTags(Connection sql, int uid, int mid) { + ArrayList jids = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT DISTINCT jid FROM jids WHERE active=1 AND user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=? UNION SELECT suser_id FROM subscr_tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND subscr_tags.tag_id=messages_tags.tag_id))"); + stmt.setInt(1, uid); + stmt.setInt(2, mid); + 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 getJIDSubscribedToComments(Connection sql, int mid, int ignore_uid) { ArrayList jids = new ArrayList(); -- cgit v1.2.3 From dded0cfb7c3b6c356e0d8703c1ebdea24924eb5e Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 19 Dec 2013 02:06:20 +0700 Subject: msg.FriendsOnly --- src/com/juick/server/MessagesQueries.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 2de875d4..64ee3b96 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -143,6 +143,7 @@ public class MessagesQueries { 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) { @@ -795,6 +796,7 @@ public class MessagesQueries { } 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); -- cgit v1.2.3 From a6b3956014a894ff4e8e1d9194c10869e5911e34 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 20 Dec 2013 00:48:41 +0700 Subject: Don't send post to it's author --- src/com/juick/server/SubscriptionsQueries.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java index ca382fc7..20540b93 100644 --- a/src/com/juick/server/SubscriptionsQueries.java +++ b/src/com/juick/server/SubscriptionsQueries.java @@ -49,9 +49,10 @@ public class SubscriptionsQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT DISTINCT jid FROM jids WHERE active=1 AND user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=? UNION SELECT suser_id FROM subscr_tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND subscr_tags.tag_id=messages_tags.tag_id))"); + stmt = sql.prepareStatement("SELECT DISTINCT jid FROM jids WHERE active=1 AND user_id!=? AND user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=? UNION SELECT suser_id FROM subscr_tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND subscr_tags.tag_id=messages_tags.tag_id))"); stmt.setInt(1, uid); - stmt.setInt(2, mid); + stmt.setInt(2, uid); + stmt.setInt(3, mid); rs = stmt.executeQuery(); rs.beforeFirst(); while (rs.next()) { -- cgit v1.2.3 From a6a3f6c811395d148a0b8d571d721443e6f121c2 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 26 Jan 2014 03:23:57 +0700 Subject: getLastPMMessages --- src/com/juick/server/PMQueries.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index b0c2911a..651524ee 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -242,4 +242,33 @@ public class PMQueries { return msgs; } + + public static ArrayList getLastPMMessages(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; + } } -- cgit v1.2.3 From 62e67a9b80fd59672ba24883f9e6a8c9b70781b1 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 27 Jan 2014 02:50:56 +0700 Subject: getLastPMSent --- src/com/juick/server/PMQueries.java | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java index 651524ee..56b91abf 100644 --- a/src/com/juick/server/PMQueries.java +++ b/src/com/juick/server/PMQueries.java @@ -243,7 +243,7 @@ public class PMQueries { return msgs; } - public static ArrayList getLastPMMessages(Connection sql, int uid) { + public static ArrayList getLastPMInbox(Connection sql, int uid) { ArrayList msgs = new ArrayList(20); PreparedStatement stmt = null; @@ -271,4 +271,33 @@ public class PMQueries { 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; + } } -- cgit v1.2.3 From 0565056a6ca2f4f72c9d2be264251667774894fc Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 27 Jan 2014 03:05:57 +0700 Subject: checkUserNameValid update --- src/com/juick/server/UserQueries.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 716f8f0f..7a8520be 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -325,7 +325,7 @@ public class UserQueries { } public static boolean checkUserNameValid(String uname) { - return uname != null && uname.length() > 1 && uname.length() < 16 && uname.matches("[a-zA-Z0-9\\-\\.\\@]+"); + 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) { -- cgit v1.2.3 From 4718f53d8e43a170e49c9f082452bcaecf6e5fe6 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 31 Jan 2014 04:10:14 +0700 Subject: getTagID -> getTag --- src/com/juick/server/TagQueries.java | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index e811caf4..82c0f9ae 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -30,19 +30,37 @@ import java.util.ArrayList; */ public class TagQueries { - public static int getTagID(Connection sql, String tag, boolean autoCreate) { - int tid = SQLHelpers.getInt(sql, "SELECT tag_id FROM tags WHERE name=?", tag, 0); + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { + com.juick.Tag ret = null; - if (tid == 0 && autoCreate) { - PreparedStatement stmt = null; - ResultSet rs = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id,name FROM tags WHERE name=?"); + stmt.setString(1, tag); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.Name = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (ret == null && autoCreate) { try { stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, tag); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.first()) { - tid = rs.getInt(1); + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.Name = tag; } } catch (SQLException e) { System.err.println(e); @@ -51,7 +69,7 @@ public class TagQueries { } } - return tid; + return ret; } public static ArrayList getUserTagsAll(Connection sql, int uid) { -- cgit v1.2.3 From 6a75cc16b2864aa4fb06e61eb9011d83497f607f Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 31 Jan 2014 20:57:46 +0700 Subject: createUser -lang --- src/com/juick/server/UserQueries.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 7a8520be..8d2d0e60 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -55,16 +55,15 @@ public class UserQueries { return hash; } - public static int createUser(Connection sql, String username, String password, String lang) { + 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,lang) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS); + stmt = sql.prepareStatement("INSERT INTO users(nick,passw) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, username); stmt.setString(2, password); - stmt.setString(3, lang); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.first()) { -- cgit v1.2.3 From 636b44d06dfbf84210c44f488659e629ed4db0ec Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 2 Feb 2014 23:21:07 +0700 Subject: MessagesQueries.getTags --- src/com/juick/server/MessagesQueries.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 64ee3b96..bf26b70b 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -352,6 +352,35 @@ public class MessagesQueries { 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); -- cgit v1.2.3 From 18c9e608fa38b136e581adb974b29e4c5a446dc9 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 9 Feb 2014 04:54:56 +0700 Subject: Votes --- src/com/juick/server/MessagesQueries.java | 6 +- src/com/juick/server/UserQueries.java | 1 + src/com/juick/server/VotesQueries.java | 91 +++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/com/juick/server/VotesQueries.java diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index bf26b70b..dcba6830 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -130,7 +130,7 @@ public class MessagesQueries { 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 FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + 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.votes 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()) { @@ -154,6 +154,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(10); msg.Place.lon = rs.getDouble(11); } + msg.Votes = rs.getInt(12); } } catch (SQLException e) { System.err.println(e); @@ -810,7 +811,7 @@ public class MessagesQueries { 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 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"); + 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.votes 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()) { @@ -837,6 +838,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(13); msg.Place.lon = rs.getDouble(14); } + msg.Votes = rs.getInt(15); msgs.add(msg); } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 8d2d0e60..a47ef815 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -284,6 +284,7 @@ public class UserQueries { 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); diff --git a/src/com/juick/server/VotesQueries.java b/src/com/juick/server/VotesQueries.java new file mode 100644 index 00000000..d3af99c1 --- /dev/null +++ b/src/com/juick/server/VotesQueries.java @@ -0,0 +1,91 @@ +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 VotesQueries { + + public static boolean voteMessage(Connection sql, int mid, int uid, int vote) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("INSERT INTO messages_votes(message_id,user_id,vote) VALUES (?,?,?)"); + stmt.setInt(1, mid); + stmt.setInt(2, uid); + stmt.setInt(3, vote); + ret = stmt.executeUpdate() > 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (ret) { + try { + String query; + if (vote > 0) { + query = "UPDATE messages SET votes=votes+1 WHERE message_id=?"; + } else { + query = "UPDATE messages SET votes=votes-1 WHERE message_id=?"; + } + stmt = sql.prepareStatement(query); + stmt.setInt(1, mid); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return ret; + } + + public static int getMessageVotes(Connection sql, int mid) { + return SQLHelpers.getInt(sql, "SELECT votes FROM messages WHERE message_id=?", mid, 0); + } + + public static boolean fillMessagesVotes(Connection sql, ArrayList msgs, int vuid) { + boolean ret = false; + + String mids = ""; + final int midsSize = msgs.size(); + for (int i = 0; i < midsSize; i++) { + if (i > 0) { + mids += ","; + } + mids += msgs.get(i).MID; + } + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT message_id,vote FROM messages_votes WHERE user_id=? AND message_id IN (" + mids + ")"); + stmt.setInt(1, vuid); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + int mid = rs.getInt(1); + for (int i = 0; i < midsSize; i++) { + if (msgs.get(i).MID == mid) { + msgs.get(i).UserVote = rs.getInt(2); + ret = true; + } + } + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } +} -- cgit v1.2.3 From 8af09519673b1549137c766eda6bdc72ba1b0b3d Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 14 Feb 2014 13:21:26 +0700 Subject: MessagesQueries lang!=fa --- src/com/juick/server/MessagesQueries.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index dcba6830..dd284d3f 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -291,21 +291,21 @@ 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 lang='ru' 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 lang!='fa' 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 lang='ru' 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 lang!='fa' 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 AND lang='ru' ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND lang!='fa' ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND lang='ru' ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 AND lang!='fa' ORDER BY message_id DESC LIMIT 20"); } } -- cgit v1.2.3 From ae4f9f1759dafdc1bf57e043450970794954324d Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 9 Mar 2014 00:56:39 +0700 Subject: -messages.lang -VotesQueries --- src/com/juick/server/MessagesQueries.java | 16 +++--- src/com/juick/server/VotesQueries.java | 91 ------------------------------- 2 files changed, 8 insertions(+), 99 deletions(-) delete mode 100644 src/com/juick/server/VotesQueries.java diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index dd284d3f..3dd8418b 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -130,7 +130,7 @@ public class MessagesQueries { 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.votes FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + 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 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()) { @@ -154,7 +154,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(10); msg.Place.lon = rs.getDouble(11); } - msg.Votes = rs.getInt(12); + msg.Likes = rs.getInt(12); } } catch (SQLException e) { System.err.println(e); @@ -291,21 +291,21 @@ 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 lang!='fa' 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=?) 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 lang!='fa' 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 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 AND lang!='fa' ORDER BY message_id DESC LIMIT 20"); + 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 privacy>0 AND lang!='fa' ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 ORDER BY message_id DESC LIMIT 20"); } } @@ -811,7 +811,7 @@ public class MessagesQueries { 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.votes 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"); + 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()) { @@ -838,7 +838,7 @@ public class MessagesQueries { msg.Place.lat = rs.getDouble(13); msg.Place.lon = rs.getDouble(14); } - msg.Votes = rs.getInt(15); + msg.Likes = rs.getInt(15); msgs.add(msg); } diff --git a/src/com/juick/server/VotesQueries.java b/src/com/juick/server/VotesQueries.java deleted file mode 100644 index d3af99c1..00000000 --- a/src/com/juick/server/VotesQueries.java +++ /dev/null @@ -1,91 +0,0 @@ -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 VotesQueries { - - public static boolean voteMessage(Connection sql, int mid, int uid, int vote) { - boolean ret = false; - - PreparedStatement stmt = null; - try { - stmt = sql.prepareStatement("INSERT INTO messages_votes(message_id,user_id,vote) VALUES (?,?,?)"); - stmt.setInt(1, mid); - stmt.setInt(2, uid); - stmt.setInt(3, vote); - ret = stmt.executeUpdate() > 0; - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - - if (ret) { - try { - String query; - if (vote > 0) { - query = "UPDATE messages SET votes=votes+1 WHERE message_id=?"; - } else { - query = "UPDATE messages SET votes=votes-1 WHERE message_id=?"; - } - stmt = sql.prepareStatement(query); - stmt.setInt(1, mid); - stmt.executeUpdate(); - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(null, stmt); - } - } - - return ret; - } - - public static int getMessageVotes(Connection sql, int mid) { - return SQLHelpers.getInt(sql, "SELECT votes FROM messages WHERE message_id=?", mid, 0); - } - - public static boolean fillMessagesVotes(Connection sql, ArrayList msgs, int vuid) { - boolean ret = false; - - String mids = ""; - final int midsSize = msgs.size(); - for (int i = 0; i < midsSize; i++) { - if (i > 0) { - mids += ","; - } - mids += msgs.get(i).MID; - } - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT message_id,vote FROM messages_votes WHERE user_id=? AND message_id IN (" + mids + ")"); - stmt.setInt(1, vuid); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - int mid = rs.getInt(1); - for (int i = 0; i < midsSize; i++) { - if (msgs.get(i).MID == mid) { - msgs.get(i).UserVote = rs.getInt(2); - ret = true; - } - } - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - return ret; - } -} -- cgit v1.2.3 From e2c257fecfd08e745c1f051bde14a32a43255f26 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 9 Mar 2014 01:13:17 +0700 Subject: setMessagePopular --- src/com/juick/server/MessagesQueries.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 3dd8418b..fa84f476 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -884,4 +884,22 @@ public class MessagesQueries { return replies; } + + public static boolean setMessagePopular(Connection sql, int mid, int popular) { + boolean ret = false; + + PreparedStatement stmt = null; + try { + 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); + } + + return ret; + } } -- cgit v1.2.3 From 2f56add386e112200dcbd7bdc3f6eefae4c1efb3 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 20 Mar 2014 02:03:21 +0700 Subject: getVKTokens, messages popularity --- src/com/juick/server/CrosspostQueries.java | 25 ++++++++++++++++++++++++- src/com/juick/server/MessagesQueries.java | 12 ++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/CrosspostQueries.java b/src/com/juick/server/CrosspostQueries.java index 09d951c1..fd677048 100644 --- a/src/com/juick/server/CrosspostQueries.java +++ b/src/com/juick/server/CrosspostQueries.java @@ -34,7 +34,7 @@ public class CrosspostQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT access_token,access_token_secret FROM twitter WHERE user_id=?"); + 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()) { @@ -54,4 +54,27 @@ public class CrosspostQueries { 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/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index fa84f476..1f4c8408 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -291,21 +291,21 @@ 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_id=-1 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, 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 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 popular>=-1 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 = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id=-1 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); stmt.setInt(1, before); } else { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE popular>=-1 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); } } @@ -560,10 +560,10 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id0 AND popular=1 ORDER BY message_id DESC LIMIT 20"); + 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=1 ORDER BY message_id DESC LIMIT 20"); + 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(); -- cgit v1.2.3 From 3caf993af6742740f9c3997c8e072cc681b10118 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 4 Apr 2014 14:57:42 +0700 Subject: getBLUsers, getBLTags, setPopular --- src/com/juick/server/MessagesQueries.java | 34 +++++-- src/com/juick/server/TagQueries.java | 21 +++++ src/com/juick/server/UserQueries.java | 143 ++++++++++++++++++------------ 3 files changed, 132 insertions(+), 66 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 1f4c8408..d2254575 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -291,21 +291,21 @@ public class MessagesQueries { try { if (visitor_uid > 1) { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE message_id=-1 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 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 popular>=-1 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=?) 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_id=-1 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + 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 popular>=-1 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); + stmt = sql.prepareStatement("SELECT message_id FROM messages WHERE hidden=0 AND privacy>0 ORDER BY message_id DESC LIMIT 20"); } } @@ -890,9 +890,17 @@ public class MessagesQueries { PreparedStatement stmt = null; try { - stmt = sql.prepareStatement("UPDATE messages SET popular=? WHERE message_id=?"); - stmt.setInt(1, popular); - stmt.setInt(2, mid); + 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); @@ -900,6 +908,18 @@ public class MessagesQueries { 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; } } diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 82c0f9ae..87704c62 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -95,4 +95,25 @@ public class TagQueries { } 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)"); + 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/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index a47ef815..1933f26a 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -31,12 +31,12 @@ 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; @@ -51,13 +51,13 @@ public class UserQueries { 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 { @@ -74,16 +74,16 @@ public class UserQueries { } 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 { @@ -102,10 +102,10 @@ public class UserQueries { } return user; } - + public static com.juick.User getUserByName(Connection sql, String username) { com.juick.User user = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -124,10 +124,10 @@ public class UserQueries { } return user; } - + public static com.juick.User getUserByJID(Connection sql, String jid) { com.juick.User user = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -147,10 +147,10 @@ public class UserQueries { } return user; } - + public static ArrayList getUsersByName(Connection sql, ArrayList unames) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -170,10 +170,10 @@ public class UserQueries { } return users; } - + public static ArrayList getUsersByID(Connection sql, ArrayList uids) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -191,13 +191,13 @@ public class UserQueries { } 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++) { @@ -206,7 +206,7 @@ public class UserQueries { } uids += users.get(i).UID; } - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -227,13 +227,13 @@ public class UserQueries { } 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 { @@ -254,26 +254,26 @@ 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 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 { @@ -293,10 +293,10 @@ public class UserQueries { } 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; @@ -311,10 +311,10 @@ public class UserQueries { Utils.finishSQL(null, stmt); } } - + return hash; } - + public static String generateHash(int len) { Random rnd = new Random(); StringBuilder sb = new StringBuilder(len); @@ -323,11 +323,11 @@ public class UserQueries { } 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; @@ -350,10 +350,10 @@ public class UserQueries { } return uid; } - + public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { int ret = defaultValue; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -370,7 +370,7 @@ public class UserQueries { } return ret; } - + public static void setUserOptionInt(Connection sql, int uid, String option, int value) { PreparedStatement stmt = null; try { @@ -384,10 +384,10 @@ public class UserQueries { Utils.finishSQL(null, stmt); } } - + public static boolean getCanMedia(Connection sql, int uid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -404,10 +404,10 @@ 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 { @@ -425,10 +425,10 @@ public class UserQueries { } return ret; } - + public static boolean isInBL(Connection sql, int uid, int check) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -446,10 +446,10 @@ public class UserQueries { } return ret; } - + public static boolean isInBLAny(Connection sql, int uid, int uid2) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -469,10 +469,10 @@ public class UserQueries { } return ret; } - + public static ArrayList checkBL(Connection sql, int visitor, ArrayList uids) { ArrayList ret = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -488,13 +488,13 @@ public class UserQueries { } 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 { @@ -512,27 +512,52 @@ public class UserQueries { } 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 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=?"); + 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); } -- cgit v1.2.3 From 2ccd7c92282e4db091c953680b099111634ed09c Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 12 Apr 2014 16:34:03 +0700 Subject: MessagesQueries.createReply --- src/com/juick/server/MessagesQueries.java | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index d2254575..c30bf50c 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -21,6 +21,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Types; import java.util.ArrayList; import java.util.Collections; @@ -30,6 +31,49 @@ import java.util.Collections; */ public class MessagesQueries { + public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { + int newrid = 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, newrid); + 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) { + newrid = 0; + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return newrid; + } + + public static int getReplyIDIncrement(Connection sql, int mid) { + int ret = 0; + + SQLHelpers.execute(sql, "LOCK TABLES messages WRITE"); + + int rid = SQLHelpers.getInt(sql, "SELECT maxreplyid FROM messages WHERE message_id=?", mid, -1); + if (rid >= 0 && SQLHelpers.executeInt(sql, "UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid) == 1) { + ret = rid + 1; + } + + SQLHelpers.execute(sql, "UNLOCK TABLES"); + + return ret; + } + public static boolean canViewThread(Connection sql, int mid, int uid) { int privacy = 0; -- cgit v1.2.3 From e286dc7e1d2ba75778220a3621343c334b4cc3da Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 12 Apr 2014 16:39:30 +0700 Subject: SQLHelpers.execute --- src/com/juick/server/SQLHelpers.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index 479fd1cc..28ccdb8c 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -16,6 +16,20 @@ import java.util.ArrayList; */ 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; -- cgit v1.2.3 From e6aecf508d4c3b115bd6811678604b8a351518d4 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 12 Apr 2014 16:42:14 +0700 Subject: SubscriptionsQueries.subscribeMessage; fixed BL in subscriptions --- src/com/juick/server/SubscriptionsQueries.java | 50 +++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java index 20540b93..ac465948 100644 --- a/src/com/juick/server/SubscriptionsQueries.java +++ b/src/com/juick/server/SubscriptionsQueries.java @@ -48,11 +48,39 @@ public class SubscriptionsQueries { 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 { - stmt = sql.prepareStatement("SELECT DISTINCT jid FROM jids WHERE active=1 AND user_id!=? AND user_id IN (SELECT suser_id FROM subscr_users WHERE user_id=? UNION SELECT suser_id FROM subscr_tags INNER JOIN messages_tags ON (messages_tags.message_id=? AND subscr_tags.tag_id=messages_tags.tag_id))"); - stmt.setInt(1, uid); - stmt.setInt(2, uid); - stmt.setInt(3, mid); + 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()) { @@ -63,6 +91,16 @@ public class SubscriptionsQueries { } 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; } @@ -87,4 +125,8 @@ public class SubscriptionsQueries { } 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; + } } -- cgit v1.2.3 From d5c3d6b5cc0decd1b22cb165f73656aade85e20f Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 13 Apr 2014 13:02:55 +0700 Subject: Utils: buildQueryArray, stmtSetStringArray --- src/com/juick/server/Utils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/com/juick/server/Utils.java b/src/com/juick/server/Utils.java index d37931d0..f97797d7 100644 --- a/src/com/juick/server/Utils.java +++ b/src/com/juick/server/Utils.java @@ -17,6 +17,7 @@ */ package com.juick.server; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -50,6 +51,24 @@ public class Utils { 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 { -- cgit v1.2.3 From 7ae2c59381b090357b87834a2f84e4f03df9f146 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 13 Apr 2014 17:21:09 +0700 Subject: TagQueries: getTags, createTag --- src/com/juick/server/TagQueries.java | 94 ++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 87704c62..901ccfe2 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -29,10 +29,10 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class TagQueries { - + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { com.juick.Tag ret = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -50,31 +50,83 @@ public class TagQueries { } finally { Utils.finishSQL(rs, stmt); } - + if (ret == null && autoCreate) { - try { - stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); - stmt.setString(1, tag); - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.first()) { - ret = new com.juick.Tag(); - ret.TID = rs.getInt(1); - ret.Name = tag; + 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(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(Utils.buildQueryArray("SELECT tag_id,name FROM tags WHERE name IN (", tags.length, ")")); + Utils.stmtSetStringArray(stmt, 1, tags); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = rs.getInt(1); + t.Name = rs.getString(2); + ret.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (tags.length != ret.size() && autoCreate) { + for (int i = 0; i < tags.length; i++) { + boolean found = false; + for (int n = 0; n < ret.size(); n++) { + if (tags[i].equals(ret.get(n).Name)) { + found = true; + break; + } + } + if (!found) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = createTag(sql, tags[i]); + t.Name = tags[i]; } - } 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 { @@ -95,10 +147,10 @@ public class TagQueries { } return tags; } - + public static ArrayList getUserBLTags(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { -- cgit v1.2.3 From f9ee0de56d757ecfe22db0611140f29d640cef3a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 13 Apr 2014 17:23:43 +0700 Subject: MessagesQueries: createMessage --- src/com/juick/server/MessagesQueries.java | 63 +++++++++++++++++++++++++++++++ src/com/juick/server/TagQueries.java | 30 +++++++-------- 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index c30bf50c..cf2dd04d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -21,6 +21,7 @@ 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; @@ -31,6 +32,68 @@ import java.util.Collections; */ 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,attachment) 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 newrid = getReplyIDIncrement(sql, mid); diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 901ccfe2..55704eac 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -29,10 +29,10 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class TagQueries { - + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { com.juick.Tag ret = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -50,19 +50,19 @@ public class TagQueries { } 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(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -81,7 +81,7 @@ public class TagQueries { } finally { Utils.finishSQL(rs, stmt); } - + if (tags.length != ret.size() && autoCreate) { for (int i = 0; i < tags.length; i++) { boolean found = false; @@ -98,13 +98,13 @@ public class TagQueries { } } } - + return ret; } - + public static int createTag(Connection sql, String name) { int ret = 0; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -120,13 +120,13 @@ public class TagQueries { } 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 { @@ -147,10 +147,10 @@ public class TagQueries { } return tags; } - + public static ArrayList getUserBLTags(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { -- cgit v1.2.3 From 29df00e4fa77ba4aeda0b9780e30c17dd57d92b2 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 20 Apr 2014 15:16:16 +0700 Subject: BL lists order, create reply bugfix --- src/com/juick/server/MessagesQueries.java | 25 +++++++++++++------------ src/com/juick/server/TagQueries.java | 2 +- src/com/juick/server/UserQueries.java | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index cf2dd04d..550da56d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -38,7 +38,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("INSERT INTO messages(user_id,attachment) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + 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); @@ -95,13 +95,13 @@ public class MessagesQueries { } public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { - int newrid = getReplyIDIncrement(sql, mid); + 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, newrid); + stmt.setInt(2, ridnew); stmt.setInt(3, uid); stmt.setInt(4, rid); if (attachment != null) { @@ -111,7 +111,7 @@ public class MessagesQueries { } stmt.setString(6, txt); if (stmt.executeUpdate() == 0) { - newrid = 0; + ridnew = 0; } } catch (SQLException e) { System.err.println(e); @@ -119,22 +119,23 @@ public class MessagesQueries { Utils.finishSQL(null, stmt); } - return newrid; + 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 ret = 0; + int rid = 0; SQLHelpers.execute(sql, "LOCK TABLES messages WRITE"); - - int rid = SQLHelpers.getInt(sql, "SELECT maxreplyid FROM messages WHERE message_id=?", mid, -1); - if (rid >= 0 && SQLHelpers.executeInt(sql, "UPDATE messages SET maxreplyid=maxreplyid+1 WHERE message_id=?", mid) == 1) { - ret = rid + 1; + 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 ret; + return rid; } public static boolean canViewThread(Connection sql, int mid, int uid) { diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 55704eac..e4d94121 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -154,7 +154,7 @@ public class TagQueries { 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)"); + 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(); diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 1933f26a..186cacc9 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -527,7 +527,7 @@ public class UserQueries { 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=?"); + 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(); -- cgit v1.2.3 From 3406753be46b637c0afee894cb12ed1bc5de9d29 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 23 Apr 2014 21:27:46 +0700 Subject: Recommendations (getJIDSubscribedToUserRecommendations) --- src/com/juick/server/MessagesQueries.java | 8 ++++++ src/com/juick/server/SubscriptionsQueries.java | 36 ++++++++++++++++++++++++++ src/com/juick/server/TagQueries.java | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 550da56d..d02985ee 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -138,6 +138,14 @@ public class MessagesQueries { 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; diff --git a/src/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java index ac465948..d0f5f308 100644 --- a/src/com/juick/server/SubscriptionsQueries.java +++ b/src/com/juick/server/SubscriptionsQueries.java @@ -126,6 +126,42 @@ public class SubscriptionsQueries { 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/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index e4d94121..4f0639f9 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -86,7 +86,7 @@ public class TagQueries { for (int i = 0; i < tags.length; i++) { boolean found = false; for (int n = 0; n < ret.size(); n++) { - if (tags[i].equals(ret.get(n).Name)) { + if (tags[i].equalsIgnoreCase(ret.get(n).Name)) { found = true; break; } -- cgit v1.2.3 From 6145ee50629a75284e790522c9840da7a1a93536 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Thu, 24 Apr 2014 01:29:44 +0700 Subject: UserQueries.getUserReadLeastPopular --- src/com/juick/server/UserQueries.java | 150 ++++++++++++++++++++-------------- 1 file changed, 88 insertions(+), 62 deletions(-) diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 186cacc9..5c1aaccf 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -31,12 +31,12 @@ 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; @@ -51,13 +51,13 @@ public class UserQueries { 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 { @@ -74,16 +74,16 @@ public class UserQueries { } 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 { @@ -102,10 +102,10 @@ public class UserQueries { } return user; } - + public static com.juick.User getUserByName(Connection sql, String username) { com.juick.User user = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -124,10 +124,10 @@ public class UserQueries { } return user; } - + public static com.juick.User getUserByJID(Connection sql, String jid) { com.juick.User user = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -147,10 +147,10 @@ public class UserQueries { } return user; } - + public static ArrayList getUsersByName(Connection sql, ArrayList unames) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -170,10 +170,10 @@ public class UserQueries { } return users; } - + public static ArrayList getUsersByID(Connection sql, ArrayList uids) { ArrayList users = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -191,13 +191,13 @@ public class UserQueries { } 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++) { @@ -206,7 +206,7 @@ public class UserQueries { } uids += users.get(i).UID; } - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -227,13 +227,13 @@ public class UserQueries { } 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 { @@ -254,26 +254,26 @@ 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 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 { @@ -293,10 +293,10 @@ public class UserQueries { } 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; @@ -311,10 +311,10 @@ public class UserQueries { Utils.finishSQL(null, stmt); } } - + return hash; } - + public static String generateHash(int len) { Random rnd = new Random(); StringBuilder sb = new StringBuilder(len); @@ -323,11 +323,11 @@ public class UserQueries { } 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; @@ -350,10 +350,10 @@ public class UserQueries { } return uid; } - + public static int getUserOptionInt(Connection sql, int uid, String option, int defaultValue) { int ret = defaultValue; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -370,7 +370,7 @@ public class UserQueries { } return ret; } - + public static void setUserOptionInt(Connection sql, int uid, String option, int value) { PreparedStatement stmt = null; try { @@ -384,10 +384,10 @@ public class UserQueries { Utils.finishSQL(null, stmt); } } - + public static boolean getCanMedia(Connection sql, int uid) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -404,10 +404,10 @@ 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 { @@ -425,10 +425,10 @@ public class UserQueries { } return ret; } - + public static boolean isInBL(Connection sql, int uid, int check) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -446,10 +446,10 @@ public class UserQueries { } return ret; } - + public static boolean isInBLAny(Connection sql, int uid, int uid2) { boolean ret = false; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -469,10 +469,10 @@ public class UserQueries { } return ret; } - + public static ArrayList checkBL(Connection sql, int visitor, ArrayList uids) { ArrayList ret = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -488,13 +488,13 @@ public class UserQueries { } 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 { @@ -512,18 +512,44 @@ public class UserQueries { } 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 { @@ -542,22 +568,22 @@ public class UserQueries { } 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); } -- cgit v1.2.3 From 874187ffc4173296beb78e0603909ac4746c3211 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 17 Jun 2014 18:37:26 +0700 Subject: getTags --- src/com/juick/server/TagQueries.java | 63 ++++++++++-------------------------- 1 file changed, 17 insertions(+), 46 deletions(-) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 4f0639f9..b570c32a 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -29,10 +29,10 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class TagQueries { - + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { com.juick.Tag ret = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -50,61 +50,32 @@ public class TagQueries { } 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(); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement(Utils.buildQueryArray("SELECT tag_id,name FROM tags WHERE name IN (", tags.length, ")")); - Utils.stmtSetStringArray(stmt, 1, tags); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - com.juick.Tag t = new com.juick.Tag(); - t.TID = rs.getInt(1); - t.Name = rs.getString(2); + + for (int i = 0; i < tags.length; i++) { + com.juick.Tag t = getTag(sql, tags[i], autoCreate); + if (t != null) { ret.add(t); } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (tags.length != ret.size() && autoCreate) { - for (int i = 0; i < tags.length; i++) { - boolean found = false; - for (int n = 0; n < ret.size(); n++) { - if (tags[i].equalsIgnoreCase(ret.get(n).Name)) { - found = true; - break; - } - } - if (!found) { - com.juick.Tag t = new com.juick.Tag(); - t.TID = createTag(sql, tags[i]); - t.Name = tags[i]; - } - } } - + return ret; } - + public static int createTag(Connection sql, String name) { int ret = 0; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -120,13 +91,13 @@ public class TagQueries { } 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 { @@ -147,10 +118,10 @@ public class TagQueries { } return tags; } - + public static ArrayList getUserBLTags(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { -- cgit v1.2.3 From c56c582c37e62acb9a658def2dafdddff8386281 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 17 Jun 2014 18:40:08 +0700 Subject: getTagNoIndex --- src/com/juick/server/TagQueries.java | 49 +++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index b570c32a..938f7ace 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -29,10 +29,10 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class TagQueries { - + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { com.juick.Tag ret = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -50,32 +50,53 @@ public class TagQueries { } 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++) { 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_meta 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 { @@ -91,13 +112,13 @@ public class TagQueries { } 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 { @@ -118,10 +139,10 @@ public class TagQueries { } return tags; } - + public static ArrayList getUserBLTags(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { -- cgit v1.2.3 From 24233da239e683ea9331a97685cc38e7981835a9 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sat, 5 Jul 2014 17:06:18 +0700 Subject: * tags bugfix --- src/com/juick/server/TagQueries.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 938f7ace..54ec862d 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -64,9 +64,11 @@ public class TagQueries { ArrayList ret = new ArrayList(); for (int i = 0; i < tags.length; i++) { - com.juick.Tag t = getTag(sql, tags[i], autoCreate); - if (t != null) { - ret.add(t); + if (!tags[i].isEmpty()) { + com.juick.Tag t = getTag(sql, tags[i], autoCreate); + if (t != null) { + ret.add(t); + } } } @@ -79,7 +81,7 @@ public class TagQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT noindex FROM tags_meta WHERE tag_id=?"); + stmt = sql.prepareStatement("SELECT noindex FROM tags WHERE tag_id=?"); stmt.setInt(1, tag_id); rs = stmt.executeQuery(); if (rs.first()) { -- cgit v1.2.3 From bddf6ce9dd9b9a1ed4b57df78ca1dcb3ed5a4bdd Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Wed, 9 Jul 2014 14:55:40 +0700 Subject: Messages privacy, getTag, tags synonyms --- src/com/juick/server/MessagesQueries.java | 67 ++++++++++++++----------------- src/com/juick/server/TagQueries.java | 32 +++++++++++++-- 2 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index d02985ee..124f3898 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -169,30 +169,7 @@ public class MessagesQueries { return privacy >= 0 || uid == owner_uid - || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)) - || (privacy == -3 && uid > 0 && hasAccessToMessage(sql, mid, uid)); - } - - public static boolean hasAccessToMessage(Connection sql, int mid, int uid) { - boolean ret = false; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT 1 FROM messages_access WHERE message_id=? AND user_id=?"); - stmt.setInt(1, mid); - stmt.setInt(2, 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; + || ((privacy == -1 || privacy == -2) && uid > 0 && UserQueries.isInWL(sql, owner_uid, uid)); } public static boolean isReadOnly(Connection sql, int mid) { @@ -445,16 +422,20 @@ public class MessagesQueries { 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=? AND messages.message_id0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); +// 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, before); - stmt.setInt(3, visitor_uid); - stmt.setInt(4, cnt); + 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=? AND (messages.privacy>0 OR messages.user_id=?) ORDER BY messages.message_id DESC LIMIT ?"); + //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, visitor_uid); - stmt.setInt(3, cnt); + stmt.setInt(2, tid); + stmt.setInt(3, visitor_uid); + stmt.setInt(4, cnt); } rs = stmt.executeQuery(); rs.beforeFirst(); @@ -589,15 +570,12 @@ public class MessagesQueries { ResultSet rs = null; try { if (before > 0) { - stmt = sql.prepareStatement("SELECT message_id FROM (SELECT message_id FROM messages_access WHERE user_id=? AND message_id 0; + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + return ret; + } } diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 54ec862d..493e8675 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -30,20 +30,44 @@ import java.util.ArrayList; */ 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,name FROM tags WHERE name=?"); + stmt = sql.prepareStatement("SELECT tag_id,synonym_id,name FROM tags WHERE name=?"); stmt.setString(1, tag); rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { + if (rs.first()) { ret = new com.juick.Tag(); ret.TID = rs.getInt(1); - ret.Name = rs.getString(2); + ret.SynonymID = rs.getInt(2); + ret.Name = rs.getString(3); } } catch (SQLException e) { System.err.println(e); -- cgit v1.2.3 From b67b2f126af0c410cdb5b1a925d19b5d2cc5b2b2 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Tue, 15 Jul 2014 16:56:38 +0700 Subject: User.Banned --- src/com/juick/server/MessagesQueries.java | 11 ++++++----- src/com/juick/server/UserQueries.java | 6 ++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 124f3898..fbdd7e69 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -951,7 +951,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,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()),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(); @@ -963,10 +963,11 @@ public class MessagesQueries { msg.User = new com.juick.User(); msg.User.UID = rs.getInt(3); msg.User.UName = rs.getString(4); - msg.Text = rs.getString(5); - msg.TimeAgo = rs.getInt(6); - msg.TimestampString = rs.getString(7); - msg.AttachmentType = rs.getString(8); + 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); } diff --git a/src/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java index 5c1aaccf..d1313715 100644 --- a/src/com/juick/server/UserQueries.java +++ b/src/com/juick/server/UserQueries.java @@ -87,13 +87,14 @@ public class UserQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT nick FROM users WHERE id=?"); + 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); @@ -109,13 +110,14 @@ public class UserQueries { PreparedStatement stmt = null; ResultSet rs = null; try { - stmt = sql.prepareStatement("SELECT id,nick FROM users WHERE nick=?"); + 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); -- cgit v1.2.3 From 13a861563ffead4cadba48e523aad2e5c6beb44c Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 25 Jul 2014 00:16:47 +0700 Subject: AdsQueries for unauthorized --- src/com/juick/server/AdsQueries.java | 6 +++++- src/com/juick/server/SQLHelpers.java | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/AdsQueries.java b/src/com/juick/server/AdsQueries.java index f1b37fb1..0fb4c0a6 100644 --- a/src/com/juick/server/AdsQueries.java +++ b/src/com/juick/server/AdsQueries.java @@ -28,7 +28,11 @@ import java.sql.SQLException; public class AdsQueries { public static int getAdMID(Connection sql, int uid) { - 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); + 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) { diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index 28ccdb8c..f5569993 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -45,6 +45,24 @@ public class SQLHelpers { 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; -- cgit v1.2.3 From 4ce6b7d6e4b7e9583e90bde93bbdece0bcd88262 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 25 Jul 2014 18:38:12 +0700 Subject: getMessageTags --- src/com/juick/server/MessagesQueries.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index fbdd7e69..739bcac1 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -332,6 +332,33 @@ public class MessagesQueries { 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(); -- cgit v1.2.3 From b5df0e1c2664a8a1a0f9042745dd608773fedce3 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 3 Aug 2014 15:36:30 +0700 Subject: Message.Hidden --- src/com/juick/server/MessagesQueries.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 739bcac1..8bef9018 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -223,7 +223,7 @@ public class MessagesQueries { 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 FROM messages INNER JOIN users ON messages.user_id=users.id WHERE messages.message_id=?"); + 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()) { @@ -248,6 +248,7 @@ public class MessagesQueries { msg.Place.lon = rs.getDouble(11); } msg.Likes = rs.getInt(12); + msg.Hidden = rs.getBoolean(13); } } catch (SQLException e) { System.err.println(e); -- cgit v1.2.3 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 --- .gitignore | 5 +- build.gradle | 1 + nbproject/build-impl.xml | 1056 ------------------- nbproject/genfiles.properties | 8 - nbproject/project.properties | 73 -- nbproject/project.xml | 25 - src/com/juick/server/AdsQueries.java | 51 - src/com/juick/server/CrosspostQueries.java | 80 -- src/com/juick/server/MessagesQueries.java | 1065 -------------------- src/com/juick/server/PMQueries.java | 303 ------ src/com/juick/server/PushQueries.java | 68 -- src/com/juick/server/SQLHelpers.java | 163 --- src/com/juick/server/SubscriptionsQueries.java | 168 --- src/com/juick/server/TagQueries.java | 189 ---- src/com/juick/server/UserQueries.java | 592 ----------- src/com/juick/server/Utils.java | 86 -- 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 ++ 26 files changed, 2768 insertions(+), 3930 deletions(-) create mode 100644 build.gradle delete mode 100644 nbproject/build-impl.xml delete mode 100644 nbproject/genfiles.properties delete mode 100644 nbproject/project.properties delete mode 100644 nbproject/project.xml delete mode 100644 src/com/juick/server/AdsQueries.java delete mode 100644 src/com/juick/server/CrosspostQueries.java delete mode 100644 src/com/juick/server/MessagesQueries.java delete mode 100644 src/com/juick/server/PMQueries.java delete mode 100644 src/com/juick/server/PushQueries.java delete mode 100644 src/com/juick/server/SQLHelpers.java delete mode 100644 src/com/juick/server/SubscriptionsQueries.java delete mode 100644 src/com/juick/server/TagQueries.java delete mode 100644 src/com/juick/server/UserQueries.java delete mode 100644 src/com/juick/server/Utils.java 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 diff --git a/.gitignore b/.gitignore index 315ea15c..e51bc30b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -/dist/ -/build/ -/nbproject/private/ \ No newline at end of file +build/* +.gradle/* diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..405a81af --- /dev/null +++ b/build.gradle @@ -0,0 +1 @@ +apply plugin: "java" diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml deleted file mode 100644 index 278d2839..00000000 --- a/nbproject/build-impl.xml +++ /dev/null @@ -1,1056 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set src.dir - Must set test.src.dir - Must set build.dir - Must set dist.dir - Must set build.classes.dir - Must set dist.javadoc.dir - Must set build.test.classes.dir - Must set build.test.results.dir - Must set build.classes.excludes - Must set dist.jar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set JVM to use for profiling in profiler.info.jvm - Must set profiler agent JVM arguments in profiler.info.jvmargs.agent - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - - - - - - java -cp "${run.classpath.with.dist.jar}" ${main.class} - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - Must select one file in the IDE or set run.class - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set debug.class - - - - - Must select one file in the IDE or set debug.class - - - - - Must set fix.includes - - - - - - - - - - - - - - - - - Must select one file in the IDE or set profile.class - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - Some tests failed; see details above. - - - - - - - - - Must select some files in the IDE or set test.includes - - - - Some tests failed; see details above. - - - - - Must select one file in the IDE or set test.class - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties deleted file mode 100644 index 972737f5..00000000 --- a/nbproject/genfiles.properties +++ /dev/null @@ -1,8 +0,0 @@ -build.xml.data.CRC32=d216b2d6 -build.xml.script.CRC32=8a81d8ba -build.xml.stylesheet.CRC32=28e38971@1.44.1.45 -# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. -# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=d216b2d6 -nbproject/build-impl.xml.script.CRC32=6acf7607 -nbproject/build-impl.xml.stylesheet.CRC32=0ae3a408@1.44.1.45 diff --git a/nbproject/project.properties b/nbproject/project.properties deleted file mode 100644 index 666e31d1..00000000 --- a/nbproject/project.properties +++ /dev/null @@ -1,73 +0,0 @@ -annotation.processing.enabled=true -annotation.processing.enabled.in.editor=false -annotation.processing.run.all.processors=true -annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=com.juick.server -application.vendor=ugnich -build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form -# This directory is removed when the project is cleaned: -build.dir=build -build.generated.dir=${build.dir}/generated -build.generated.sources.dir=${build.dir}/generated-sources -# Only compile against the classpath explicitly listed here: -build.sysclasspath=ignore -build.test.classes.dir=${build.dir}/test/classes -build.test.results.dir=${build.dir}/test/results -# Uncomment to specify the preferred debugger connection transport: -#debug.transport=dt_socket -debug.classpath=\ - ${run.classpath} -debug.test.classpath=\ - ${run.test.classpath} -# This directory is removed when the project is cleaned: -dist.dir=dist -dist.jar=${dist.dir}/com.juick.server.jar -dist.javadoc.dir=${dist.dir}/javadoc -endorsed.classpath= -excludes= -includes=** -jar.compress=false -javac.classpath=\ - ${reference.com_juick.jar} -# Space-separated list of extra javac options -javac.compilerargs= -javac.deprecation=false -javac.processorpath=\ - ${javac.classpath} -javac.source=1.6 -javac.target=1.6 -javac.test.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -javac.test.processorpath=\ - ${javac.test.classpath} -javadoc.additionalparam= -javadoc.author=false -javadoc.encoding=${source.encoding} -javadoc.noindex=false -javadoc.nonavbar=false -javadoc.notree=false -javadoc.private=false -javadoc.splitindex=true -javadoc.use=true -javadoc.version=false -javadoc.windowtitle= -meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=true -platform.active=default_platform -project.com_juick=../com.juick -reference.com_juick.jar=${project.com_juick}/dist/com.juick.jar -run.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -# Space-separated list of JVM arguments used when running the project -# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value -# or test-sys-prop.name=value to set system properties for unit tests): -run.jvmargs= -run.test.classpath=\ - ${javac.test.classpath}:\ - ${build.test.classes.dir} -source.encoding=UTF-8 -src.dir=src -test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml deleted file mode 100644 index 3a3d1bf9..00000000 --- a/nbproject/project.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - org.netbeans.modules.java.j2seproject - - - com.juick.server - - - - - - - - - - com_juick - jar - - jar - clean - jar - - - - diff --git a/src/com/juick/server/AdsQueries.java b/src/com/juick/server/AdsQueries.java deleted file mode 100644 index 0fb4c0a6..00000000 --- a/src/com/juick/server/AdsQueries.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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/com/juick/server/CrosspostQueries.java b/src/com/juick/server/CrosspostQueries.java deleted file mode 100644 index fd677048..00000000 --- a/src/com/juick/server/CrosspostQueries.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java deleted file mode 100644 index 8bef9018..00000000 --- a/src/com/juick/server/MessagesQueries.java +++ /dev/null @@ -1,1065 +0,0 @@ -/* - * 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/com/juick/server/PMQueries.java b/src/com/juick/server/PMQueries.java deleted file mode 100644 index 56b91abf..00000000 --- a/src/com/juick/server/PMQueries.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * 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/com/juick/server/PushQueries.java b/src/com/juick/server/PushQueries.java deleted file mode 100644 index 84f42722..00000000 --- a/src/com/juick/server/PushQueries.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java deleted file mode 100644 index f5569993..00000000 --- a/src/com/juick/server/SQLHelpers.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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/com/juick/server/SubscriptionsQueries.java b/src/com/juick/server/SubscriptionsQueries.java deleted file mode 100644 index d0f5f308..00000000 --- a/src/com/juick/server/SubscriptionsQueries.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * 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/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java deleted file mode 100644 index 493e8675..00000000 --- a/src/com/juick/server/TagQueries.java +++ /dev/null @@ -1,189 +0,0 @@ -/* - * 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/com/juick/server/UserQueries.java b/src/com/juick/server/UserQueries.java deleted file mode 100644 index d1313715..00000000 --- a/src/com/juick/server/UserQueries.java +++ /dev/null @@ -1,592 +0,0 @@ -/* - * 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/com/juick/server/Utils.java b/src/com/juick/server/Utils.java deleted file mode 100644 index f97797d7..00000000 --- a/src/com/juick/server/Utils.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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) { - } - } - } -} 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 96191fa7b653ed4b50705f6151d440dec3dc8de2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 24 Oct 2015 20:12:46 +0300 Subject: drop unused files --- build.xml | 74 --------------------------------------------------------------- 1 file changed, 74 deletions(-) delete mode 100644 build.xml diff --git a/build.xml b/build.xml deleted file mode 100644 index cb4ef59d..00000000 --- a/build.xml +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - - - - - - Builds, tests, and runs the project com.juick.server. - - - -- cgit v1.2.3 From 340582b97d26625307174c692c3775c89a5f41f2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 26 Oct 2015 16:22:24 +0300 Subject: ignore IDEA files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e51bc30b..39842337 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/* .gradle/* +*.iml \ No newline at end of file -- cgit v1.2.3 From 9a8539699c88f5d4bf40a65501038cefddc014be Mon Sep 17 00:00:00 2001 From: Ippolitov A. Igor Date: Tue, 27 Oct 2015 10:46:13 +0300 Subject: insert null instead of '' for tags --- src/com/juick/server/MessagesQueries.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index 8bef9018..17e9f832 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -81,7 +81,11 @@ public class MessagesQueries { try { stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); stmt.setInt(1, mid); - stmt.setString(2, tagsNames); + if (tagsNames == "") { + stmt.setNull(2, Types.VARCHAR); + } else { + stmt.setString(2, tagsNames); + } stmt.setString(3, txt); stmt.executeUpdate(); } catch (SQLException e) { @@ -733,6 +737,7 @@ public class MessagesQueries { PreparedStatement stmt = null; ResultSet rs = null; try { + if (before > 0) { stmt = sqlSearch.prepareStatement("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id 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(+) 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(-) 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(-) 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(-) 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(-) 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(+) 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(-) 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(-) 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(+) 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(+) 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 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(+) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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(-) 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