Search in sources :

Example 11 with Closer

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

the class SQLDriver method getAll.

@Override
public List<RegionDatabase> getAll() throws StorageException {
    Closer closer = Closer.create();
    try {
        List<RegionDatabase> stores = new ArrayList<>();
        Connection connection = closer.register(getConnection());
        Statement stmt = connection.createStatement();
        ResultSet rs = closer.register(stmt.executeQuery("SELECT name FROM " + config.getTablePrefix() + "world"));
        while (rs.next()) {
            stores.add(get(rs.getString(1)));
        }
        return stores;
    } catch (SQLException e) {
        throw new StorageException("Failed to fetch list of worlds", e);
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) RegionDatabase(com.sk89q.worldguard.protection.managers.storage.RegionDatabase) StorageException(com.sk89q.worldguard.protection.managers.storage.StorageException)

Example 12 with Closer

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

the class SQLRegionDatabase method chooseWorldId.

/**
 * Get the ID for this world from the database or pick a new one if
 * an entry does not exist yet.
 *
 * @param worldName the world name
 * @return a world ID
 * @throws SQLException on a database access error
 */
private int chooseWorldId(String worldName) throws SQLException {
    Closer closer = Closer.create();
    try {
        Connection conn = closer.register(getConnection());
        PreparedStatement stmt = closer.register(conn.prepareStatement("SELECT id FROM " + config.getTablePrefix() + "world WHERE name = ? LIMIT 0, 1"));
        stmt.setString(1, worldName);
        ResultSet worldResult = closer.register(stmt.executeQuery());
        if (worldResult.next()) {
            return worldResult.getInt("id");
        } else {
            PreparedStatement stmt2 = closer.register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "world  (id, name) VALUES (null, ?)", Statement.RETURN_GENERATED_KEYS));
            stmt2.setString(1, worldName);
            stmt2.execute();
            ResultSet generatedKeys = stmt2.getGeneratedKeys();
            if (generatedKeys.next()) {
                return generatedKeys.getInt(1);
            } else {
                throw new SQLException("Expected result, got none");
            }
        }
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 13 with Closer

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

the class SQLRegionDatabase method saveAll.

@Override
public void saveAll(Set<ProtectedRegion> regions) throws StorageException {
    checkNotNull(regions);
    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.saveAll(regions);
        } 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 14 with Closer

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

the class SQLDriver method tryQuery.

/**
 * Try to execute a query and return true if it did not fail.
 *
 * @param conn the connection to run the query on
 * @param sql the SQL query
 * @return true if the query did not end in error
 */
private boolean tryQuery(Connection conn, String sql) {
    Closer closer = Closer.create();
    try {
        Statement statement = closer.register(conn.createStatement());
        statement.executeQuery(sql);
        return true;
    } catch (SQLException ex) {
        return false;
    } finally {
        closer.closeQuietly();
    }
}
Also used : Closer(com.sk89q.worldguard.util.io.Closer) SQLException(java.sql.SQLException) Statement(java.sql.Statement)

Example 15 with Closer

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

the class SQLRegionDatabase method loadAll.

@Override
public Set<ProtectedRegion> loadAll(FlagRegistry flagRegistry) throws StorageException {
    initialize();
    Closer closer = Closer.create();
    DataLoader loader;
    try {
        try {
            loader = new DataLoader(this, closer.register(getConnection()), flagRegistry);
        } catch (SQLException e) {
            throw new StorageException("Failed to get a connection to the database", e);
        }
        try {
            return loader.load();
        } 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)

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