diff --git a/build.gradle b/build.gradle index efaa29a..842fa96 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ repositories { dependencies { compile "org.spongepowered:spongeapi:${project.apiVersion}" - compile 'com.github.flibiostudio:utils:7233207' + compile 'com.github.flibiostudio:utils:164f3b6' } shadowJar { diff --git a/gradle.properties b/gradle.properties index 56d2cde..667bdf0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,5 +2,5 @@ name=EconomyLite owner=Flibio inceptionYear=2015 currentYear=2018 -version=2.14.3 +version=2.14.4 apiVersion=7.1.0-SNAPSHOT \ No newline at end of file diff --git a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java index f702c20..dd2534d 100644 --- a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java @@ -17,12 +17,16 @@ import org.spongepowered.api.service.economy.account.VirtualAccount; import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; +import javax.sql.DataSource; + public class PlayerServiceCommon implements PlayerEconService { private SqlManager manager; @@ -48,12 +52,9 @@ public PlayerServiceCommon(SqlManager manager, boolean h2) { private void repair(boolean h2) { if (h2) { - try { - ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEPLAYERS'").get(); - rs.next(); - rs.getString(1); + if (needRepair("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEPLAYERS'")) { logger.info("Repairing the database..."); - } catch (Exception e) { + } else { logger.debug("Database repairs not necessary!"); return; } @@ -66,12 +67,9 @@ private void repair(boolean h2) { logger.info("Repairs complete!"); return; } else { - try { - ResultSet rs = manager.executeQuery("show index from economyliteplayers where Column_name='uuid'").get(); - rs.next(); - rs.getString(1); + if (needRepair("show index from economyliteplayers where Column_name='uuid'")) { logger.info("Repairing the database..."); - } catch (Exception e) { + } else { logger.debug("Database repairs not necessary!"); return; } @@ -86,6 +84,29 @@ private void repair(boolean h2) { } } + /** + * Returns true if database needs to be repaired. + */ + private boolean needRepair(String sql) { + DataSource source = manager.getDataSource(); + try { + Connection con = source.getConnection(); + try { + PreparedStatement ps = con.prepareStatement(sql); + ps.closeOnCompletion(); + ResultSet rs = ps.executeQuery(); + rs.next(); + rs.getString(1); + } finally { + con.close(); + } + } catch (Exception e) { + return false; + } + + return true; + } + public boolean isWorking() { return manager.testConnection(); } @@ -183,18 +204,29 @@ public boolean setBalanceAll(BigDecimal balance, Currency currency, Cause cause) public List getAccountsMigration() { List accounts = new ArrayList<>(); - Optional rOpt = manager.executeQuery("SELECT * FROM economyliteplayers"); - if (rOpt.isPresent()) { - ResultSet rs = rOpt.get(); + try { + Connection con = manager.getDataSource().getConnection(); try { - while (rs.next()) { - accounts.add(rs.getString("uuid") + "%-%" + rs.getDouble("balance") + "%-%" + rs.getString("currency")); + PreparedStatement ps = con.prepareStatement("SELECT * FROM economyliteplayers"); + ps.closeOnCompletion(); + + ResultSet rs = ps.executeQuery(); + try { + while (rs.next()) { + accounts.add(rs.getString("uuid") + "%-%" + rs.getDouble("balance") + "%-%" + rs.getString("currency")); + } + rs.close(); + return accounts; + } catch (Exception e) { + logger.error(e.getMessage()); + return accounts; } - return accounts; - } catch (Exception e) { - logger.error(e.getMessage()); - return accounts; + + } finally { + con.close(); } + } catch (Exception e) { + e.printStackTrace(); } return accounts; } diff --git a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java index 105b91f..82a82f3 100644 --- a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java @@ -17,12 +17,16 @@ import org.spongepowered.api.service.economy.account.VirtualAccount; import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; +import javax.sql.DataSource; + public class VirtualServiceCommon implements VirtualEconService { private SqlManager manager; @@ -53,12 +57,9 @@ public VirtualServiceCommon(SqlManager manager, boolean h2) { private void repair(boolean h2) { if (h2) { - try { - ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEVIRTS'").get(); - rs.next(); - rs.getString(1); + if (needRepair("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEVIRTS'")) { logger.info("Repairing the database..."); - } catch (Exception e) { + } else { logger.debug("Database repairs not necessary!"); return; } @@ -71,12 +72,9 @@ private void repair(boolean h2) { logger.info("Repairs complete!"); return; } else { - try { - ResultSet rs = manager.executeQuery("show index from economylitevirts where Column_name='id'").get(); - rs.next(); - rs.getString(1); + if (needRepair("show index from economylitevirts where Column_name='id'")) { logger.info("Repairing the database..."); - } catch (Exception e) { + } else { logger.debug("Database repairs not necessary!"); return; } @@ -91,6 +89,29 @@ private void repair(boolean h2) { } } + /** + * Returns true if database needs to be repaired. + */ + private boolean needRepair(String sql) { + DataSource source = manager.getDataSource(); + try { + Connection con = source.getConnection(); + try { + PreparedStatement ps = con.prepareStatement(sql); + ps.closeOnCompletion(); + ResultSet rs = ps.executeQuery(); + rs.next(); + rs.getString(1); + } finally { + con.close(); + } + } catch (Exception e) { + return false; + } + + return true; + } + public boolean isWorking() { return manager.testConnection(); }