Merge pull request #316 from zreed/sqltableprefix

This commit is contained in:
sk89q 2014-07-06 15:36:59 -07:00
parent d827e01760
commit 894a42ea13
2 changed files with 41 additions and 39 deletions

View File

@ -111,6 +111,7 @@ public class ConfigurationManager {
public String sqlDsn; public String sqlDsn;
public String sqlUsername; public String sqlUsername;
public String sqlPassword; public String sqlPassword;
public String sqlTablePrefix;
/** /**
* Construct the object. * Construct the object.
@ -168,6 +169,7 @@ public void load() {
sqlDsn = config.getString("regions.sql.dsn", "jdbc:mysql://localhost/worldguard"); sqlDsn = config.getString("regions.sql.dsn", "jdbc:mysql://localhost/worldguard");
sqlUsername = config.getString("regions.sql.username", "worldguard"); sqlUsername = config.getString("regions.sql.username", "worldguard");
sqlPassword = config.getString("regions.sql.password", "worldguard"); sqlPassword = config.getString("regions.sql.password", "worldguard");
sqlTablePrefix = config.getString("regions.sql.table-prefix", "");
// Load configurations for each world // Load configurations for each world
for (World world : plugin.getServer().getWorlds()) { for (World world : plugin.getServer().getWorlds()) {

View File

@ -76,7 +76,7 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
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
verTest = this.conn.prepareStatement( verTest = this.conn.prepareStatement(
"SELECT `world_id` FROM `region_cuboid` LIMIT 0,1;" "SELECT `world_id` FROM `" + config.sqlTablePrefix + "region_cuboid` LIMIT 0,1;"
); );
verTest.execute(); verTest.execute();
} catch (SQLException ex) { } catch (SQLException ex) {
@ -89,7 +89,7 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
worldStmt = conn.prepareStatement( worldStmt = conn.prepareStatement(
"SELECT `id` FROM " + "SELECT `id` FROM " +
"`world` " + "`" + config.sqlTablePrefix + "world` " +
"WHERE `name` = ? LIMIT 0,1" "WHERE `name` = ? LIMIT 0,1"
); );
@ -101,7 +101,7 @@ public MySQLDatabase(ConfigurationManager config, String world, Logger logger) t
} else { } else {
insertWorldStatement = this.conn.prepareStatement( insertWorldStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`world` " + "`" + config.sqlTablePrefix + "world` " +
"(`id`, `name`) VALUES (null, ?)", "(`id`, `name`) VALUES (null, ?)",
Statement.RETURN_GENERATED_KEYS Statement.RETURN_GENERATED_KEYS
); );
@ -170,9 +170,9 @@ private void loadFlags(ProtectedRegion region) {
"SELECT " + "SELECT " +
"`region_flag`.`flag`, " + "`region_flag`.`flag`, " +
"`region_flag`.`value` " + "`region_flag`.`value` " +
"FROM `region_flag` " + "FROM `" + config.sqlTablePrefix + "region_flag` AS `region_flag` " +
"WHERE `region_flag`.`region_id` = ? " + "WHERE `region_id` = ? " +
"AND `region_flag`.`world_id` = " + this.worldDbId "AND `world_id` = " + this.worldDbId
); );
flagsStatement.setString(1, region.getId().toLowerCase()); flagsStatement.setString(1, region.getId().toLowerCase());
@ -225,8 +225,8 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
"SELECT " + "SELECT " +
"`user`.`name`, " + "`user`.`name`, " +
"`region_players`.`owner` " + "`region_players`.`owner` " +
"FROM `region_players` " + "FROM `" + config.sqlTablePrefix + "region_players` AS `region_players` " +
"LEFT JOIN `user` ON ( " + "LEFT JOIN `" + config.sqlTablePrefix + "user` AS `user` ON ( " +
"`region_players`.`user_id` = " + "`region_players`.`user_id` = " +
"`user`.`id`) " + "`user`.`id`) " +
"WHERE `region_players`.`region_id` = ? " + "WHERE `region_players`.`region_id` = ? " +
@ -256,8 +256,8 @@ private void loadOwnersAndMembers(ProtectedRegion region) {
"SELECT " + "SELECT " +
"`group`.`name`, " + "`group`.`name`, " +
"`region_groups`.`owner` " + "`region_groups`.`owner` " +
"FROM `region_groups` " + "FROM `" + config.sqlTablePrefix + "region_groups` AS `region_groups` " +
"LEFT JOIN `group` ON ( " + "LEFT JOIN `" + config.sqlTablePrefix + "group` AS `group` ON ( " +
"`region_groups`.`group_id` = " + "`region_groups`.`group_id` = " +
"`group`.`id`) " + "`group`.`id`) " +
"WHERE `region_groups`.`region_id` = ? " + "WHERE `region_groups`.`region_id` = ? " +
@ -296,8 +296,8 @@ private void loadGlobal() {
"`region`.`id`, " + "`region`.`id`, " +
"`region`.`priority`, " + "`region`.`priority`, " +
"`parent`.`id` AS `parent` " + "`parent`.`id` AS `parent` " +
"FROM `region` " + "FROM `" + config.sqlTablePrefix + "region` AS `region` " +
"LEFT JOIN `region` AS `parent` " + "LEFT JOIN `" + config.sqlTablePrefix + "region` AS `parent` " +
"ON (`region`.`parent` = `parent`.`id` " + "ON (`region`.`parent` = `parent`.`id` " +
"AND `region`.`world_id` = `parent`.`world_id`) " + "AND `region`.`world_id` = `parent`.`world_id`) " +
"WHERE `region`.`type` = 'global' " + "WHERE `region`.`type` = 'global' " +
@ -356,11 +356,11 @@ private void loadCuboid() {
"`region`.`id`, " + "`region`.`id`, " +
"`region`.`priority`, " + "`region`.`priority`, " +
"`parent`.`id` AS `parent` " + "`parent`.`id` AS `parent` " +
"FROM `region_cuboid` " + "FROM `" + config.sqlTablePrefix + "region_cuboid` AS `region_cuboid` " +
"LEFT JOIN `region` " + "LEFT JOIN `" + config.sqlTablePrefix + "region` AS `region` " +
"ON (`region_cuboid`.`region_id` = `region`.`id` " + "ON (`region_cuboid`.`region_id` = `region`.`id` " +
"AND `region_cuboid`.`world_id` = `region`.`world_id`) " + "AND `region_cuboid`.`world_id` = `region`.`world_id`) " +
"LEFT JOIN `region` AS `parent` " + "LEFT JOIN `" + config.sqlTablePrefix + "region` AS `parent` " +
"ON (`region`.`parent` = `parent`.`id` " + "ON (`region`.`parent` = `parent`.`id` " +
"AND `region`.`world_id` = `parent`.`world_id`) " + "AND `region`.`world_id` = `parent`.`world_id`) " +
"WHERE `region`.`world_id` = ? " "WHERE `region`.`world_id` = ? "
@ -433,11 +433,11 @@ private void loadPoly2d() {
"`region`.`id`, " + "`region`.`id`, " +
"`region`.`priority`, " + "`region`.`priority`, " +
"`parent`.`id` AS `parent` " + "`parent`.`id` AS `parent` " +
"FROM `region_poly2d` " + "FROM `" + config.sqlTablePrefix + "region_poly2d` AS `region_poly2d` " +
"LEFT JOIN `region` " + "LEFT JOIN `" + config.sqlTablePrefix + "region` AS `region` " +
"ON (`region_poly2d`.`region_id` = `region`.`id` " + "ON (`region_poly2d`.`region_id` = `region`.`id` " +
"AND `region_poly2d`.`world_id` = `region`.`world_id`) " + "AND `region_poly2d`.`world_id` = `region`.`world_id`) " +
"LEFT JOIN `region` AS `parent` " + "LEFT JOIN `" + config.sqlTablePrefix + "region` AS `parent` " +
"ON (`region`.`parent` = `parent`.`id` " + "ON (`region`.`parent` = `parent`.`id` " +
"AND `region`.`world_id` = `parent`.`world_id`) " + "AND `region`.`world_id` = `parent`.`world_id`) " +
"WHERE `region`.`world_id` = ? " "WHERE `region`.`world_id` = ? "
@ -450,7 +450,7 @@ private void loadPoly2d() {
"SELECT " + "SELECT " +
"`region_poly2d_point`.`x`, " + "`region_poly2d_point`.`x`, " +
"`region_poly2d_point`.`z` " + "`region_poly2d_point`.`z` " +
"FROM `region_poly2d_point` " + "FROM `" + config.sqlTablePrefix + "region_poly2d_point` AS `region_poly2d_point` " +
"WHERE `region_poly2d_point`.`region_id` = ? " + "WHERE `region_poly2d_point`.`region_id` = ? " +
"AND `region_poly2d_point`.`world_id` = " + this.worldDbId "AND `region_poly2d_point`.`world_id` = " + this.worldDbId
); );
@ -571,7 +571,7 @@ private Map<String,Integer> getUserIds(String... usernames) {
"SELECT " + "SELECT " +
"`user`.`id`, " + "`user`.`id`, " +
"`user`.`name` " + "`user`.`name` " +
"FROM `user` " + "FROM `" + config.sqlTablePrefix + "user` AS `user` " +
"WHERE `name` IN (%s)", "WHERE `name` IN (%s)",
RegionDBUtil.preparePlaceHolders(usernames.length) RegionDBUtil.preparePlaceHolders(usernames.length)
) )
@ -587,7 +587,7 @@ private Map<String,Integer> getUserIds(String... usernames) {
insertUserStatement = this.conn.prepareStatement( insertUserStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`user` ( " + "`" + config.sqlTablePrefix + "user` ( " +
"`id`, " + "`id`, " +
"`name`" + "`name`" +
") VALUES (null, ?)", ") VALUES (null, ?)",
@ -642,7 +642,7 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
"SELECT " + "SELECT " +
"`group`.`id`, " + "`group`.`id`, " +
"`group`.`name` " + "`group`.`name` " +
"FROM `group` " + "FROM `" + config.sqlTablePrefix + "group` AS `group` " +
"WHERE `name` IN (%s)", "WHERE `name` IN (%s)",
RegionDBUtil.preparePlaceHolders(groupnames.length) RegionDBUtil.preparePlaceHolders(groupnames.length)
) )
@ -658,7 +658,7 @@ private Map<String,Integer> getGroupIds(String... groupnames) {
insertGroupStatement = this.conn.prepareStatement( insertGroupStatement = this.conn.prepareStatement(
"INSERT INTO " + "INSERT INTO " +
"`group` ( " + "`" + config.sqlTablePrefix + "group` ( " +
"`id`, " + "`id`, " +
"`name`" + "`name`" +
") VALUES (null, ?)", ") VALUES (null, ?)",
@ -718,7 +718,7 @@ public void save() throws ProtectionDatabaseException {
try { try {
getAllRegionsStatement = this.conn.prepareStatement( getAllRegionsStatement = this.conn.prepareStatement(
"SELECT `region`.`id` FROM " + "SELECT `region`.`id` FROM " +
"`region` " + "`" + config.sqlTablePrefix + "region` AS `region` " +
"WHERE `world_id` = ? " "WHERE `world_id` = ? "
); );
@ -775,7 +775,7 @@ public void save() throws ProtectionDatabaseException {
if (entry.getValue().getParent() == null) continue; if (entry.getValue().getParent() == null) continue;
setParentStatement = this.conn.prepareStatement( setParentStatement = this.conn.prepareStatement(
"UPDATE `region` SET " + "UPDATE `" + config.sqlTablePrefix + "region` SET " +
"`parent` = ? " + "`parent` = ? " +
"WHERE `id` = ? AND `world_id` = " + this.worldDbId "WHERE `id` = ? AND `world_id` = " + this.worldDbId
); );
@ -796,7 +796,7 @@ public void save() throws ProtectionDatabaseException {
PreparedStatement removeRegion = null; PreparedStatement removeRegion = null;
try { try {
removeRegion = this.conn.prepareStatement( removeRegion = this.conn.prepareStatement(
"DELETE FROM `region` WHERE `id` = ? " "DELETE FROM `" + config.sqlTablePrefix + "region` WHERE `id` = ? "
); );
removeRegion.setString(1, name); removeRegion.setString(1, name);
@ -814,7 +814,7 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
PreparedStatement clearCurrentFlagStatement = null; PreparedStatement clearCurrentFlagStatement = null;
try { try {
clearCurrentFlagStatement = this.conn.prepareStatement( clearCurrentFlagStatement = this.conn.prepareStatement(
"DELETE FROM `region_flag` " + "DELETE FROM `" + config.sqlTablePrefix + "region_flag` " +
"WHERE `region_id` = ? " + "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId "AND `world_id` = " + this.worldDbId
); );
@ -830,7 +830,7 @@ private void updateFlags(ProtectedRegion region) throws SQLException {
PreparedStatement insertFlagStatement = null; PreparedStatement insertFlagStatement = null;
try { try {
insertFlagStatement = this.conn.prepareStatement( insertFlagStatement = this.conn.prepareStatement(
"INSERT INTO `region_flag` ( " + "INSERT INTO `" + config.sqlTablePrefix + "region_flag` ( " +
"`id`, " + "`id`, " +
"`region_id`, " + "`region_id`, " +
"`world_id`, " + "`world_id`, " +
@ -869,7 +869,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
try { try {
deleteUsersForRegion = this.conn.prepareStatement( deleteUsersForRegion = this.conn.prepareStatement(
"DELETE FROM `region_players` " + "DELETE FROM `" + config.sqlTablePrefix + "region_players` " +
"WHERE `region_id` = ? " + "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId + " " + "AND `world_id` = " + this.worldDbId + " " +
"AND `owner` = ?" "AND `owner` = ?"
@ -880,7 +880,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
deleteUsersForRegion.execute(); deleteUsersForRegion.execute();
insertUsersForRegion = this.conn.prepareStatement( insertUsersForRegion = this.conn.prepareStatement(
"INSERT INTO `region_players` " + "INSERT INTO `" + config.sqlTablePrefix + "region_players` " +
"(`region_id`, `world_id`, `user_id`, `owner`) " + "(`region_id`, `world_id`, `user_id`, `owner`) " +
"VALUES (?, " + this.worldDbId + ", ?, ?)" "VALUES (?, " + this.worldDbId + ", ?, ?)"
); );
@ -896,7 +896,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
} }
deleteGroupsForRegion = this.conn.prepareStatement( deleteGroupsForRegion = this.conn.prepareStatement(
"DELETE FROM `region_groups` " + "DELETE FROM `" + config.sqlTablePrefix + "region_groups` " +
"WHERE `region_id` = ? " + "WHERE `region_id` = ? " +
"AND `world_id` = " + this.worldDbId + " " + "AND `world_id` = " + this.worldDbId + " " +
"AND `owner` = ?" "AND `owner` = ?"
@ -907,7 +907,7 @@ private void updatePlayerAndGroups(ProtectedRegion region, Boolean owners) throw
deleteGroupsForRegion.execute(); deleteGroupsForRegion.execute();
insertGroupsForRegion = this.conn.prepareStatement( insertGroupsForRegion = this.conn.prepareStatement(
"INSERT INTO `region_groups` " + "INSERT INTO `" + config.sqlTablePrefix + "region_groups` " +
"(`region_id`, `world_id`, `group_id`, `owner`) " + "(`region_id`, `world_id`, `group_id`, `owner`) " +
"VALUES (?, " + this.worldDbId + ", ?, ?)" "VALUES (?, " + this.worldDbId + ", ?, ?)"
); );
@ -937,7 +937,7 @@ private void insertRegion(ProtectedRegion region, String type) throws SQLExcepti
PreparedStatement insertRegionStatement = null; PreparedStatement insertRegionStatement = null;
try { try {
insertRegionStatement = this.conn.prepareStatement( insertRegionStatement = this.conn.prepareStatement(
"INSERT INTO `region` (" + "INSERT INTO `" + config.sqlTablePrefix + "region` (" +
"`id`, " + "`id`, " +
"`world_id`, " + "`world_id`, " +
"`type`, " + "`type`, " +
@ -968,7 +968,7 @@ private void insertRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
PreparedStatement insertCuboidRegionStatement = null; PreparedStatement insertCuboidRegionStatement = null;
try { try {
insertCuboidRegionStatement = this.conn.prepareStatement( insertCuboidRegionStatement = this.conn.prepareStatement(
"INSERT INTO `region_cuboid` (" + "INSERT INTO `" + config.sqlTablePrefix + "region_cuboid` (" +
"`region_id`, " + "`region_id`, " +
"`world_id`, " + "`world_id`, " +
"`min_z`, " + "`min_z`, " +
@ -1003,7 +1003,7 @@ private void insertRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
PreparedStatement insertPoly2dRegionStatement = null; PreparedStatement insertPoly2dRegionStatement = null;
try { try {
insertPoly2dRegionStatement = this.conn.prepareStatement( insertPoly2dRegionStatement = this.conn.prepareStatement(
"INSERT INTO `region_poly2d` (" + "INSERT INTO `" + config.sqlTablePrefix + "region_poly2d` (" +
"`region_id`, " + "`region_id`, " +
"`world_id`, " + "`world_id`, " +
"`max_y`, " + "`max_y`, " +
@ -1039,7 +1039,7 @@ private void updatePoly2dPoints(ProtectedPolygonalRegion region) throws SQLExcep
clearPoly2dPointsForRegionStatement.execute(); clearPoly2dPointsForRegionStatement.execute();
insertPoly2dPointStatement = this.conn.prepareStatement( insertPoly2dPointStatement = this.conn.prepareStatement(
"INSERT INTO `region_poly2d_point` (" + "INSERT INTO `" + config.sqlTablePrefix + "region_poly2d_point` (" +
"`id`, " + "`id`, " +
"`region_id`, " + "`region_id`, " +
"`world_id`, " + "`world_id`, " +
@ -1070,7 +1070,7 @@ private void updateRegion(ProtectedRegion region, String type) throws SQLExcepti
PreparedStatement updateRegionStatement = null; PreparedStatement updateRegionStatement = null;
try { try {
updateRegionStatement = this.conn.prepareStatement( updateRegionStatement = this.conn.prepareStatement(
"UPDATE `region` SET " + "UPDATE `" + config.sqlTablePrefix + "region` SET " +
"`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId "`priority` = ? WHERE `id` = ? AND `world_id` = " + this.worldDbId
); );
@ -1094,7 +1094,7 @@ private void updateRegionCuboid(ProtectedCuboidRegion region) throws SQLExceptio
PreparedStatement updateCuboidRegionStatement = null; PreparedStatement updateCuboidRegionStatement = null;
try { try {
updateCuboidRegionStatement = this.conn.prepareStatement( updateCuboidRegionStatement = this.conn.prepareStatement(
"UPDATE `region_cuboid` SET " + "UPDATE `" + config.sqlTablePrefix + "region_cuboid` SET " +
"`min_z` = ?, " + "`min_z` = ?, " +
"`min_y` = ?, " + "`min_y` = ?, " +
"`min_x` = ?, " + "`min_x` = ?, " +
@ -1128,7 +1128,7 @@ private void updateRegionPoly2D(ProtectedPolygonalRegion region) throws SQLExcep
PreparedStatement updatePoly2dRegionStatement = null; PreparedStatement updatePoly2dRegionStatement = null;
try { try {
updatePoly2dRegionStatement = this.conn.prepareStatement( updatePoly2dRegionStatement = this.conn.prepareStatement(
"UPDATE `region_poly2d` SET " + "UPDATE `" + config.sqlTablePrefix + "region_poly2d` SET " +
"`max_y` = ?, " + "`max_y` = ?, " +
"`min_y` = ? " + "`min_y` = ? " +
"WHERE `region_id` = ? " + "WHERE `region_id` = ? " +