Search in sources :

Example 1 with SQLiteConnector

use of dev.rosewood.rosegarden.database.SQLiteConnector in project RoseGarden by Rosewood-Development.

the class DataMigrationManager method reload.

@Override
public void reload() {
    AbstractDataManager dataManager = this.rosePlugin.getManager(AbstractDataManager.class);
    DatabaseConnector databaseConnector = dataManager.getDatabaseConnector();
    databaseConnector.connect((connection -> {
        int currentMigration = -1;
        boolean migrationsExist;
        String query;
        if (databaseConnector instanceof SQLiteConnector) {
            query = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?";
        } else {
            query = "SHOW TABLES LIKE ?";
        }
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, this.getMigrationsTableName());
            migrationsExist = statement.executeQuery().next();
        }
        if (!migrationsExist) {
            // No migration table exists, create one
            String createTable = "CREATE TABLE " + this.getMigrationsTableName() + " (migration_version INT NOT NULL)";
            try (PreparedStatement statement = connection.prepareStatement(createTable)) {
                statement.execute();
            }
            // Insert primary row into migration table
            String insertRow = "INSERT INTO " + this.getMigrationsTableName() + " VALUES (?)";
            try (PreparedStatement statement = connection.prepareStatement(insertRow)) {
                statement.setInt(1, -1);
                statement.execute();
            }
        } else {
            // Grab the current migration version
            String selectVersion = "SELECT migration_version FROM " + this.getMigrationsTableName();
            try (PreparedStatement statement = connection.prepareStatement(selectVersion)) {
                ResultSet result = statement.executeQuery();
                result.next();
                currentMigration = result.getInt("migration_version");
            }
        }
        // Grab required migrations
        int finalCurrentMigration = currentMigration;
        List<DataMigration> requiredMigrations = this.migrations.stream().filter(x -> x.getRevision() > finalCurrentMigration).sorted(Comparator.comparingInt(DataMigration::getRevision)).collect(Collectors.toList());
        // Nothing to migrate, abort
        if (requiredMigrations.isEmpty())
            return;
        // Migrate the data
        for (DataMigration dataMigration : requiredMigrations) dataMigration.migrate(databaseConnector, connection, dataManager.getTablePrefix());
        // Set the new current migration to be the highest migrated to
        currentMigration = requiredMigrations.stream().mapToInt(DataMigration::getRevision).max().orElse(-1);
        String updateVersion = "UPDATE " + this.getMigrationsTableName() + " SET migration_version = ?";
        try (PreparedStatement statement = connection.prepareStatement(updateVersion)) {
            statement.setInt(1, currentMigration);
            statement.execute();
        }
    }));
}
Also used : DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) List(java.util.List) ResultSet(java.sql.ResultSet) DataMigration(dev.rosewood.rosegarden.database.DataMigration) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) RoseGardenUtils(dev.rosewood.rosegarden.utils.RoseGardenUtils) PreparedStatement(java.sql.PreparedStatement) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) RosePlugin(dev.rosewood.rosegarden.RosePlugin) ArrayList(java.util.ArrayList) DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) List(java.util.List) ArrayList(java.util.ArrayList) DataMigration(dev.rosewood.rosegarden.database.DataMigration)

Example 2 with SQLiteConnector

use of dev.rosewood.rosegarden.database.SQLiteConnector in project RoseStacker by Rosewood-Development.

the class EpicSpawnersPluginConverter method convert.

