diff options
author | Ugnich Anton | 2014-04-12 16:34:03 +0700 |
---|---|---|
committer | Ugnich Anton | 2014-04-12 16:34:03 +0700 |
commit | 2ccd7c92282e4db091c953680b099111634ed09c (patch) | |
tree | 91aaf31c19cd9d6f4edf7922f309a396f521c58b /src/com/juick | |
parent | 3caf993af6742740f9c3997c8e072cc681b10118 (diff) |
MessagesQueries.createReply
Diffstat (limited to 'src/com/juick')
-rw-r--r-- | src/com/juick/server/MessagesQueries.java | 44 |
1 files changed, 44 insertions, 0 deletions
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; |