174 lines
6.1 KiB
Java
174 lines
6.1 KiB
Java
package net.nevet5gi.buzzbot.database;
|
|
|
|
import net.nevet5gi.buzzbot.Config;
|
|
import net.nevet5gi.buzzbot.objects.BanData;
|
|
import net.nevet5gi.buzzbot.objects.GuildData;
|
|
import net.nevet5gi.buzzbot.objects.MuteData;
|
|
import net.nevet5gi.buzzbot.objects.WarnData;
|
|
|
|
import java.sql.*;
|
|
|
|
public class SqlDB {
|
|
private Connection connect;
|
|
private Statement statement;
|
|
private ResultSet resultSet;
|
|
|
|
public SqlDB() {
|
|
try {
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
connect = DriverManager.getConnection("jdbc:mysql://" + Config.getConfig().getString("database.url") + "/" + Config.getConfig().getString("database.database"), Config.getConfig().getString("database.user"), Config.getConfig().getString("database.password"));
|
|
statement = connect.createStatement();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void insertBan(BanData ban, String table) {
|
|
try {
|
|
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + ban.getUserId() + ", '" + ban.getUserName() + "', '" + ban.getDate() + "', '" + ban.getTime() + "', " + ban.getBanType() + ", " + ban.getBanLength() + ", '" + ban.getBanReason() + "', '" + ban.getModName() + "', " + ban.getModId() + ", '" + ban.getServerName() + "', " + ban.getServerId() + ", 1)");
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
}
|
|
|
|
public void insertUnban(long userId, String table) {
|
|
|
|
}
|
|
|
|
public void insertMute(MuteData mute, String table) {
|
|
try {
|
|
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + mute.getUserId() + ", '" + mute.getUserName() + "', '" + mute.getDate() + "', '" + mute.getTime() + "', " + mute.getMuteLength() + ", '" + mute.getMuteReason() + "', '" + mute.getModName() + "', " + mute.getModId() + ", '" + mute.getServerName() + "', " + mute.getServerId() + ")");
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
}
|
|
|
|
public void insertWarn(WarnData warn, String table) {
|
|
try {
|
|
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + warn.getUserId() + ", '" + warn.getUserName() + "', '" + warn.getDate() + "', '" + warn.getTime() + "', " + warn.getBanType() + ", " + warn.getBanLength() + ", '" + warn.getBanReason() + "', '" + warn.getModName() + "', " + warn.getModId() + ", '" + warn.getServerName() + "', " + warn.getServerId() + ")");
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
}
|
|
|
|
public BanData queryBan(long userId, String table) {
|
|
BanData ban = new BanData();
|
|
|
|
try {
|
|
resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE userId=" + userId);
|
|
|
|
while (resultSet.next()) {
|
|
ban.setUserId(resultSet.getLong("user_id"));
|
|
ban.setUserName(resultSet.getString("user_name"));
|
|
ban.setDate(resultSet.getDate("ban_date"));
|
|
ban.setTime(resultSet.getTime("ban_time"));
|
|
ban.setBanType(resultSet.getBoolean("ban_type"));
|
|
ban.setBanLength(resultSet.getInt("ban_length"));
|
|
ban.setBanReason(resultSet.getString("ban_reason"));
|
|
ban.setModName(resultSet.getString("mod_name"));
|
|
ban.setModId(resultSet.getLong("mod_id"));
|
|
ban.setServerName(resultSet.getString("server_name"));
|
|
ban.setServerId(resultSet.getLong("server_id"));
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
close();
|
|
return ban;
|
|
}
|
|
|
|
public MuteData queryMute(long userId, String table) {
|
|
MuteData mute = new MuteData();
|
|
|
|
try {
|
|
resultSet = statement.executeQuery("SELECT * FROM " + table + "WHERE userId=" + userId);
|
|
|
|
while (resultSet.next()) {
|
|
mute.setUserId(resultSet.getLong("user_id"));
|
|
mute.setUserName(resultSet.getString("user_name"));
|
|
//mute.setDate(resultSet.);
|
|
}
|
|
} catch (SQLException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
close();
|
|
return mute;
|
|
}
|
|
|
|
public WarnData queryWarn(long userId, String table) {
|
|
WarnData warn = new WarnData();
|
|
|
|
try {
|
|
resultSet = statement.executeQuery("SELECT * FROM " + table + "WHERE userId=" + userId);
|
|
|
|
while (resultSet.next()) {
|
|
warn.setUserId(resultSet.getLong("user_id"));
|
|
warn.setUserName(resultSet.getString("user_name"));
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
return warn;
|
|
}
|
|
|
|
public void addGuild(GuildData guild) {
|
|
try {
|
|
statement.executeUpdate("INSERT INTO guild_settings VALUES ('" + guild.getName() + "', " + guild.getId() + ", '" + guild.getGroup() + "', " + guild.getProfanityLevel() + ")");
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
}
|
|
|
|
public GuildData getGuildData(long guildId) {
|
|
GuildData guild = new GuildData();
|
|
|
|
try {
|
|
resultSet = statement.executeQuery("SELECT * FROM guild_settings WHERE guild_id=" + guildId);
|
|
|
|
while (resultSet.next()) {
|
|
guild.setName(resultSet.getString("guild_name"));
|
|
guild.setId(resultSet.getLong("guild_id"));
|
|
guild.setGroup(resultSet.getString("guild_group"));
|
|
guild.setProfanityLevel(resultSet.getInt("profanity_level"));
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
close();
|
|
return guild;
|
|
}
|
|
|
|
private void close() {
|
|
try {
|
|
if (resultSet != null) {
|
|
resultSet.close();
|
|
}
|
|
|
|
if (statement != null) {
|
|
statement.close();
|
|
}
|
|
|
|
if (connect != null) {
|
|
connect.close();
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
}
|