mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-30 20:21:50 +01:00
Fallback to utf8 encoding if utf8mb4 isn't supported (#1497)
This commit is contained in:
parent
544ffbad19
commit
821d1cb8dd
@ -61,6 +61,7 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.sql.BatchUpdateException;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@ -68,6 +69,7 @@ import java.sql.SQLException;
|
|||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -173,12 +175,13 @@ public class SqlStorage implements StorageImplementation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||||
try (Connection connection = this.connectionFactory.getConnection()) {
|
List<String> queries = new LinkedList<>();
|
||||||
try (Statement s = connection.createStatement()) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
if (line.startsWith("--") || line.startsWith("#")) continue;
|
if (line.startsWith("--") || line.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
|
|
||||||
@ -187,13 +190,43 @@ public class SqlStorage implements StorageImplementation {
|
|||||||
sb.deleteCharAt(sb.length() - 1);
|
sb.deleteCharAt(sb.length() - 1);
|
||||||
|
|
||||||
String result = this.statementProcessor.apply(sb.toString().trim());
|
String result = this.statementProcessor.apply(sb.toString().trim());
|
||||||
if (!result.isEmpty()) s.addBatch(result);
|
if (!result.isEmpty()) {
|
||||||
|
queries.add(result);
|
||||||
|
}
|
||||||
|
|
||||||
// reset
|
// reset
|
||||||
sb = new StringBuilder();
|
sb = new StringBuilder();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try (Connection connection = this.connectionFactory.getConnection()) {
|
||||||
|
boolean utf8mb4Unsupported = false;
|
||||||
|
|
||||||
|
try (Statement s = connection.createStatement()) {
|
||||||
|
for (String query : queries) {
|
||||||
|
s.addBatch(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
s.executeBatch();
|
s.executeBatch();
|
||||||
|
} catch (BatchUpdateException e) {
|
||||||
|
if (e.getMessage().contains("Unknown character set")) {
|
||||||
|
utf8mb4Unsupported = true;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// try again
|
||||||
|
if (utf8mb4Unsupported) {
|
||||||
|
try (Statement s = connection.createStatement()) {
|
||||||
|
for (String query : queries) {
|
||||||
|
s.addBatch(query.replace("utf8mb4", "utf8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
s.executeBatch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user