Search in sources :

Example 6 with Closer

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();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) PreparedStatement(java.sql.PreparedStatement)

Example 7 with Closer

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();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) Flag(com.sk89q.worldguard.protection.flags.Flag)

Example 8 with Closer

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();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) PreparedStatement(java.sql.PreparedStatement)

Example 9 with Closer

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();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) PreparedStatement(java.sql.PreparedStatement)

Example 10 with Closer

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();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) Flyway(org.flywaydb.core.Flyway) FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) Connection(java.sql.Connection) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Aggregations

Closer (com.sk89q.worldguard.util.io.Closer)25 PreparedStatement (java.sql.PreparedStatement)19 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)12 ResultSet (java.sql.ResultSet)10 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)7 SQLException (java.sql.SQLException)6 StorageException (com.sk89q.worldguard.protection.managers.storage.StorageException)5 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)2 DefaultDomain (com.sk89q.worldguard.domains.DefaultDomain)2 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Flag (com.sk89q.worldguard.protection.flags.Flag)1 RegionDatabase (com.sk89q.worldguard.protection.managers.storage.RegionDatabase)1 Flyway (org.flywaydb.core.Flyway)1