@@ -111,90 +111,75 @@ private Poll loadPollFromResultSet(ResultSet rs) throws SQLException {
111111 }
112112
113113 private void loadVotesForPoll (Poll poll ) {
114- try (PreparedStatement stmt = connection .prepareStatement (
115- "SELECT * FROM votes WHERE poll_id = ?" )) {
116- stmt .setString (1 , poll .getId ());
117-
118- try (ResultSet rs = stmt .executeQuery ()) {
114+ executeQuery ("SELECT * FROM votes WHERE poll_id = ?" ,
115+ stmt -> stmt .setString (1 , poll .getId ()),
116+ rs -> {
119117 while (rs .next ()) {
120118 String playerUuid = rs .getString ("player_uuid" );
121119 String option = rs .getString ("option" );
122120
123121 poll .getPlayerVotes ().put (UUID .fromString (playerUuid ), option );
124122 poll .getVotes ().put (option , poll .getVotes ().getOrDefault (option , 0 ) + 1 );
125123 }
126- }
127- } catch (SQLException e ) {
128- plugin .getLogger ().severe ("Failed to load votes for poll " + poll .getId () + ": " + e .getMessage ());
129- }
124+ },
125+ "Failed to load votes for poll " + poll .getId ()
126+ );
130127 }
131128
132129 public void savePoll (Poll poll ) {
133- try (PreparedStatement stmt = connection .prepareStatement (
134- "INSERT INTO polls (id, question, options, created_at, expires_at, created_by, active) VALUES (?, ?, ?, ?, ?, ?, ?)" )) {
135- stmt .setInt (1 , Integer .parseInt (poll .getId ()));
136- stmt .setString (2 , poll .getQuestion ());
137- stmt .setString (3 , String .join ("|" , poll .getOptions ()));
138- stmt .setString (4 , poll .getCreatedAt ().format (DATE_FORMATTER ));
139- stmt .setString (5 , poll .getExpiresAt ().format (DATE_FORMATTER ));
140- stmt .setString (6 , poll .getCreatedBy ());
141- stmt .setInt (7 , poll .isActive () ? 1 : 0 );
142-
143- stmt .executeUpdate ();
144- activePolls .put (poll .getId (), poll );
145- } catch (SQLException e ) {
146- plugin .getLogger ().severe ("Failed to save poll: " + e .getMessage ());
147- }
130+ executeUpdate ("INSERT INTO polls (id, question, options, created_at, expires_at, created_by, active) VALUES (?, ?, ?, ?, ?, ?, ?)" ,
131+ stmt -> {
132+ stmt .setInt (1 , Integer .parseInt (poll .getId ()));
133+ stmt .setString (2 , poll .getQuestion ());
134+ stmt .setString (3 , String .join ("|" , poll .getOptions ()));
135+ stmt .setString (4 , poll .getCreatedAt ().format (DATE_FORMATTER ));
136+ stmt .setString (5 , poll .getExpiresAt ().format (DATE_FORMATTER ));
137+ stmt .setString (6 , poll .getCreatedBy ());
138+ stmt .setInt (7 , poll .isActive () ? 1 : 0 );
139+ },
140+ "Failed to save poll"
141+ );
142+ activePolls .put (poll .getId (), poll );
148143 }
149144
150145 public void saveVote (String pollId , UUID playerId , String option ) {
151- try (PreparedStatement stmt = connection .prepareStatement (
152- "INSERT INTO votes (poll_id, player_uuid, option, voted_at) VALUES (?, ?, ?, ?)" )) {
153- stmt .setInt (1 , Integer .parseInt (pollId ));
154- stmt .setString (2 , playerId .toString ());
155- stmt .setString (3 , option );
156- stmt .setString (4 , LocalDateTime .now ().format (DATE_FORMATTER ));
157-
158- stmt .executeUpdate ();
159- } catch (SQLException e ) {
160- plugin .getLogger ().severe ("Failed to save vote: " + e .getMessage ());
161- }
146+ executeUpdate ("INSERT INTO votes (poll_id, player_uuid, option, voted_at) VALUES (?, ?, ?, ?)" ,
147+ stmt -> {
148+ stmt .setInt (1 , Integer .parseInt (pollId ));
149+ stmt .setString (2 , playerId .toString ());
150+ stmt .setString (3 , option );
151+ stmt .setString (4 , LocalDateTime .now ().format (DATE_FORMATTER ));
152+ },
153+ "Failed to save vote"
154+ );
162155 }
163156
164157 public void updatePollStatus (String pollId , boolean active ) {
165- try (PreparedStatement stmt = connection .prepareStatement (
166- "UPDATE polls SET active = ? WHERE id = ?" )) {
167- stmt .setInt (1 , active ? 1 : 0 );
168- stmt .setInt (2 , Integer .parseInt (pollId ));
169-
170- stmt .executeUpdate ();
171-
172- if (!active ) {
173- activePolls .remove (pollId );
174- }
175- } catch (SQLException e ) {
176- plugin .getLogger ().severe ("Failed to update poll status: " + e .getMessage ());
158+ executeUpdate ("UPDATE polls SET active = ? WHERE id = ?" ,
159+ stmt -> {
160+ stmt .setInt (1 , active ? 1 : 0 );
161+ stmt .setInt (2 , Integer .parseInt (pollId ));
162+ },
163+ "Failed to update poll status"
164+ );
165+
166+ if (!active ) {
167+ activePolls .remove (pollId );
177168 }
178169 }
179170
180171 public void deletePoll (String pollId ) {
181- try {
182- try (PreparedStatement stmt = connection .prepareStatement (
183- "DELETE FROM votes WHERE poll_id = ?" )) {
184- stmt .setInt (1 , Integer .parseInt (pollId ));
185- stmt .executeUpdate ();
186- }
187-
188- try (PreparedStatement stmt = connection .prepareStatement (
189- "DELETE FROM polls WHERE id = ?" )) {
190- stmt .setInt (1 , Integer .parseInt (pollId ));
191- stmt .executeUpdate ();
192- }
193-
194- activePolls .remove (pollId );
195- } catch (SQLException e ) {
196- plugin .getLogger ().severe ("Failed to delete poll: " + e .getMessage ());
197- }
172+ executeUpdate ("DELETE FROM votes WHERE poll_id = ?" ,
173+ stmt -> stmt .setInt (1 , Integer .parseInt (pollId )),
174+ "Failed to delete votes for poll"
175+ );
176+
177+ executeUpdate ("DELETE FROM polls WHERE id = ?" ,
178+ stmt -> stmt .setInt (1 , Integer .parseInt (pollId )),
179+ "Failed to delete poll"
180+ );
181+
182+ activePolls .remove (pollId );
198183 }
199184
200185 public Collection <Poll > getActivePolls () {
@@ -206,20 +191,17 @@ public Poll getPoll(String pollId) {
206191 }
207192
208193 public void cleanupExpiredPolls () {
209- try (PreparedStatement stmt = connection .prepareStatement (
210- "SELECT id FROM polls WHERE active = 1 AND expires_at <= ?" )) {
211- stmt .setString (1 , LocalDateTime .now ().format (DATE_FORMATTER ));
212-
213- try (ResultSet rs = stmt .executeQuery ()) {
194+ executeQuery ("SELECT id FROM polls WHERE active = 1 AND expires_at <= ?" ,
195+ stmt -> stmt .setString (1 , LocalDateTime .now ().format (DATE_FORMATTER )),
196+ rs -> {
214197 while (rs .next ()) {
215198 String pollId = String .valueOf (rs .getInt ("id" ));
216199 plugin .getLogger ().info ("Cleaning up expired poll " + pollId );
217200 updatePollStatus (pollId , false );
218201 }
219- }
220- } catch (SQLException e ) {
221- plugin .getLogger ().severe ("Failed to cleanup expired polls: " + e .getMessage ());
222- }
202+ },
203+ "Failed to cleanup expired polls"
204+ );
223205 }
224206
225207 public String getNextPollId () {
@@ -235,4 +217,30 @@ public void shutdown() {
235217 }
236218 }
237219 }
220+
221+ private void executeQuery (String sql , SQLConsumer <PreparedStatement > parameterSetter ,
222+ SQLConsumer <ResultSet > resultProcessor , String errorMessage ) {
223+ try (PreparedStatement stmt = connection .prepareStatement (sql )) {
224+ parameterSetter .accept (stmt );
225+ try (ResultSet rs = stmt .executeQuery ()) {
226+ resultProcessor .accept (rs );
227+ }
228+ } catch (SQLException e ) {
229+ plugin .getLogger ().severe (errorMessage + ": " + e .getMessage ());
230+ }
231+ }
232+
233+ private void executeUpdate (String sql , SQLConsumer <PreparedStatement > parameterSetter , String errorMessage ) {
234+ try (PreparedStatement stmt = connection .prepareStatement (sql )) {
235+ parameterSetter .accept (stmt );
236+ stmt .executeUpdate ();
237+ } catch (SQLException e ) {
238+ plugin .getLogger ().severe (errorMessage + ": " + e .getMessage ());
239+ }
240+ }
241+
242+ @ FunctionalInterface
243+ private interface SQLConsumer <T > {
244+ void accept (T t ) throws SQLException ;
245+ }
238246}
0 commit comments