mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-08-20 19:52:45 +02:00
Implemented MySQL for Sponge #564
This commit is contained in:
parent
3c81926ef8
commit
c67d57e5e9
|
@ -7,7 +7,9 @@ package com.djrapitops.plan.system.database;
|
|||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.system.database.databases.sql.MySQLDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SQLiteDB;
|
||||
import com.djrapitops.plan.system.database.databases.sql.SpongeMySQLDB;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plugin.api.Check;
|
||||
|
||||
/**
|
||||
* Bukkit Database system that initializes SQLite and MySQL database objects.
|
||||
|
@ -18,7 +20,7 @@ public class ServerDBSystem extends DBSystem {
|
|||
|
||||
@Override
|
||||
protected void initDatabase() throws DBInitException {
|
||||
databases.add(new MySQLDB());
|
||||
databases.add(Check.isSpongeAvailable() ? new SpongeMySQLDB() : new MySQLDB());
|
||||
databases.add(new SQLiteDB());
|
||||
|
||||
String dbType = Settings.DB_TYPE.toString().toLowerCase().trim();
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package com.djrapitops.plan.system.database.databases.sql;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import org.apache.commons.dbcp2.BasicDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
@ -12,7 +14,7 @@ import java.sql.SQLException;
|
|||
*/
|
||||
public class MySQLDB extends SQLDB {
|
||||
|
||||
private BasicDataSource dataSource;
|
||||
protected DataSource dataSource;
|
||||
|
||||
public MySQLDB() {
|
||||
}
|
||||
|
@ -21,8 +23,9 @@ public class MySQLDB extends SQLDB {
|
|||
* Setups the {@link BasicDataSource}
|
||||
*/
|
||||
@Override
|
||||
public void setupDataSource() {
|
||||
dataSource = new BasicDataSource();
|
||||
public void setupDataSource() throws DBInitException {
|
||||
BasicDataSource dataSource = new BasicDataSource();
|
||||
this.dataSource = dataSource;
|
||||
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
|
||||
|
||||
String host = Settings.DB_HOST.toString();
|
||||
|
@ -62,7 +65,9 @@ public class MySQLDB extends SQLDB {
|
|||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
dataSource.close();
|
||||
if (dataSource != null && dataSource instanceof BasicDataSource) {
|
||||
((BasicDataSource) dataSource).close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Log.toLog(this.getClass(), e);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package com.djrapitops.plan.system.database.databases.sql;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.service.sql.SqlService;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* MySQLDB implementation for Sponge since default driver is not available.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class SpongeMySQLDB extends MySQLDB {
|
||||
|
||||
@Override
|
||||
public void setupDataSource() throws DBInitException {
|
||||
Optional<SqlService> sqlServiceProvider = Sponge.getServiceManager().provide(SqlService.class);
|
||||
if (!sqlServiceProvider.isPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String host = Settings.DB_HOST.toString();
|
||||
String port = Integer.toString(Settings.DB_PORT.getNumber());
|
||||
String database = Settings.DB_DATABASE.toString();
|
||||
String launchOptions = Settings.DB_LAUNCH_OPTIONS.toString();
|
||||
if (launchOptions.isEmpty() || !launchOptions.startsWith("?") || launchOptions.endsWith("&")) {
|
||||
Log.error("Launch Options were faulty, using default (?rewriteBatchedStatements=true&useSSL=false)");
|
||||
launchOptions = "?rewriteBatchedStatements=true&useSSL=false";
|
||||
}
|
||||
|
||||
String url = host + ":" + port + "/" + database + launchOptions;
|
||||
String username = Settings.DB_USER.toString();
|
||||
String password = Settings.DB_PASS.toString();
|
||||
try {
|
||||
this.dataSource = sqlServiceProvider.get().getDataSource(
|
||||
"jdbc:mysql://" + username + ":" + password + "@" + url
|
||||
);
|
||||
} catch (SQLException e) {
|
||||
throw new DBInitException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user