Skip to content

Commit 7ce14da

Browse files
PotatoCraft-Studio#273 feat: mysql-connect-options settings
1 parent f161285 commit 7ce14da

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/main/java/org/maxgamer/quickshop/QuickShop.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,14 @@ private boolean setupDatabase() {
11841184
String port = dbCfg.getString("port");
11851185
String database = dbCfg.getString("database");
11861186
boolean useSSL = dbCfg.getBoolean("usessl");
1187-
dbCore = new MySQLCore(this, Objects.requireNonNull(host, "MySQL host can't be null"), Objects.requireNonNull(user, "MySQL username can't be null"), Objects.requireNonNull(pass, "MySQL password can't be null"), Objects.requireNonNull(database, "MySQL database name can't be null"), Objects.requireNonNull(port, "MySQL port can't be null"), useSSL);
1187+
Map<String, String> optionsMap = new HashMap<>();
1188+
for (String options : dbCfg.getStringList("mysql-connect-options")) {
1189+
String[] strings = options.split("=", 2);
1190+
if (strings.length == 2) {
1191+
optionsMap.put(strings[0], strings[1]);
1192+
}
1193+
}
1194+
dbCore = new MySQLCore(this, Objects.requireNonNull(host, "MySQL host can't be null"), Objects.requireNonNull(user, "MySQL username can't be null"), Objects.requireNonNull(pass, "MySQL password can't be null"), Objects.requireNonNull(database, "MySQL database name can't be null"), Objects.requireNonNull(port, "MySQL port can't be null"), useSSL, optionsMap);
11881195
} else {
11891196
// SQLite database - Doing this handles file creation
11901197
dbCore = new SQLiteCore(this, new File(this.getDataFolder(), "shops.db"));
@@ -2201,6 +2208,10 @@ private void updateConfig(int selectedVersion) throws IOException {
22012208
getConfig().set("shop.create-needs-select-type", false);
22022209
getConfig().set("config-version", ++selectedVersion);
22032210
}
2211+
if (selectedVersion == 161) {
2212+
getConfig().set("database.mysql-connect-options", new ArrayList<>(Arrays.asList("autoReconnect=true", "useUnicode=true", "characterEncoding=utf8")));
2213+
getConfig().set("config-version", ++selectedVersion);
2214+
}
22042215
if (getConfig().isSet("shop.shop")) {
22052216
getConfig().set("shop.shop", null);
22062217
}

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Convert.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import java.io.File;
3838
import java.util.ArrayList;
3939
import java.util.Collections;
40+
import java.util.HashMap;
4041
import java.util.List;
42+
import java.util.Map;
4143
import java.util.Objects;
4244
import java.util.logging.Level;
4345

@@ -84,10 +86,17 @@ public void onCommand(@NotNull ConsoleCommandSender sender, @NotNull String comm
8486
String port = dbCfg.getString("port");
8587
String databaseStr = dbCfg.getString("database");
8688
boolean useSSL = dbCfg.getBoolean("usessl");
89+
Map<String, String> optionsMap = new HashMap<>();
90+
for (String options : dbCfg.getStringList("mysql-connect-options")) {
91+
String[] strings = options.split("=", 2);
92+
if (strings.length == 2) {
93+
optionsMap.put(strings[0], strings[1]);
94+
}
95+
}
8796
running = true;
8897
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> {
8998
try {
90-
AbstractDatabaseCore dbCore = new MySQLCore(plugin, Objects.requireNonNull(host, "MySQL host can't be null"), Objects.requireNonNull(user, "MySQL username can't be null"), Objects.requireNonNull(pass, "MySQL password can't be null"), Objects.requireNonNull(databaseStr, "MySQL database name can't be null"), Objects.requireNonNull(port, "MySQL port can't be null"), useSSL);
99+
AbstractDatabaseCore dbCore = new MySQLCore(plugin, Objects.requireNonNull(host, "MySQL host can't be null"), Objects.requireNonNull(user, "MySQL username can't be null"), Objects.requireNonNull(pass, "MySQL password can't be null"), Objects.requireNonNull(databaseStr, "MySQL database name can't be null"), Objects.requireNonNull(port, "MySQL port can't be null"), useSSL, optionsMap);
91100
DatabaseManager databaseManager = new DatabaseManager(QuickShop.getInstance(), dbCore);
92101
sender.sendMessage(ChatColor.GREEN + "Converting...");
93102
transferShops(new SimpleDatabaseHelper(plugin, databaseManager), sender);

src/main/java/org/maxgamer/quickshop/database/MySQLCore.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.sql.SQLException;
2828
import java.util.ArrayList;
2929
import java.util.List;
30+
import java.util.Map;
3031
import java.util.Properties;
3132

3233
public class MySQLCore extends AbstractDatabaseCore {
@@ -52,14 +53,17 @@ public MySQLCore(
5253
@NotNull String pass,
5354
@NotNull String database,
5455
@NotNull String port,
55-
boolean useSSL) {
56+
boolean useSSL, Map<String, String> options) {
5657
this.plugin = plugin;
5758
info = new Properties();
5859
info.setProperty("autoReconnect", "true");
5960
info.setProperty("user", user);
6061
info.setProperty("password", pass);
6162
info.setProperty("useUnicode", "true");
6263
info.setProperty("characterEncoding", "utf8");
64+
for (Map.Entry<String, String> entry : options.entrySet()) {
65+
info.setProperty(entry.getKey(), entry.getValue());
66+
}
6367
//info.setProperty("maxReconnects", "65535");
6468
// info.setProperty("failOverReadOnly", "false");
6569
info.setProperty("useSSL", String.valueOf(useSSL));

src/main/resources/config.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# QuickShop-Reremake Plugin Configuration
22

33
#Do not touch this if you don't know what you're doing!
4-
config-version: 161
4+
config-version: 162
55

66
#Set the default language code the plugin should use, it will apply to:
77
#Item Name
@@ -135,7 +135,7 @@ use-caching: true
135135
#1=Adventure (Removed In Current Version)
136136
chat-type: 0
137137

138-
#MySQL database settings.
138+
#Mysql Database settings.
139139
database:
140140
#false = use local SQLite database.
141141
#true = use local/remote MySQL database.
@@ -145,15 +145,23 @@ database:
145145
database: quickshop
146146
user: root
147147
password: passwd
148-
#Set prefix to "none" to remove prefix
148+
#Table prefix, set prefix to "none" to remove prefix
149149
prefix: "qs_"
150+
#Whether to use ssl when using mysql
150151
usessl: false
151152
#Whether to use async queue?
152153
queue: true
153154
#How long should the interval between each commit be when using queue (in seconds)?
154155
queue-commit-interval: 2
155156
#Auto set table encoding to utf8mb4 (beta)
156157
auto-fix-encoding-issue-in-database: false
158+
#Connect options when using mysql
159+
#Each entry contains one option, using key=value format
160+
#Built-in options as follows, you can change value or add new option
161+
mysql-connect-options:
162+
- "autoReconnect=true"
163+
- "useUnicode=true"
164+
- "characterEncoding=utf8"
157165

158166
#Limits the number of shops a person can create and own at a time.
159167
limits:

0 commit comments

Comments
 (0)