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

106 lines
3.9 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;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
public class SQLConnection extends PlexBase
{
private HikariDataSource dataSource;
public SQLConnection()
{
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-05 18:14:53 +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-05 18:14:53 +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), " +
"`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();
2022-04-05 18:14:53 +00:00
}
catch (SQLException throwables)
2020-10-28 03:49:56 +00:00
{
throwables.printStackTrace();
}
}
public Connection getCon()
{
2020-10-28 03:49:56 +00:00
try
{
return dataSource.getConnection();
2022-04-05 18:14:53 +00:00
}
catch (SQLException e)
2020-10-28 03:49:56 +00:00
{
e.printStackTrace();
}
return null;
}
}