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();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations