From 44baf82c8587a3d56c92b32023bf6301710eccc7 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sat, 8 Dec 2018 12:23:28 +0100 Subject: [server] DB Updater: Use helper methods --- .../org/openslx/bwlp/sat/database/Updater.java | 64 +++++----------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java index 3750ab2d..8fcd70a9 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Updater.java @@ -23,13 +23,8 @@ public class Updater { private static void addLectureLocationMapTable() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement tablesStmt = connection.prepareStatement("SHOW TABLES"); - ResultSet tables = tablesStmt.executeQuery(); - while (tables.next()) { - if (tables.getString(1).equals("lecture_x_location")) { - return; // Table exists, don't do anything - } - } + if (tableExists(connection, "lecture_x_location")) + return; // Add table MysqlStatement tableAddStmt = connection.prepareStatement("CREATE TABLE `lecture_x_location` (" + " `lectureid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL," @@ -52,13 +47,8 @@ public class Updater { private static void addLocationPrivateField() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement checkStmt = connection.prepareStatement("DESCRIBE lecture"); - ResultSet cols = checkStmt.executeQuery(); - while (cols.next()) { - if (cols.getString("Field").equals("islocationprivate")) { - return; // Field exists, don't do anything - } - } + if (getColumnType(connection, "lecture", "islocationprivate") != null) + return; // Field exists, don't do anything // Add field to table MysqlStatement columnAddStmt = connection.prepareStatement("ALTER TABLE lecture" + " ADD islocationprivate TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 AFTER isprivate"); @@ -73,13 +63,8 @@ public class Updater { private static void addHasUsbAccessField() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement checkStmt = connection.prepareStatement("DESCRIBE lecture"); - ResultSet cols = checkStmt.executeQuery(); - while (cols.next()) { - if (cols.getString("Field").equals("hasusbaccess")) { - return; // Field exists, don't do anything - } - } + if (getColumnType(connection, "lecture", "hasusbaccess") != null) + return; // Field exists, don't do anything // Add field to table MysqlStatement columnAddStmt = connection.prepareStatement("ALTER TABLE lecture" + " ADD hasusbaccess TINYINT(1) UNSIGNED NOT NULL DEFAULT 1 AFTER hasinternetaccess"); @@ -94,13 +79,8 @@ public class Updater { private static void addLogTable() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement tablesStmt = connection.prepareStatement("SHOW TABLES"); - ResultSet tables = tablesStmt.executeQuery(); - while (tables.next()) { - if (tables.getString(1).equals("actionlog")) { - return; // Table exists, don't do anything - } - } + if (tableExists(connection, "actionlog")) + return; // Add table MysqlStatement tableAddStmt = connection.prepareStatement("CREATE TABLE `actionlog` (" + " `actionid` int(11) NOT NULL AUTO_INCREMENT," @@ -133,14 +113,8 @@ public class Updater { */ private static void fixEmailFieldLength() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement checkStmt = connection.prepareStatement("DESCRIBE user"); - ResultSet cols = checkStmt.executeQuery(); - while (cols.next()) { - if (cols.getString("Field").equals("email") - && "varchar(100)".equalsIgnoreCase(cols.getString("Type"))) { - return; // Field exists and has proper type, don't do anything - } - } + if ("varchar(100)".equals(getColumnType(connection, "user", "email"))) + return; // Already 100 chars long, don'T do anything MysqlStatement upStmt = connection.prepareStatement("ALTER TABLE user CHANGE email" + " email VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL"); upStmt.executeUpdate(); @@ -154,13 +128,8 @@ public class Updater { private static void addNetworkShares() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement tablesStmt = connection.prepareStatement("SHOW TABLES"); - ResultSet tables = tablesStmt.executeQuery(); - while (tables.next()) { - if (tables.getString(1).equals("networkshare")) { - return; // Table exists, don't do anything - } - } + if (tableExists(connection, "networkshare")) + return; // Add table MysqlStatement tableAddStmt = connection.prepareStatement( "CREATE TABLE `networkshare` (" @@ -187,13 +156,8 @@ public class Updater { private static void addLectureFilter() throws SQLException { try (MysqlConnection connection = Database.getConnection()) { - MysqlStatement tablesStmt = connection.prepareStatement("SHOW TABLES"); - ResultSet tables = tablesStmt.executeQuery(); - while (tables.next()) { - if (tables.getString(1).equals("lecturefilter")) { - return; // Table exists, don't do anything - } - } + if (tableExists(connection, "lecturefilter")) + return; // Add table MysqlStatement tableAddStmt = connection.prepareStatement( "CREATE TABLE `lecturefilter` (" -- cgit v1.2.3-55-g7522