2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.storage;
|
2020-10-27 18:14:34 +00:00
|
|
|
|
2022-04-04 08:36:50 +00:00
|
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.PlexBase;
|
2020-10-27 18:14:34 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2020-10-29 02:10:47 +00:00
|
|
|
public class SQLConnection extends PlexBase
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2022-04-04 08:36:50 +00:00
|
|
|
private HikariDataSource dataSource;
|
2020-10-27 18:14:34 +00:00
|
|
|
|
2022-04-04 08:36:50 +00:00
|
|
|
public SQLConnection()
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2020-10-28 03:49:56 +00:00
|
|
|
String host = plugin.config.getString("data.central.hostname");
|
|
|
|
int port = plugin.config.getInt("data.central.port");
|
|
|
|
String username = plugin.config.getString("data.central.user");
|
|
|
|
String password = plugin.config.getString("data.central.password");
|
|
|
|
String database = plugin.config.getString("data.central.db");
|
2022-04-04 08:36:50 +00:00
|
|
|
|
|
|
|
HikariConfig config = new HikariConfig();
|
|
|
|
config.addDataSourceProperty("cachePrepStmts", "true");
|
|
|
|
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
|
|
|
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
|
|
|
|
|
|
|
this.dataSource = new HikariDataSource();
|
|
|
|
dataSource.setMaxLifetime(15000);
|
|
|
|
dataSource.setIdleTimeout(15000 * 2);
|
|
|
|
dataSource.setConnectionTimeout(15000 * 4);
|
|
|
|
dataSource.setMinimumIdle(2);
|
|
|
|
dataSource.setMaximumPoolSize(10);
|
2020-10-28 03:49:56 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (plugin.config.getString("data.central.storage").equalsIgnoreCase("sqlite"))
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2022-04-04 08:36:50 +00:00
|
|
|
dataSource.setJdbcUrl("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath());
|
2020-10-29 02:10:47 +00:00
|
|
|
plugin.setStorageType(StorageType.SQLITE);
|
2022-04-05 18:14:53 +00:00
|
|
|
}
|
|
|
|
else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
|
2020-10-27 18:14:34 +00:00
|
|
|
{
|
2020-10-29 02:35:14 +00:00
|
|
|
Class.forName("org.mariadb.jdbc.Driver");
|
2022-04-04 08:36:50 +00:00
|
|
|
dataSource.setJdbcUrl("jdbc:mariadb://" + host + ":" + port + "/" + database);
|
|
|
|
dataSource.setUsername(username);
|
|
|
|
dataSource.setPassword(password);
|
2021-01-03 07:21:15 +00:00
|
|
|
Plex.get().setStorageType(StorageType.MARIADB);
|
2020-10-27 18:14:34 +00:00
|
|
|
}
|
2022-04-05 18:14:53 +00:00
|
|
|
}
|
|
|
|
catch (ClassNotFoundException throwables)
|
2022-04-04 08:36:50 +00:00
|
|
|
{
|
|
|
|
throwables.printStackTrace();
|
2020-10-28 03:49:56 +00:00
|
|
|
}
|
2022-04-04 08:36:50 +00:00
|
|
|
|
|
|
|
try (Connection con = getCon())
|
|
|
|
{
|
|
|
|
con.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (" +
|
|
|
|
"`uuid` VARCHAR(46) NOT NULL, " +
|
|
|
|
"`name` VARCHAR(18), " +
|
2022-04-05 20:40:25 +00:00
|
|
|
"`login_msg` VARCHAR(2000), " +
|
|
|
|
"`prefix` VARCHAR(2000), " +
|
2022-04-04 08:36:50 +00:00
|
|
|
"`rank` VARCHAR(20), " +
|
|
|
|
"`ips` VARCHAR(2000), " +
|
|
|
|
"`coins` BIGINT, " +
|
|
|
|
"`vanished` BOOLEAN, " +
|
|
|
|
"`commandspy` BOOLEAN, " +
|
|
|
|
"PRIMARY KEY (`uuid`));").execute();
|
|
|
|
con.prepareStatement("CREATE TABLE IF NOT EXISTS `punishments` (" +
|
|
|
|
"`punished` VARCHAR(46) NOT NULL, " +
|
|
|
|
"`punisher` VARCHAR(46), " +
|
|
|
|
"`punishedUsername` VARCHAR(16), " +
|
|
|
|
"`ip` VARCHAR(2000), " +
|
|
|
|
"`type` VARCHAR(30), " +
|
|
|
|
"`reason` VARCHAR(2000), " +
|
|
|
|
"`customTime` BOOLEAN, " +
|
|
|
|
"`active` BOOLEAN, " +
|
|
|
|
"`endDate` BIGINT" +
|
|
|
|
");").execute();
|
2022-04-07 00:18:12 +00:00
|
|
|
con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" +
|
2022-04-07 02:00:28 +00:00
|
|
|
"`id` INT NOT NULL AUTO_INCREMENT, " +
|
2022-04-07 00:18:12 +00:00
|
|
|
"`uuid` VARCHAR(46) NOT NULL, " +
|
|
|
|
"`written_by` VARCHAR(16), " +
|
|
|
|
"`note` VARCHAR(2000), " +
|
2022-04-07 00:38:15 +00:00
|
|
|
"`timestamp` BIGINT" +
|
2022-04-07 00:18:12 +00:00
|
|
|
");").execute();
|
2022-04-05 18:14:53 +00:00
|
|
|
}
|
|
|
|
catch (SQLException throwables)
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-10-27 18:14:34 +00:00
|
|
|
throwables.printStackTrace();
|
|
|
|
}
|
2022-04-04 08:36:50 +00:00
|
|
|
}
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2022-04-04 08:36:50 +00:00
|
|
|
public Connection getCon()
|
|
|
|
{
|
2020-10-28 03:49:56 +00:00
|
|
|
try
|
|
|
|
{
|
2022-04-04 08:36:50 +00:00
|
|
|
return dataSource.getConnection();
|
2022-04-05 18:14:53 +00:00
|
|
|
}
|
|
|
|
catch (SQLException e)
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2022-04-04 08:36:50 +00:00
|
|
|
e.printStackTrace();
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|
2022-04-04 08:36:50 +00:00
|
|
|
return null;
|
2020-10-27 18:14:34 +00:00
|
|
|
}
|
|
|
|
}
|