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(+) (limited to 'src') 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