Add optional MySQL implementation, part 2. See #312

This commit is contained in:
PikaMug 2020-11-18 05:09:18 -05:00
parent 31da631295
commit 7df3da82b3
4 changed files with 53 additions and 2 deletions

View File

@ -132,6 +132,12 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version> <version>8.0.22</version>
<exclusions>
<exclusion>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.zaxxer</groupId> <groupId>com.zaxxer</groupId>
@ -208,7 +214,7 @@
<goal>shade</goal> <goal>shade</goal>
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar> <minimizeJar>false</minimizeJar>
<createDependencyReducedPom>false</createDependencyReducedPom> <createDependencyReducedPom>false</createDependencyReducedPom>
</configuration> </configuration>
</execution> </execution>
@ -218,8 +224,26 @@
<artifactSet> <artifactSet>
<includes> <includes>
<include>me.*</include> <include>me.*</include>
<include>com.zaxxer:HikariCP</include>
<include>org.slf4j:slf4j-simple</include>
<include>org.slf4j:slf4j-api</include>
<include>mysql</include>
</includes> </includes>
</artifactSet> </artifactSet>
<relocations>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.blackvein.quests.libs.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>me.blackvein.quests.libs.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>com.mysql</pattern>
<shadedPattern>me.blackvein.quests.libs.mysql</shadedPattern>
</relocation>
</relocations>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -244,6 +244,8 @@ public class Quests extends JavaPlugin implements ConversationAbandonedListener
final Quester quester = getQuester(p.getUniqueId()); final Quester quester = getQuester(p.getUniqueId());
quester.saveData(); quester.saveData();
} }
getLogger().info("Closing storage...");
storage.close();
} }
public boolean isLoading() { public boolean isLoading() {

View File

@ -15,6 +15,8 @@ package me.blackvein.quests.storage.implementation.sql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID; import java.util.UUID;
import java.util.function.Function; import java.util.function.Function;
@ -62,6 +64,29 @@ public class SqlStorage implements StorageImplementation {
@Override @Override
public void init() throws Exception { public void init() throws Exception {
connectionFactory.init(plugin); connectionFactory.init(plugin);
try (Connection c = connectionFactory.getConnection()) {
final String createStatement = "CREATE TABLE IF NOT EXISTS `" + getTableName()
+ "` (`uuid` VARCHAR(36) NOT NULL, "
+ "`hasjournal` BOOL NOT NULL, "
+ "PRIMARY KEY (`uuid`)"
+ ") DEFAULT CHARSET = utf8mb4";
try (Statement s = c.createStatement()) {
try {
s.execute(createStatement);
} catch (final SQLException e) {
if (e.getMessage().contains("Unknown character set")) {
s.execute(createStatement.replace("utf8mb4", "utf8"));
} else {
throw e;
}
}
}
}
}
protected String getTableName() {
return this.statementProcessor.apply("{prefix}players");
} }
@Override @Override

View File

@ -37,7 +37,7 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
@Override @Override
protected void configureDatabase(final HikariConfig config, final String address, final String port, protected void configureDatabase(final HikariConfig config, final String address, final String port,
final String databaseName, final String username, final String password) { final String databaseName, final String username, final String password) {
config.setDriverClassName("com.mysql.cj.jdbc.NonRegisteringDriver"); config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + databaseName); config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + databaseName);
config.setUsername(username); config.setUsername(username);
config.setPassword(password); config.setPassword(password);