use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class RegionUpdater method replaceDomainGroups.
private void replaceDomainGroups() throws SQLException {
// Remove groups
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("DELETE FROM " + config.getTablePrefix() + "region_groups " + "WHERE region_id = ? " + "AND world_id = " + worldId));
for (List<ProtectedRegion> partition : Lists.partition(domainsToReplace, StatementBatch.MAX_BATCH_SIZE)) {
for (ProtectedRegion region : partition) {
stmt.setString(1, region.getId());
stmt.addBatch();
}
stmt.executeBatch();
}
} finally {
closer.closeQuietly();
}
// Add groups
closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region_groups " + "(region_id, world_id, group_id, owner) " + "VALUES (?, " + worldId + ", ?, ?)"));
StatementBatch batch = new StatementBatch(stmt, StatementBatch.MAX_BATCH_SIZE);
for (ProtectedRegion region : domainsToReplace) {
// owner = false
insertDomainGroups(stmt, batch, region, region.getMembers(), false);
// owner = true
insertDomainGroups(stmt, batch, region, region.getOwners(), true);
}
batch.executeRemaining();
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class RegionUpdater method replaceFlags.
private void replaceFlags() throws SQLException {
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("DELETE FROM " + config.getTablePrefix() + "region_flag " + "WHERE region_id = ? " + "AND world_id = " + worldId));
for (List<ProtectedRegion> partition : Lists.partition(flagsToReplace, StatementBatch.MAX_BATCH_SIZE)) {
for (ProtectedRegion region : partition) {
stmt.setString(1, region.getId());
stmt.addBatch();
}
stmt.executeBatch();
}
} finally {
closer.closeQuietly();
}
closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region_flag " + "(id, region_id, world_id, flag, value) " + "VALUES " + "(null, ?, " + worldId + ", ?, ?)"));
StatementBatch batch = new StatementBatch(stmt, StatementBatch.MAX_BATCH_SIZE);
for (ProtectedRegion region : flagsToReplace) {
for (Map.Entry<Flag<?>, Object> entry : region.getFlags().entrySet()) {
if (entry.getValue() == null)
continue;
Object flag = marshalFlagValue(entry.getKey(), entry.getValue());
stmt.setString(1, region.getId());
stmt.setString(2, entry.getKey().getName());
stmt.setObject(3, flag);
batch.addBatch();
}
}
batch.executeRemaining();
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class RegionUpdater method updateRegionTypes.
private void updateRegionTypes() throws SQLException {
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("UPDATE " + config.getTablePrefix() + "region " + "SET type = ?, priority = ?, parent = NULL " + "WHERE id = ? AND world_id = " + worldId));
for (List<ProtectedRegion> partition : Lists.partition(typesToUpdate, StatementBatch.MAX_BATCH_SIZE)) {
for (ProtectedRegion region : partition) {
stmt.setString(1, SQLRegionDatabase.getRegionTypeName(region));
stmt.setInt(2, region.getPriority());
stmt.setString(3, region.getId());
stmt.addBatch();
}
stmt.executeBatch();
}
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class RegionUpdater method replaceDomainUsers.
private void replaceDomainUsers() throws SQLException {
// Remove users
Closer closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("DELETE FROM " + config.getTablePrefix() + "region_players " + "WHERE region_id = ? " + "AND world_id = " + worldId));
for (List<ProtectedRegion> partition : Lists.partition(domainsToReplace, StatementBatch.MAX_BATCH_SIZE)) {
for (ProtectedRegion region : partition) {
stmt.setString(1, region.getId());
stmt.addBatch();
}
stmt.executeBatch();
}
} finally {
closer.closeQuietly();
}
// Add users
closer = Closer.create();
try {
PreparedStatement stmt = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region_players " + "(region_id, world_id, user_id, owner) " + "VALUES (?, " + worldId + ", ?, ?)"));
StatementBatch batch = new StatementBatch(stmt, StatementBatch.MAX_BATCH_SIZE);
for (ProtectedRegion region : domainsToReplace) {
// owner = false
insertDomainUsers(stmt, batch, region, region.getMembers(), false);
// owner = true
insertDomainUsers(stmt, batch, region, region.getOwners(), true);
}
batch.executeRemaining();
} finally {
closer.closeQuietly();
}
}
use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.
the class SQLDriver method migrate.
/**
* Attempt to migrate the tables to the latest version.
*
* @throws StorageException thrown if migration fails
* @throws SQLException thrown on SQL error
*/
private void migrate() throws SQLException, StorageException {
Closer closer = Closer.create();
Connection conn = closer.register(getConnection());
try {
// Check some tables
boolean tablesExist;
boolean isRecent;
boolean isBeforeMigrations;
boolean hasMigrations;
try {
tablesExist = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
isRecent = tryQuery(conn, "SELECT world_id FROM " + config.getTablePrefix() + "region_cuboid LIMIT 1");
isBeforeMigrations = !tryQuery(conn, "SELECT uuid FROM " + config.getTablePrefix() + "user LIMIT 1");
hasMigrations = tryQuery(conn, "SELECT * FROM " + config.getTablePrefix() + "migrations LIMIT 1");
} finally {
closer.closeQuietly();
}
// We don't bother with migrating really old tables
if (tablesExist && !isRecent) {
throw new StorageException("Sorry, your tables are too old for the region SQL auto-migration system. " + "Please run region_manual_update_20110325.sql on your database, which comes " + "with WorldGuard or can be found in http://github.com/sk89q/worldguard");
}
// Our placeholders
Map<String, String> placeHolders = new HashMap<>();
placeHolders.put("tablePrefix", config.getTablePrefix());
Flyway flyway = new Flyway();
// checks and issue messages appropriately
if (!hasMigrations) {
flyway.setInitOnMigrate(true);
if (tablesExist) {
// Detect if this is before migrations
if (isBeforeMigrations) {
flyway.setInitVersion(MigrationVersion.fromVersion("1"));
}
log.log(Level.INFO, "The SQL region tables exist but the migrations table seems to not exist yet. Creating the migrations table...");
} else {
// By default, if Flyway sees any tables at all in the schema, it
// will assume that we are up to date, so we have to manually
// check ourselves and then ask Flyway to start from the beginning
// if our test table doesn't exist
flyway.setInitVersion(MigrationVersion.fromVersion("0"));
log.log(Level.INFO, "SQL region tables do not exist: creating...");
}
}
flyway.setClassLoader(getClass().getClassLoader());
flyway.setLocations("migrations/region/" + getMigrationFolderName());
flyway.setDataSource(config.getDsn(), config.getUsername(), config.getPassword());
flyway.setTable(config.getTablePrefix() + "migrations");
flyway.setPlaceholders(placeHolders);
flyway.setValidateOnMigrate(false);
flyway.migrate();
} catch (FlywayException e) {
throw new StorageException("Failed to migrate tables", e);
} finally {
closer.closeQuietly();
}
}
Aggregations