33import lombok .RequiredArgsConstructor ;
44import net .javadiscord .javabot .data .h2db .message_cache .model .CachedMessage ;
55import org .jetbrains .annotations .NotNull ;
6+ import org .springframework .dao .DataAccessException ;
7+ import org .springframework .jdbc .core .BatchPreparedStatementSetter ;
8+ import org .springframework .jdbc .core .JdbcTemplate ;
9+ import org .springframework .jdbc .core .RowMapper ;
10+ import org .springframework .stereotype .Repository ;
611
712import java .sql .*;
8- import java .util .ArrayList ;
913import java .util .List ;
1014
1115/**
1216 * Dao class that represents the QOTW_POINTS SQL Table.
1317 */
1418@ RequiredArgsConstructor
19+ @ Repository
1520public class MessageCacheRepository {
16- private final Connection con ;
21+ private final JdbcTemplate jdbcTemplate ;
1722
1823 /**
1924 * Inserts a new {@link CachedMessage} object.
@@ -22,16 +27,11 @@ public class MessageCacheRepository {
2227 * @return Whether there were rows affected by this process.
2328 * @throws SQLException If an error occurs.
2429 */
25- public boolean insert (CachedMessage message ) throws SQLException {
26- try (PreparedStatement stmt = con .prepareStatement ("INSERT INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
27- Statement .RETURN_GENERATED_KEYS
28- )) {
29- stmt .setLong (1 , message .getMessageId ());
30- stmt .setLong (2 , message .getAuthorId ());
31- stmt .setString (3 , message .getMessageContent ());
32- int rows = stmt .executeUpdate ();
33- return rows > 0 ;
34- }
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 ;
3535 }
3636
3737 /**
@@ -40,19 +40,23 @@ public boolean insert(CachedMessage message) throws SQLException {
4040 * @param messages The List to insert.
4141 * @throws SQLException If an error occurs.
4242 */
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 (?, ?, ?)" ,
45- Statement .RETURN_GENERATED_KEYS
46- )) {
47- con .setAutoCommit (false );
48- for (CachedMessage msg : messages ) {
49- stmt .setLong (1 , msg .getMessageId ());
50- stmt .setLong (2 , msg .getAuthorId ());
51- stmt .setString (3 , msg .getMessageContent ());
52- stmt .executeUpdate ();
53- }
54- con .commit ();
55- }
43+ public void insertList (@ NotNull List <CachedMessage > messages ) throws DataAccessException {
44+ jdbcTemplate .batchUpdate ("MERGE INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
45+ new BatchPreparedStatementSetter () {
46+ @ Override
47+ public void setValues (PreparedStatement stmt , int i ) throws SQLException {
48+ CachedMessage msg = messages .get (i );
49+ stmt .setLong (1 , msg .getMessageId ());
50+ stmt .setLong (2 , msg .getAuthorId ());
51+ stmt .setString (3 , msg .getMessageContent ());
52+ stmt .executeUpdate ();
53+ }
54+
55+ @ Override
56+ public int getBatchSize () {
57+ return messages .size ();
58+ }
59+ });
5660 }
5761
5862 /**
@@ -62,15 +66,10 @@ public void insertList(@NotNull List<CachedMessage> messages) throws SQLExceptio
6266 * @return Whether there were rows affected by this process.
6367 * @throws SQLException If an error occurs.
6468 */
65- public boolean update (@ NotNull CachedMessage message ) throws SQLException {
66- try (PreparedStatement stmt = con .prepareStatement ("UPDATE message_cache SET message_content = ? WHERE message_id = ?" ,
67- Statement .RETURN_GENERATED_KEYS
68- )) {
69- stmt .setString (1 , message .getMessageContent ());
70- stmt .setLong (2 , message .getMessageId ());
71- int rows = stmt .executeUpdate ();
72- return rows > 0 ;
73- }
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 ;
7473 }
7574
7675 /**
@@ -80,14 +79,9 @@ public boolean update(@NotNull CachedMessage message) throws SQLException {
8079 * @return Whether there were rows affected by this process.
8180 * @throws SQLException If an error occurs.
8281 */
83- public boolean delete (long messageId ) throws SQLException {
84- try (PreparedStatement stmt = con .prepareStatement ("DELETE FROM message_cache WHERE message_id = ?" ,
85- Statement .RETURN_GENERATED_KEYS
86- )) {
87- stmt .setLong (1 , messageId );
88- int rows = stmt .executeUpdate ();
89- return rows > 0 ;
90- }
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 ;
9185 }
9286
9387 /**
@@ -96,15 +90,8 @@ public boolean delete(long messageId) throws SQLException {
9690 * @return A {@link List} of {@link CachedMessage}s.
9791 * @throws SQLException If anything goes wrong.
9892 */
99- public List <CachedMessage > getAll () throws SQLException {
100- try (PreparedStatement s = con .prepareStatement ("SELECT * FROM message_cache" )) {
101- ResultSet rs = s .executeQuery ();
102- List <CachedMessage > cachedMessages = new ArrayList <>();
103- while (rs .next ()) {
104- cachedMessages .add (this .read (rs ));
105- }
106- return cachedMessages ;
107- }
93+ public List <CachedMessage > getAll () throws DataAccessException {
94+ return jdbcTemplate .query ("SELECT * FROM message_cache" ,(RowMapper <CachedMessage >) (rs , rowNum ) -> this .read (rs ));
10895 }
10996
11097 /**
@@ -114,14 +101,9 @@ public List<CachedMessage> getAll() throws SQLException {
114101 * @return If any rows we're affected.
115102 * @throws SQLException If anything goes wrong.
116103 */
117- public boolean delete (int amount ) throws SQLException {
118- try (PreparedStatement stmt = con .prepareStatement ("DELETE FROM message_cache LIMIT ?" ,
119- Statement .RETURN_GENERATED_KEYS
120- )) {
121- stmt .setInt (1 , amount );
122- int rows = stmt .executeUpdate ();
123- return rows > 0 ;
124- }
104+ public boolean delete (int amount ) throws DataAccessException {
105+ int rows = jdbcTemplate .update ("DELETE FROM message_cache LIMIT ?" , amount );
106+ return rows > 0 ;
125107 }
126108
127109 private CachedMessage read (ResultSet rs ) throws SQLException {
0 commit comments