118 lines
4.4 KiB
Java
118 lines
4.4 KiB
Java
package me.monster.ranks.database;
|
|
|
|
import me.monster.ranks.Main;
|
|
import me.monster.ranks.database.managers.groups;
|
|
import me.monster.ranks.database.managers.permissions;
|
|
import me.monster.ranks.database.managers.players;
|
|
import me.monster.ranks.database.models.groupPermissions;
|
|
import me.monster.ranks.config.configManager;
|
|
import me.monster.ranks.database.models.ranks;
|
|
import me.monster.ranks.database.models.userInfo;
|
|
|
|
import java.sql.*;
|
|
import java.util.List;
|
|
|
|
public class dbManager {
|
|
|
|
private static Connection connection;
|
|
|
|
public static Connection getConnection() throws SQLException {
|
|
|
|
if(connection != null){
|
|
return connection;
|
|
}
|
|
|
|
String port = configManager.configFile().getString("mysql.port");
|
|
String host = configManager.configFile().getString("mysql.host");
|
|
String user = configManager.configFile().getString("mysql.user");
|
|
String password = configManager.configFile().getString("mysql.password");
|
|
String database = configManager.configFile().getString("mysql.database");
|
|
String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
|
|
|
|
Connection connection = DriverManager.getConnection(url, user, password);
|
|
|
|
dbManager.connection = connection;
|
|
|
|
Main.getLogger("Connected to database.");
|
|
|
|
return connection;
|
|
}
|
|
|
|
public void initializeDatabase() throws SQLException {
|
|
|
|
Statement statement = getConnection().createStatement();
|
|
|
|
String player_infoSQL = "CREATE TABLE IF NOT EXISTS player_info (uuid varchar(36), playerRank TEXT, temp INT, date DATETIME)";
|
|
String groupsSQL = "CREATE TABLE IF NOT EXISTS groups (groupName TEXT, groupPrefix TEXT, groupSuffix TEXT, groupWeight INT, groupParent TEXT)";
|
|
String groupsPermissionsSQL = "CREATE TABLE IF NOT EXISTS group_permissions (groupName TEXT, permission TEXT, value INT, context TEXT)";
|
|
|
|
|
|
statement.execute(player_infoSQL);
|
|
statement.execute(groupsSQL);
|
|
statement.execute(groupsPermissionsSQL);
|
|
|
|
statement.close();
|
|
|
|
}
|
|
|
|
//################################################
|
|
//########### Player Info Section ################
|
|
//################################################
|
|
|
|
public static userInfo findPlayerGroupByUUID(String string) throws SQLException {
|
|
return players.findPlayerGroupByUUID(string);
|
|
}
|
|
public static List<userInfo> findPlayerGroups(String uuid) throws SQLException {
|
|
return players.findPlayerGroups(uuid);
|
|
}
|
|
public static void deletePlayerGroups(userInfo databaseModel) throws SQLException {
|
|
players.deletePlayerGroups(databaseModel);
|
|
}
|
|
public static void updatePlayerGroups(userInfo databaseModel) throws SQLException {
|
|
players.updatePlayerGroups(databaseModel);
|
|
}
|
|
public static void createPlayer(userInfo databaseModel) throws SQLException {
|
|
players.createPlayer(databaseModel);
|
|
}
|
|
|
|
//################################################
|
|
//############# Groups Section ##################
|
|
//################################################
|
|
|
|
public static void createGroup(ranks databaseModel) throws SQLException {
|
|
groups.createGroup(databaseModel);
|
|
}
|
|
|
|
public static void updateGroup(ranks databaseModel) throws SQLException {
|
|
groups.updateGroup(databaseModel);
|
|
}
|
|
|
|
public static void deleteGroup(ranks databaseModel) throws SQLException {
|
|
groups.deleteGroup(databaseModel);
|
|
}
|
|
public static ranks findGroupByName(String groupName) throws SQLException {
|
|
return groups.findGroupByName(groupName);
|
|
}
|
|
|
|
//################################################
|
|
//########### Permission Section ################
|
|
//################################################
|
|
|
|
public static List<groupPermissions> fetchGroupPermissions(String groupName) throws SQLException {
|
|
return permissions.fetchGroupPermissions(groupName);
|
|
}
|
|
|
|
public static void updateGroupPermission(groupPermissions permission) throws SQLException {
|
|
permissions.updateGroupPermission(permission);
|
|
}
|
|
|
|
public static void addGroupPermission(groupPermissions permission) throws SQLException {
|
|
permissions.addGroupPermission(permission);
|
|
}
|
|
|
|
public static void deleteGroupPermission(String groupName, String permission, String context) throws SQLException {
|
|
permissions.deleteGroupPermission(groupName, permission, context);
|
|
}
|
|
|
|
}
|