From c4029e7b71c1422ac7edb5e578a2638b6394ac7c Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Sat, 11 Jun 2011 20:52:24 +0200 Subject: [PATCH] Cleanup --- src/de/diddiz/LogBlock/Updater.java | 45 ++++++++++------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/de/diddiz/LogBlock/Updater.java b/src/de/diddiz/LogBlock/Updater.java index 3e64ee9..33e71df 100644 --- a/src/de/diddiz/LogBlock/Updater.java +++ b/src/de/diddiz/LogBlock/Updater.java @@ -26,44 +26,29 @@ class Updater final Statement state = conn.createStatement(); final DatabaseMetaData dbm = conn.getMetaData(); conn.setAutoCommit(true); - if (!dbm.getTables(null, null, "lb-players", null).next()) { - log.log(Level.INFO, "[LogBlock] Crating table lb-players."); - state.execute("CREATE TABLE `lb-players` (playerid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, playername varchar(32) NOT NULL DEFAULT '-', PRIMARY KEY (playerid), UNIQUE (playername))"); - if (!dbm.getTables(null, null, "lb-players", null).next()) - throw new SQLException("Table lb-players not found"); - } + createTable(dbm, state, "lb-players", "(playerid SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, playername varchar(32) NOT NULL DEFAULT '-', PRIMARY KEY (playerid), UNIQUE (playername))"); for (final String table : logblock.getConfig().tables.values()) { - if (!dbm.getTables(null, null, table, null).next()) { - log.log(Level.INFO, "[LogBlock] Crating table " + table + "."); - state.execute("CREATE TABLE `" + table + "` (id INT NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, playerid SMALLINT UNSIGNED NOT NULL, replaced TINYINT UNSIGNED NOT NULL, type TINYINT UNSIGNED NOT NULL, data TINYINT UNSIGNED NOT NULL, x SMALLINT NOT NULL, y TINYINT UNSIGNED NOT NULL, z SMALLINT NOT NULL, PRIMARY KEY (id), KEY coords (x, z, y), KEY date (date))"); - if (!dbm.getTables(null, null, table, null).next()) - throw new SQLException("Table " + table + " not found"); - } - if (!dbm.getTables(null, null, table + "-sign", null).next()) { - log.log(Level.INFO, "[LogBlock] Crating table " + table + "-sign."); - state.execute("CREATE TABLE `" + table + "-sign` (id INT NOT NULL, signtext TEXT, PRIMARY KEY (id));"); - if (!dbm.getTables(null, null, table + "-sign", null).next()) - throw new SQLException("Table " + table + "-sign not found"); - } if (dbm.getTables(null, null, table + "-chest", null).next() && state.executeQuery("SELECT * FROM `" + table + "-chest` LIMIT 1").getMetaData().getColumnCount() != 4) // Chest table update state.execute("DROP TABLE `" + table + "-chest`"); - if (!dbm.getTables(null, null, table + "-chest", null).next()) { - log.log(Level.INFO, "[LogBlock] Crating table " + table + "-chest."); - state.execute("CREATE TABLE `" + table + "-chest` (id INT NOT NULL, itemtype SMALLINT UNSIGNED NOT NULL, itemamount SMALLINT NOT NULL, itemdata TINYINT UNSIGNED NOT NULL, PRIMARY KEY (id))"); - if (!dbm.getTables(null, null, table + "-chest", null).next()) - throw new SQLException("Table " + table + "-chest not found"); - } - if (logblock.getConfig().logKills && !dbm.getTables(null, null, table + "-kills", null).next()) { - log.log(Level.INFO, "[LogBlock] Crating table " + table + "-kills."); - state.execute("CREATE TABLE `" + table + "-kills` (id INT UNSIGNED NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, killer SMALLINT UNSIGNED, victim SMALLINT UNSIGNED NOT NULL, weapon SMALLINT UNSIGNED NOT NULL, PRIMARY KEY (id));"); - if (!dbm.getTables(null, null, table + "-kills", null).next()) - throw new SQLException("Table " + table + "-kills not found"); - } + createTable(dbm, state, table, "(id INT NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, playerid SMALLINT UNSIGNED NOT NULL, replaced TINYINT UNSIGNED NOT NULL, type TINYINT UNSIGNED NOT NULL, data TINYINT UNSIGNED NOT NULL, x SMALLINT NOT NULL, y TINYINT UNSIGNED NOT NULL, z SMALLINT NOT NULL, PRIMARY KEY (id), KEY coords (x, z, y), KEY date (date))"); + createTable(dbm, state, table + "-sign", "(id INT NOT NULL, signtext TEXT, PRIMARY KEY (id))"); + createTable(dbm, state, table + "-chest", "(id INT NOT NULL, itemtype SMALLINT UNSIGNED NOT NULL, itemamount SMALLINT NOT NULL, itemdata TINYINT UNSIGNED NOT NULL, PRIMARY KEY (id))"); + if (logblock.getConfig().logKills) + createTable(dbm, state, table + "-kills", "(id INT UNSIGNED NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, killer SMALLINT UNSIGNED, victim SMALLINT UNSIGNED NOT NULL, weapon SMALLINT UNSIGNED NOT NULL, PRIMARY KEY (id))"); } state.close(); conn.close(); } + private void createTable(DatabaseMetaData dbm, Statement state, String table, String query) throws SQLException { + if (!dbm.getTables(null, null, table, null).next()) { + log.log(Level.INFO, "[LogBlock] Crating table " + table + "."); + state.execute("CREATE TABLE `" + table + "` " + query); + if (!dbm.getTables(null, null, table, null).next()) + throw new SQLException("Table " + table + " not found and failed to create"); + } + } + String checkVersion() { try { return readURL(new URL("http://diddiz.insane-architects.net/lbuptodate.php?v=" + logblock.getDescription().getVersion()));