package net.crystalyx.bukkit.simplyperms.io; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.sql.Connection; import net.crystalyx.bukkit.simplyperms.SimplyPlugin; public class ConfigSQL implements PermsConfig { private SimplyPlugin plugin; private Connection connection; private String table_players; private String table_groups; private String column_playerid; private String column_groupname; private String column_permission; private String column_world; private String column_value; private String column_date; public ConfigSQL(SimplyPlugin plugin) { this.plugin = plugin; table_players = plugin.getConfig().getString("db/table/players"); table_groups = plugin.getConfig().getString("db/table/groups"); column_playerid = plugin.getConfig().getString("db/column/playerid"); column_groupname = plugin.getConfig().getString("db/column/groupname"); column_permission = plugin.getConfig().getString("db/column/permission"); column_world = plugin.getConfig().getString("db/column/world"); column_value = plugin.getConfig().getString("db/column/value"); column_date = plugin.getConfig().getString("db/column/date"); } private boolean init() { try { if (connection != null) { connection.close(); } connection = DriverManager.getConnection("jdbc:" + plugin.getConfig().getString("db/type") + "://" + plugin.getConfig().getString("db/host") + ":" + plugin.getConfig().getString("db/port") + "/" + plugin.getConfig().getString("db/database"), plugin.getConfig().getString("db/user"), plugin.getConfig().getString("db/pass")); return true; } catch (SQLException e) { plugin.debug(e.getMessage()); return false; } } public boolean checkDatabase() { if (init()) { String date = (!column_date.isEmpty()) ? column_date + " DATETIME," : ""; try { PreparedStatement sql = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + table_players + "(" + column_playerid + " VARCHAR(16) NOT NULL, " + column_world + " VARCHAR(30) NOT NULL, " + column_permission + " VARCHAR(100) NOT NULL, " + column_value + " SMALLINT NOT NULL, " + date + " PRIMARY KEY (" + column_playerid + ", " + column_world + ", " + column_permission + "))"); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); return false; } try { PreparedStatement sql = connection.prepareStatement("CREATE TABLE IF NOT EXISTS " + table_groups + "(" + column_playerid + " VARCHAR(16) NOT NULL, " + column_groupname + " VARCHAR(30) NOT NULL, " + date + " PRIMARY KEY (" + column_playerid + ", " + column_groupname + "))"); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); return false; } return true; } return false; } @Override public void removePlayer(String player) { removePlayerPermissions(player); removePlayerGroups(player); } @Override public void removePlayerGroups(String player) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("DELETE FROM " + table_groups + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); } } } @Override public void removePlayerGroup(String player, String group) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("DELETE FROM " + table_groups + " WHERE " + column_playerid + " = ? AND " + column_groupname + " = ?"); sql.setString(1, player); sql.setString(2, group); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); } } } private void addPlayerGroup(String player, String group, boolean first) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("INSERT INTO " + table_groups + "(" + column_playerid + ", " + column_groupname + ((!column_date.isEmpty()) ? ", " + column_date : "") + ") VALUES(?, ?" + ((!column_date.isEmpty()) ? ", NOW()" : "") + ")"); sql.setString(1, player); sql.setString(2, group); sql.executeUpdate(); } catch (SQLException e) { if (first) { removePlayerGroup(player, group); addPlayerGroup(player, group, false); } else { plugin.debug(e.getMessage()); } } } } @Override public void addPlayerGroup(String player, String group) { addPlayerGroup(player, group, true); } private void addPlayerPermission(String player, String world, String permission, boolean value, boolean first) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("INSERT INTO " + table_players + "(" + column_playerid + ", " + column_world + ", " + column_permission + ", " + column_value + ((!column_date.isEmpty()) ? ", " + column_date : "") + ") VALUES(?, ?, ?, ?" + ((!column_date.isEmpty()) ? ", NOW()" : "") + ")"); sql.setString(1, player); sql.setString(2, world); sql.setString(3, permission); sql.setBoolean(4, value); sql.executeUpdate(); } catch (SQLException e) { if (first) { removePlayerPermission(player, world, permission); addPlayerPermission(player, world, permission, value, false); } else { plugin.debug(e.getMessage()); } } } } @Override public void addPlayerPermission(String player, String permission, boolean value) { addPlayerPermission(player, "", permission, value); } @Override public void addPlayerPermission(String player, String world, String permission, boolean value) { addPlayerPermission(player, world, permission, value, true); } @Override public void removePlayerPermissions(String player) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("DELETE FROM " + table_players + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); } } } @Override public void removePlayerPermission(String player, String permission) { removePlayerPermission(player, "", permission); } @Override public void removePlayerPermission(String player, String world, String permission) { if (init()) { try { PreparedStatement sql = connection.prepareStatement("DELETE FROM " + table_players + " WHERE " + column_playerid + " = ? AND " + column_world + " = ? AND " + column_permission + " = ?"); sql.setString(1, player); sql.setString(2, world); sql.setString(3, permission); sql.executeUpdate(); } catch (SQLException e) { plugin.debug(e.getMessage()); } } } @Override public List getPlayers(String group) { List results = new ArrayList(); if (init()) { try { PreparedStatement sql = connection.prepareStatement("SELECT " + column_playerid + " FROM " + table_groups + " WHERE " + column_groupname + " = ?"); sql.setString(1, group); ResultSet players = sql.executeQuery(); while (players.next()) { results.add(players.getString(column_playerid)); } } catch (SQLException e) { plugin.debug(e.getMessage()); } } return results; } @Override public List getPlayerGroups(String player) { List results = new ArrayList(); if (init()) { try { PreparedStatement sql = connection.prepareStatement("SELECT " + column_groupname + " FROM " + table_groups + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); ResultSet players = sql.executeQuery(); while (players.next()) { results.add(players.getString(column_groupname)); } } catch (SQLException e) { plugin.debug(e.getMessage()); } } return results; } @Override public Map getPlayerPermissions(String player) { return getPlayerPermissions(player, ""); } @Override public Map getPlayerPermissions(String player, String world) { Map results = new HashMap(); if (init()) { try { PreparedStatement sql = connection.prepareStatement("SELECT " + column_permission + ", " + column_value + " FROM " + table_players + " WHERE " + column_playerid + " = ? AND " + column_world + " = ?"); sql.setString(1, player); sql.setString(2, world); ResultSet permissions = sql.executeQuery(); while (permissions.next()) { results.put(permissions.getString(column_permission), permissions.getBoolean(column_value)); } } catch (SQLException e) { plugin.debug(e.getMessage()); } } return results; } @Override public boolean isPlayerInDB(String player) { if (init()) { try { int count = 0; PreparedStatement sql = connection.prepareStatement("SELECT COUNT(" + column_playerid + ") FROM " + table_groups + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); ResultSet results = sql.executeQuery(); if (results.next()) { count += results.getInt(1); } sql = connection.prepareStatement("SELECT COUNT(" + column_playerid + ") FROM " + table_players + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); results = sql.executeQuery(); if (results.next()) { count += results.getInt(1); } return count > 0; } catch (SQLException e) { plugin.debug(e.getMessage()); return false; } } return false; } @Override public List getPlayerWorlds(String player) { List results = new ArrayList(); if (init()) { try { PreparedStatement sql = connection.prepareStatement("SELECT " + column_world + " FROM " + table_players + " WHERE " + column_playerid + " = ?"); sql.setString(1, player); ResultSet players = sql.executeQuery(); while (players.next()) { results.add(players.getString(column_world)); } } catch (SQLException e) { plugin.debug(e.getMessage()); } } return results; } @Override public List getAllPlayers() { List results = new ArrayList(); if (init()) { try { PreparedStatement sql = connection.prepareStatement("SELECT " + column_playerid + " FROM " + table_groups + " GROUP BY " + column_playerid); ResultSet players = sql.executeQuery(); while (players.next()) { results.add(players.getString(column_playerid)); } sql = connection.prepareStatement("SELECT " + column_playerid + " FROM " + table_players + " GROUP BY " + column_playerid); players = sql.executeQuery(); while (players.next()) { if (!results.contains(players.getString(column_playerid))) { results.add(players.getString(column_playerid)); } } } catch (SQLException e) { plugin.debug(e.getMessage()); } } return results; } }