Plex/server/src/main/java/dev/plex/storage/SQLConnection.java

134 lines
4.8 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.storage;
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;
2023-03-08 20:26:10 +00:00
import lombok.Getter;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
2022-04-07 07:37:31 +00:00
@Getter
2022-04-17 05:27:04 +00:00
public class SQLConnection implements PlexBase
{
private HikariDataSource dataSource;
public SQLConnection()
{
2022-04-07 07:37:31 +00:00
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("sqlite") && !plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
{
2022-04-09 05:08:03 +00:00
return;
2022-04-07 07:37:31 +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");
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"))
{
dataSource.setJdbcUrl("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath());
plugin.setStorageType(StorageType.SQLITE);
2022-04-09 05:08:03 +00:00
}
else if (plugin.config.getString("data.central.storage").equalsIgnoreCase("mariadb"))
{
2020-10-29 02:35:14 +00:00
Class.forName("org.mariadb.jdbc.Driver");
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);
}
2022-04-09 05:08:03 +00:00
}
catch (ClassNotFoundException throwables)
{
throwables.printStackTrace();
2020-10-28 03:49:56 +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), " +
"`rank` VARCHAR(20), " +
2022-08-03 00:03:04 +00:00
"`adminActive` BOOLEAN, " +
"`staffChat` BOOLEAN, " +
"`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:05:29 +00:00
"`id` INT NOT NULL, " +
2022-04-07 00:18:12 +00:00
"`uuid` VARCHAR(46) NOT NULL, " +
2022-04-07 02:39:45 +00:00
"`written_by` VARCHAR(46), " +
2022-04-07 00:18:12 +00:00
"`note` VARCHAR(2000), " +
2022-04-07 00:38:15 +00:00
"`timestamp` BIGINT" +
2022-04-07 00:18:12 +00:00
");").execute();
con.prepareStatement("CREATE TABLE IF NOT EXISTS `permissions` (" +
"`uuid` VARCHAR(46) NOT NULL," +
"`permission` VARCHAR(1000) NOT NULL," +
"`allowed` BOOLEAN" +
2022-04-13 02:08:12 +00:00
");").execute();
2022-08-03 00:03:04 +00:00
// Plex 1.2
2023-03-08 20:26:10 +00:00
try
{
2022-08-03 00:03:04 +00:00
con.prepareStatement("ALTER TABLE `players` ADD COLUMN `staffChat` BOOLEAN DEFAULT false;").execute();
2023-03-08 20:26:10 +00:00
}
catch (SQLException ignored)
{
}
2022-04-09 05:08:03 +00:00
}
catch (SQLException throwables)
2020-10-28 03:49:56 +00:00
{
throwables.printStackTrace();
}
}
public Connection getCon()
{
2022-04-07 07:37:31 +00:00
if (this.dataSource == null)
{
return null;
}
2020-10-28 03:49:56 +00:00
try
{
return dataSource.getConnection();
2022-04-09 05:08:03 +00:00
}
catch (SQLException e)
2020-10-28 03:49:56 +00:00
{
e.printStackTrace();
}
return null;
}
}