Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.relisoft.plugin;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.Bukkit;
- import com.mysql.jdbc.Statement;
- import cz.relisoft.mysql.CustomConnection;
- import cz.relisoft.plugin.models.Groups;
- import cz.relisoft.plugin.models.Permission;
- import cz.relisoft.plugin.models.Player;
- import cz.relisoft.plugin.models.PlayerJoins;
- import cz.relisoft.plugin.models.PlayerQuit;
- import cz.relisoft.plugin.models.Spawns;
- import cz.relisoft.plugin.models.Vip;
- import cz.relisoft.plugin.models.Warp;
- public class SQLModels {
- Main main;
- CustomConnection cC;
- Player playerModel;
- PlayerJoins playerJoinsModel;
- PlayerQuit playerQuitModel;
- Warp warpModel;
- Permission permissionModel;
- Groups groupModel;
- Spawns spawns;
- Vip vipModel;
- public SQLModels(Main main) {
- this.main = main;
- this.cC = main.getCustomConnection();
- this.playerModel = new Player();
- this.playerJoinsModel = new PlayerJoins();
- this.playerQuitModel = new PlayerQuit();
- this.warpModel = new Warp();
- this.permissionModel = new Permission();
- this.groupModel = new Groups();
- this.spawns = new Spawns();
- this.vipModel = new Vip();
- }
- private class AsyncUpdate implements Runnable {
- private String query;
- private Statement statement;
- public AsyncUpdate(Statement statement, String query) {
- this.query = query;
- this.statement = statement;
- }
- @Override
- public void run() {
- try {
- this.statement.executeUpdate(query);
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private class AsyncSelect implements Runnable {
- private String query;
- private Statement statement;
- private ResultCallback result;
- public AsyncSelect(Statement statement, String query, final ResultCallback result) {
- this.query = query;
- this.statement = statement;
- this.result = result;
- }
- @Override
- public void run() {
- try {
- ResultSet result = this.statement.executeQuery(query);
- this.result.done(result);
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- private class AsyncCreateTables implements Runnable {
- private Statement statement;
- private Player playerModel;
- private PlayerJoins playerJoinsModel;
- private PlayerQuit playerQuitModel;
- private Warp warpModel;
- private Spawns spawnModel;
- private Vip vipModel;
- public AsyncCreateTables(Statement statement,
- Player playerModel,
- PlayerJoins playerJoinsModel,
- PlayerQuit playerQuitModel,
- Warp warp, Permission permission,
- Groups groups,
- Spawns spawns,
- Vip vip) {
- this.statement = statement;
- this.playerModel = playerModel;
- this.playerJoinsModel = playerJoinsModel;
- this.playerQuitModel = playerQuitModel;
- this.warpModel = warp;
- this.spawnModel = spawns;
- this.vipModel = vip;
- }
- @Override
- public void run() {
- try {
- this.statement.executeUpdate(this.playerModel.getTableQuery());
- this.statement.executeUpdate(this.playerJoinsModel.getTableQuery());
- this.statement.executeUpdate(this.playerQuitModel.getTableQuery());
- this.statement.executeUpdate(this.warpModel.getTableQuery());
- this.statement.executeUpdate(this.spawnModel.getTableQuery());
- this.statement.executeUpdate(this.vipModel.getTableQuery());
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void createTables() throws ClassNotFoundException, SQLException {
- Statement statement = (Statement) this.main.getCustomConnection().getConnection().createStatement();
- Bukkit.getScheduler().runTaskAsynchronously(this.main, new AsyncCreateTables(statement,
- this.playerModel,
- this.playerJoinsModel,
- this.playerQuitModel,
- this.warpModel,
- this.permissionModel,
- this.groupModel,
- this.spawns,
- this.vipModel));
- }
- public void createUpdateQuery(String query) throws ClassNotFoundException, SQLException {
- Statement statement = (Statement) this.main.getCustomConnection().getConnection().createStatement();
- Bukkit.getScheduler().runTaskAsynchronously(this.main, new AsyncUpdate(statement, query));
- }
- public void createSelectQuery(String query,ResultCallback result) throws ClassNotFoundException, SQLException {
- Statement statement = (Statement) this.main.getCustomConnection().getConnection().createStatement();
- Bukkit.getScheduler().runTaskAsynchronously(this.main, new AsyncSelect(statement, query,result));
- }
- public ResultSet createSelectQuerySync(String query) throws SQLException, ClassNotFoundException {
- Statement statement = (Statement) this.main.getCustomConnection().getConnection().createStatement();
- ResultSet result = statement.executeQuery(query);
- return result;
- }
- public void createUpdateQuerySync(String query) throws SQLException, ClassNotFoundException {
- Statement statement = (Statement) this.main.getCustomConnection().getConnection().createStatement();
- statement.executeUpdate(query);
- }
- public abstract class ResultCallback {
- public abstract void done(ResultSet res) throws SQLException, ClassNotFoundException;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement