|
| 1 | +package com.diamondfire.helpbot.bot.command.impl.other.dev; |
| 2 | + |
| 3 | +import com.diamondfire.helpbot.bot.command.argument.ArgumentSet; |
| 4 | +import com.diamondfire.helpbot.bot.command.argument.impl.parsing.types.MessageArgument; |
| 5 | +import com.diamondfire.helpbot.bot.command.help.*; |
| 6 | +import com.diamondfire.helpbot.bot.command.impl.Command; |
| 7 | +import com.diamondfire.helpbot.bot.command.permissions.Permission; |
| 8 | +import com.diamondfire.helpbot.bot.events.CommandEvent; |
| 9 | +import com.diamondfire.helpbot.sys.database.impl.DatabaseQuery; |
| 10 | +import com.diamondfire.helpbot.sys.database.impl.queries.BasicQuery; |
| 11 | +import com.diamondfire.helpbot.util.StringUtil; |
| 12 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 13 | + |
| 14 | +import java.io.*; |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | + |
| 19 | +public class QueryCommand extends Command { |
| 20 | + |
| 21 | + @Override |
| 22 | + public String getName() { |
| 23 | + return "query"; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public HelpContext getHelpContext() { |
| 28 | + return new HelpContext() |
| 29 | + .description("Executes given query.") |
| 30 | + .category(CommandCategory.OTHER) |
| 31 | + .addArgument( |
| 32 | + new HelpContextArgument() |
| 33 | + .name("query") |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public ArgumentSet compileArguments() { |
| 39 | + return new ArgumentSet() |
| 40 | + .addArgument("query", new MessageArgument()); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Permission getPermission() { |
| 45 | + return Permission.BOT_DEVELOPER; |
| 46 | + } |
| 47 | + |
| 48 | + @SuppressWarnings("LanguageMismatch") |
| 49 | + @Override |
| 50 | + public void run(CommandEvent event) { |
| 51 | + String query = event.getArgument("query"); |
| 52 | + EmbedBuilder builder = new EmbedBuilder(); |
| 53 | + builder.setTitle("SQL Result"); |
| 54 | + |
| 55 | + try { |
| 56 | + new DatabaseQuery() |
| 57 | + .query(new BasicQuery(query)) |
| 58 | + .compile() |
| 59 | + .run((set) -> { |
| 60 | + int width = set.getResult().getMetaData().getColumnCount(); |
| 61 | + List<String> objects = new ArrayList<>(); |
| 62 | + for (ResultSet resultSet : set) { |
| 63 | + HashMap<String, String> entries = new HashMap<>(); |
| 64 | + for (int i = 1; i <= width; i++) { |
| 65 | + String columnName = resultSet.getMetaData().getColumnName(i); |
| 66 | + entries.put(columnName, String.valueOf(resultSet.getObject(i))); |
| 67 | + } |
| 68 | + objects.add(StringUtil.asciidocStyle(entries)); |
| 69 | + } |
| 70 | + |
| 71 | + for (int i = 0; i < objects.size(); i++) { |
| 72 | + if (i > 25) { |
| 73 | + break; |
| 74 | + } |
| 75 | + builder.addField("Row: " + (i + 1), String.format("```asciidoc\n%s```", objects.get(i)), false); |
| 76 | + } |
| 77 | + |
| 78 | + event.getReplyHandler().reply(builder); |
| 79 | + }); |
| 80 | + } catch (IllegalStateException e) { |
| 81 | + StringWriter sw = new StringWriter(); |
| 82 | + e.printStackTrace(new PrintWriter(sw)); |
| 83 | + String sStackTrace = sw.toString(); |
| 84 | + builder.setTitle("Query failed! " + e.getClass().getName()); |
| 85 | + event.getChannel().sendMessageEmbeds(builder.build()).queue(); |
| 86 | + event.getChannel().sendMessage(String.format("```%s```", sStackTrace.length() >= 1500 ? sStackTrace.substring(0, 1500) : sStackTrace)).queue(); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments