Search in sources :

Example 21 with Connexion

use of util.Connexion in project KaellyBot by Kaysoro.

the class ChannelLanguage method getChannelLanguages.

public static synchronized Map<Long, ChannelLanguage> getChannelLanguages() {
    if (channelLanguages == null) {
        channelLanguages = new ConcurrentHashMap<>();
        Connexion connexion = Connexion.getInstance();
        Connection connection = connexion.getConnection();
        try {
            PreparedStatement query = connection.prepareStatement("SELECT lang, id_chan FROM Channel_Language");
            ResultSet resultSet = query.executeQuery();
            while (resultSet.next()) {
                long idChan = Long.parseLong(resultSet.getString("id_chan"));
                Language lang = Language.valueOf(resultSet.getString("lang"));
                IChannel chan = ClientConfig.DISCORD().getChannelByID(idChan);
                if (chan != null && !chan.isDeleted())
                    channelLanguages.put(chan.getLongID(), new ChannelLanguage(lang, idChan));
                else {
                    new ChannelLanguage(lang, idChan).removeToDatabase();
                    LOG.info("Chan deleted : " + idChan);
                }
            }
        } catch (SQLException e) {
            Reporter.report(e);
            LOG.error("getChannlLanguages", e);
        }
    }
    return channelLanguages;
}
Also used : IChannel(sx.blah.discord.handle.obj.IChannel) Language(enums.Language) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Connexion(util.Connexion)

Example 22 with Connexion

use of util.Connexion in project KaellyBot by Kaysoro.

the class ChannelLanguage method removeToDatabase.

public synchronized void removeToDatabase() {
    getChannelLanguages().remove(getChannelId());
    Connexion connexion = Connexion.getInstance();
    Connection connection = connexion.getConnection();
    try {
        PreparedStatement request = connection.prepareStatement("DELETE FROM Channel_Language WHERE id_chan = ?;");
        request.setString(1, String.valueOf(getChannelId()));
        request.executeUpdate();
    } catch (SQLException e) {
        Reporter.report(e, ClientConfig.DISCORD().getChannelByID(getChannelId()).getGuild(), ClientConfig.DISCORD().getChannelByID(getChannelId()));
        LOG.error("removeToDatabase", e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Connexion(util.Connexion)

Example 23 with Connexion

use of util.Connexion in project KaellyBot by Kaysoro.

the class CommandForbidden method addToDatabase.

public synchronized void addToDatabase() {
    if (!isSaved) {
        Connexion connexion = Connexion.getInstance();
        Connection connection = connexion.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO Command_Guild(id_guild, name_command) VALUES(?, ?);");
            preparedStatement.setString(1, getGuild().getId());
            preparedStatement.setString(2, getCommand().getName());
            preparedStatement.executeUpdate();
            getGuild().getForbiddenCommands().put(getCommand().getName(), this);
            isSaved = true;
        } catch (SQLException e) {
            Reporter.report(e, ClientConfig.DISCORD().getGuildByID(Long.parseLong(getGuild().getId())));
            LOG.error("addToDatabase", e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Connexion(util.Connexion)

Example 24 with Connexion

use of util.Connexion in project KaellyBot by Kaysoro.

the class CommandForbidden method getForbiddenCommands.

public static synchronized Map<String, CommandForbidden> getForbiddenCommands(Guild g) {
    Map<String, CommandForbidden> commands = new ConcurrentHashMap<>();
    Connexion connexion = Connexion.getInstance();
    Connection connection = connexion.getConnection();
    try {
        PreparedStatement query = connection.prepareStatement("SELECT name_command FROM Command_Guild WHERE id_guild = (?);");
        query.setString(1, g.getId());
        ResultSet resultSet = query.executeQuery();
        while (resultSet.next()) {
            String commandName = resultSet.getString("name_command");
            CommandForbidden tmp = new CommandForbidden(CommandManager.getCommand(commandName), g, true);
            commands.put(tmp.getCommand().getName(), tmp);
        }
    } catch (SQLException e) {
        Reporter.report(e, ClientConfig.DISCORD().getGuildByID(Long.parseLong(g.getId())));
        LOG.error("getForbiddenCommands", e);
    }
    return commands;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Connexion(util.Connexion)

Aggregations

Connexion (util.Connexion)24 Connection (java.sql.Connection)17 PreparedStatement (java.sql.PreparedStatement)17 SQLException (java.sql.SQLException)17 ResultSet (java.sql.ResultSet)6 IChannel (sx.blah.discord.handle.obj.IChannel)2 City (enums.City)1 Job (enums.Job)1 Language (enums.Language)1 Order (enums.Order)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 IGuild (sx.blah.discord.handle.obj.IGuild)1