Added DB entities

This commit is contained in:
anthorob committed 2021-09-29 20:14:22 -04:00
1 parent 1b24a55ca6
commit 1312c5fa53
14 files changed
+351 -92

No files matched your search

@@ -21,11 +21,13 @@ public class AsyncMySqlAction {
private final Object[] params; private final Object[] params;
private final MysqlDatabase.CommandType type; private final MysqlDatabase.CommandType type;
private final MysqlDatabase db; private final MysqlDatabase db;
public final String tableName;
public AsyncMySqlAction(MysqlDatabase db, String sql, Object... params) { public AsyncMySqlAction(MysqlDatabase db, String sql, Object... params) {
this.db = db; this.db = db;
this.sqlString = sql; this.sqlString = sql;
this.params = params; this.params = params;
this.tableName = sqlString.toUpperCase().split(" ")[1];
type = MysqlDatabase.CommandType.stringToType(sqlString.toUpperCase().split(" ")[0]); type = MysqlDatabase.CommandType.stringToType(sqlString.toUpperCase().split(" ")[0]);
} }
@@ -56,7 +58,7 @@ public class AsyncMySqlAction {
case SELECT: case SELECT:
ThreadManager.dispatch(executor, () -> { ThreadManager.dispatch(executor, () -> {
ResultSet result = db.prepareAndExecute(this.sqlString, this.params); ResultSet result = db.prepareAndExecute(this.sqlString, this.params);
MTableModel table = MysqlDatabase.convertToTableModel(result); MTableModel table = MysqlDatabase.convertToTableModel(result, tableName);
success.accept(table); success.accept(table);
}); });
@@ -85,7 +87,7 @@ public class AsyncMySqlAction {
case SELECT: case SELECT:
Callable<MTableModel> task = () -> { Callable<MTableModel> task = () -> {
ResultSet result = db.prepareAndExecute(this.sqlString, this.params); ResultSet result = db.prepareAndExecute(this.sqlString, this.params);
MTableModel table = MysqlDatabase.convertToTableModel(result); MTableModel table = MysqlDatabase.convertToTableModel(result, tableName);
return table; return table;
}; };
@@ -248,13 +248,42 @@ public class MysqlDatabase {
/** /**
* @param rs The ResultSet returned by the SELECT query * @param rs The ResultSet returned by the SELECT query
* @return a {@link MTableModel} with the appropriate column and rows * @return a {@link MTableModel} with the appropriate column and rows
* @throws SQLException */
public static MTableModel convertToTableModel(ResultSet rs, String tableName) {
MTableModel table = new MTableModel();
try {
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 1; i < metadata.getColumnCount() + 1; i++) {
table.addColumn(metadata.getColumnName(i));
}
while (rs.next()) {
MRowModel row = new MRowModel(rs.getRow() - 1);
for (int i = 0; i < metadata.getColumnCount(); i++) {
row.addColumnToRow(rs.getObject(i + 1));
}
table.addRow(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
return table;
}
/**
* @param rs The ResultSet returned by the SELECT query
* @return a {@link MTableModel} with the appropriate column and rows
*/ */
public static MTableModel convertToTableModel(ResultSet rs) { public static MTableModel convertToTableModel(ResultSet rs) {
MTableModel table = new MTableModel(); MTableModel table = new MTableModel();
try { try {
ResultSetMetaData metadata = rs.getMetaData(); ResultSetMetaData metadata = rs.getMetaData();
for (int i = 1; i < metadata.getColumnCount() + 1; i++) { for (int i = 1; i < metadata.getColumnCount() + 1; i++) {
table.addColumn(metadata.getColumnName(i)); table.addColumn(metadata.getColumnName(i));
} }
@@ -1,14 +1,11 @@
package netneutrality.db.controllers; package netneutrality.db.controllers;
import java.sql.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import netneutrality.ThreadManager;
import netneutrality.db.AbstractController; import netneutrality.db.AbstractController;
import netneutrality.db.MysqlDatabase;
import netneutrality.db.models.MBlacklist; import netneutrality.db.models.MBlacklist;
import netneutrality.db.models.MTableModel; import netneutrality.db.models.MTableModel;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -81,20 +78,15 @@ public class DBBlacklist extends AbstractController {
@Deprecated @Deprecated
public void getBlacklistForWord(String word, String serverId, Consumer<MBlacklist> success) { public void getBlacklistForWord(String word, String serverId, Consumer<MBlacklist> success) {
ThreadManager.getExecutor("db-action").submit(() -> { database.prepareAndExecuteAsync("SELECT * FROM blacklist WHERE word = ? AND serverId = ?", word, serverId).async((table -> {
MBlacklist blacklisted = null; MBlacklist blacklisted = null;
if (isAlreadyBlacklisted(word, serverId)) { if (table.getNbRows() > 0) {
ResultSet rs = database.prepareAndExecute("SELECT * FROM blacklist WHERE word = ? AND serverId = ?", word, serverId); blacklisted = new MBlacklist(table.getRow(0));
MTableModel table = MysqlDatabase.convertToTableModel(rs);
if (table.getNbRows() > 0) {
blacklisted = new MBlacklist(table.getRow(0));
}
} }
success.accept(blacklisted); success.accept(blacklisted);
});
}));
} }
@@ -0,0 +1,48 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import netneutrality.MusicBot;
import netneutrality.db.DBController;
import netneutrality.db.models.MTableModel;
public abstract class AbstractEntity implements EntityInterface {
private final String primaryKey;
private final String[] primaryKeys;
protected MTableModel table;
public AbstractEntity(String primaryKey, String... primaryKeys) throws ArrayIndexOutOfBoundsException {
this.primaryKey = primaryKey;
this.primaryKeys = primaryKeys;
fillTableModel();
if (!table.isEmpty())
convertFromTableModel(table);
}
private void fillTableModel() throws ArrayIndexOutOfBoundsException {
StringBuilder whereClause = new StringBuilder();
for (String pk : getPrimaryKeys()) {
whereClause.append(pk + " = ?");
if (getPrimaryKeys().length > 1)
whereClause.append(" AND ");
}
String sql = "SELECT * FROM " + getTableName() + " WHERE " + whereClause;
System.out.println(sql + " - pk: " + primaryKey);
if (this.primaryKeys.length > 0)
this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey, this.primaryKeys).blocking();
else
this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey).blocking();
System.out.println(table);
}
}
@@ -7,8 +7,13 @@ package netneutrality.db.entities;/*
import netneutrality.db.models.MTableModel; import netneutrality.db.models.MTableModel;
public interface EntityInterface<E> { public interface EntityInterface {
public E ConvertFromTableModel(MTableModel table); String getTableName();
String[] getPrimaryKeys();
boolean exists();
void convertFromTableModel(MTableModel table);
} }
@@ -1,16 +0,0 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
public class Guild {
private int guildID;
public Guild() {
}
}
@@ -0,0 +1,53 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import netneutrality.db.models.MTableModel;
public class OGuild extends AbstractEntity {
private String guildID;
public boolean logging;
public String logchannel;
public OGuildSetting settings;
public OGuild(String guildId) {
super(guildId);
this.settings = new OGuildSetting(this);
}
@Override
public boolean exists() {
return guildID != null;
}
public String getGuildID() {
return guildID;
}
@Override
public String getTableName() {
return "guilds";
}
@Override
public String[] getPrimaryKeys() {
return new String[] {"guild_id"};
}
@Override
public void convertFromTableModel(MTableModel table) {
this.guildID = (String) table.getRow(0).get(0);
this.logging = ((Integer) table.getRow(0).get(1)) == 1;
this.logchannel = (String) table.getRow(0).get(2);
}
}
@@ -0,0 +1,74 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import netneutrality.db.models.MRowModel;
import netneutrality.db.models.MTableModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class OGuildSetting extends AbstractEntity {
private final OGuild guild;
private Map<String, String> settings;
public OGuildSetting(OGuild OGuild) {
super(OGuild.getGuildID());
this.guild = OGuild;
}
public OGuild getGuild() {
return guild;
}
public Map<String, String> getSettings() {
return settings;
}
public String getSettingByKey(String key) {
if (settings.containsKey(key)) {
return settings.get(key);
}
return null;
}
@Override
public String getTableName() {
return "guild_settings";
}
@Override
public String[] getPrimaryKeys() {
return new String[] {"guild_id"};
}
@Override
public boolean exists() {
return !settings.isEmpty();
}
@Override
public void convertFromTableModel(MTableModel table) {
settings = new HashMap<>();
List<String> columns = table.getColumns();
MRowModel row = table.getRowModel(0);
for (int i = 0; i < columns.size(); i++) {
if (i == 0) continue;
String key = columns.get(i).replace('_', '.');
String value = (row.getItem(i) == null ? "" : row.getItem(i).toString());
settings.put(key, value);
}
}
}
@@ -0,0 +1,66 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import netneutrality.command.CommandType;
import netneutrality.db.models.MTableModel;
import java.util.List;
public class OPermission extends AbstractEntity {
private final String guild_id;
private final String role_id;
public CommandType type;
public String command;
public OPermission(String guild_id, String role_id) {
super(guild_id, role_id);
this.guild_id = guild_id;
this.role_id = role_id;
}
@Override
public boolean exists() {
return guild_id != null && role_id != null;
}
public String getGuildId() {
return guild_id;
}
public String getRoleId() {
return role_id;
}
public CommandType getType() {
return type;
}
public String getCommand() {
return command;
}
@Override
public String getTableName() {
return "permissions";
}
@Override
public String[] getPrimaryKeys() {
return new String[] {"guild_id", "role_id"};
}
@Override
public void convertFromTableModel(MTableModel table) {
this.type = (CommandType) table.getRow(0).get(2);
this.command = (String) table.getRow(0).get(3);
}
}
@@ -1,53 +0,0 @@
package netneutrality.db.entities;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import netneutrality.command.CommandType;
import netneutrality.db.models.MTableModel;
import java.util.List;
public class Permission implements EntityInterface<Permission> {
private final String guild_id;
private final String role_id;
private final CommandType type;
private final String command;
public Permission(String guild_id, String role_id, CommandType type, String command) {
this.guild_id = guild_id;
this.role_id = role_id;
this.type = type;
this.command = command;
}
public Permission(List<Object> row) {
this.guild_id = (String) row.get(0);
this.role_id = (String) row.get(1);
this.type = (CommandType) row.get(2);
this.command = (String) row.get(3);
}
public String getGuild_id() {
return guild_id;
}
public String getRole_id() {
return role_id;
}
public CommandType getType() {
return type;
}
public String getCommand() {
return command;
}
@Override
public Permission ConvertFromTableModel(MTableModel table) {
return null;
}
}
@@ -11,17 +11,27 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
public class MRowModel { public class MRowModel {
private ArrayList<Object> row; private final ArrayList<Object> row;
private int rowIndex;
public MRowModel() { public MRowModel() {
row = new ArrayList<>(); row = new ArrayList<>();
} }
public MRowModel(Integer rowIndex) {
row = new ArrayList<>();
this.rowIndex = rowIndex;
}
public MRowModel(Object... objs) { public MRowModel(Object... objs) {
row = new ArrayList<>(); row = new ArrayList<>();
row.addAll(Arrays.asList(objs)); row.addAll(Arrays.asList(objs));
} }
public int getRowIndex() {
return rowIndex;
}
public void addColumnToRow(Object obj) { public void addColumnToRow(Object obj) {
row.add(obj); row.add(obj);
} }
@@ -9,7 +9,8 @@ package netneutrality.db.models;
import java.util.ArrayList; import java.util.ArrayList;
public class MRowsModel { public class MRowsModel {
private ArrayList<MRowModel> rows; private final ArrayList<MRowModel> rows;
private String[] primaryKeys;
public MRowsModel() { public MRowsModel() {
rows = new ArrayList<>(); rows = new ArrayList<>();
@@ -26,6 +27,10 @@ public class MRowsModel {
return null; return null;
} }
public int size() {
return rows.size();
}
public void addColumnInRow(int index, Object obj) { public void addColumnInRow(int index, Object obj) {
rows.get(index).addColumnToRow(obj); rows.get(index).addColumnToRow(obj);
} }
@@ -3,36 +3,49 @@ package netneutrality.db.models;
/* /*
* Copyright (c) anthorob * Copyright (c) anthorob
* *
* This file is closed-source. It should not be distributed or copied in any way or form * https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/ */
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
public class MTableModel { public class MTableModel {
private String name;
private final List<String> column; private final List<String> column;
private final MRowsModel rows; private final MRowsModel rows;
public MTableModel() { public MTableModel() {
this.name = null;
this.column = new LinkedList<>(); this.column = new LinkedList<>();
this.rows = new MRowsModel(); this.rows = new MRowsModel();
} }
public MTableModel(String tableName) { public MTableModel(String tableName) {
this.name = tableName;
this.column = new LinkedList<>(); this.column = new LinkedList<>();
this.rows = new MRowsModel(); this.rows = new MRowsModel();
} }
public MTableModel(String... columns) { public MTableModel(String... columns) {
this.name = null;
this.column = new LinkedList<>(); this.column = new LinkedList<>();
this.rows = new MRowsModel(); this.rows = new MRowsModel();
this.column.addAll(Arrays.asList(columns)); this.column.addAll(Arrays.asList(columns));
} }
public String getName() {
return name;
}
public void setName(String name) {
if (this.name != null)
this.name = name;
}
public MTableModel addColumn(String name) { public MTableModel addColumn(String name) {
column.add(name); column.add(name);
return this; return this;
@@ -75,6 +88,6 @@ public class MTableModel {
@Override @Override
public String toString() { public String toString() {
return "Column: [" + column + "], Rows (" + getNbRows() + "): [" + rows + "]"; return "Column: " + column + ", Rows (" + getNbRows() + "): " + rows;
} }
} }
@@ -0,0 +1,31 @@
package netneutrality.handler;/*
* Copyright (c) anthorob
*
* https://xtremcommunity.com/
* https://discord.gg/CdFcyQ
*/
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent;
import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
public class ReactionHandler extends ListenerAdapter {
@Override
public void onGuildMessageReactionAdd(@NotNull GuildMessageReactionAddEvent event) {
Member member = event.getMember();
}
@Override
public void onGuildMessageReactionRemove(@NotNull GuildMessageReactionRemoveEvent event) {
super.onGuildMessageReactionRemove(event);
}
@Override
public void onGuildMessageUpdate(@NotNull GuildMessageUpdateEvent event) {
super.onGuildMessageUpdate(event);
}
}