diff options
Diffstat (limited to 'juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java')
-rw-r--r-- | juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java index 0ebd2838..bcce56ca 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -43,11 +43,10 @@ import org.springframework.util.Assert; import javax.inject.Inject; import java.sql.*; +import java.time.Instant; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; +import java.util.Date; import java.util.stream.Collectors; /** @@ -178,16 +177,15 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Override public int createReply(final int mid, final int rid, final int uid, final String txt, final String attachment) { int ridnew = getReplyIDIncrement(mid); - getJdbcTemplate().execute("SET @ts=CURRENT_TIMESTAMP()"); + Date ts = Date.from(Instant.now()); getJdbcTemplate().update("INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt, ts) " + - "VALUES (?, ?, ?, ?, ?, ?, @ts)", - new Object[]{mid, ridnew, uid, rid, attachment, txt}, - new int[]{Types.INTEGER, Types.SMALLINT, Types.INTEGER, Types.SMALLINT, Types.CHAR, Types.VARCHAR}); + "VALUES (?, ?, ?, ?, ?, ?, ?)", + mid, ridnew, uid, rid, attachment, txt, ts); if (ridnew > 0) { getJdbcTemplate().update( - "UPDATE messages SET replies = replies + 1, updated=@ts WHERE message_id = ?", - mid); + "UPDATE messages SET replies = replies + 1, updated=? WHERE message_id = ?", + ts, mid); } return ridnew; } @@ -197,9 +195,6 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return getJdbcTemplate().execute((ConnectionCallback<Integer>) conn -> { conn.setAutoCommit(false); final int replyNo; - try (PreparedStatement ps = conn.prepareStatement("START TRANSACTION")) { - ps.executeUpdate(); - } try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) { ps.setInt(1, mid); try (ResultSet resultSet = ps.executeQuery()) { @@ -317,7 +312,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "LEFT JOIN favorites " + "ON messages.message_id = favorites.message_id " + "WHERE messages.message_id = ? " - + "GROUP BY mid, rid, replyto, uid, nick, banned, ago, ts, readonly, " + + "GROUP BY mid, rid, replyto, uid, nick, banned, ago, messages.ts, readonly, " + "privacy, replies, attach, place_id, lat, lon, tags, repliesby, q", new MessageMapper(), mid); @@ -906,12 +901,12 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ return getJdbcTemplate().queryForList("SELECT replies.message_id FROM replies " + "INNER JOIN messages ON replies.message_id = messages.message_id " + "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " + - "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND replies.ts > DATE_ADD(now(), INTERVAL -2 HOUR) " + - "AND messages.popular=0 GROUP BY message_id having COUNT(DISTINCT(replies.user_id)) > 5 " + + "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND replies.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " + + "AND messages.popular=0 GROUP BY messages.message_id having COUNT(DISTINCT(replies.user_id)) > 5 " + "UNION ALL SELECT favorites.message_id FROM favorites " + "INNER JOIN messages ON messages.message_id = favorites.message_id " + "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " + - "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND favorites.ts > DATE_ADD(NOW(), INTERVAL -2 HOUR) " + - "AND messages.popular=0 GROUP BY message_id HAVING COUNT(DISTINCT favorites.user_id) > 1;", Integer.class); + "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND favorites.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " + + "AND messages.popular=0 GROUP BY messages.message_id HAVING COUNT(DISTINCT favorites.user_id) > 1;", Integer.class); } } |