Skip to content

Commit

Permalink
SQL forbedringer
Browse files Browse the repository at this point in the history
  • Loading branch information
Baktus79 committed Nov 11, 2021
1 parent 2a3949e commit 2323b44
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 252 deletions.
19 changes: 17 additions & 2 deletions src/main/java/no/vestlandetmc/fv/bukkit/FVBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ public void onEnable() {
Config.initialize();

MessageHandler.sendConsole("[" + getDescription().getPrefix() + "] Kontakter databasen...");
new MySqlPool().initialize();

try {
MySqlPool.getConnection();
new MySqlPool().initialize();
MessageHandler.sendConsole("[" + getDescription().getPrefix() + "] Kontakt med database er oppnådd");
this.getServer().getPluginManager().registerEvents(new PlayerListener(), this);
MySQLHandler.sqlEnabled = true;
Expand Down Expand Up @@ -80,4 +79,20 @@ private void downloadLibs() throws IOException {
}
}
}

@Override
public void onDisable() {
try {
MessageHandler.sendConsole("[" + getDescription().getPrefix() + "] Stenger databasen...");

if(!MySqlPool.getDataSource().getConnection().isClosed()) {
MySqlPool.getDataSource().getConnection().close();
}

MessageHandler.sendConsole("[" + getDescription().getPrefix() + "] Kontakten til database er stengt...");

} catch (final SQLException e) {
e.printStackTrace();
}
}
}
29 changes: 15 additions & 14 deletions src/main/java/no/vestlandetmc/fv/bukkit/commands/SubCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,24 @@ public void reload() {
if(!isConsole) { MessageHandler.sendMessage(player, message); }
else { MessageHandler.sendConsole(message); }

new MySqlPool().initialize();

try {
MySqlPool.getConnection();
if(!isConsole) { MessageHandler.sendMessage(player, sqlEnabled); }
else { MessageHandler.sendConsole(sqlEnabled); }
new MySqlPool().initialize();
} catch (final SQLException e) {
e.printStackTrace();
}

if(!isConsole) { MessageHandler.sendMessage(player, sqlSS); }
else { MessageHandler.sendConsole(sqlSS); }
if(!isConsole) { MessageHandler.sendMessage(player, sqlEnabled); }
else { MessageHandler.sendConsole(sqlEnabled); }

MySQLHandler.sqlEnabled = true;
} catch (final SQLException e) {
if(!isConsole) { MessageHandler.sendMessage(player, sqlDisable); }
else {
MySQLHandler.sqlEnabled = false;
MessageHandler.sendConsole(sqlDisable);
}
if(!isConsole) { MessageHandler.sendMessage(player, sqlSS); }
else { MessageHandler.sendConsole(sqlSS); }

MySQLHandler.sqlEnabled = true;

if(!isConsole) { MessageHandler.sendMessage(player, sqlDisable); }
else {
MySQLHandler.sqlEnabled = false;
MessageHandler.sendConsole(sqlDisable);
}

if(!isConsole) { MessageHandler.sendMessage(player, litebans); }
Expand Down
186 changes: 91 additions & 95 deletions src/main/java/no/vestlandetmc/fv/bukkit/database/MySQLHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,8 @@
public class MySQLHandler {

public static boolean sqlEnabled = false;
private Connection connection;

public MySQLHandler() {
try {
if(connection == null) {
connection = MySqlPool.getConnection();
}
} catch (final SQLException e) {
e.printStackTrace();
}
}

public void close() throws SQLException {
if (connection != null) {
connection.close();
}
}

public boolean execute(String sql) throws SQLException {
final boolean success = connection.createStatement().execute(sql);
return success;
}
public MySQLHandler() { }

/**
* Sjekk om en spiller har blitt varslet.
Expand All @@ -47,20 +27,25 @@ public boolean execute(String sql) throws SQLException {
*/
public boolean erVarslet(UUID uuid) throws SQLException {
final String sql = "SELECT uuid FROM fellesvarsling WHERE uuid=?";
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString());

final ResultSet set = statement.executeQuery();
try(Connection connection = MySqlPool.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(sql);) {
statement.setString(1, uuid.toString());

while (set.next()) {
return true;
}
final ResultSet set = statement.executeQuery();

if(connection != null)
connection.close();
while (set.next()) {
return true;
}

if(statement != null)
statement.close();
if(connection != null)
connection.close();

if(statement != null)
statement.close();

} catch(final Exception e) {
e.printStackTrace();
}

return false;

Expand All @@ -75,51 +60,55 @@ public boolean erVarslet(UUID uuid) throws SQLException {
*/
public void getInfo(Player player, UUID uuid) throws SQLException {
final String sql = "SELECT id, type, server, expire, reason FROM fellesvarsling WHERE uuid=? ORDER BY timestamp DESC";
final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString());

final String user = NameFetcher.getName(uuid);
final ResultSet set = statement.executeQuery();
final long unixTime = System.currentTimeMillis();
int i = 1;
try(Connection connection = MySqlPool.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(sql);) {
statement.setString(1, uuid.toString());

if(player == null) { MessageHandler.sendConsole("&e--- ==== &6Varslinger - " + user + " &e==== ---"); }
else { MessageHandler.sendMessage(player, "&e--- ==== &6Varslinger - " + user + " &e==== ---"); }
final String user = NameFetcher.getName(uuid);
final ResultSet set = statement.executeQuery();
final long unixTime = System.currentTimeMillis();
int i = 1;

while (set.next()) {
final int id = set.getInt("id");
final String type = set.getString("type");
final String server = set.getString("server");
String expire = set.getLong("expire") >= unixTime ? " &8[&aAktiv&8] " : " &8[&cUtgått&8] ";
final String reason = set.getString("reason");
if(player == null) { MessageHandler.sendConsole("&e--- ==== &6Varslinger - " + user + " &e==== ---"); }
else { MessageHandler.sendMessage(player, "&e--- ==== &6Varslinger - " + user + " &e==== ---"); }

String typeText = null;
while (set.next()) {
final int id = set.getInt("id");
final String type = set.getString("type");
final String server = set.getString("server");
String expire = set.getLong("expire") >= unixTime ? " &8[&aAktiv&8] " : " &8[&cUtgått&8] ";
final String reason = set.getString("reason");

if(set.getLong("expire") == -1) { expire = " &8[&aAktiv&8] "; }
else if(type.equals("manuell")) { expire = " "; }
String typeText = null;

if(type.equals("ban")) { typeText = "&eHar blitt utestengt" + expire + "&efra"; }
else if(type.equals("mute")) { typeText = "&eHar fått mute" + expire + "&epå"; }
else if(type.equals("warning")) { typeText = "&eHar fått advarsel" + expire + "&epå"; }
else if(type.equals("kick")) { typeText = "&eHar fått kick på"; }
else if(type.equals("manuell")) { typeText = "&eBle lagt til av"; }
if(set.getLong("expire") == -1) { expire = " &8[&aAktiv&8] "; }
else if(type.equals("manuell")) { expire = " "; }

final String messageConsole = "&eID: &6" + id + " " + typeText + " &6" + server + "&e: " + reason;
final String messagePlayer = "&eID: &6" + id + " " + typeText + " &6" + server + ".";
if(type.equals("ban")) { typeText = "&eHar blitt utestengt" + expire + "&efra"; }
else if(type.equals("mute")) { typeText = "&eHar fått mute" + expire + "&epå"; }
else if(type.equals("warning")) { typeText = "&eHar fått advarsel" + expire + "&epå"; }
else if(type.equals("kick")) { typeText = "&eHar fått kick på"; }
else if(type.equals("manuell")) { typeText = "&eBle lagt til av"; }

if(player == null) { MessageHandler.sendConsole(messageConsole); }
else { MessageHandler.clickableMessage(player, messagePlayer, reason, null); }
final String messageConsole = "&eID: &6" + id + " " + typeText + " &6" + server + "&e: " + reason;
final String messagePlayer = "&eID: &6" + id + " " + typeText + " &6" + server + ".";

i++;
if(player == null) { MessageHandler.sendConsole(messageConsole); }
else { MessageHandler.clickableMessage(player, messagePlayer, reason, null); }

if(i == 10) { break; }
}
i++;

if(connection != null)
connection.close();
if(i == 10) { break; }
}

if(statement != null)
statement.close();
if(connection != null)
connection.close();

if(statement != null)
statement.close();
} catch(final Exception e) {
e.printStackTrace();
}

}

Expand All @@ -135,23 +124,26 @@ public void getInfo(Player player, UUID uuid) throws SQLException {
public void setUser(UUID uuid, String type, long expire, String reason) throws SQLException {
final long timestamp = System.currentTimeMillis() / 1000L;
final String sql = "INSERT INTO fellesvarsling (uuid, type, server, timestamp, expire, reason) VALUES (?, ?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql);

statement.setString(1, uuid.toString());
statement.setString(2, type);
statement.setString(3, Config.SERVERNAME);
statement.setLong(4, timestamp);
statement.setLong(5, expire);
statement.setString(6, reason);
try(Connection connection = MySqlPool.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(sql);) {
statement.setString(1, uuid.toString());
statement.setString(2, type);
statement.setString(3, Config.SERVERNAME);
statement.setLong(4, timestamp);
statement.setLong(5, expire);
statement.setString(6, reason);

statement.executeUpdate();
statement.executeUpdate();

if(connection != null)
connection.close();
if(connection != null)
connection.close();

if(statement != null)
statement.close();
if(statement != null)
statement.close();

} catch(final Exception e) {
e.printStackTrace();
}
}

/**
Expand All @@ -162,23 +154,26 @@ public void setUser(UUID uuid, String type, long expire, String reason) throws S
* @throws SQLException
*/
public boolean deleteUser(int id) throws SQLException {
boolean complete;
boolean complete = false;
final String sql = "DELETE FROM fellesvarsling WHERE id=? AND server=?";
final PreparedStatement statement = connection.prepareStatement(sql);

statement.setInt(1, id);
statement.setString(2, Config.SERVERNAME);
try(Connection connection = MySqlPool.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(sql);) {
statement.setInt(1, id);
statement.setString(2, Config.SERVERNAME);

if(statement.executeUpdate() == 0) {
complete = false;
} else { complete = true; }
if(statement.executeUpdate() == 0) {
complete = false;
} else { complete = true; }


if(connection != null)
connection.close();
if(connection != null)
connection.close();

if(statement != null)
statement.close();
if(statement != null)
statement.close();
} catch(final Exception e) {
e.printStackTrace();
}

return complete;
}
Expand All @@ -193,16 +188,17 @@ public void deleteOld(int days) throws SQLException {
final long daysSeconds = days * 24 * 60 * 60;
final long unixTime = System.currentTimeMillis() / 1000L - daysSeconds;
final String sql = "DELETE FROM fellesvarsling WHERE timestamp<?";
final PreparedStatement statement = connection.prepareStatement(sql);

statement.setLong(1, unixTime);

if(connection != null)
connection.close();
try(Connection connection = MySqlPool.getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement(sql);) {
statement.setLong(1, unixTime);

if(statement != null)
statement.close();
if(connection != null)
connection.close();

if(statement != null)
statement.close();
} catch(final Exception e) {
e.printStackTrace();
}
}

}
34 changes: 21 additions & 13 deletions src/main/java/no/vestlandetmc/fv/bukkit/database/MySqlPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ public class MySqlPool {
private static HikariConfig cfg = new HikariConfig();
private static HikariDataSource ds;

public MySqlPool() {}
public MySqlPool() { }

public static Connection getConnection() throws SQLException {
return ds.getConnection();
public static HikariDataSource getDataSource() throws SQLException {
return ds;
}

public void initialize() {
public void initialize() throws SQLException {
if(Config.SQLTYPE.equalsIgnoreCase("mysql")) {
cfg.addDataSourceProperty("cachePrepStmts", "true");
cfg.addDataSourceProperty("prepStmtCacheSize", "250");
cfg.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
cfg.addDataSourceProperty("useServerPrepStmts", "true");
cfg.addDataSourceProperty("useLocalSessionState", "true");
cfg.addDataSourceProperty("rewriteBatchedStatements", "true");
cfg.addDataSourceProperty("cacheResultSetMetadata", "true");
cfg.addDataSourceProperty("cacheServerConfiguration", "true");
cfg.addDataSourceProperty("elideSetAutoCommits", "true");
cfg.addDataSourceProperty("maintainTimeStats", "false");
cfg.addDataSourceProperty("requireSSL", Config.ENABLE_SSL);
cfg.setJdbcUrl("jdbc:mysql://" + Config.HOST + ":" + Config.PORT + "/" + Config.DATABASE);
}

else if(Config.SQLTYPE.equalsIgnoreCase("mariadb")) {
cfg.setJdbcUrl("jdbc:mariadb://" + Config.HOST + ":" + Config.PORT + "/" + Config.DATABASE);
cfg.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
cfg.addDataSourceProperty("serverName", Config.HOST);
cfg.addDataSourceProperty("port", Config.PORT);
cfg.addDataSourceProperty("databaseName", Config.DATABASE);
}

else { return; }
Expand All @@ -39,7 +49,6 @@ else if(Config.SQLTYPE.equalsIgnoreCase("mariadb")) {
cfg.setMaxLifetime(Config.CON_LIFETIME);
cfg.addDataSourceProperty("user", Config.USER);
cfg.addDataSourceProperty("password", Config.PASSWORD);
cfg.addDataSourceProperty("requireSSL", Config.ENABLE_SSL);

ds = new HikariDataSource(cfg);

Expand All @@ -53,12 +62,11 @@ else if(Config.SQLTYPE.equalsIgnoreCase("mariadb")) {
+ "reason TEXT"
+ ")";

try {
Connection connection;
connection = getConnection();
connection.createStatement().execute(sql);
} catch (final SQLException e) {
e.printStackTrace();
}
Connection con;
con = getDataSource().getConnection();
con.createStatement().execute(sql);

if(con != null)
con.close();
}
}
Loading

0 comments on commit 2323b44

Please sign in to comment.