I gotta start committing more consistently
This commit is contained in:
169
src/main/java/net/nevet5gi/buzzbot/database/SqlDB.java
Normal file
169
src/main/java/net/nevet5gi/buzzbot/database/SqlDB.java
Normal file
@@ -0,0 +1,169 @@
|
||||
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 {
|
||||
//TODO Make this a two way class for reading and writing from db
|
||||
|
||||
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.getUserDiscriminator() + "', '" + ban.getDate() + "', '" + ban.getTime() + "', " + ban.getBanType() + ", " + ban.getBanLength() + ", '" + ban.getBanReason() + "', '" + ban.getModName() + "', " + ban.getModId() + ", '" + ban.getServerName() + "', " + ban.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
public void insertMute(MuteData mute, String table) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + mute.getUserId() + ", '" + mute.getUserName() + "', '" + mute.getUserDiscriminator() + "', '" + mute.getDate() + "', '" + mute.getTime() + "', " + mute.getMuteLength() + ", '" + mute.getMuteReason() + "', '" + mute.getModName() + "', " + mute.getModId() + ", '" + mute.getServerName() + "', " + mute.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void insertWarn(WarnData warn, String table) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + warn.getUserId() + ", '" + warn.getUserName() + "', '" + warn.getUserDiscriminator() + "', '" + warn.getDate() + "', '" + warn.getTime() + "', " + warn.getBanType() + ", " + warn.getBanLength() + ", '" + warn.getBanReason() + "', '" + warn.getModName() + "', " + warn.getModId() + ", '" + warn.getServerName() + "', " + warn.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public BanData queryBan(long userId, String table) {
|
||||
BanData ban = new BanData();
|
||||
|
||||
try {
|
||||
//TODO Make this use GuildUtils in order to get the proper group for the server
|
||||
|
||||
resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE userId=" + userId);
|
||||
|
||||
while (resultSet.next()) {
|
||||
ban.setUserId(resultSet.getLong("userid"));
|
||||
ban.setUserName(resultSet.getString("username"));
|
||||
ban.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
ban.setDate(resultSet.getDate("bandate"));
|
||||
ban.setTime(resultSet.getTime("bantime"));
|
||||
ban.setBanType(resultSet.getBoolean("bantype"));
|
||||
ban.setBanLength(resultSet.getInt("banlength"));
|
||||
ban.setBanReason(resultSet.getString("banreason"));
|
||||
ban.setModName(resultSet.getString("modname"));
|
||||
ban.setModId(resultSet.getLong("modid"));
|
||||
ban.setServerName(resultSet.getString("servername"));
|
||||
ban.setServerId(resultSet.getLong("serverid"));
|
||||
}
|
||||
} 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("userid"));
|
||||
mute.setUserName(resultSet.getString("username"));
|
||||
mute.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
//mute.setDate(resultSet.);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
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("userid"));
|
||||
warn.setUserName(resultSet.getString("username"));
|
||||
warn.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user