@Override
public void convert() {
    // Query their database ourselves
    DatabaseConnector connector = new SQLiteConnector(this.epicSpawners);
    connector.connect(connection -> {
        Set<StackedSpawner> stackedSpawners = new HashSet<>();
        String tablePrefix = this.epicSpawners.getDataManager().getTablePrefix();
        try (Statement statement = connection.createStatement()) {
            String query = "SELECT amount, world, x, y, z FROM " + tablePrefix + "placed_spawners ps JOIN " + tablePrefix + "spawner_stacks ss ON ps.id = ss.spawner_id";
            ResultSet result = statement.executeQuery(query);
            while (result.next()) {
                World world = Bukkit.getWorld(result.getString("world"));
                if (world == null)
                    continue;
                int amount = result.getInt("amount");
                double x = result.getDouble("x");
                double y = result.getDouble("y");
                double z = result.getDouble("z");
                Location location = new Location(world, x, y, z);
                stackedSpawners.add(new StackedSpawner(amount, location));
            }
        }
        if (!stackedSpawners.isEmpty()) {
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
                for (StackedSpawner stackedSpawner : stackedSpawners) stackManager.createSpawnerStack(stackedSpawner.getLocation().getBlock(), stackedSpawner.getStackSize(), false);
            });
        }
    });
}
Also used : DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) Statement(java.sql.Statement) StackManager(dev.rosewood.rosestacker.manager.StackManager) World(org.bukkit.World) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 3 with SQLiteConnector

use of dev.rosewood.rosegarden.database.SQLiteConnector in project RoseStacker by Rosewood-Development.

the class DataManager method setConversionHandlers.

public void setConversionHandlers(Set<ConverterType> converterTypes) {
    this.databaseConnector.connect(connection -> {
        String insertIgnore;
        if (this.databaseConnector instanceof SQLiteConnector) {
            insertIgnore = "INSERT OR IGNORE INTO ";
        } else {
            insertIgnore = "INSERT IGNORE ";
        }
        String query = insertIgnore + this.getTablePrefix() + "convert_handler (name) VALUES (?)";
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            for (ConverterType converterType : converterTypes) {
                statement.setString(1, converterType.name());
                statement.addBatch();
            }
            statement.executeBatch();
        }
    });
}
Also used : SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) PreparedStatement(java.sql.PreparedStatement) ConverterType(dev.rosewood.rosestacker.conversion.ConverterType)

Example 4 with SQLiteConnector

use of dev.rosewood.rosegarden.database.SQLiteConnector in project RoseStacker by Rosewood-Development.

the class WildStackerPluginConverter method convert.

@Override
public void convert() {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    // Force save loaded data
    SystemManager systemHandler = WildStackerAPI.getWildStacker().getSystemManager();
    systemHandler.performCacheSave();
    // Go through the database to be able to load all information
    DatabaseConnector connector = new SQLiteConnector(this.wildStacker, "database");
    connector.connect(connection -> {
        // Load barrels (blocks)
        Set<StackedBlock> stackedBlocks = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM barrels");
            while (result.next()) {
                Location location = this.parseLocation(result.getString("location"), ',');
                if (location == null)
                    continue;
                int amount = result.getInt("stackAmount");
                Material type = systemHandler.getStackedSnapshot(location.getChunk()).getStackedBarrelItem(location).getValue().getType();
                Block block = location.getBlock();
                // Remove hologram thingy
                StackedBarrel barrel = systemHandler.getStackedBarrel(block);
                if (barrel != null)
                    barrel.removeDisplayBlock();
                // Set the block type to the stack type since we just removed the hologram thingy
                block.setType(type);
                // Stacks of 1 aren't really stacks
                if (amount == 1)
                    continue;
                stackedBlocks.add(new StackedBlock(amount, block));
            }
        }
        // Load spawners
        Set<StackedSpawner> stackedSpawners = new HashSet<>();
        try (Statement statement = connection.createStatement()) {
            ResultSet result = statement.executeQuery("SELECT location, stackAmount FROM spawners");
            while (result.next()) {
                Location location = this.parseLocation(result.getString("location"), ',');
                if (location == null)
                    continue;
                Block block = location.getBlock();
                BlockState blockState = block.getState();
                if (!(blockState instanceof CreatureSpawner))
                    continue;
                int amount = result.getInt("stackAmount");
                stackedSpawners.add(new StackedSpawner(amount, location));
            }
        }
        if (!stackedBlocks.isEmpty() || !stackedSpawners.isEmpty()) {
            Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
                for (StackedBlock stackedBlock : stackedBlocks) stackManager.createBlockStack(stackedBlock.getLocation().getBlock(), stackedBlock.getStackSize());
                for (StackedSpawner stackedSpawner : stackedSpawners) stackManager.createSpawnerStack(stackedSpawner.getLocation().getBlock(), stackedSpawner.getStackSize(), false);
            });
        }
    });
}
Also used : DatabaseConnector(dev.rosewood.rosegarden.database.DatabaseConnector) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) Statement(java.sql.Statement) SystemManager(com.bgsoftware.wildstacker.api.handlers.SystemManager) Material(org.bukkit.Material) CreatureSpawner(org.bukkit.block.CreatureSpawner) BlockState(org.bukkit.block.BlockState) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) ResultSet(java.sql.ResultSet) StackedBarrel(com.bgsoftware.wildstacker.api.objects.StackedBarrel) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) HashSet(java.util.HashSet) Location(org.bukkit.Location)

