diff --git a/pom.xml b/pom.xml index 91a3d5d04..ec1ac972e 100644 --- a/pom.xml +++ b/pom.xml @@ -395,7 +395,7 @@ com.microsoft.sqlserver mssql-jdbc - 6.3.0.jre8-preview + 6.3.3.jre8-preview compile true diff --git a/src/main/java/fr/xephi/authme/datasource/MsSQL.java b/src/main/java/fr/xephi/authme/datasource/MsSQL.java index c16118c72..90bcff38a 100644 --- a/src/main/java/fr/xephi/authme/datasource/MsSQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MsSQL.java @@ -6,6 +6,7 @@ import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory; import fr.xephi.authme.settings.Settings; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.sql.Statement; @@ -25,19 +26,96 @@ public class MsSQL extends SqlDataSource { super.setConnectionArguments(); // Database URL - ds.setDataSourceClassName("com.microsoft.sqlserver.jdbc.SQLServerDataSource"); - ds.setJdbcUrl("jdbc:sqlserver://" + host + ":" + port + ";databaseName="+ database + ";user=" + username + ";password=" + password); + ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + ds.setJdbcUrl("jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + database); + ds.addDataSourceProperty("user", username); + ds.addDataSourceProperty("password", password); + ds.addDataSourceProperty("database.name", database); } @Override protected void checkTables() throws SQLException { try (Connection con = getConnection(); Statement st = con.createStatement()) { // Create table with ID column if it doesn't exist - String sql = "IF object_id('" + tableName + "') IS NOT NULL\n" + String sql = "IF object_id('" + tableName + "') IS NOT NULL" + " CREATE TABLE " + tableName + " (" + col.ID + " INT CHECK (" + col.ID + " > 0) IDENTITY" - + ", PRIMARY KEY (" + col.ID + ")) CHARACTER SET = utf8;"; - st.executeUpdate(sql); + + ", PRIMARY KEY (" + col.ID + "));"; + //st.executeUpdate(sql); + } + } + + @Override + protected void checkColumns() throws SQLException { + try (Connection con = getConnection(); Statement st = con.createStatement()) { + DatabaseMetaData md = con.getMetaData(); + if (isColumnMissing(md, col.NAME)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD " + col.NAME + " NVARCHAR(255) NOT NULL UNIQUE;"); + } + + if (isColumnMissing(md, col.REAL_NAME)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD " + col.REAL_NAME + " NVARCHAR(255) NOT NULL;"); + } + + if (isColumnMissing(md, col.PASSWORD)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD " + col.PASSWORD + " NVARCHAR(255) NOT NULL;"); + } + + if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + col.SALT + " NVARCHAR(255);"); + } + + if (isColumnMissing(md, col.IP)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD " + col.IP + " NVARCHAR(40) NOT NULL;"); + } + + if (isColumnMissing(md, col.LAST_LOGIN)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;"); + } else { + migrateLastLoginColumn(con, md); + } + + if (isColumnMissing(md, col.LASTLOC_X)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0' , ADD " + + col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0' , ADD " + + col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';"); + } else { + st.executeUpdate("ALTER TABLE " + tableName + " MODIFY " + + col.LASTLOC_X + " 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';"); + } + + if (isColumnMissing(md, col.LASTLOC_WORLD)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.LASTLOC_WORLD + " NVARCHAR(255) NOT NULL DEFAULT 'world';"); + } + + if (isColumnMissing(md, col.LASTLOC_YAW)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.LASTLOC_YAW + " FLOAT;"); + } + + if (isColumnMissing(md, col.LASTLOC_PITCH)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.LASTLOC_PITCH + " FLOAT;"); + } + + if (isColumnMissing(md, col.EMAIL)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.EMAIL + " NVARCHAR(255) DEFAULT 'your@email.com';"); + } + + if (isColumnMissing(md, col.IS_LOGGED)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD " + + col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0';"); + } } } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 463c9df6d..081a64514 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -7,6 +7,7 @@ import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.DatabaseSettings; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.sql.Statement; @@ -70,6 +71,80 @@ public class MySQL extends SqlDataSource { } } + @Override + protected void checkColumns() throws SQLException { + try (Connection con = getConnection(); Statement st = con.createStatement()) { + DatabaseMetaData md = con.getMetaData(); + if (isColumnMissing(md, col.NAME)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";"); + } + + if (isColumnMissing(md, col.REAL_NAME)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";"); + } + + if (isColumnMissing(md, col.PASSWORD)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;"); + } + + if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);"); + } + + if (isColumnMissing(md, col.IP)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD COLUMN " + col.IP + " VARCHAR(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;"); + } + + if (isColumnMissing(md, col.LAST_LOGIN)) { + st.executeUpdate("ALTER TABLE " + tableName + + " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;"); + } else { + migrateLastLoginColumn(con, md); + } + + if (isColumnMissing(md, col.LASTLOC_X)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + 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_Z + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_Y); + } else { + st.executeUpdate("ALTER TABLE " + tableName + " MODIFY " + + col.LASTLOC_X + " 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';"); + } + + if (isColumnMissing(md, col.LASTLOC_WORLD)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z); + } + + if (isColumnMissing(md, col.LASTLOC_YAW)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + col.LASTLOC_YAW + " FLOAT;"); + } + + if (isColumnMissing(md, col.LASTLOC_PITCH)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + col.LASTLOC_PITCH + " FLOAT;"); + } + + if (isColumnMissing(md, col.EMAIL)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD); + } + + if (isColumnMissing(md, col.IS_LOGGED)) { + st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + + col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL); + } + } + } + @Override public DataSourceType getType() { return DataSourceType.MYSQL; diff --git a/src/main/java/fr/xephi/authme/datasource/SqlDataSource.java b/src/main/java/fr/xephi/authme/datasource/SqlDataSource.java index ea30f8fd0..4a3cf77bc 100644 --- a/src/main/java/fr/xephi/authme/datasource/SqlDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/SqlDataSource.java @@ -138,80 +138,9 @@ public abstract class SqlDataSource implements DataSource { /** * Creates any required column if they don't exist. */ - private void checkColumns() throws SQLException { - try (Connection con = getConnection(); Statement st = con.createStatement()) { - DatabaseMetaData md = con.getMetaData(); - if (isColumnMissing(md, col.NAME)) { - st.executeUpdate("ALTER TABLE " + tableName - + " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";"); - } + protected abstract void checkColumns() throws SQLException; - if (isColumnMissing(md, col.REAL_NAME)) { - st.executeUpdate("ALTER TABLE " + tableName - + " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";"); - } - - if (isColumnMissing(md, col.PASSWORD)) { - st.executeUpdate("ALTER TABLE " + tableName - + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;"); - } - - if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);"); - } - - if (isColumnMissing(md, col.IP)) { - st.executeUpdate("ALTER TABLE " + tableName - + " ADD COLUMN " + col.IP + " VARCHAR(40) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;"); - } - - if (isColumnMissing(md, col.LAST_LOGIN)) { - st.executeUpdate("ALTER TABLE " + tableName - + " ADD COLUMN " + col.LAST_LOGIN + " BIGINT NOT NULL DEFAULT 0;"); - } else { - migrateLastLoginColumn(con, md); - } - - if (isColumnMissing(md, col.LASTLOC_X)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + 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_Z + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_Y); - } else { - st.executeUpdate("ALTER TABLE " + tableName + " MODIFY " - + col.LASTLOC_X + " 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';"); - } - - if (isColumnMissing(md, col.LASTLOC_WORLD)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z); - } - - if (isColumnMissing(md, col.LASTLOC_YAW)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + col.LASTLOC_YAW + " FLOAT;"); - } - - if (isColumnMissing(md, col.LASTLOC_PITCH)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + col.LASTLOC_PITCH + " FLOAT;"); - } - - if (isColumnMissing(md, col.EMAIL)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + col.EMAIL + " VARCHAR(255) DEFAULT 'your@email.com' AFTER " + col.LASTLOC_WORLD); - } - - if (isColumnMissing(md, col.IS_LOGGED)) { - st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " - + col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL); - } - } - } - - private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { + protected boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) { return !rs.next(); } @@ -662,7 +591,7 @@ public abstract class SqlDataSource implements DataSource { * * @throws SQLException . */ - private void migrateLastLoginColumn(Connection con, DatabaseMetaData metaData) throws SQLException { + protected void migrateLastLoginColumn(Connection con, DatabaseMetaData metaData) throws SQLException { final int columnType; try (ResultSet rs = metaData.getColumns(null, null, tableName, col.LAST_LOGIN)) { if (!rs.next()) {