22
33import lombok .RequiredArgsConstructor ;
44import net .javadiscord .javabot .data .h2db .message_cache .model .CachedMessage ;
5+ import org .jetbrains .annotations .NotNull ;
56
67import java .sql .*;
78import java .util .ArrayList ;
@@ -39,16 +40,18 @@ public boolean insert(CachedMessage message) throws SQLException {
3940 * @param messages The List to insert.
4041 * @throws SQLException If an error occurs.
4142 */
42- public void insertList (List <CachedMessage > messages ) throws SQLException {
43- try (PreparedStatement stmt = con .prepareStatement ("INSERT INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
43+ public void insertList (@ NotNull List <CachedMessage > messages ) throws SQLException {
44+ try (PreparedStatement stmt = con .prepareStatement ("MERGE INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
4445 Statement .RETURN_GENERATED_KEYS
4546 )) {
47+ con .setAutoCommit (false );
4648 for (CachedMessage msg : messages ) {
4749 stmt .setLong (1 , msg .getMessageId ());
4850 stmt .setLong (2 , msg .getAuthorId ());
4951 stmt .setString (3 , msg .getMessageContent ());
5052 stmt .executeUpdate ();
5153 }
54+ con .commit ();
5255 }
5356 }
5457
@@ -59,7 +62,7 @@ public void insertList(List<CachedMessage> messages) throws SQLException {
5962 * @return Whether there were rows affected by this process.
6063 * @throws SQLException If an error occurs.
6164 */
62- public boolean update (CachedMessage message ) throws SQLException {
65+ public boolean update (@ NotNull CachedMessage message ) throws SQLException {
6366 try (PreparedStatement stmt = con .prepareStatement ("UPDATE message_cache SET message_content = ? WHERE message_id = ?" ,
6467 Statement .RETURN_GENERATED_KEYS
6568 )) {
0 commit comments