66import org .springframework .dao .DataAccessException ;
77import org .springframework .jdbc .core .BatchPreparedStatementSetter ;
88import org .springframework .jdbc .core .JdbcTemplate ;
9- import org .springframework .jdbc .core .RowMapper ;
109import org .springframework .stereotype .Repository ;
1110
1211import java .sql .*;
12+ import java .util .ArrayList ;
13+ import java .util .LinkedHashMap ;
1314import java .util .List ;
15+ import java .util .Map ;
16+ import java .util .Map .Entry ;
1417
1518/**
1619 * Dao class that represents the QOTW_POINTS SQL Table.
2023public class MessageCacheRepository {
2124 private final JdbcTemplate jdbcTemplate ;
2225
23- /**
24- * Inserts a new {@link CachedMessage} object.
25- *
26- * @param message The Message to insert.
27- * @return Whether there were rows affected by this process.
28- * @throws SQLException If an error occurs.
29- */
30- public boolean insert (CachedMessage message ) throws DataAccessException {
31- int rows = jdbcTemplate .update (
32- "INSERT INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
33- message .getMessageId (), message .getAuthorId (), message .getMessageContent ());
34- return rows > 0 ;
35- }
36-
3726 /**
3827 * Inserts a {@link List} of {@link CachedMessage} objects.
3928 *
@@ -57,31 +46,30 @@ public int getBatchSize() {
5746 return messages .size ();
5847 }
5948 });
60- }
49+ List <Map .Entry <CachedMessage , Integer >> attachments =new ArrayList <>();
50+ for (CachedMessage msg : messages ) {
51+ for (int i = 0 ; i < msg .getAttachments ().size (); i ++) {
52+ attachments .add (Map .entry (msg , i ));
53+ }
54+ }
55+ jdbcTemplate .batchUpdate ("MERGE INTO message_cache_attachments (message_id, attachment_index, link) VALUES (?, ?, ?)" ,
56+ new BatchPreparedStatementSetter () {
6157
62- /**
63- * Edit an existing {@link CachedMessage} object.
64- *
65- * @param message The new Message object.
66- * @return Whether there were rows affected by this process.
67- * @throws SQLException If an error occurs.
68- */
69- public boolean update (@ NotNull CachedMessage message ) throws DataAccessException {
70- int rows = jdbcTemplate .update ("UPDATE message_cache SET message_content = ? WHERE message_id = ?" ,
71- message .getMessageContent (), message .getMessageId ());
72- return rows > 0 ;
73- }
58+ @ Override
59+ public void setValues (PreparedStatement stmt , int i ) throws SQLException {
60+ Entry <CachedMessage , Integer > entry = attachments .get (i );
61+ CachedMessage msg = entry .getKey ();
62+ Integer attachmentIndex = entry .getValue ();
63+ stmt .setLong (1 , msg .getMessageId ());
64+ stmt .setInt (2 , attachmentIndex );
65+ stmt .setString (3 , msg .getAttachments ().get (attachmentIndex ));
66+ }
7467
75- /**
76- * Deletes a single {@link CachedMessage} object from the Message Cache.
77- *
78- * @param messageId The message's id.
79- * @return Whether there were rows affected by this process.
80- * @throws SQLException If an error occurs.
81- */
82- public boolean delete (long messageId ) throws DataAccessException {
83- int rows = jdbcTemplate .update ("DELETE FROM message_cache WHERE message_id = ?" , messageId );
84- return rows > 0 ;
68+ @ Override
69+ public int getBatchSize () {
70+ return attachments .size ();
71+ }
72+ });
8573 }
8674
8775 /**
@@ -91,7 +79,17 @@ public boolean delete(long messageId) throws DataAccessException {
9179 * @throws SQLException If anything goes wrong.
9280 */
9381 public List <CachedMessage > getAll () throws DataAccessException {
94- return jdbcTemplate .query ("SELECT * FROM message_cache" ,(RowMapper <CachedMessage >) (rs , rowNum ) -> this .read (rs ));
82+ List <CachedMessage > messagesWithLink = jdbcTemplate .query (
83+ "SELECT * FROM message_cache LEFT JOIN message_cache_attachments ON message_cache.message_id = message_cache_attachments.message_id" ,
84+ (rs , rowNum ) -> this .read (rs ));
85+ Map <Long , CachedMessage > messages =new LinkedHashMap <>();
86+ for (CachedMessage msg : messagesWithLink ) {
87+ CachedMessage previous = messages .putIfAbsent (msg .getMessageId (), msg );
88+ if (previous !=null ) {
89+ previous .getAttachments ().addAll (msg .getAttachments ());
90+ }
91+ }
92+ return new ArrayList <>(messages .values ());
9593 }
9694
9795 /**
@@ -103,14 +101,22 @@ public List<CachedMessage> getAll() throws DataAccessException {
103101 */
104102 public boolean delete (int amount ) throws DataAccessException {
105103 int rows = jdbcTemplate .update ("DELETE FROM message_cache LIMIT ?" , amount );
106- return rows > 0 ;
104+ if (rows > 0 ){
105+ jdbcTemplate .update ("DELETE FROM message_cache_attachments WHERE message_id NOT IN (SELECT message_id FROM message_cache)" );
106+ return true ;
107+ }
108+ return false ;
107109 }
108110
109111 private CachedMessage read (ResultSet rs ) throws SQLException {
110112 CachedMessage cachedMessage = new CachedMessage ();
111- cachedMessage .setMessageId (rs .getLong ("message_id" ));
113+ cachedMessage .setMessageId (rs .getLong ("message_cache. message_id" ));
112114 cachedMessage .setAuthorId (rs .getLong ("author_id" ));
113115 cachedMessage .setMessageContent (rs .getString ("message_content" ));
116+ String attachment = rs .getString ("link" );
117+ if (attachment !=null ) {
118+ cachedMessage .getAttachments ().add (attachment );
119+ }
114120 return cachedMessage ;
115121 }
116122}
0 commit comments