/** * Retrieves an SQL ResultSet from the destinations table. This method builds an SQL query string * from the parameters supplied and then executes the query. Use the getters to retrieve the * results. * * @return true or false depending on whether any data matches the query */ public boolean resultSet() { PreparedStatement statement = null; ResultSet rs = null; String wheres = ""; if (where != null) { StringBuilder sbw = new StringBuilder(); for (Map.Entry<String, Object> entry : where.entrySet()) { sbw.append(entry.getKey()).append(" = ? AND "); } wheres = " WHERE " + sbw.toString().substring(0, sbw.length() - 5); } String query = "SELECT * FROM next" + wheres; try { service.testConnection(connection); statement = connection.prepareStatement(query); if (where != null) { int s = 1; for (Map.Entry<String, Object> entry : where.entrySet()) { if (entry.getValue().getClass().equals(String.class)) { statement.setString(s, entry.getValue().toString()); } else { statement.setInt(s, plugin.getUtils().parseInt(entry.getValue().toString())); } s++; } where.clear(); } rs = statement.executeQuery(); if (rs.isBeforeFirst()) { while (rs.next()) { this.next_id = rs.getInt("next_id"); this.tardis_id = rs.getInt("tardis_id"); this.world = plugin.getServer().getWorld(rs.getString("world")); this.x = rs.getInt("x"); this.y = rs.getInt("y"); this.z = rs.getInt("z"); this.direction = COMPASS.valueOf(rs.getString("direction")); this.submarine = rs.getBoolean("submarine"); } } else { return false; } } catch (SQLException e) { plugin.debug("ResultSet error for destinations table! " + e.getMessage()); return false; } finally { try { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } } catch (SQLException e) { plugin.debug("Error closing destinations table! " + e.getMessage()); } } return this.world != null; }
@Override public void run() { PreparedStatement ps = null; String updates; String wheres; StringBuilder sbu = new StringBuilder(); StringBuilder sbw = new StringBuilder(); for (Map.Entry<String, Object> entry : data.entrySet()) { sbu.append(entry.getKey()).append(" = ?,"); } for (Map.Entry<String, Object> entry : where.entrySet()) { sbw.append(entry.getKey()).append(" = "); if (entry.getValue().getClass().equals(String.class) || entry.getValue().getClass().equals(UUID.class)) { sbw.append("'").append(entry.getValue()).append("' AND "); } else { sbw.append(entry.getValue()).append(" AND "); } } where.clear(); updates = sbu.toString().substring(0, sbu.length() - 1); wheres = sbw.toString().substring(0, sbw.length() - 5); String query = "UPDATE " + table + " SET " + updates + " WHERE " + wheres; try { service.testConnection(connection); ps = connection.prepareStatement(query); int s = 1; for (Map.Entry<String, Object> entry : data.entrySet()) { if (entry.getValue().getClass().equals(String.class) || entry.getValue().getClass().equals(UUID.class)) { ps.setString(s, entry.getValue().toString()); } if (entry.getValue() instanceof Integer) { ps.setInt(s, (Integer) entry.getValue()); } if (entry.getValue() instanceof Long) { ps.setLong(s, (Long) entry.getValue()); } s++; } data.clear(); ps.executeUpdate(); } catch (SQLException e) { plugin.debug("Update error for " + table + "! " + e.getMessage()); } finally { try { if (ps != null) { ps.close(); } } catch (SQLException e) { plugin.debug("Error closing " + table + "! " + e.getMessage()); } } }
@Override public void run() { Statement statement = null; try { service.testConnection(connection); statement = connection.createStatement(); String select = "SELECT c_id FROM controls WHERE tardis_id = " + id + " AND type = " + type + " AND secondary = " + s; ResultSet rs = statement.executeQuery(select); if (rs.isBeforeFirst()) { // update String update = "UPDATE controls SET location = '" + l + "' WHERE c_id = " + rs.getInt("c_id"); statement.executeUpdate(update); } else { // insert String insert = "INSERT INTO controls (tardis_id, type, location, secondary) VALUES (" + id + ", " + type + ", '" + l + "', " + s + ")"; statement.executeUpdate(insert); } } catch (SQLException e) { plugin.debug("Insert control error! " + e.getMessage()); } finally { try { if (statement != null) { statement.close(); } } catch (SQLException e) { plugin.debug("Error closing insert control statement! " + e.getMessage()); } } }
/** Convert pre-TARDIS v2.3 controls to the new system. */ public void convertControls() { ResultSetTardis rs = new ResultSetTardis(plugin, null, "", true); if (rs.resultSet()) { int i = 0; ArrayList<HashMap<String, String>> data = rs.getData(); Statement del = null; PreparedStatement ps = null; try { service.testConnection(connection); // clear the controls table first - just incase they have reset `conversion_done` in the // config del = connection.createStatement(); del.executeUpdate("DELETE FROM controls"); // insert values from tardis table ps = connection.prepareStatement( "INSERT INTO controls (tardis_id, type, location) VALUES (?,?,?)"); for (HashMap<String, String> map : data) { int id = plugin.getUtils().parseInt(map.get("tardis_id")); String tmph; if (map.get("handbrake") == null || map.get("handbrake").isEmpty()) { tmph = estimateHandbrake(map.get("size"), map.get("chameleon")); plugin .getConsole() .sendMessage( plugin.getPluginName() + ChatColor.RED + "Handbrake location not found, making an educated guess..."); } else { tmph = map.get("handbrake"); } String tmpb; if (map.get("button") == null || map.get("button").isEmpty()) { tmpb = estimateButton(map.get("size"), map.get("chameleon")); plugin .getConsole() .sendMessage( plugin.getPluginName() + ChatColor.RED + "Button location not found, making an educated guess..."); } else { tmpb = map.get("button"); } String tmpa; if (map.get("artron_button") == null || map.get("artron_button").isEmpty()) { tmpa = estimateArtron(map.get("size"), map.get("chameleon")); plugin .getConsole() .sendMessage( plugin.getPluginName() + ChatColor.RED + "Artron Button location not found, making an educated guess..."); } else { tmpa = map.get("artron_button"); } String[] tmpr = new String[4]; if (map.get("repeater0") == null || map.get("repeater0").isEmpty()) { tmpr = estimateRepeaters(map.get("size"), map.get("chameleon")); plugin .getConsole() .sendMessage( plugin.getPluginName() + ChatColor.RED + "Repeater locations not found, making an educated guess..."); } else { tmpr[0] = map.get("repeater0"); tmpr[1] = map.get("repeater1"); tmpr[2] = map.get("repeater2"); tmpr[3] = map.get("repeater3"); } String hb = plugin.getUtils().makeLocationStr(tmph); String bn = plugin.getUtils().makeLocationStr(tmpb); String ab = plugin.getUtils().makeLocationStr(tmpa); ps.setInt(1, id); ps.setInt(2, 0); ps.setString(3, hb); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 1); ps.setString(3, bn); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 2); ps.setString(3, tmpr[0]); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 3); ps.setString(3, tmpr[1]); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 4); ps.setString(3, tmpr[2]); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 5); ps.setString(3, tmpr[3]); ps.addBatch(); ps.setInt(1, id); ps.setInt(2, 6); ps.setString(3, ab); ps.addBatch(); connection.setAutoCommit(false); ps.executeBatch(); connection.setAutoCommit(true); i++; } } catch (SQLException e) { plugin.debug("Control conversion error: " + e.getMessage()); } finally { if (del != null) { try { del.close(); } catch (SQLException e) { plugin.debug("Control delete statement close error: " + e.getMessage()); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { plugin.debug("Control prepared statement close error: " + e.getMessage()); } } } if (i > 0) { plugin .getConsole() .sendMessage(plugin.getPluginName() + "Converted " + i + " control consoles"); plugin.getConfig().set("conversions.conversion_done", true); plugin.saveConfig(); } } }
/** * Retrieves an SQL ResultSet from the destinations table. This method builds an SQL query string * from the parameters supplied and then executes the query. Use the getters to retrieve the * results. * * @return true or false depending on whether any data matches the query */ public boolean resultSet() { PreparedStatement statement = null; ResultSet rs = null; String wheres = ""; if (where != null) { StringBuilder sbw = new StringBuilder(); for (Map.Entry<String, Object> entry : where.entrySet()) { sbw.append(entry.getKey()).append(" = ? AND "); } wheres = " WHERE " + sbw.toString().substring(0, sbw.length() - 5); } String query = "SELECT * FROM destinations" + wheres; try { service.testConnection(connection); statement = connection.prepareStatement(query); if (where != null) { int s = 1; for (Map.Entry<String, Object> entry : where.entrySet()) { if (entry.getValue().getClass().equals(String.class)) { statement.setString(s, entry.getValue().toString()); } else { statement.setInt(s, plugin.utils.parseInt(entry.getValue().toString())); } s++; } where.clear(); } rs = statement.executeQuery(); if (rs.isBeforeFirst()) { while (rs.next()) { if (multiple) { HashMap<String, String> row = new HashMap<String, String>(); ResultSetMetaData rsmd = rs.getMetaData(); int columns = rsmd.getColumnCount(); for (int i = 1; i < columns + 1; i++) { row.put(rsmd.getColumnName(i).toLowerCase(Locale.ENGLISH), rs.getString(i)); } data.add(row); } this.dest_id = rs.getInt("dest_id"); this.tardis_id = rs.getInt("tardis_id"); this.dest_name = rs.getString("dest_name"); this.world = rs.getString("world"); this.x = rs.getInt("x"); this.y = rs.getInt("y"); this.z = rs.getInt("z"); this.direction = rs.getString("direction"); this.submarine = rs.getBoolean("submarine"); this.bind = rs.getString("bind"); this.type = rs.getInt("type"); } } else { return false; } } catch (SQLException e) { plugin.debug("ResultSet error for destinations table! " + e.getMessage()); return false; } finally { try { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } } catch (SQLException e) { plugin.debug("Error closing destinations table! " + e.getMessage()); } } return true; }