|
11 | 11 | import simplexity.simplepms.saving.objects.PlayerSettings; |
12 | 12 |
|
13 | 13 | import java.sql.Connection; |
| 14 | +import java.sql.DatabaseMetaData; |
14 | 15 | import java.sql.PreparedStatement; |
15 | 16 | import java.sql.ResultSet; |
16 | 17 | import java.sql.SQLException; |
|
19 | 20 | import java.util.UUID; |
20 | 21 | import java.util.concurrent.CompletableFuture; |
21 | 22 |
|
22 | | -@SuppressWarnings({"CallToPrintStackTrace", "SqlResolve", "StringTemplateMigration"}) |
| 23 | +@SuppressWarnings({"CallToPrintStackTrace", "SqlResolve", "StringTemplateMigration", "SameParameterValue"}) |
23 | 24 | public class SqlHandler { |
24 | 25 |
|
25 | 26 | private SqlHandler() { |
@@ -60,6 +61,7 @@ player_uuid VARCHAR (36) NOT NULL PRIMARY KEY, |
60 | 61 | messages_disabled BOOLEAN NOT NULL |
61 | 62 | );"""); |
62 | 63 | playerSettingsInitStatement.execute(); |
| 64 | + updateDatabaseColumns(); |
63 | 65 | } catch (SQLException e) { |
64 | 66 | logger.warn("Failed to connect to database: {}", e.getMessage(), e); |
65 | 67 | } |
@@ -167,6 +169,67 @@ public void updateSettings(UUID playerUUID, boolean socialSpyEnabled, boolean me |
167 | 169 |
|
168 | 170 | } |
169 | 171 |
|
| 172 | + private void updateDatabaseColumns() { |
| 173 | + if (ConfigHandler.getInstance().isMysqlEnabled()) { |
| 174 | + doesMysqlColumnExist("blocklist", "blocked_player_name").thenAccept(exists -> { |
| 175 | + if (!exists) { |
| 176 | + addColumn("blocklist", "blocked_player_name", "VARCHAR(256)", ""); |
| 177 | + } |
| 178 | + }); |
| 179 | + } else { |
| 180 | + doesSqliteColumnExist("blocklist", "blocked_player_name").thenAccept(exists -> { |
| 181 | + if (!exists) { |
| 182 | + addColumn("blocklist", "blocked_player_name", "VARCHAR(256)", ""); |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private CompletableFuture<Boolean> doesSqliteColumnExist(String tableName, String columnName) { |
| 189 | + return CompletableFuture.supplyAsync(() -> { |
| 190 | + String query = "PRAGMA table_info(" + tableName + ")"; |
| 191 | + try (Connection connection = getConnection()) { |
| 192 | + PreparedStatement statement = connection.prepareStatement(query); |
| 193 | + ResultSet resultSet = statement.executeQuery(); |
| 194 | + while (resultSet.next()) { |
| 195 | + if (columnName.equalsIgnoreCase(resultSet.getString("name"))) { |
| 196 | + return true; |
| 197 | + } |
| 198 | + } |
| 199 | + } catch (SQLException e) { |
| 200 | + logger.warn("Failed to to check for column {} in table {}: {}", columnName, tableName, e.getMessage(), e); |
| 201 | + } |
| 202 | + return false; |
| 203 | + }); |
| 204 | + } |
| 205 | + |
| 206 | + private CompletableFuture<Boolean> doesMysqlColumnExist(String tableName, String columnName) { |
| 207 | + return CompletableFuture.supplyAsync(() -> { |
| 208 | + try (Connection connection = getConnection()) { |
| 209 | + DatabaseMetaData metaData = connection.getMetaData(); |
| 210 | + ResultSet resultSet = metaData.getColumns(null, null, tableName, columnName); |
| 211 | + return resultSet.next(); |
| 212 | + } catch (SQLException e) { |
| 213 | + logger.warn("Failed to check for column {} in table {}: {}", columnName, tableName, e.getMessage(), e); |
| 214 | + } |
| 215 | + return false; |
| 216 | + }); |
| 217 | + } |
| 218 | + |
| 219 | + // Possibly extremely cursed way to do this :cackle: |
| 220 | + |
| 221 | + private void addColumn(String tableName, String columnName, String dataType, String constraints) { |
| 222 | + String query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + dataType + constraints + ";"; |
| 223 | + try (Connection connection = getConnection()) { |
| 224 | + PreparedStatement statement = connection.prepareStatement(query); |
| 225 | + statement.executeUpdate(); |
| 226 | + logger.info("Added new column '{}' to table '{}'", columnName, tableName); |
| 227 | + } catch (SQLException e) { |
| 228 | + logger.warn("Failed to add new column {} to table {}: {}", columnName, tableName, e.getMessage(), e); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + |
170 | 233 | private void setupConfig() { |
171 | 234 | if (!ConfigHandler.getInstance().isMysqlEnabled()) { |
172 | 235 | hikariConfig.setJdbcUrl("jdbc:sqlite:" + SimplePMs.getInstance().getDataFolder() + "/simple-pms.db"); |
|
0 commit comments