Cleanup - reduce duplication in MySQL class

This commit is contained in:
ljacqu 2016-07-02 08:06:17 +02:00
parent 1cadbd2345
commit 73a9b5ce0c

View File

@ -31,34 +31,23 @@ import java.util.Set;
public class MySQL implements DataSource { public class MySQL implements DataSource {
private final String host; private String host;
private final String port; private String port;
private final String username; private String username;
private final String password; private String password;
private final String database; private String database;
private final String tableName; private String tableName;
private final List<String> columnOthers; private List<String> columnOthers;
private final Columns col; private Columns col;
private final HashAlgorithm hashAlgorithm; private HashAlgorithm hashAlgorithm;
private HikariDataSource ds; private HikariDataSource ds;
private final String phpBbPrefix; private String phpBbPrefix;
private final int phpBbGroup; private int phpBbGroup;
private final String wordpressPrefix; private String wordpressPrefix;
public MySQL(NewSetting settings) throws ClassNotFoundException, SQLException, PoolInitializationException { public MySQL(NewSetting settings) throws ClassNotFoundException, SQLException, PoolInitializationException {
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST); setParameters(settings);
this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT);
this.username = settings.getProperty(DatabaseSettings.MYSQL_USERNAME);
this.password = settings.getProperty(DatabaseSettings.MYSQL_PASSWORD);
this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE);
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
this.columnOthers = settings.getProperty(HooksSettings.MYSQL_OTHER_USERNAME_COLS);
this.col = new Columns(settings);
this.hashAlgorithm = settings.getProperty(SecuritySettings.PASSWORD_HASH);
this.phpBbPrefix = settings.getProperty(HooksSettings.PHPBB_TABLE_PREFIX);
this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID);
this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX);
// Set the connection arguments (and check if connection is ok) // Set the connection arguments (and check if connection is ok)
try { try {
@ -67,7 +56,7 @@ public class MySQL implements DataSource {
if (e instanceof IllegalArgumentException) { if (e instanceof IllegalArgumentException) {
ConsoleLogger.showError("Invalid database arguments! Please check your configuration!"); ConsoleLogger.showError("Invalid database arguments! Please check your configuration!");
ConsoleLogger.showError("If this error persists, please report it to the developer!"); ConsoleLogger.showError("If this error persists, please report it to the developer!");
throw new IllegalArgumentException(e); throw e;
} }
if (e instanceof PoolInitializationException) { if (e instanceof PoolInitializationException) {
ConsoleLogger.showError("Can't initialize database connection! Please check your configuration!"); ConsoleLogger.showError("Can't initialize database connection! Please check your configuration!");
@ -91,6 +80,11 @@ public class MySQL implements DataSource {
@VisibleForTesting @VisibleForTesting
MySQL(NewSetting settings, HikariDataSource hikariDataSource) { MySQL(NewSetting settings, HikariDataSource hikariDataSource) {
ds = hikariDataSource;
setParameters(settings);
}
private void setParameters(NewSetting settings) {
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST); this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT); this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT);
this.username = settings.getProperty(DatabaseSettings.MYSQL_USERNAME); this.username = settings.getProperty(DatabaseSettings.MYSQL_USERNAME);
@ -103,7 +97,6 @@ public class MySQL implements DataSource {
this.phpBbPrefix = settings.getProperty(HooksSettings.PHPBB_TABLE_PREFIX); this.phpBbPrefix = settings.getProperty(HooksSettings.PHPBB_TABLE_PREFIX);
this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID); this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID);
this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX); this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX);
ds = hikariDataSource;
} }
private void setConnectionArguments() throws RuntimeException { private void setConnectionArguments() throws RuntimeException {
@ -148,9 +141,7 @@ public class MySQL implements DataSource {
} }
private void setupConnection() throws SQLException { private void setupConnection() throws SQLException {
try (Connection con = getConnection()) { try (Connection con = getConnection(); Statement st = con.createStatement()) {
Statement st = con.createStatement();
DatabaseMetaData md = con.getMetaData();
// Create table if not exists. // Create table if not exists.
String sql = "CREATE TABLE IF NOT EXISTS " + tableName + " (" String sql = "CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ col.ID + " INTEGER AUTO_INCREMENT," + col.ID + " INTEGER AUTO_INCREMENT,"
@ -169,96 +160,77 @@ public class MySQL implements DataSource {
+ ");"; + ");";
st.executeUpdate(sql); st.executeUpdate(sql);
ResultSet rs = md.getColumns(null, null, tableName, col.NAME); DatabaseMetaData md = con.getMetaData();
if (!rs.next()) { if (isColumnMissing(md, col.NAME)) {
st.executeUpdate("ALTER TABLE " + tableName st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";"); + " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";");
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.REAL_NAME); if (isColumnMissing(md, col.REAL_NAME)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";"); + " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";");
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.PASSWORD); if (isColumnMissing(md, col.PASSWORD)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) NOT NULL;"); + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) NOT NULL;");
} }
rs.close();
if (!col.SALT.isEmpty()) { if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) {
rs = md.getColumns(null, null, tableName, col.SALT); st.executeUpdate("ALTER TABLE " + tableName
if (!rs.next()) { + " ADD COLUMN " + col.SALT + " VARCHAR(255);");
st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.SALT + " VARCHAR(255);");
}
rs.close();
} }
rs = md.getColumns(null, null, tableName, col.IP); if (isColumnMissing(md, col.IP)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.IP + " VARCHAR(40) NOT NULL;"); + " ADD COLUMN " + col.IP + " VARCHAR(40) NOT NULL;");
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.LAST_LOGIN); if (isColumnMissing(md, col.LAST_LOGIN)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;"); + " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;");
} else { } else {
migrateLastLoginColumnToBigInt(con, rs); migrateLastLoginColumnToBigInt(con, md);
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.LASTLOC_X); if (isColumnMissing(md, col.LASTLOC_X)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LAST_LOGIN + " , ADD " + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LAST_LOGIN + " , ADD "
+ col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_X + " , ADD " + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_X + " , ADD "
+ col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_Y); + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_Y);
} } else {
rs.close();
rs = md.getColumns(null, null, tableName, col.LASTLOC_X);
if (rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " MODIFY " st.executeUpdate("ALTER TABLE " + tableName + " MODIFY "
+ col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY " + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY "
+ col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY " + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0', MODIFY "
+ col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';"); + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';");
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.LASTLOC_WORLD); if (isColumnMissing(md, col.LASTLOC_WORLD)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z); + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z);
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.EMAIL); if (isColumnMissing(md, col.EMAIL)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD); + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD);
} }
rs.close();
rs = md.getColumns(null, null, tableName, col.IS_LOGGED); if (isColumnMissing(md, col.IS_LOGGED)) {
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL); + col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL);
} }
rs.close();
st.close(); st.close();
} }
ConsoleLogger.info("MySQL setup finished"); ConsoleLogger.info("MySQL setup finished");
} }
private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
return !rs.next();
}
}
@Override @Override
public boolean isAuthAvailable(String user) { public boolean isAuthAvailable(String user) {
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
@ -933,10 +905,14 @@ public class MySQL implements DataSource {
* Check if the lastlogin column is of type timestamp and, if so, revert it to the bigint format. * Check if the lastlogin column is of type timestamp and, if so, revert it to the bigint format.
* *
* @param con Connection to the database * @param con Connection to the database
* @param rs ResultSet containing meta data for the lastlogin column * @param metaData metaData meta data of the database
*/ */
private void migrateLastLoginColumnToBigInt(Connection con, ResultSet rs) throws SQLException { private void migrateLastLoginColumnToBigInt(Connection con, DatabaseMetaData metaData) throws SQLException {
final int columnType = rs.getInt("DATA_TYPE"); final int columnType;
try (ResultSet rs = metaData.getColumns(null, null, tableName, col.LAST_LOGIN)) {
columnType = rs.getInt("DATA_TYPE");
}
if (columnType == Types.TIMESTAMP) { if (columnType == Types.TIMESTAMP) {
ConsoleLogger.info("Migrating lastlogin column from timestamp to bigint"); ConsoleLogger.info("Migrating lastlogin column from timestamp to bigint");
final String lastLoginOld = col.LAST_LOGIN + "_old"; final String lastLoginOld = col.LAST_LOGIN + "_old";