Search in sources :

Example 16 with Closer

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

the class SQLRegionDatabase method saveChanges.

@Override
public void saveChanges(RegionDifference difference) throws DifferenceSaveException, StorageException {
    checkNotNull(difference);
    initialize();
    Closer closer = Closer.create();
    DataUpdater updater;
    try {
        try {
            updater = new DataUpdater(this, closer.register(getConnection()));
        } catch (SQLException e) {
            throw new StorageException("Failed to get a connection to the database", e);
        }
        try {
            updater.saveChanges(difference.getChanged(), difference.getRemoved());
        } catch (SQLException e) {
            throw new StorageException("Failed to save the region data to the database", e);
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 17 with Closer

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

the class TableCache method fetch.

/**
 * Fetch from the database rows that match the given entries, otherwise
 * create new entries and assign them an ID.
 *
 * @param entries a list of entries
 * @throws SQLException thrown on SQL error
 */
public void fetch(Collection<V> entries) throws SQLException {
    synchronized (LOCK) {
        // Lock across all cache instances
        checkNotNull(entries);
        // Get a list of missing entries
        List<V> fetchList = new ArrayList<>();
        for (V entry : entries) {
            if (!cache.containsKey(toKey(entry))) {
                fetchList.add(entry);
            }
        }
        if (fetchList.isEmpty()) {
            // Nothing to do
            return;
        }
        // Search for entries
        for (List<V> partition : Lists.partition(fetchList, MAX_NUMBER_PER_QUERY)) {
            Closer closer = Closer.create();
            try {
                PreparedStatement statement = closer.register(conn.prepareStatement(String.format("SELECT id, " + fieldName + " " + "FROM `" + config.getTablePrefix() + tableName + "` " + "WHERE " + fieldName + " IN (%s)", StatementUtils.preparePlaceHolders(partition.size()))));
                String[] values = new String[partition.size()];
                int i = 0;
                for (V entry : partition) {
                    values[i] = fromType(entry);
                    i++;
                }
                StatementUtils.setValues(statement, values);
                ResultSet results = closer.register(statement.executeQuery());
                while (results.next()) {
                    cache.put(toKey(toType(results.getString(fieldName))), results.getInt("id"));
                }
            } finally {
                closer.closeQuietly();
            }
        }
        List<V> missing = new ArrayList<>();
        for (V entry : fetchList) {
            if (!cache.containsKey(toKey(entry))) {
                missing.add(entry);
            }
        }
        // Insert entries that are missing
        if (!missing.isEmpty()) {
            Closer closer = Closer.create();
            try {
                PreparedStatement statement = closer.register(conn.prepareStatement("INSERT INTO `" + config.getTablePrefix() + tableName + "` (id, " + fieldName + ") VALUES (null, ?)", Statement.RETURN_GENERATED_KEYS));
                for (V entry : missing) {
                    statement.setString(1, fromType(entry));
                    statement.execute();
                    ResultSet generatedKeys = statement.getGeneratedKeys();
                    if (generatedKeys.next()) {
                        cache.put(toKey(entry), generatedKeys.getInt(1));
                    } else {
                        log.warning("Could not get the database ID for entry " + entry);
                    }
                }
            } finally {
                closer.closeQuietly();
            }
        }
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 18 with Closer

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

the class DataLoader method loadCuboids.

private void loadCuboids() throws SQLException {
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT g.min_z, g.min_y, g.min_x, " + "       g.max_z, g.max_y, g.max_x, " + "       r.id, r.priority, p.id AS parent " + "FROM " + config.getTablePrefix() + "region_cuboid AS g " + "LEFT JOIN " + config.getTablePrefix() + "region AS r " + "          ON (g.region_id = r.id AND g.world_id = r.world_id) " + "LEFT JOIN " + config.getTablePrefix() + "region AS p " + "          ON (r.parent = p.id AND r.world_id = p.world_id) " + "WHERE r.world_id = " + worldId));
        ResultSet rs = closer.register(stmt.executeQuery());
        while (rs.next()) {
            BlockVector3 pt1 = BlockVector3.at(rs.getInt("min_x"), rs.getInt("min_y"), rs.getInt("min_z"));
            BlockVector3 pt2 = BlockVector3.at(rs.getInt("max_x"), rs.getInt("max_y"), rs.getInt("max_z"));
            BlockVector3 min = pt1.getMinimum(pt2);
            BlockVector3 max = pt1.getMaximum(pt2);
            ProtectedRegion region = new ProtectedCuboidRegion(rs.getString("id"), min, max);
            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) ResultSet(java.sql.ResultSet) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) PreparedStatement(java.sql.PreparedStatement) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) BlockVector3(com.sk89q.worldedit.math.BlockVector3)

Example 19 with Closer

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

the class DataLoader method loadPolygons.

private void loadPolygons() throws SQLException {
    ListMultimap<String, BlockVector2> pointsCache = ArrayListMultimap.create();
    // First get all the vertices and store them in memory
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT region_id, x, z " + "FROM " + config.getTablePrefix() + "region_poly2d_point " + "WHERE world_id = " + worldId));
        ResultSet rs = closer.register(stmt.executeQuery());
        while (rs.next()) {
            pointsCache.put(rs.getString("region_id"), BlockVector2.at(rs.getInt("x"), rs.getInt("z")));
        }
    } finally {
        closer.closeQuietly();
    }
    // Now we pull the regions themselves
    closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT g.min_y, g.max_y, r.id, r.priority, p.id AS parent " + "FROM " + config.getTablePrefix() + "region_poly2d AS g " + "LEFT JOIN " + config.getTablePrefix() + "region AS r " + "          ON (g.region_id = r.id AND g.world_id = r.world_id) " + "LEFT JOIN " + config.getTablePrefix() + "region AS p " + "          ON (r.parent = p.id AND r.world_id = p.world_id) " + "WHERE r.world_id = " + worldId));
        ResultSet rs = closer.register(stmt.executeQuery());
        while (rs.next()) {
            String id = rs.getString("id");
            // Get the points from the cache
            List<BlockVector2> points = pointsCache.get(id);
            if (points.size() < 3) {
                log.log(Level.WARNING, "Invalid polygonal region '" + id + "': region has " + points.size() + " point(s) (less than the required 3). Skipping this region.");
                continue;
            }
            Integer minY = rs.getInt("min_y");
            Integer maxY = rs.getInt("max_y");
            ProtectedRegion region = new ProtectedPolygonalRegion(id, points, minY, maxY);
            region.setPriority(rs.getInt("priority"));
            loaded.put(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) ResultSet(java.sql.ResultSet) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) PreparedStatement(java.sql.PreparedStatement) BlockVector2(com.sk89q.worldedit.math.BlockVector2)

Example 20 with Closer

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

the class DataUpdater method getExistingRegions.

private Map<String, String> getExistingRegions() throws SQLException {
    Map<String, String> existing = new HashMap<>();
    Closer closer = Closer.create();
    try {
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT id, type " + "FROM " + config.getTablePrefix() + "region " + "WHERE world_id = " + worldId));
        ResultSet resultSet = closer.register(stmt.executeQuery());
        while (resultSet.next()) {
            existing.put(resultSet.getString("id"), resultSet.getString("type"));
        }
        return existing;
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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