same fix for normal sqlite

This commit is contained in:
DNx5 2015-09-15 16:31:07 +07:00
parent 6f22aa779b
commit c473e81eb9

View File

@ -1,19 +1,13 @@
package fr.xephi.authme.datasource; package fr.xephi.authme.datasource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SQLite implements DataSource { public class SQLite implements DataSource {
private String database; private String database;
@ -34,7 +28,7 @@ public class SQLite implements DataSource {
private String columnLogged; private String columnLogged;
private String columnRealName; private String columnRealName;
public SQLite() { public SQLite() throws ClassNotFoundException, SQLException {
this.database = Settings.getMySQLDatabase; this.database = Settings.getMySQLDatabase;
this.tableName = Settings.getMySQLTablename; this.tableName = Settings.getMySQLTablename;
this.columnName = Settings.getMySQLColumnName; this.columnName = Settings.getMySQLColumnName;
@ -55,29 +49,13 @@ public class SQLite implements DataSource {
try { try {
this.connect(); this.connect();
this.setup(); this.setup();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException | SQLException cnf) {
ConsoleLogger.showError(e.getMessage()); ConsoleLogger.showError("Can't use SQLITE... !");
if (Settings.isStopEnabled) { throw cnf;
ConsoleLogger.showError("Can't use SQLITE... ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
} catch (SQLException e) {
ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use SQLITE... ! SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown();
}
if (!Settings.isStopEnabled)
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
return;
} }
} }
private synchronized void connect() private synchronized void connect() throws ClassNotFoundException, SQLException {
throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
ConsoleLogger.info("SQLite driver loaded"); ConsoleLogger.info("SQLite driver loaded");
this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db"); this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db");
@ -273,7 +251,7 @@ public class SQLite implements DataSource {
public List<String> autoPurgeDatabase(long until) { public List<String> autoPurgeDatabase(long until) {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<>();
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
pst.setLong(1, until); pst.setLong(1, until);
@ -284,7 +262,7 @@ public class SQLite implements DataSource {
return list; return list;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -423,7 +401,7 @@ public class SQLite implements DataSource {
public List<String> getAllAuthsByName(PlayerAuth auth) { public List<String> getAllAuthsByName(PlayerAuth auth) {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
pst.setString(1, auth.getIp()); pst.setString(1, auth.getIp());
@ -434,9 +412,9 @@ public class SQLite implements DataSource {
return countIp; return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -447,7 +425,7 @@ public class SQLite implements DataSource {
public List<String> getAllAuthsByIp(String ip) { public List<String> getAllAuthsByIp(String ip) {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countIp = new ArrayList<String>(); List<String> countIp = new ArrayList<>();
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
pst.setString(1, ip); pst.setString(1, ip);
@ -458,9 +436,9 @@ public class SQLite implements DataSource {
return countIp; return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -471,7 +449,7 @@ public class SQLite implements DataSource {
public List<String> getAllAuthsByEmail(String email) { public List<String> getAllAuthsByEmail(String email) {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
List<String> countEmail = new ArrayList<String>(); List<String> countEmail = new ArrayList<>();
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
pst.setString(1, email); pst.setString(1, email);
@ -482,9 +460,9 @@ public class SQLite implements DataSource {
return countEmail; return countEmail;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return new ArrayList<String>(); return new ArrayList<>();
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
return new ArrayList<String>(); return new ArrayList<>();
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);
@ -542,11 +520,9 @@ public class SQLite implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
} }
return;
} }
@Override @Override
@ -560,11 +536,9 @@ public class SQLite implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
} }
return;
} }
@Override @Override
@ -577,18 +551,16 @@ public class SQLite implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
} }
return;
} }
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
int result = 0; int result = 0;
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
try { try {
pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
rs = pst.executeQuery(); rs = pst.executeQuery();
@ -614,23 +586,21 @@ public class SQLite implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return;
} finally { } finally {
close(pst); close(pst);
} }
return;
} }
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>(); List<PlayerAuth> auths = new ArrayList<>();
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = null; PlayerAuth pAuth;
if (rs.getString(columnIp).isEmpty()) { if (rs.getString(columnIp).isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} else { } else {
@ -640,8 +610,7 @@ public class SQLite implements DataSource {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} }
} }
if (pAuth != null) auths.add(pAuth);
auths.add(pAuth);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
@ -654,14 +623,14 @@ public class SQLite implements DataSource {
@Override @Override
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>(); List<PlayerAuth> auths = new ArrayList<>();
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs;
try { try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;"); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLogged + "=1;");
rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = null; PlayerAuth pAuth;
if (rs.getString(columnIp).isEmpty()) { if (rs.getString(columnIp).isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} else { } else {
@ -671,8 +640,7 @@ public class SQLite implements DataSource {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName));
} }
} }
if (pAuth != null) auths.add(pAuth);
auths.add(pAuth);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());