Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public class SQLiteUUIDService implements UUIDService, Consumer<List<UUIDMapping
public SQLiteUUIDService(final String fileName) {
this.sqlite =
new SQLite(FileUtils.getFile(PlotSquared.platform().getDirectory(), fileName));
try {
this.sqlite.openConnection();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}

try (PreparedStatement stmt = getConnection().prepareStatement(
"CREATE TABLE IF NOT EXISTS `usercache` (uuid VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, PRIMARY KEY (uuid))")) {
Expand Down
3 changes: 3 additions & 0 deletions Core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ dependencies {
api(libs.arkitektonika)
api(libs.paster)
api(libs.informativeAnnotations)

// Database
implementation(libs.hikaricp)
}

tasks.processResources {
Expand Down
21 changes: 0 additions & 21 deletions Core/src/main/java/com/plotsquared/core/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,6 @@
*/
public abstract class Database {

public abstract Connection forceConnection() throws SQLException, ClassNotFoundException;

/**
* Opens a connection with the database.
*
* @return Opened connection
* @throws SQLException if the connection can not be opened
* @throws ClassNotFoundException if the driver cannot be found
*/
public abstract Connection openConnection() throws SQLException, ClassNotFoundException;

/**
* Checks if a connection is open with the database.
*
* @return {@code true} if the connection is open
* @throws SQLException if the connection cannot be checked
*/
public abstract boolean checkConnection() throws SQLException;

/**
* Gets the connection with the database.
*
Expand All @@ -72,7 +53,6 @@ public abstract class Database {
* @param query Query to be run
* @return the results of the query
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/
public abstract ResultSet querySQL(String query) throws SQLException, ClassNotFoundException;

Expand All @@ -84,7 +64,6 @@ public abstract class Database {
* @param query Query to be run
* @return Result Code, see {@link Statement#executeUpdate(String)}
* @throws SQLException If the query cannot be executed
* @throws ClassNotFoundException If the driver cannot be found; see {@link #openConnection()}
*/
public abstract int updateSQL(String query) throws SQLException, ClassNotFoundException;

Expand Down
73 changes: 27 additions & 46 deletions Core/src/main/java/com/plotsquared/core/database/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package com.plotsquared.core.database;

import com.plotsquared.core.configuration.Storage;
import com.plotsquared.core.util.StringMan;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -35,12 +35,7 @@
*/
public class MySQL extends Database {

private final String user;
private final String database;
private final String password;
private final String port;
private final String hostname;
private Connection connection;
private final HikariDataSource hikariDataSource;

/**
* Creates a new MySQL instance.
Expand All @@ -52,66 +47,52 @@ public class MySQL extends Database {
* @param password Password
*/
public MySQL(String hostname, String port, String database, String username, String password) {
this.hostname = hostname;
this.port = port;
this.database = database;
this.user = username;
this.password = password;
this.connection = null;
}

@Override
public Connection forceConnection() throws SQLException {
this.connection = DriverManager.getConnection(
"jdbc:mysql://" + this.hostname + ':' + this.port + '/' + this.database + "?"
+ StringMan.join(Storage.MySQL.PROPERTIES, "&"), this.user, this.password);
return this.connection;
}

@Override
public Connection openConnection() throws SQLException {
if (checkConnection()) {
return this.connection;
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + hostname + ':' + port + '/' + database);
config.setUsername(username);
config.setPassword(password);
for (final String property : Storage.MySQL.PROPERTIES) {
if (property.contains("=")) {
String[] splittedProperty = property.split("=");
if (splittedProperty.length != 2) {
continue;
}
String key = splittedProperty[0];
String value = splittedProperty[1];
config.addDataSourceProperty(key,value);
}
}
return forceConnection();
}

@Override
public boolean checkConnection() throws SQLException {
return (this.connection != null) && !this.connection.isClosed();
this.hikariDataSource = new HikariDataSource(config);
}

@Override
public Connection getConnection() {
return this.connection;
try {
return this.hikariDataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
public boolean closeConnection() throws SQLException {
if (this.connection == null) {
if (this.hikariDataSource == null) {
return false;
}
this.connection.close();
this.connection = null;
this.hikariDataSource.close();
return true;
}

@Override
public ResultSet querySQL(String query) throws SQLException {
if (checkConnection()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
try (Statement statement = this.getConnection().createStatement()) {
return statement.executeQuery(query);
}
}

@Override
public int updateSQL(String query) throws SQLException {
if (checkConnection()) {
openConnection();
}
try (Statement statement = this.connection.createStatement()) {
try (Statement statement = this.getConnection().createStatement()) {
return statement.executeUpdate(query);
}
}
Expand Down
Loading