This commit is contained in:
Robin Kupper
2011-06-11 20:52:24 +02:00
parent 9aa4657e5f
commit c4029e7b71

View File

@@ -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()));