Search in sources :

Example 21 with Closer

use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.

the class RegionInserter method insertPolygons.

private void insertPolygons() throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region_poly2d " + "(region_id, world_id, max_y, min_y) " + "VALUES " + "(?, " + worldId + ", ?, ?)"));
        for (List<ProtectedPolygonalRegion> partition : Lists.partition(polygons, StatementBatch.MAX_BATCH_SIZE)) {
            for (ProtectedPolygonalRegion region : partition) {
                stmt.setString(1, region.getId());
                stmt.setInt(2, region.getMaximumPoint().getBlockY());
                stmt.setInt(3, region.getMinimumPoint().getBlockY());
                stmt.addBatch();
            }
            stmt.executeBatch();
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) PreparedStatement(java.sql.PreparedStatement)

Example 22 with Closer

use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.

the class RegionInserter method insertRegionTypes.

private void insertRegionTypes() throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region " + "(id, world_id, type, priority, parent) " + "VALUES " + "(?, ?, ?, ?, NULL)"));
        for (List<ProtectedRegion> partition : Lists.partition(all, StatementBatch.MAX_BATCH_SIZE)) {
            for (ProtectedRegion region : partition) {
                stmt.setString(1, region.getId());
                stmt.setInt(2, worldId);
                stmt.setString(3, SQLRegionDatabase.getRegionTypeName(region));
                stmt.setInt(4, region.getPriority());
                stmt.addBatch();
            }
            stmt.executeBatch();
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) PreparedStatement(java.sql.PreparedStatement)

Example 23 with Closer

use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.

the class RegionRemover method removeRows.

private void removeRows(Collection<String> names, String table, String field) throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("DELETE FROM " + config.getTablePrefix() + table + " WHERE " + field + " = ? AND world_id = " + worldId));
        StatementBatch batch = new StatementBatch(stmt, StatementBatch.MAX_BATCH_SIZE);
        for (String name : names) {
            stmt.setString(1, name);
            batch.addBatch();
        }
        batch.executeRemaining();
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) PreparedStatement(java.sql.PreparedStatement)

Example 24 with Closer

use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.

the class DataLoader method loadGlobals.

private void loadGlobals() throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT r.id, r.priority, p.id AS parent " + "FROM " + config.getTablePrefix() + "region AS r " + "LEFT JOIN " + config.getTablePrefix() + "region AS p " + "          ON (r.parent = p.id AND r.world_id = p.world_id) " + "WHERE r.type = 'global' AND r.world_id = " + worldId));
        ResultSet rs = closer.register(stmt.executeQuery());
        while (rs.next()) {
            ProtectedRegion region = new GlobalProtectedRegion(rs.getString("id"));
            region.setPriority(rs.getInt("priority"));
            loaded.put(rs.getString("id"), region);
            String parentId = rs.getString("parent");
            if (parentId != null) {
                parentSets.put(region, parentId);
            }
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ResultSet(java.sql.ResultSet) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) PreparedStatement(java.sql.PreparedStatement)

Example 25 with Closer

use of com.sk89q.worldguard.util.io.Closer in project WorldGuard by EngineHub.

the class DataLoader method loadFlags.

private void loadFlags() throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT region_id, flag, value " + "FROM " + config.getTablePrefix() + "region_flag " + "WHERE world_id = " + worldId + " AND region_id IN " + "(SELECT id FROM " + config.getTablePrefix() + "region " + "WHERE world_id = " + worldId + ")"));
        ResultSet rs = closer.register(stmt.executeQuery());
        Table<String, String, Object> data = HashBasedTable.create();
        while (rs.next()) {
            data.put(rs.getString("region_id"), rs.getString("flag"), unmarshalFlagValue(rs.getString("value")));
        }
        for (Entry<String, Map<String, Object>> entry : data.rowMap().entrySet()) {
            ProtectedRegion region = loaded.get(entry.getKey());
            region.setFlags(flagRegistry.unmarshal(entry.getValue(), true));
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ResultSet(java.sql.ResultSet) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

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