Search in sources :

Example 1 with QueryExecutionException

use of com.datastax.driver.core.exceptions.QueryExecutionException in project FlareBot by FlareBot.

the class QueryCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    try {
        CassandraController.runUnsafeTask(conn -> {
            ResultSet set = conn.execute(MessageUtils.getMessage(args, 0));
            List<String> header = new ArrayList<>();
            List<List<String>> table = new ArrayList<>();
            int columnsCount = set.getColumnDefinitions().size();
            for (int i = 0; i < columnsCount; i++) {
                header.add(set.getColumnDefinitions().getName(i));
            }
            for (Row setRow : set) {
                List<String> row = new ArrayList<>();
                for (int i = 0; i < columnsCount; i++) {
                    String value = setRow.getObject(i).toString();
                    row.add(value.substring(0, Math.min(30, value.length())));
                }
                table.add(row);
            }
            String output = MessageUtils.makeAsciiTable(header, table, null);
            if (output.length() < 2000) {
                channel.sendMessage(output).queue();
            } else {
                MessageUtils.sendErrorMessage("The query result set was very large, it has been posted to paste [here](" + MessageUtils.paste(output) + ")", channel, sender);
            }
        });
    } catch (QueryExecutionException | QueryValidationException e) {
        EmbedBuilder eb = new EmbedBuilder();
        eb.setTitle("Failed to execute query");
        eb.addField("Error", "```\n" + e.getMessage() + "\n```", false);
        channel.sendMessage(eb.build()).queue();
    }
}
Also used : QueryExecutionException(com.datastax.driver.core.exceptions.QueryExecutionException) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) QueryValidationException(com.datastax.driver.core.exceptions.QueryValidationException) ResultSet(com.datastax.driver.core.ResultSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Row(com.datastax.driver.core.Row)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 QueryExecutionException (com.datastax.driver.core.exceptions.QueryExecutionException)1 QueryValidationException (com.datastax.driver.core.exceptions.QueryValidationException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1