use of dev.rosewood.rosegarden.database.MySQLConnector in project PlayerPoints by Rosewood-Development.
the class _1_Create_Tables method migrate.
@Override
public void migrate(DatabaseConnector connector, Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
String query;
if (connector instanceof SQLiteConnector) {
query = "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?";
} else {
query = "SHOW TABLES LIKE ?";
}
// Check if the old table already exists, if it does then try renaming the table to playerpoints_points and the 'playername' column to 'uuid'
boolean exists;
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, "playerpoints");
exists = statement.executeQuery().next();
}
if (exists) {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("ALTER TABLE playerpoints RENAME TO " + tablePrefix + "points");
} catch (Exception ignored) {
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("ALTER TABLE " + tablePrefix + "points RENAME COLUMN playername TO uuid");
} catch (Exception ignored) {
}
} else {
// Create points table
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("CREATE TABLE " + tablePrefix + "points (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "uuid VARCHAR(36) NOT NULL, " + "points INTEGER NOT NULL, " + "UNIQUE (uuid)" + ")");
}
}
// Attempt to import legacy data if it exists and we are using SQLite
// First make sure there isn't already any data in the database for some reason
PlayerPoints plugin = PlayerPoints.getInstance();
DataManager dataManager = plugin.getManager(DataManager.class);
File file = new File(plugin.getDataFolder(), "storage.yml");
if (dataManager.getTopSortedPoints(1).isEmpty() && file.exists() && connector instanceof SQLiteConnector) {
try {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
ConfigurationSection section = configuration.getConfigurationSection("Points");
if (section == null)
section = configuration.getConfigurationSection("Players");
if (section == null) {
plugin.getLogger().warning("Malformed storage.yml file.");
return;
}
SortedSet<SortedPlayer> data = new TreeSet<>();
for (String uuid : section.getKeys(false)) data.add(new SortedPlayer(UUID.fromString(uuid), section.getInt(uuid)));
plugin.getManager(DataManager.class).importData(data);
plugin.getLogger().warning("Imported legacy data from storage.yml");
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of dev.rosewood.rosegarden.database.MySQLConnector in project RoseStacker by Rosewood-Development.
the class _1_Create_Tables_Stacks method migrate.
@Override
public void migrate(DatabaseConnector connector, Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = connector instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
String blob = connector instanceof MySQLConnector ? "LONGBLOB" : "BLOB";
// Create StackedEntity table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "stacked_entity (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "entity_uuid VARCHAR(36) NOT NULL, " + "stack_entities " + blob + " NOT NULL, " + "world VARCHAR(255) NOT NULL, " + "chunk_x INTEGER NOT NULL, " + "chunk_z INTEGER NOT NULL, " + "UNIQUE (entity_uuid)" + ")");
}
// Create StackedItem table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "stacked_item (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "entity_uuid VARCHAR(36) NOT NULL, " + "stack_size INTEGER NOT NULL, " + "world VARCHAR(255) NOT NULL, " + "chunk_x INTEGER NOT NULL, " + "chunk_z INTEGER NOT NULL, " + "UNIQUE (entity_uuid)" + ")");
}
// Create StackedBlock table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "stacked_block (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "stack_size INTEGER NOT NULL, " + "world VARCHAR(255) NOT NULL, " + "chunk_x INTEGER NOT NULL, " + "chunk_z INTEGER NOT NULL, " + "block_x INTEGER NOT NULL, " + "block_y INTEGER NOT NULL, " + "block_z INTEGER NOT NULL, " + "UNIQUE (world, chunk_x, chunk_z, block_x, block_y, block_z)" + ")");
}
// Create StackedSpawner table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "stacked_spawner (" + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + "stack_size INTEGER NOT NULL, " + "world VARCHAR(255) NOT NULL, " + "chunk_x INTEGER NOT NULL, " + "chunk_z INTEGER NOT NULL, " + "block_x INTEGER NOT NULL, " + "block_y INTEGER NOT NULL, " + "block_z INTEGER NOT NULL, " + "UNIQUE (world, chunk_x, chunk_z, block_x, block_y, block_z)" + ")");
}
// Index the tables by world, chunk_x, and chunk_z
try (Statement statement = connection.createStatement()) {
statement.addBatch("CREATE INDEX " + tablePrefix + "stacked_block_index ON " + tablePrefix + "stacked_block (world, chunk_x, chunk_z)");
statement.addBatch("CREATE INDEX " + tablePrefix + "stacked_entity_index ON " + tablePrefix + "stacked_entity (world, chunk_x, chunk_z)");
statement.addBatch("CREATE INDEX " + tablePrefix + "stacked_item_index ON " + tablePrefix + "stacked_item (world, chunk_x, chunk_z)");
statement.addBatch("CREATE INDEX " + tablePrefix + "stacked_spawner_index ON " + tablePrefix + "stacked_spawner (world, chunk_x, chunk_z)");
statement.executeBatch();
}
}
use of dev.rosewood.rosegarden.database.MySQLConnector 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);
}
}
use of dev.rosewood.rosegarden.database.MySQLConnector in project PlayerPoints by Rosewood-Development.
the class ImportCommand method execute.
@Override
public void execute(PlayerPoints plugin, CommandSender sender, String[] args) {
LocaleManager localeManager = plugin.getManager(LocaleManager.class);
File file = new File(plugin.getDataFolder(), "storage.yml");
if (!file.exists()) {
localeManager.sendMessage(sender, "command-import-no-backup");
return;
}
if (args.length < 1 || !args[0].equalsIgnoreCase("confirm")) {
String databaseType = plugin.getManager(DataManager.class).getDatabaseConnector() instanceof MySQLConnector ? "MySQL" : "SQLite";
localeManager.sendMessage(sender, "command-import-warning", StringPlaceholders.single("type", databaseType));
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
ConfigurationSection section = configuration.getConfigurationSection("Points");
if (section == null)
section = configuration.getConfigurationSection("Players");
if (section == null) {
plugin.getLogger().warning("Malformed storage.yml file.");
return;
}
SortedSet<SortedPlayer> data = new TreeSet<>();
for (String uuid : section.getKeys(false)) data.add(new SortedPlayer(UUID.fromString(uuid), section.getInt(uuid)));
plugin.getManager(DataManager.class).importData(data);
localeManager.sendMessage(sender, "command-import-success");
});
}
Aggregations