Example 5 with SQLiteConnector

use of dev.rosewood.rosegarden.database.SQLiteConnector in project RoseGarden by Rosewood-Development.

the class AbstractDataManager method reload.

@Override
public final void reload() {
    try {
        AbstractConfigurationManager configurationManager = this.rosePlugin.getManager(AbstractConfigurationManager.class);
        Map<String, RoseSetting> roseSettings = configurationManager.getSettings();
        if (roseSettings.get("mysql-settings.enabled").getBoolean()) {
            String hostname = roseSettings.get("mysql-settings.hostname").getString();
            int port = roseSettings.get("mysql-settings.port").getInt();
            String database = roseSettings.get("mysql-settings.database-name").getString();
            String username = roseSettings.get("mysql-settings.user-name").getString();
            String password = roseSettings.get("mysql-settings.user-password").getString();
            boolean useSSL = roseSettings.get("mysql-settings.use-ssl").getBoolean();
            int poolSize = roseSettings.get("mysql-settings.connection-pool-size").getInt();
            this.databaseConnector = new MySQLConnector(this.rosePlugin, hostname, port, database, username, password, useSSL, poolSize);
            this.rosePlugin.getLogger().info("Data handler connected using MySQL.");
        } else {
            this.databaseConnector = new SQLiteConnector(this.rosePlugin);
            this.databaseConnector.cleanup();
            this.rosePlugin.getLogger().info("Data handler connected using SQLite.");
        }
    } catch (Exception ex) {
        this.rosePlugin.getLogger().severe("Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
        ex.printStackTrace();
        Bukkit.getPluginManager().disablePlugin(this.rosePlugin);
    }
}
Also used : RoseSetting(dev.rosewood.rosegarden.config.RoseSetting) SQLiteConnector(dev.rosewood.rosegarden.database.SQLiteConnector) MySQLConnector(dev.rosewood.rosegarden.database.MySQLConnector)

Aggregations

SQLiteConnector (dev.rosewood.rosegarden.database.SQLiteConnector)5 DatabaseConnector (dev.rosewood.rosegarden.database.DatabaseConnector)3 ResultSet (java.sql.ResultSet)3 StackManager (dev.rosewood.rosestacker.manager.StackManager)2 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 HashSet (java.util.HashSet)2 Location (org.bukkit.Location)2 SystemManager (com.bgsoftware.wildstacker.api.handlers.SystemManager)1 StackedBarrel (com.bgsoftware.wildstacker.api.objects.StackedBarrel)1 RosePlugin (dev.rosewood.rosegarden.RosePlugin)1 RoseSetting (dev.rosewood.rosegarden.config.RoseSetting)1 DataMigration (dev.rosewood.rosegarden.database.DataMigration)1 MySQLConnector (dev.rosewood.rosegarden.database.MySQLConnector)1 RoseGardenUtils (dev.rosewood.rosegarden.utils.RoseGardenUtils)1 ConverterType (dev.rosewood.rosestacker.conversion.ConverterType)1 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1