Create SQL data source utils class

- Extract logic used in SQLite and MySQL for logging and closing SQL objects
    - Decided to leave buildAuthFromResultSet methods individually as this might be more implementation-specific
- Rename DataSource#close to DataSource#closeConnection to fix conflict with static import
This commit is contained in:
ljacqu
2017-04-18 22:55:39 +02:00
parent 04ca36fe53
commit 07633a89c8
9 changed files with 226 additions and 69 deletions
@@ -21,6 +21,9 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.close;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
/**
*/
public class SQLite implements DataSource {
@@ -60,10 +63,6 @@ public class SQLite implements DataSource {
this.con = connection;
}
private static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error while executing SQL statement:", e);
}
private void connect() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
ConsoleLogger.info("SQLite driver loaded");
@@ -384,7 +383,7 @@ public class SQLite implements DataSource {
}
@Override
public void close() {
public void closeConnection() {
try {
if (con != null && !con.isClosed()) {
con.close();
@@ -394,36 +393,6 @@ public class SQLite implements DataSource {
}
}
private static void close(Statement st) {
if (st != null) {
try {
st.close();
} catch (SQLException ex) {
logSqlException(ex);
}
}
}
private static void close(Connection con) {
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
logSqlException(ex);
}
}
}
private static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
logSqlException(ex);
}
}
}
@Override
public List<String> getAllAuthsByIp(String ip) {
PreparedStatement pst = null;