Merge pull request #313 from TheCutter/master

This commit is contained in:
sk89q 2014-07-06 15:09:51 -07:00
commit d827e01760

View File

@ -66,34 +66,40 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
String world1 = world; String world1 = world;
this.logger = logger; this.logger = logger;
PreparedStatement worldStmt = null;
ResultSet worldResult = null;
PreparedStatement insertWorldStatement = null;
try { try {
connect(); connect();
PreparedStatement verTest = null;
try { try {
// Test if the database is up to date, if not throw a critical error // Test if the database is up to date, if not throw a critical error
PreparedStatement verTest = this.conn.prepareStatement( verTest = this.conn.prepareStatement(
"SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;" "SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;"
); );
verTest.execute(); verTest.execute();
} catch (SQLException ex) { } catch (SQLException ex) {
throw new InvalidTableFormatException( throw new InvalidTableFormatException(
"region_storage_update_20110325.sql" "region_storage_update_20110325.sql"
); );
} finally {
closeResource(verTest);
} }
PreparedStatement worldStmt = conn.prepareStatement( worldStmt = conn.prepareStatement(
"SELECT `id` FROM " + "SELECT `id` FROM " +
"`world` " + "`world` " +
"WHERE `name` = ? LIMIT 0,1" "WHERE `name` = ? LIMIT 0,1"
); );
worldStmt.setString(1, world1); worldStmt.setString(1, world1);
ResultSet worldResult = worldStmt.executeQuery(); worldResult = worldStmt.executeQuery();
if (worldResult.first()) { if (worldResult.first()) {
this.worldDbId = worldResult.getInt("id"); this.worldDbId = worldResult.getInt("id");
} else { } else {
PreparedStatement insertWorldStatement = this.conn.prepareStatement( insertWorldStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`world` " + "`world` " +
"(`id`, `name`) VALUES (null, ?)", "(`id`, `name`) VALUES (null, ?)",
@ -112,6 +118,10 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
// We havn't connected to the databases, or there was an error // We havn't connected to the databases, or there was an error
// initialising the world record, so there is no point continuing // initialising the world record, so there is no point continuing
return; return;
} finally {
closeResource(worldResult);
closeResource(worldStmt);
closeResource(insertWorldStatement);
} }
if (this.worldDbId <= 0) { if (this.worldDbId <= 0) {
@ -132,20 +142,20 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
} }
private void connect() throws SQLException { private void connect() throws SQLException {
if (conn != null) { if (conn != null) {
// Make a dummy query to check the connnection is alive. // Check if the connection is still alive/valid.
try { try {
conn.prepareStatement("SELECT 1;").execute(); conn.isValid(2);
} catch (SQLException ex) { } catch (SQLException ex) {
// Test if the dummy query failed because the connection is dead, // Test if validation failed because the connection is dead,
// and if it is mark the connection as closed (the MySQL Driver // and if it is mark the connection as closed (the MySQL Driver
// does not ensure that the connection is marked as closed unless // does not ensure that the connection is marked as closed unless
// the close() method has been called. // the close() method has been called.
if ("08S01".equals(ex.getSQLState())) { if ("08S01".equals(ex.getSQLState())) {
conn.close(); conn.close();
} }
} }
} }
if (conn == null || conn.isClosed()) { if (conn == null || conn.isClosed()) {
conn = DriverManager.getConnection(config.sqlDsn, config.sqlUsername, config.sqlPassword); conn = DriverManager.getConnection(config.sqlDsn, config.sqlUsername, config.sqlPassword);
} }
@ -153,8 +163,10 @@ private void connect() throws SQLException {
private void loadFlags(ProtectedRegion region) { private void loadFlags(ProtectedRegion region) {
// @TODO: Iterate _ONCE_ // @TODO: Iterate _ONCE_
PreparedStatement flagsStatement = null;
ResultSet flagsResultSet = null;
try { try {
PreparedStatement flagsStatement = this.conn.prepareStatement( flagsStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region_flag`.`flag`, " + "`region_flag`.`flag`, " +
"`region_flag`.`value` " + "`region_flag`.`value` " +
@ -164,7 +176,7 @@ private void loadFlags(ProtectedRegion region) {
); );
flagsStatement.setString(1, region.getId().toLowerCase()); flagsStatement.setString(1, region.getId().toLowerCase());
ResultSet flagsResultSet = flagsStatement.executeQuery(); flagsResultSet = flagsStatement.executeQuery();
Map<String,Object> regionFlags = new HashMap<String,Object>(); Map<String,Object> regionFlags = new HashMap<String,Object>();
while (flagsResultSet.next()) { while (flagsResultSet.next()) {
@ -186,6 +198,9 @@ private void loadFlags(ProtectedRegion region) {
"Unable to load flags for region " "Unable to load flags for region "
+ region.getId().toLowerCase() + ": " + ex.getMessage() + region.getId().toLowerCase() + ": " + ex.getMessage()
); );
} finally {
closeResource(flagsResultSet);
closeResource(flagsStatement);
} }
} }
@ -203,8 +218,10 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
DefaultDomain owners = new DefaultDomain(); DefaultDomain owners = new DefaultDomain();
DefaultDomain members = new DefaultDomain(); DefaultDomain members = new DefaultDomain();
ResultSet userSet = null;
PreparedStatement usersStatement = null;
try { try {
PreparedStatement usersStatement = this.conn.prepareStatement( usersStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`user`.`name`, " + "`user`.`name`, " +
"`region_players`.`owner` " + "`region_players`.`owner` " +
@ -217,7 +234,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
); );
usersStatement.setString(1, region.getId().toLowerCase()); usersStatement.setString(1, region.getId().toLowerCase());
ResultSet userSet = usersStatement.executeQuery(); userSet = usersStatement.executeQuery();
while(userSet.next()) { while(userSet.next()) {
if (userSet.getBoolean("owner")) { if (userSet.getBoolean("owner")) {
owners.addPlayer(userSet.getString("name")); owners.addPlayer(userSet.getString("name"));
@ -227,10 +244,15 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
} }
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Unable to load users for region " + region.getId().toLowerCase() + ": " + ex.getMessage()); logger.warning("Unable to load users for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
} finally {
closeResource(userSet);
closeResource(usersStatement);
} }
PreparedStatement groupsStatement = null;
ResultSet groupSet = null;
try { try {
PreparedStatement groupsStatement = this.conn.prepareStatement( groupsStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`group`.`name`, " + "`group`.`name`, " +
"`region_groups`.`owner` " + "`region_groups`.`owner` " +
@ -243,7 +265,7 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
); );
groupsStatement.setString(1, region.getId().toLowerCase()); groupsStatement.setString(1, region.getId().toLowerCase());
ResultSet groupSet = groupsStatement.executeQuery(); groupSet = groupsStatement.executeQuery();
while(groupSet.next()) { while(groupSet.next()) {
if (groupSet.getBoolean("owner")) { if (groupSet.getBoolean("owner")) {
owners.addGroup(groupSet.getString("name")); owners.addGroup(groupSet.getString("name"));
@ -253,6 +275,9 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
} }
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage()); logger.warning("Unable to load groups for region " + region.getId().toLowerCase() + ": " + ex.getMessage());
} finally {
closeResource(groupSet);
closeResource(groupsStatement);
} }
region.setOwners(owners); region.setOwners(owners);
@ -263,8 +288,10 @@ private void loadGlobal() {
Map<String,ProtectedRegion> regions = Map<String,ProtectedRegion> regions =
new HashMap<String,ProtectedRegion>(); new HashMap<String,ProtectedRegion>();
PreparedStatement globalRegionStatement = null;
ResultSet globalResultSet = null;
try { try {
PreparedStatement globalRegionStatement = this.conn.prepareStatement( globalRegionStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region`.`id`, " + "`region`.`id`, " +
"`region`.`priority`, " + "`region`.`priority`, " +
@ -278,7 +305,7 @@ private void loadGlobal() {
); );
globalRegionStatement.setInt(1, this.worldDbId); globalRegionStatement.setInt(1, this.worldDbId);
ResultSet globalResultSet = globalRegionStatement.executeQuery(); globalResultSet = globalRegionStatement.executeQuery();
while (globalResultSet.next()) { while (globalResultSet.next()) {
ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id")); ProtectedRegion region = new GlobalProtectedRegion(globalResultSet.getString("id"));
@ -303,6 +330,9 @@ private void loadGlobal() {
logger.warning("\t\tCause: " + t.getMessage()); logger.warning("\t\tCause: " + t.getMessage());
t = t.getCause(); t = t.getCause();
} }
} finally {
closeResource(globalResultSet);
closeResource(globalRegionStatement);
} }
globalRegions = regions; globalRegions = regions;
@ -312,8 +342,10 @@ private void loadCuboid() {
Map<String,ProtectedRegion> regions = Map<String,ProtectedRegion> regions =
new HashMap<String,ProtectedRegion>(); new HashMap<String,ProtectedRegion>();
PreparedStatement cuboidRegionStatement = null;
ResultSet cuboidResultSet = null;
try { try {
PreparedStatement cuboidRegionStatement = this.conn.prepareStatement( cuboidRegionStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region_cuboid`.`min_z`, " + "`region_cuboid`.`min_z`, " +
"`region_cuboid`.`min_y`, " + "`region_cuboid`.`min_y`, " +
@ -335,7 +367,7 @@ private void loadCuboid() {
); );
cuboidRegionStatement.setInt(1, this.worldDbId); cuboidRegionStatement.setInt(1, this.worldDbId);
ResultSet cuboidResultSet = cuboidRegionStatement.executeQuery(); cuboidResultSet = cuboidRegionStatement.executeQuery();
while (cuboidResultSet.next()) { while (cuboidResultSet.next()) {
Vector pt1 = new Vector( Vector pt1 = new Vector(
@ -378,6 +410,9 @@ private void loadCuboid() {
logger.warning("\t\tCause: " + t.getMessage()); logger.warning("\t\tCause: " + t.getMessage());
t = t.getCause(); t = t.getCause();
} }
} finally {
closeResource(cuboidResultSet);
closeResource(cuboidRegionStatement);
} }
cuboidRegions = regions; cuboidRegions = regions;
@ -387,8 +422,11 @@ private void loadPoly2d() {
Map<String,ProtectedRegion> regions = Map<String,ProtectedRegion> regions =
new HashMap<String,ProtectedRegion>(); new HashMap<String,ProtectedRegion>();
PreparedStatement poly2dRegionStatement = null;
ResultSet poly2dResultSet = null;
PreparedStatement poly2dVectorStatement = null;
try { try {
PreparedStatement poly2dRegionStatement = this.conn.prepareStatement( poly2dRegionStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region_poly2d`.`min_y`, " + "`region_poly2d`.`min_y`, " +
"`region_poly2d`.`max_y`, " + "`region_poly2d`.`max_y`, " +
@ -406,9 +444,9 @@ private void loadPoly2d() {
); );
poly2dRegionStatement.setInt(1, this.worldDbId); poly2dRegionStatement.setInt(1, this.worldDbId);
ResultSet poly2dResultSet = poly2dRegionStatement.executeQuery(); poly2dResultSet = poly2dRegionStatement.executeQuery();
PreparedStatement poly2dVectorStatement = this.conn.prepareStatement( poly2dVectorStatement = this.conn.prepareStatement(
"SELECT " + "SELECT " +
"`region_poly2d_point`.`x`, " + "`region_poly2d_point`.`x`, " +
"`region_poly2d_point`.`z` " + "`region_poly2d_point`.`z` " +
@ -439,6 +477,8 @@ private void loadPoly2d() {
continue; continue;
} }
closeResource(poly2dVectorResultSet);
ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY); ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
region.setPriority(poly2dResultSet.getInt("priority")); region.setPriority(poly2dResultSet.getInt("priority"));
@ -461,6 +501,10 @@ private void loadPoly2d() {
logger.warning("\t\tCause: " + t.getMessage()); logger.warning("\t\tCause: " + t.getMessage());
t = t.getCause(); t = t.getCause();
} }
} finally {
closeResource(poly2dResultSet);
closeResource(poly2dRegionStatement);
closeResource(poly2dVectorStatement);
} }
poly2dRegions = regions; poly2dRegions = regions;
@ -518,8 +562,11 @@ private Map<String,Integer> getUserIds(String... usernames) {
if (usernames.length < 1) return users; if (usernames.length < 1) return users;
ResultSet findUsersResults = null;
PreparedStatement insertUserStatement = null;
PreparedStatement findUsersStatement = null;
try { try {
PreparedStatement findUsersStatement = this.conn.prepareStatement( findUsersStatement = this.conn.prepareStatement(
String.format( String.format(
"SELECT " + "SELECT " +
"`user`.`id`, " + "`user`.`id`, " +
@ -532,13 +579,13 @@ private Map<String,Integer> getUserIds(String... usernames) {
RegionDBUtil.setValues(findUsersStatement, usernames); RegionDBUtil.setValues(findUsersStatement, usernames);
ResultSet findUsersResults = findUsersStatement.executeQuery(); findUsersResults = findUsersStatement.executeQuery();
while(findUsersResults.next()) { while(findUsersResults.next()) {
users.put(findUsersResults.getString("name"), findUsersResults.getInt("id")); users.put(findUsersResults.getString("name"), findUsersResults.getInt("id"));
} }
PreparedStatement insertUserStatement = this.conn.prepareStatement( insertUserStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`user` ( " + "`user` ( " +
"`id`, " + "`id`, " +
@ -567,6 +614,10 @@ private Map<String,Integer> getUserIds(String... usernames) {
logger.warning(t.getMessage()); logger.warning(t.getMessage());
t = t.getCause(); t = t.getCause();
} }
} finally {
closeResource(findUsersResults);
closeResource(findUsersStatement);
closeResource(insertUserStatement);
} }
return users; return users;
@ -582,8 +633,11 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
if (groupnames.length < 1) return groups; if (groupnames.length < 1) return groups;
PreparedStatement findGroupsStatement = null;
ResultSet findGroupsResults = null;
PreparedStatement insertGroupStatement = null;
try { try {
PreparedStatement findGroupsStatement = this.conn.prepareStatement( findGroupsStatement = this.conn.prepareStatement(
String.format( String.format(
"SELECT " + "SELECT " +
"`group`.`id`, " + "`group`.`id`, " +
@ -596,13 +650,13 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
RegionDBUtil.setValues(findGroupsStatement, groupnames); RegionDBUtil.setValues(findGroupsStatement, groupnames);
ResultSet findGroupsResults = findGroupsStatement.executeQuery(); findGroupsResults = findGroupsStatement.executeQuery();
while(findGroupsResults.next()) { while(findGroupsResults.next()) {
groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id")); groups.put(findGroupsResults.getString("name"), findGroupsResults.getInt("id"));
} }
PreparedStatement insertGroupStatement = this.conn.prepareStatement( insertGroupStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`group` ( " + "`group` ( " +
"`id`, " + "`id`, " +
@ -625,6 +679,10 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
} }
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage()); logger.warning("Could not get the database id for the groups " + groupnames.toString() + ex.getMessage());
} finally {
closeResource(findGroupsResults);
closeResource(findGroupsStatement);
closeResource(insertGroupStatement);
} }
return groups; return groups;
@ -655,21 +713,26 @@ public void save() throws ProtectionDatabaseException {
List<String> regionsInDatabase = new ArrayList<String>(); List<String> regionsInDatabase = new ArrayList<String>();
PreparedStatement getAllRegionsStatement = null;
ResultSet getAllRegionsResult = null;
try { try {
PreparedStatement getAllRegionsStatement = this.conn.prepareStatement( getAllRegionsStatement = this.conn.prepareStatement(
"SELECT `region`.`id` FROM " + "SELECT `region`.`id` FROM " +
"`region` " + "`region` " +
"WHERE `world_id` = ? " "WHERE `world_id` = ? "
); );
getAllRegionsStatement.setInt(1, this.worldDbId); getAllRegionsStatement.setInt(1, this.worldDbId);
ResultSet getAllRegionsResult = getAllRegionsStatement.executeQuery(); getAllRegionsResult = getAllRegionsStatement.executeQuery();
while(getAllRegionsResult.next()) { while(getAllRegionsResult.next()) {
regionsInDatabase.add(getAllRegionsResult.getString("id")); regionsInDatabase.add(getAllRegionsResult.getString("id"));
} }
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Could not get region list for save comparison: " + ex.getMessage()); logger.warning("Could not get region list for save comparison: " + ex.getMessage());
} finally {
closeResource(getAllRegionsResult);
closeResource(getAllRegionsStatement);
} }
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) { for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
@ -707,10 +770,11 @@ public void save() throws ProtectionDatabaseException {
} }
for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) { for (Map.Entry<String, ProtectedRegion> entry : regions.entrySet()) {
PreparedStatement setParentStatement = null;
try { try {
if (entry.getValue().getParent() == null) continue; if (entry.getValue().getParent() == null) continue;
PreparedStatement setParentStatement = this.conn.prepareStatement( setParentStatement = this.conn.prepareStatement(
"UPDATE `region` SET " + "UPDATE `region` SET " +
"`parent` = ? " + "`parent` = ? " +
"WHERE `id` = ? AND `world_id` = " + this.worldDbId "WHERE `id` = ? AND `world_id` = " + this.worldDbId
@ -723,12 +787,15 @@ public void save() throws ProtectionDatabaseException {
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Could not save region parents " + entry.getValue().getId().toLowerCase() + ": " + ex.getMessage()); logger.warning("Could not save region parents " + entry.getValue().getId().toLowerCase() + ": " + ex.getMessage());
throw new ProtectionDatabaseException(ex); throw new ProtectionDatabaseException(ex);
} finally {
closeResource(setParentStatement);
} }
} }
for (String name : regionsInDatabase) { for (String name : regionsInDatabase) {
PreparedStatement removeRegion = null;
try { try {
PreparedStatement removeRegion = this.conn.prepareStatement( removeRegion = this.conn.prepareStatement(
"DELETE FROM `region` WHERE `id` = ? " "DELETE FROM `region` WHERE `id` = ? "
); );
@ -736,41 +803,53 @@ public void save() throws ProtectionDatabaseException {
removeRegion.execute(); removeRegion.execute();
} catch (SQLException ex) { } catch (SQLException ex) {
logger.warning("Could not remove region from database " + name + ": " + ex.getMessage()); logger.warning("Could not remove region from database " + name + ": " + ex.getMessage());
} finally {
closeResource(removeRegion);
} }
} }
} }
private void updateFlags(ProtectedRegion region) throws SQLException { private void updateFlags(ProtectedRegion region) throws SQLException {
PreparedStatement clearCurrentFlagStatement = this.conn.prepareStatement( PreparedStatement clearCurrentFlagStatement = null;
"DELETE FROM `region_flag` " + try {
"WHERE `region_id` = ? " + clearCurrentFlagStatement = this.conn.prepareStatement(
"AND `world_id` = " + this.worldDbId "DELETE FROM `region_flag` " +
); "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId
clearCurrentFlagStatement.setString(1, region.getId().toLowerCase());
clearCurrentFlagStatement.execute();
for (Map.Entry<Flag<?>, Object> entry : region.getFlags().entrySet()) {
if (entry.getValue() == null) continue;
Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue()));
PreparedStatement insertFlagStatement = this.conn.prepareStatement(
"INSERT INTO `region_flag` ( " +
"`id`, " +
"`region_id`, " +
"`world_id`, " +
"`flag`, " +
"`value` " +
") VALUES (null, ?, " + this.worldDbId + ", ?, ?)"
); );
insertFlagStatement.setString(1, region.getId().toLowerCase()); clearCurrentFlagStatement.setString(1, region.getId().toLowerCase());
insertFlagStatement.setString(2, entry.getKey().getName()); clearCurrentFlagStatement.execute();
insertFlagStatement.setObject(3, flag);
insertFlagStatement.execute(); for (Map.Entry<Flag<?>, Object> entry : region.getFlags().entrySet()) {
if (entry.getValue() == null) continue;
Object flag = sqlMarshal(marshalFlag(entry.getKey(), entry.getValue()));
PreparedStatement insertFlagStatement = null;
try {
insertFlagStatement = this.conn.prepareStatement(
"INSERT INTO `region_flag` ( " +
"`id`, " +
"`region_id`, " +
"`world_id`, " +
"`flag`, " +
"`value` " +
") VALUES (null, ?, " + this.worldDbId + ", ?, ?)"
);
insertFlagStatement.setString(1, region.getId().toLowerCase());
insertFlagStatement.setString(2, entry.getKey().getName());
insertFlagStatement.setObject(3, flag);
insertFlagStatement.execute();
} finally {
closeResource(insertFlagStatement);
}
}
} finally {
closeResource(clearCurrentFlagStatement);
} }
} }
@ -783,57 +862,69 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
domain = region.getMembers(); domain = region.getMembers();
} }
PreparedStatement deleteUsersForRegion = this.conn.prepareStatement( PreparedStatement deleteUsersForRegion = null;
"DELETE FROM `region_players` " + PreparedStatement insertUsersForRegion = null;
"WHERE `region_id` = ? " + PreparedStatement deleteGroupsForRegion = null;
"AND `world_id` = " + this.worldDbId + " " + PreparedStatement insertGroupsForRegion = null;
"AND `owner` = ?"
);
deleteUsersForRegion.setString(1, region.getId().toLowerCase()); try {
deleteUsersForRegion.setBoolean(2, owners); deleteUsersForRegion = this.conn.prepareStatement(
deleteUsersForRegion.execute(); "DELETE FROM `region_players` " +
"WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId + " " +
"AND `owner` = ?"
);
PreparedStatement insertUsersForRegion = this.conn.prepareStatement( deleteUsersForRegion.setString(1, region.getId().toLowerCase());
"INSERT INTO `region_players` " + deleteUsersForRegion.setBoolean(2, owners);
"(`region_id`, `world_id`, `user_id`, `owner`) " + deleteUsersForRegion.execute();
"VALUES (?, " + this.worldDbId + ", ?, ?)"
);
Set<String> var = domain.getPlayers(); insertUsersForRegion = this.conn.prepareStatement(
"INSERT INTO `region_players` " +
"(`region_id`, `world_id`, `user_id`, `owner`) " +
"VALUES (?, " + this.worldDbId + ", ?, ?)"
);
for (Integer player : getUserIds(var.toArray(new String[var.size()])).values()) { Set<String> var = domain.getPlayers();
insertUsersForRegion.setString(1, region.getId().toLowerCase());
insertUsersForRegion.setInt(2, player);
insertUsersForRegion.setBoolean(3, owners);
insertUsersForRegion.execute(); for (Integer player : getUserIds(var.toArray(new String[var.size()])).values()) {
} insertUsersForRegion.setString(1, region.getId().toLowerCase());
insertUsersForRegion.setInt(2, player);
insertUsersForRegion.setBoolean(3, owners);
PreparedStatement deleteGroupsForRegion = this.conn.prepareStatement( insertUsersForRegion.execute();
"DELETE FROM `region_groups` " + }
"WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId + " " +
"AND `owner` = ?"
);
deleteGroupsForRegion.setString(1, region.getId().toLowerCase()); deleteGroupsForRegion = this.conn.prepareStatement(
deleteGroupsForRegion.setBoolean(2, owners); "DELETE FROM `region_groups` " +
deleteGroupsForRegion.execute(); "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId + " " +
"AND `owner` = ?"
);
PreparedStatement insertGroupsForRegion = this.conn.prepareStatement( deleteGroupsForRegion.setString(1, region.getId().toLowerCase());
"INSERT INTO `region_groups` " + deleteGroupsForRegion.setBoolean(2, owners);
"(`region_id`, `world_id`, `group_id`, `owner`) " + deleteGroupsForRegion.execute();
"VALUES (?, " + this.worldDbId + ", ?, ?)"
);
Set<String> groupVar = domain.getGroups(); insertGroupsForRegion = this.conn.prepareStatement(
for (Integer group : getGroupIds(groupVar.toArray(new String[groupVar.size()])).values()) { "INSERT INTO `region_groups` " +
insertGroupsForRegion.setString(1, region.getId().toLowerCase()); "(`region_id`, `world_id`, `group_id`, `owner`) " +
insertGroupsForRegion.setInt(2, group); "VALUES (?, " + this.worldDbId + ", ?, ?)"
insertGroupsForRegion.setBoolean(3, owners); );
insertGroupsForRegion.execute(); Set<String> groupVar = domain.getGroups();
for (Integer group : getGroupIds(groupVar.toArray(new String[groupVar.size()])).values()) {
insertGroupsForRegion.setString(1, region.getId().toLowerCase());
insertGroupsForRegion.setInt(2, group);
insertGroupsForRegion.setBoolean(3, owners);
insertGroupsForRegion.execute();
}
} finally {
closeResource(deleteGroupsForRegion);
closeResource(deleteUsersForRegion);
closeResource(insertGroupsForRegion);
closeResource(insertUsersForRegion);
} }
} }
@ -843,22 +934,27 @@ private <V> Object marshalFlag(Flag<V> flag, Object val) {
} }
private void insertRegion(ProtectedRegion region, String type) throws SQLException { private void insertRegion(ProtectedRegion region, String type) throws SQLException {
PreparedStatement insertRegionStatement = this.conn.prepareStatement( PreparedStatement insertRegionStatement = null;
"INSERT INTO `region` (" + try {
"`id`, " + insertRegionStatement = this.conn.prepareStatement(
"`world_id`, " + "INSERT INTO `region` (" +
"`type`, " + "`id`, " +
"`priority`, " + "`world_id`, " +
"`parent` " + "`type`, " +
") VALUES (?, ?, ?, ?, null)" "`priority`, " +
); "`parent` " +
") VALUES (?, ?, ?, ?, null)"
);
insertRegionStatement.setString(1, region.getId().toLowerCase()); insertRegionStatement.setString(1, region.getId().toLowerCase());
insertRegionStatement.setInt(2, this.worldDbId); insertRegionStatement.setInt(2, this.worldDbId);
insertRegionStatement.setString(3, type); insertRegionStatement.setString(3, type);
insertRegionStatement.setInt(4, region.getPriority()); insertRegionStatement.setInt(4, region.getPriority());
insertRegionStatement.execute(); insertRegionStatement.execute();
} finally {
closeResource(insertRegionStatement);
}
updateFlags(region); updateFlags(region);
@ -869,82 +965,100 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException { private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
insertRegion(region, "cuboid"); insertRegion(region, "cuboid");
PreparedStatement insertCuboidRegionStatement = this.conn.prepareStatement( PreparedStatement insertCuboidRegionStatement = null;
"INSERT INTO `region_cuboid` (" + try {
"`region_id`, " + insertCuboidRegionStatement = this.conn.prepareStatement(
"`world_id`, " + "INSERT INTO `region_cuboid` (" +
"`min_z`, " + "`region_id`, " +
"`min_y`, " + "`world_id`, " +
"`min_x`, " + "`min_z`, " +
"`max_z`, " + "`min_y`, " +
"`max_y`, " + "`min_x`, " +
"`max_x` " + "`max_z`, " +
") VALUES (?, " + this.worldDbId + ", ?, ?, ?, ?, ?, ?)" "`max_y`, " +
); "`max_x` " +
") VALUES (?, " + this.worldDbId + ", ?, ?, ?, ?, ?, ?)"
);
BlockVector min = region.getMinimumPoint(); BlockVector min = region.getMinimumPoint();
BlockVector max = region.getMaximumPoint(); BlockVector max = region.getMaximumPoint();
insertCuboidRegionStatement.setString(1, region.getId().toLowerCase()); insertCuboidRegionStatement.setString(1, region.getId().toLowerCase());
insertCuboidRegionStatement.setInt(2, min.getBlockZ()); insertCuboidRegionStatement.setInt(2, min.getBlockZ());
insertCuboidRegionStatement.setInt(3, min.getBlockY()); insertCuboidRegionStatement.setInt(3, min.getBlockY());
insertCuboidRegionStatement.setInt(4, min.getBlockX()); insertCuboidRegionStatement.setInt(4, min.getBlockX());
insertCuboidRegionStatement.setInt(5, max.getBlockZ()); insertCuboidRegionStatement.setInt(5, max.getBlockZ());
insertCuboidRegionStatement.setInt(6, max.getBlockY()); insertCuboidRegionStatement.setInt(6, max.getBlockY());
insertCuboidRegionStatement.setInt(7, max.getBlockX()); insertCuboidRegionStatement.setInt(7, max.getBlockX());
insertCuboidRegionStatement.execute(); insertCuboidRegionStatement.execute();
} finally {
closeResource(insertCuboidRegionStatement);
}
} }
private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException { private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
insertRegion(region, "poly2d"); insertRegion(region, "poly2d");
PreparedStatement insertPoly2dRegionStatement = this.conn.prepareStatement( PreparedStatement insertPoly2dRegionStatement = null;
"INSERT INTO `region_poly2d` (" + try {
"`region_id`, " + insertPoly2dRegionStatement = this.conn.prepareStatement(
"`world_id`, " + "INSERT INTO `region_poly2d` (" +
"`max_y`, " + "`region_id`, " +
"`min_y` " + "`world_id`, " +
") VALUES (?, " + this.worldDbId + ", ?, ?)" "`max_y`, " +
); "`min_y` " +
") VALUES (?, " + this.worldDbId + ", ?, ?)"
);
insertPoly2dRegionStatement.setString(1, region.getId().toLowerCase()); insertPoly2dRegionStatement.setString(1, region.getId().toLowerCase());
insertPoly2dRegionStatement.setInt(2, region.getMaximumPoint().getBlockY()); insertPoly2dRegionStatement.setInt(2, region.getMaximumPoint().getBlockY());
insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY()); insertPoly2dRegionStatement.setInt(3, region.getMinimumPoint().getBlockY());
insertPoly2dRegionStatement.execute(); insertPoly2dRegionStatement.execute();
} finally {
closeResource(insertPoly2dRegionStatement);
}
updatePoly2dPoints(region); updatePoly2dPoints(region);
} }
private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException { private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLException {
PreparedStatement clearPoly2dPointsForRegionStatement = this.conn.prepareStatement( PreparedStatement clearPoly2dPointsForRegionStatement = null;
"DELETE FROM `region_poly2d_point` " + PreparedStatement insertPoly2dPointStatement = null;
"WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId
);
clearPoly2dPointsForRegionStatement.setString(1, region.getId().toLowerCase()); try {
clearPoly2dPointsForRegionStatement = this.conn.prepareStatement(
"DELETE FROM `region_poly2d_point` " +
"WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId
);
clearPoly2dPointsForRegionStatement.execute(); clearPoly2dPointsForRegionStatement.setString(1, region.getId().toLowerCase());
PreparedStatement insertPoly2dPointStatement = this.conn.prepareStatement( clearPoly2dPointsForRegionStatement.execute();
"INSERT INTO `region_poly2d_point` (" +
"`id`, " +
"`region_id`, " +
"`world_id`, " +
"`z`, " +
"`x` " +
") VALUES (null, ?, " + this.worldDbId + ", ?, ?)"
);
String lowerId = region.getId().toLowerCase(); insertPoly2dPointStatement = this.conn.prepareStatement(
for (BlockVector2D point : region.getPoints()) { "INSERT INTO `region_poly2d_point` (" +
insertPoly2dPointStatement.setString(1, lowerId); "`id`, " +
insertPoly2dPointStatement.setInt(2, point.getBlockZ()); "`region_id`, " +
insertPoly2dPointStatement.setInt(3, point.getBlockX()); "`world_id`, " +
"`z`, " +
"`x` " +
") VALUES (null, ?, " + this.worldDbId + ", ?, ?)"
);
insertPoly2dPointStatement.execute(); String lowerId = region.getId().toLowerCase();
for (BlockVector2D point : region.getPoints()) {
insertPoly2dPointStatement.setString(1, lowerId);
insertPoly2dPointStatement.setInt(2, point.getBlockZ());
insertPoly2dPointStatement.setInt(3, point.getBlockX());
insertPoly2dPointStatement.execute();
}
} finally {
closeResource(clearPoly2dPointsForRegionStatement);
closeResource(insertPoly2dPointStatement);
} }
} }
@ -953,15 +1067,20 @@ private void insertRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
} }
private void updateRegion(ProtectedRegion region, String type) throws SQLException { private void updateRegion(ProtectedRegion region, String type) throws SQLException {
PreparedStatement updateRegionStatement = this.conn.prepareStatement( PreparedStatement updateRegionStatement = null;
"UPDATE `region` SET " + try {
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId updateRegionStatement = this.conn.prepareStatement(
); "UPDATE `region` SET " +
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId
);
updateRegionStatement.setInt(1, region.getPriority()); updateRegionStatement.setInt(1, region.getPriority());
updateRegionStatement.setString(2, region.getId().toLowerCase()); updateRegionStatement.setString(2, region.getId().toLowerCase());
updateRegionStatement.execute(); updateRegionStatement.execute();
} finally {
closeResource(updateRegionStatement);
}
updateFlags(region); updateFlags(region);
@ -972,49 +1091,58 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException { private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLException {
updateRegion(region, "cuboid"); updateRegion(region, "cuboid");
PreparedStatement updateCuboidRegionStatement = this.conn.prepareStatement( PreparedStatement updateCuboidRegionStatement = null;
"UPDATE `region_cuboid` SET " + try {
"`min_z` = ?, " + updateCuboidRegionStatement = this.conn.prepareStatement(
"`min_y` = ?, " + "UPDATE `region_cuboid` SET " +
"`min_x` = ?, " + "`min_z` = ?, " +
"`max_z` = ?, " + "`min_y` = ?, " +
"`max_y` = ?, " + "`min_x` = ?, " +
"`max_x` = ? " + "`max_z` = ?, " +
"WHERE `region_id` = ? " + "`max_y` = ?, " +
"AND `world_id` = " + this.worldDbId "`max_x` = ? " +
); "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId
);
BlockVector min = region.getMinimumPoint(); BlockVector min = region.getMinimumPoint();
BlockVector max = region.getMaximumPoint(); BlockVector max = region.getMaximumPoint();
updateCuboidRegionStatement.setInt(1, min.getBlockZ()); updateCuboidRegionStatement.setInt(1, min.getBlockZ());
updateCuboidRegionStatement.setInt(2, min.getBlockY()); updateCuboidRegionStatement.setInt(2, min.getBlockY());
updateCuboidRegionStatement.setInt(3, min.getBlockX()); updateCuboidRegionStatement.setInt(3, min.getBlockX());
updateCuboidRegionStatement.setInt(4, max.getBlockZ()); updateCuboidRegionStatement.setInt(4, max.getBlockZ());
updateCuboidRegionStatement.setInt(5, max.getBlockY()); updateCuboidRegionStatement.setInt(5, max.getBlockY());
updateCuboidRegionStatement.setInt(6, max.getBlockX()); updateCuboidRegionStatement.setInt(6, max.getBlockX());
updateCuboidRegionStatement.setString(7, region.getId().toLowerCase()); updateCuboidRegionStatement.setString(7, region.getId().toLowerCase());
updateCuboidRegionStatement.execute(); updateCuboidRegionStatement.execute();
} finally {
closeResource(updateCuboidRegionStatement);
}
} }
private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException { private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLException {
updateRegion(region, "poly2d"); updateRegion(region, "poly2d");
PreparedStatement updatePoly2dRegionStatement = this.conn.prepareStatement( PreparedStatement updatePoly2dRegionStatement = null;
"UPDATE `region_poly2d` SET " + try {
"`max_y` = ?, " + updatePoly2dRegionStatement = this.conn.prepareStatement(
"`min_y` = ? " + "UPDATE `region_poly2d` SET " +
"WHERE `region_id` = ? " + "`max_y` = ?, " +
"AND `world_id` = " + this.worldDbId "`min_y` = ? " +
); "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId
);
updatePoly2dRegionStatement.setInt(1, region.getMaximumPoint().getBlockY()); updatePoly2dRegionStatement.setInt(1, region.getMaximumPoint().getBlockY());
updatePoly2dRegionStatement.setInt(2, region.getMinimumPoint().getBlockY()); updatePoly2dRegionStatement.setInt(2, region.getMinimumPoint().getBlockY());
updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase()); updatePoly2dRegionStatement.setString(3, region.getId().toLowerCase());
updatePoly2dRegionStatement.execute();
updatePoly2dRegionStatement.execute();
} finally {
closeResource(updatePoly2dRegionStatement);
}
updatePoly2dPoints(region); updatePoly2dPoints(region);
} }
@ -1022,6 +1150,22 @@ private void updateRegionGlobal(GlobalProtectedRegion region) throws SQLExceptio
updateRegion(region, "global"); updateRegion(region, "global");
} }
private void closeResource(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {}
}
}
private void closeResource(Statement st) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {}
}
}
@Override @Override
public Map<String, ProtectedRegion> getRegions() { public Map<String, ProtectedRegion> getRegions() {
return regions; return regions;